126 lines
4.0 KiB
PHP
126 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace addon\message\model;
|
|
|
|
use app\model\BaseModel;
|
|
use think\Exception;
|
|
use think\facade\Db;
|
|
|
|
class Message extends BaseModel{
|
|
|
|
private $types = [
|
|
1 => '交易信息',
|
|
2 => '系统消息',
|
|
3 => '通知消息',
|
|
4 => '留言反馈',
|
|
];
|
|
|
|
/**
|
|
* Common: 获取消息列表
|
|
* Author: wu-hui
|
|
* Time: 2023/03/04 15:02
|
|
* @return array
|
|
* @throws \think\db\exception\DbException
|
|
*/
|
|
public function getList($siteId){
|
|
// 参数获取
|
|
$page = input('page',1);
|
|
$pageSize = input('page_size',PAGE_LIST_ROWS);
|
|
$title = input('title','');
|
|
$type = input('type','');
|
|
// 条件生成
|
|
$where = [
|
|
['a.site_id','=',$siteId],
|
|
['a.type','in',[2,3]],
|
|
];
|
|
if($title) $where[] = ['a.message_title','like',"%{$title}%"];
|
|
if($type) $where[] = ['a.type','=',$type];
|
|
// 列表获取
|
|
$result = Db::name('message_center')
|
|
->alias('a')
|
|
->field('a.id,a.type,a.created_time,a.message_title,count(mcr.message_center_id) as total')
|
|
->join('message_center_read mcr','mcr.message_center_id = a.id', 'left')
|
|
->where($where)
|
|
->group('a.id')
|
|
->order('a.id','DESC')
|
|
->paginate(['list_rows' => $pageSize,'page' => $page]);
|
|
if($result) $result = $result->toArray();
|
|
|
|
$list = [
|
|
'count' => $result['total'],
|
|
'list' => $result['data'],
|
|
'page_count' => $result['last_page'],
|
|
];
|
|
return $this->success($list);
|
|
}
|
|
/**
|
|
* Common: 添加消息
|
|
* Author: wu-hui
|
|
* Time: 2023/03/04 14:09
|
|
* @param int $siteId
|
|
* @param string $content
|
|
* @param int $type
|
|
* @param string|int $member_id
|
|
* @param int $orderId
|
|
* @param string $title
|
|
* @return array
|
|
*/
|
|
public function addMessage(int $siteId,string $content,int $type,$member_id = 'all',int $orderId = 0,$title = ''){
|
|
if(!$content) return $this->error('','消息不能为空!');
|
|
// 记录消息信息
|
|
try{
|
|
$messageId = Db::name('message_center')
|
|
->insertGetId([
|
|
'site_id' => $siteId,
|
|
'type' => $type,
|
|
'message_title' => $title ?? $this->types[$type],
|
|
'message_content' => $content,
|
|
'created_time' => time()
|
|
]);
|
|
// 关联用户信息
|
|
if($member_id == 'all'){
|
|
// 发送给全部用户的消息
|
|
$time = time();
|
|
$insertData = Db::name('member')
|
|
->field("site_id,member_id,{$messageId} as message_center_id,{$time} as created_time")
|
|
->where('site_id',$siteId)
|
|
->select();
|
|
if($insertData){
|
|
$insertData = $insertData->toArray();
|
|
Db::name('message_center_read')->insertAll($insertData);
|
|
}
|
|
}
|
|
else{
|
|
// 发送给某个用户
|
|
Db::name('message_center_read')
|
|
->insert([
|
|
'site_id' => $siteId,
|
|
'member_id' => $member_id,
|
|
'created_time' => time(),
|
|
'order_id' => $orderId,
|
|
'message_center_id' => $messageId,
|
|
]);
|
|
}
|
|
|
|
return $this->success();
|
|
}catch(Exception $e){
|
|
return $this->error('',$e->getMessage());
|
|
}
|
|
|
|
}
|
|
/**
|
|
* Common: 删除消息
|
|
* Author: wu-hui
|
|
* Time: 2023/03/04 15:48
|
|
* @param $id
|
|
* @return array
|
|
*/
|
|
public function delInfo($id){
|
|
// 删除消息信息
|
|
Model('message_center')->delete(['id'=>$id]);
|
|
Model('message_center_read')->delete(['message_center_id'=>$id]);
|
|
|
|
return $this->success();
|
|
}
|
|
|
|
} |