197 lines
6.4 KiB
PHP
197 lines
6.4 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 = '',$image = '',$phone = ''){
|
||
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(),
|
||
'image_list' => $image,
|
||
]);
|
||
// 关联用户信息
|
||
if($member_id == 'all'){
|
||
// 发送给全部用户的消息
|
||
$time = time();
|
||
$insertData = Db::name('member')
|
||
->field("site_id,member_id,{$messageId} as message_center_id,{$time} as created_time,{$phone} as phone")
|
||
->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,
|
||
'phone' => $phone,
|
||
]);
|
||
}
|
||
|
||
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();
|
||
}
|
||
/**
|
||
* Common: 获取留言信息
|
||
* Author: wu-hui
|
||
* Time: 2023/03/04 18:07
|
||
* @param $siteId
|
||
* @return array
|
||
* @throws \think\db\exception\DbException
|
||
*/
|
||
public function getRemarks($siteId){
|
||
// 参数获取
|
||
$page = input('page',1);
|
||
$pageSize = input('page_size',PAGE_LIST_ROWS);
|
||
$nickname = input('nickname','');
|
||
$type = (int)input('type',0);
|
||
// 条件生成
|
||
$where = [
|
||
['a.site_id','=',$siteId],
|
||
['a.type','=',4]
|
||
];
|
||
if($nickname) $where[] = ['m.username|m.nickname','like',"%{$nickname}%"];
|
||
if($type > 0) $where[] = ['mcr.is_see','=',$type == 1 ? 0 : 1];// 是否已读:0=未读,1=已读
|
||
// 列表获取
|
||
$result = Db::name('message_center')
|
||
->alias('a')
|
||
->field('a.id,a.type,a.created_time,mcr.phone,mcr.member_id,mcr.is_see,m.username,m.nickname,m.headimg')
|
||
->join('message_center_read mcr','mcr.message_center_id = a.id', 'left')
|
||
->join('member m','m.member_id = mcr.member_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/06 9:38
|
||
* @param array $where
|
||
* @param string $field
|
||
* @return array
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function getInfo($where = [], $field = '*'){
|
||
$result = Db::name('message_center')
|
||
->alias('a')
|
||
->field($field)
|
||
->join('message_center_read mcr','mcr.message_center_id = a.id', 'left')
|
||
->join('member m','m.member_id = mcr.member_id','left')
|
||
->where($where)
|
||
->find();
|
||
|
||
return $this->success($result);
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
} |