cxfoot/models/sms/SmsRecord.php
2023-10-27 14:25:12 +08:00

109 lines
3.0 KiB
PHP

<?php
namespace app\models\sms;
use Yii;
use app\components\DbUtils;
use app\models\Model;
use app\components\SiteHelper;
use app\components\EncryptHelper;
use app\components\IPUtils;
/**
* This is the model class for table "{{%sms_record}}".
*
* @property int $id ID
* @property int $cx_mch_id 平台商户ID
* @property int $user_id 用户ID
* @property string $mobile_phone 手机号
* @property string $mobile_prefix 手机号国家代码
* @property string $tpl_code 短信模板
* @property string $content 短信内容
* @property string|null $ipv4 IPV4
* @property string|null $ipv6 IPV6
* @property int $created_at 添加时间
*/
class SmsRecord extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return '{{%sms_record}}';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['mobile_phone', 'mobile_prefix', 'tpl_code', 'content', ], 'trim'],
[['cx_mch_id', 'user_id', 'created_at'], 'integer'],
[['mobile_phone', 'mobile_prefix', 'tpl_code', 'content'], 'required'],
[['mobile_phone', 'ipv6'], 'string', 'max' => 128],
[['mobile_prefix'], 'string', 'max' => 16],
[['tpl_code', 'ipv4'], 'string', 'max' => 64],
[['content'], 'string', 'max' => 1024],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'cx_mch_id' => '平台商户ID',
'user_id' => '用户ID',
'mobile_phone' => '手机号',
'mobile_prefix' => '手机号国家代码',
'tpl_code' => '短信模板',
'content' => '短信内容',
'ipv4' => 'IPV4',
'ipv6' => 'IPV6',
'created_at' => '添加时间',
];
}
public function beforeSave($insert) {
if(parent::beforeSave($insert)){
if($this->isNewRecord){
$this->created_at = time();
}
return true;
} else {
return false;
}
}
/**
* 记录
* @param string $mobile 未加密的手机号
*/
public static function logger($mobile, $tpl_code, $content, $user_id = 0, $cx_mch_id = 0, $mobile_prefix = '86')
{
$mobile = EncryptHelper::encryptMobilePhone($mobile);
$model = new SmsRecord();
$model->cx_mch_id = $cx_mch_id;
$model->user_id = $user_id;
$model->mobile_phone = $mobile;
$model->mobile_prefix = $mobile_prefix;
$model->tpl_code = $tpl_code;
$model->content = $content;
$model->ipv4 = IPUtils::getIp();
if(!$model->save()){
return SiteHelper::getModelError($model);
}
return [
'code' => 0,
'msg' => 'ok'
];
}
}