DiverseYouthNightSchool/application/common/model/school/MessageConfig.php

286 lines
7.7 KiB
PHP
Raw Normal View History

<?php
namespace app\common\model\school;
2024-12-23 18:21:02 +08:00
use bw\Common;
use think\Cache;
use think\Model;
use traits\model\SoftDelete;
class MessageConfig extends Model
{
use SoftDelete;
// 表名
protected $name = 'school_message_config';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'integer';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = 'deletetime';
2024-12-23 18:21:02 +08:00
protected $messageTemplate = null;
protected $templateData = [];
protected $templateString = "";
protected $messageMiniType = "other";
protected $messagePlatform="user";
protected $messageOperId=0;
protected $messageOperType="system";
protected $messageToType="user";
protected $messageStatus = "system";
protected $messageParams = [];
2025-01-02 18:07:25 +08:00
protected $message_title = "";
2024-12-23 18:21:02 +08:00
//创建上面变量的get和set方法
public function setMessageMiniType($value){
$this->messageMiniType = $value;
return $this;
}
public function getMessageMiniType(){
return $this->messageMiniType;
}
public function setMessagePlatform($value){
$this->messagePlatform = $value;
return $this;
}
public function getMessagePlatform(){
return $this->messagePlatform;
}
public function setMessageOperId($value){
$this->messageOperId = $value;
return $this;
}
public function getMessageOperId(){
return $this->messageOperId;
}
public function setMessageOperType($value){
$this->messageOperType = $value;
return $this;
}
public function getMessageOperType(){
return $this->messageOperType;
}
public function setMessageToType($value){
$this->messageToType = $value;
return $this;
}
public function getMessageToType(){
return $this->messageToType;
}
public function setMessageStatus($value){
$this->messageStatus = $value;
return $this;
}
public function getMessageStatus(){
return $this->messageStatus;
}
public function setMessageParams($value){
$this->messageParams = $value;
return $this;
}
public function getMessageParams(){
return $this->messageParams;
}
protected $toUid = 0;
// 追加属性
protected $append = [
'status_text',
'wechat_wap_text',
'message_text',
'selfmail_text'
];
protected static function init()
{
self::afterInsert(function ($row) {
if (!$row['weigh']) {
$pk = $row->getPk();
$row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
}
});
}
public function getStatusList()
{
return ['1' => __('Status 1'), '2' => __('Status 2')];
}
public function getWechatWapList()
{
return ['1' => __('Wechat_wap 1'), '2' => __('Wechat_wap 2')];
}
public function getMessageList()
{
return ['1' => __('Message 1'), '2' => __('Message 2')];
}
public function getSelfmailList()
{
return ['1' => __('Selfmail 1'), '2' => __('Selfmail 2')];
}
public function getStatusTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
$list = $this->getStatusList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getWechatWapTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['wechat_wap']) ? $data['wechat_wap'] : '');
$list = $this->getWechatWapList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getMessageTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['message']) ? $data['message'] : '');
$list = $this->getMessageList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getSelfmailTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['selfmail']) ? $data['selfmail'] : '');
$list = $this->getSelfmailList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getLogoImageAttr($value, $data)
{
if (!empty($value)) return cdnurl($value, true);
}
2024-12-23 18:21:02 +08:00
public function setTemplate($event_name,$params=[],$to_uid=0){
$this->messageTemplate = MessageConfigItem::where("event_two",$event_name)->find();
if(!$this->messageTemplate) throw new \Exception("消息模板不存在");
//设置模板内容
if($params){
$this->setTemplateData($params);
}
if($to_uid)$this->setToUid($to_uid);
return $this;
}
public function setTemplateData( $params,$expression = '{{KEYWORD}}'){
$this->templateData = $params;
$this->setMessageContent($expression);
return $this;
}
public function getMessageContent(){
return $this->templateString;
}
2025-01-02 18:07:25 +08:00
2024-12-23 18:21:02 +08:00
public function getTemplate(){
return $this->messageTemplate;
}
public function getToUid(){
return $this->toUid;
}
2024-12-23 18:21:02 +08:00
public function setToUid($uid){
$this->toUid = $uid;
return $this;
}
2025-01-02 18:07:25 +08:00
public function getMessageTitle(){
return $this->message_title;
}
public function setMessageTitle($title){
$this->message_title = $title;
return $this;
}
2024-12-23 18:21:02 +08:00
/**
* 得到语音播报内容(讯飞语音)
*/
public function setMessageContent($expression = '{{KEYWORD}}'){
$template = $this->messageTemplate;
$template_content = $template['selfmail_template_text'];
//解析获取文本内容
$this->templateString = Common::parsePrintTemplateString($template_content,$this->templateData,$expression);
2025-01-02 18:07:25 +08:00
$message_title = $template['selfmail_title'];
//解析获取文本内容
$this->message_title = Common::parsePrintTemplateString($message_title,$this->templateData,$expression);
2024-12-23 18:21:02 +08:00
return $this;
}
2024-12-23 18:21:02 +08:00
public function sendSelfmail($to_uid=0){
if($to_uid)$this->setToUid($to_uid);
$template = $this->messageTemplate;
if(!$template) return true;
if($template['selfmail'] == '1'){
if(!$this->toUid) throw new \Exception("发送对象不能为空");
$message_content = $this->getMessageContent();
2025-01-02 18:07:25 +08:00
$message_title = $this->getMessageTitle();
2024-12-23 18:21:02 +08:00
if(!$message_content || !$message_title)return false;
Message::$event_name = $template["event"];
Message::send($message_title,$message_content,$this->getMessageMiniType(),$this->toUid,$this->messageToType,$this->messageStatus,$this->messagePlatform,$this->messageParams,$this->messageOperId, $this->messageOperType);
}else{
return true;
}
}
public function sendWechatWap($to_uid=0){
if($to_uid)$this->setToUid($to_uid);
$template = $this->messageTemplate;
if(!$template) return true;
if($template['wechat_wap'] == '1'){
if(!$this->toUid) throw new \Exception("发送对象不能为空");
}else{
return true;
}
}
public function sendMobileMessage($to_uid=0){
if($to_uid)$this->setToUid($to_uid);
$template = $this->messageTemplate;
if(!$template) return true;
if($template['message'] == '1'){
if(!$this->toUid) throw new \Exception("发送对象不能为空");
}else{
return true;
}
}
public function sendMessage($to_uid=0){
$this->sendSelfmail($to_uid);
$this->sendMobileMessage($to_uid);
$this->sendWechatWap($to_uid);
return $this;
}
}