cxfoot/models/sms/SmsTpl.php
2023-10-24 14:54:18 +08:00

225 lines
7.5 KiB
PHP
Raw 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'; //球车超时报警
/**
* {@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_VALIDATE_IDENTIFY,
'label' => '身份验证验证码',
'tpl_code' => '',
'tip' => '验证码${code},您正在进行身份验证,打死不要告诉别人哦!',
],
[
'key' => self::TYPE_LOGIN_CONFIRM,
'label' => '登录确认验证码',
'tpl_code' => '',
'tip' => '验证码${code},您正在登录,若非本人操作,请勿泄露。',
],
[
'key' => self::TYPE_LOGIN_EXCEPTION,
'label' => '登录异常验证码',
'tpl_code' => '',
'tip' => '验证码${code},您正尝试异地登录,若非本人操作,请勿泄露。',
],
[
'key' => self::TYPE_USER_SIGNUP,
'label' => '用户注册验证码',
'tpl_code' => '',
'tip' => '验证码${code},您正在注册成为新用户,感谢您的支持!',
],
[
'key' => self::TYPE_MODIFY_PASSWORD,
'label' => '修改密码验证码',
'tpl_code' => '',
'tip' => '验证码${code},您正在尝试修改登录密码,请妥善保管账户信息。',
],
[
'key' => self::TYPE_ALTER_INFO,
'label' => '信息变更验证码',
'tpl_code' => '',
'tip' => '验证码${code},您正在尝试变更重要信息,请妥善保管账户信息。',
],
[
'key' => self::TYPE_BALL_RETURN,
'label' => '申请归还',
'tpl_code' => '',
'tip' => '尊敬的${name},编号为${numbers}球车申请归还,请尽快前往确认。',
],
[
'key' => self::TYPE_BALL_ELECTRIC,
'label' => '低电量告警',
'tpl_code' => '',
'tip' => '尊敬的${name},编号为${numbers}球车电量剩余${rato}%,请尽快对此车进行充电,避免影响客户用车体验。',
],
[
'key' => self::TYPE_BALL_ERROR,
'label' => '报警通知',
'tpl_code' => '',
'tip' => '尊敬的${name},编号为${numbers}球车${error}。请及时处理,避免影响客户用车体验。',
],
[
'key' => self::TYPE_BALL_TIME_ERROR,
'label' => '超时未归还报警通知',
'tpl_code' => '',
'tip' => '尊敬的${name},编号为${numbers}球车${error}。请及时处理,避免影响客户用车体验。',
],
];
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_VALIDATE_IDENTIFY,
'1' => self::TYPE_LOGIN_CONFIRM,
'2' => self::TYPE_LOGIN_EXCEPTION,
'3' => self::TYPE_USER_SIGNUP,
'4' => self::TYPE_MODIFY_PASSWORD,
'5' => self::TYPE_ALTER_INFO,
'6' => self::TYPE_BALL_RETURN,
'7' => self::TYPE_BALL_ELECTRIC,
'8' => self::TYPE_BALL_ERROR,
];
}
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;
}
}