145 lines
4.1 KiB
PHP
145 lines
4.1 KiB
PHP
<?php
|
||
|
||
namespace app\models\msg;
|
||
|
||
use Yii;
|
||
use app\models\Model;
|
||
|
||
|
||
/**
|
||
* This is the model class for table "{{%sys_msg}}".
|
||
*
|
||
* @property int $id ID
|
||
* @property int $cx_mch_id 平台商户ID
|
||
* @property int $type 消息类型
|
||
* @property int $to_user_id 接收用户:0=所有用户
|
||
* @property string $title 标题
|
||
* @property int $is_richtext 是否富文本:0=否,1=是
|
||
* @property string $content 消息内容
|
||
* @property int $status 状态:0=草稿,1=已发布
|
||
* @property int $order_id 订单ID
|
||
* @property int $created_at 添加时间
|
||
* @property int $updated_at 更新时间
|
||
* @property int $deleted_at 删除时间
|
||
* @property int $published_at 发布时间
|
||
* @property int $is_delete 是否删除:0=否,1=是
|
||
*/
|
||
class SysMsg extends \yii\db\ActiveRecord
|
||
{
|
||
const STATUS_DRAFT = 0;
|
||
const STATUS_PUBLISHED = 1;
|
||
|
||
//消息类型
|
||
const TYPE_DEFAULT_SYS_MSG = 0; //默认系统消息
|
||
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public static function tableName()
|
||
{
|
||
return '{{%sys_msg}}';
|
||
}
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public function rules()
|
||
{
|
||
return [
|
||
[['title', 'content'], 'trim'],
|
||
[['cx_mch_id', 'type', 'to_user_id', 'is_richtext', 'status', 'order_id', 'created_at', 'updated_at', 'deleted_at', 'published_at', 'is_delete'], 'integer'],
|
||
[['title', 'content'], 'required'],
|
||
[['content'], 'string'],
|
||
[['title'], 'string', 'max' => 256],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public function attributeLabels()
|
||
{
|
||
return [
|
||
'id' => 'ID',
|
||
'cx_mch_id' => '平台商户ID',
|
||
'type' => '消息类型',
|
||
'to_user_id' => '接收用户:0=所有用户',
|
||
'title' => '标题',
|
||
'is_richtext' => '是否富文本:0=否,1=是',
|
||
'content' => '消息内容',
|
||
'status' => '状态:0=草稿,1=已发布',
|
||
'order_id' => '订单ID',
|
||
'created_at' => '添加时间',
|
||
'updated_at' => '更新时间',
|
||
'deleted_at' => '删除时间',
|
||
'published_at' => '发布时间',
|
||
'is_delete' => '是否删除:0=否,1=是',
|
||
];
|
||
}
|
||
|
||
public function beforeSave($insert) {
|
||
if(parent::beforeSave($insert)){
|
||
if($this->isNewRecord){
|
||
$this->created_at = time();
|
||
}
|
||
$this->updated_at = time();
|
||
if($this->is_delete == 1)
|
||
$this->deleted_at = time();
|
||
$this->htmlTagFilter();
|
||
return true;
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public function htmlTagFilter()
|
||
{
|
||
$this->title = Model::htmlTagFilter($this->title);
|
||
$this->content = Model::htmlTagFilter($this->content);
|
||
}
|
||
|
||
|
||
public static function statusLabels()
|
||
{
|
||
return [
|
||
'0' => '草稿',
|
||
'1' => '已发布',
|
||
];
|
||
}
|
||
|
||
public static function getStatus($status)
|
||
{
|
||
$labels = self::statusLabels();
|
||
return isset($labels[$status]) ? $labels[$status] : "未知";
|
||
}
|
||
|
||
/**
|
||
* 发送系统消息通知
|
||
* @param integer $user_id 用户ID
|
||
* @param string $title 标题
|
||
* @param string $content 消息内容
|
||
* @param integer $type 消息类型
|
||
* @param integer $is_richtext 是否富文本
|
||
* @param integer $order_id 订单ID
|
||
* @param integer $cx_mch_id 平台商户ID
|
||
*/
|
||
public static function sendSysMsgToUser($user_id, $title, $content, $type, $is_richtext = 0, $order_id = 0, $cx_mch_id = 0)
|
||
{
|
||
$model = new SysMsg();
|
||
$model->cx_mch_id = $cx_mch_id;
|
||
$model->type = $type;
|
||
$model->to_user_id = $user_id;
|
||
$model->title = $title;
|
||
$model->content = $content;
|
||
$model->status = SysMsg::STATUS_PUBLISHED;
|
||
$model->published_at = time();
|
||
$model->is_richtext = $is_richtext;
|
||
$model->order_id = $order_id;
|
||
if(!$model->save()){
|
||
return (new Model())->getModelError($model);
|
||
}
|
||
return Model::asReturnSuccess();
|
||
}
|
||
}
|