cxgj/models/sms/SmsTpl.php
2023-11-27 09:45:13 +08:00

188 lines
5.9 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\models\sms;
use Yii;
use app\components\DbUtils;
use app\components\FlashStorage;
use app\models\Model;
/**
* This is the model class for table "{{%sms_tpl}}".
*
* @property int $id ID
* @property int $cx_mch_id 平台商户ID
* @property int $type 类型0=阿里云1=腾讯云
* @property string $key KEY
* @property string $tpl_code 短信模板
* @property int $is_delete 是否删除0=否1=是
* @property int $created_at 添加时间
*/
class SmsTpl extends \yii\db\ActiveRecord
{
const TYPE_VALIDATE_IDENTIFY = 'validate_identify';
const TYPE_LOGIN_CONFIRM = 'login_confirm';
const TYPE_USER_SIGNUP = 'user_signup';
const TYPE_LOGIN_EXCEPTION = 'login_exception';
const TYPE_MODIFY_PASSWORD = 'modify_password';
const TYPE_ALTER_INFO = 'alter_info';
const TYPE_BALL_RETURN = 'ball_return'; //球车还车提醒
const TYPE_BALL_ELECTRIC = 'ball_electric'; //球车低电量告警
const TYPE_BALL_ERROR = 'ball_error'; //球车报警
const TYPE_BALL_TIME_ERROR = 'ball_time_error'; //球车超时报警
const TYPE_ORDER_GOODS_CLOSE = 'order_goods_close';//订单未支付超一天未支付-商品-短信
const TYPE_STORE_DZ_YES = 'store_dz_yes';//门店同意
const TYPE_STORE_DZ_NO = 'store_dz_no';//门店不同意
const TYPE_STORE_BJ = 'store_dz_bj';//门店保洁
/**
* {@inheritdoc}
*/
public static function tableName()
{
return '{{%sms_tpl}}';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['cx_mch_id', 'type', 'is_delete', 'created_at'], 'integer'],
[['key', 'tpl_code'], 'required'],
[['key', 'tpl_code'], 'string', 'max' => 64],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'cx_mch_id' => '平台商户ID',
'type' => '类型0=阿里云1=腾讯云',
'key' => 'KEY',
'tpl_code' => '短信模板',
'is_delete' => '是否删除0=否1=是',
'created_at' => '添加时间',
];
}
public function beforeSave($insert) {
if(parent::beforeSave($insert)){
if($this->isNewRecord){
$this->created_at = time();
}
$this->htmlTagFilter();
return true;
} else {
return false;
}
}
public function htmlTagFilter()
{
$this->key = Model::htmlTagFilter($this->key);
$this->tpl_code = Model::htmlTagFilter($this->tpl_code);
}
public static function getTplList($type = SmsSetting::TYPE_ALIYUN, $cx_mch_id = 0)
{
$list = [
[
'key' => self::TYPE_ORDER_GOODS_CLOSE,
'label' => '商品订单支付提醒',
'tpl_code' => '',
'tip' => '您在冠亿城小程序中有一笔待支付订单:${order_no},如您已支付请忽略',
],
[
'key' => self::TYPE_STORE_DZ_YES,
'label' => '同意成为店长',
'tpl_code' => '',
'tip' => '您预约门店[${store}]店长,已预约成功,地址:${location},时间:${date1}-${date2},门店联系电话:${phone}',
],
[
'key' => self::TYPE_STORE_DZ_NO,
'label' => '拒绝成为店长',
'tpl_code' => '',
'tip' => '您预约提交的${store}门店 ${date},预约已审核,${msg}',
],
[
'key' => self::TYPE_STORE_BJ,
'label' => '通知保洁',
'tpl_code' => '',
'tip' => '打扫门店:${store},包厢:${name},门店地址:${location},门店联系电话:${phone}',
],
];
foreach ($list as $index => $item){
$item['tpl_code'] = self::getTpl($item['key'], $type, $cx_mch_id);
$list[$index] = $item;
}
return $list;
}
public static function getTpl($key, $type = SmsSetting::TYPE_ALIYUN, $cx_mch_id = 0)
{
$cache_key = "{$key}_{$type}_{$cx_mch_id}";
$cache_val = FlashStorage::getCache($cache_key);
$cache_val = YII_ENV_PROD ? $cache_val : false;
if($cache_val === false){
$model = SmsTpl::findOne([
'cx_mch_id' => $cx_mch_id,
'type' => $type,
'key' => $key,
'is_delete' => 0
]);
$cache_val = $model != null ? $model->tpl_code : null;
if($cache_val != null){
FlashStorage::setCache($cache_key, $cache_val);
}
}
return $cache_val;
}
public static function tplKeyLables()
{
return [
'0' => self::TYPE_ORDER_GOODS_CLOSE,
'1' => self::TYPE_STORE_DZ_YES,
'2' => self::TYPE_STORE_DZ_NO,
'3' => self::TYPE_STORE_BJ,
];
}
public static function getTplKey($type)
{
$lables = self::tplKeyLables();
return isset($lables[$type]) ? $lables[$type] : self::TYPE_VALIDATE_IDENTIFY;
}
public static function typeLabels()
{
$cache_key = "_s_t_t_";
$cache_val = FlashStorage::getCache($cache_key);
if($cache_val !== false)
return $cache_val;
$tpl_list = self::getTplList();
$tpl_key_labels = self::tplKeyLables();
$list = [];
foreach ($tpl_key_labels as $key => $val){
foreach ($tpl_list as $index => $item){
if($item['key'] == $val){
$list[] = [
'type' => $key,
'label' => $item['label']
];
break;
}
}
}
FlashStorage::setCache($cache_key, $list,86400);
return $list;
}
}