123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- /**
- * 消息
- */
- class Message extends Api
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = ['*'];
- //消息页信息
- public function index(){
- $rs = [
- 'msg_first' => Db::name('message')->where('user_id',$this->auth->id)->order('id desc')->find(),
- 'msg_unread_num' => Db::name('message')->where('user_id',$this->auth->id)->where('status',0)->count(),
- 'msgsys_first' => Db::name('message_sys')->order('id desc')->find(),
- ];
- //系统消息未读数量
- $sys_ids = Db::name('message_sys')->column('id');
- $all_num = count($sys_ids);
- $read_num = Db::name('user_messagesys')->where('user_id',$this->auth->id)->where('msg_id','IN',$sys_ids)->count();
- $rs['msgsys_unread_num'] = $all_num > $read_num ? $all_num - $read_num : 0; //因公告被删除,未读公告没来得及删除,相减可能会负数
- $this->success(1,$rs);
- }
- //个人消息全部改为已读
- public function message_read(){
- Db::startTrans();
- //读取即为已读
- $map = [
- 'user_id' => $this->auth->id,
- 'status' => 0,
- ];
- $rs = Db::name('message')->where($map)->update(['status'=>1]);
- if($rs === false){
- Db::rollback();
- $this->error('操作失败');
- }
- //系统消息
- //全部未读
- $rs = Db::name('user_messagesys')->where('user_id',$this->auth->id)->delete();
- if($rs === false){
- Db::rollback();
- $this->error('操作失败');
- }
- //循环全部已读
- $list = Db::name('message_sys')->column('id');
- if(!empty($list)){
- $newall = [];
- foreach($list as $key => $msg_id){
- $newall[] = [
- 'user_id' => $this->auth->id,
- 'msg_id' => $msg_id,
- ];
- }
- if(!empty($newall)){
- $rs = Db::name('user_messagesys')->insertAll($newall);
- if($rs === false){
- Db::rollback();
- $this->error('操作失败');
- }
- }
- }
- Db::commit();
- $this->success();
- }
- //我的个人消息列表
- public function mylist(){
- $list = Db::name('message')->where('user_id',$this->auth->id)->autopage()->order('id desc')->select();
- //读取即为已读
- $map = [
- 'user_id' => $this->auth->id,
- 'status' => 0,
- ];
- Db::name('message')->where($map)->update(['status'=>1]);
- $this->success('success',$list);
- }
- /*
- * 获取系统消息列表
- */
- public function getmessagesys() {
- $list = Db::name('message_sys')->autopage()->order('id desc')->select();
- Db::startTrans();
- //系统消息
- //全部未读
- $rs = Db::name('user_messagesys')->where('user_id',$this->auth->id)->delete();
- if($rs === false){
- Db::rollback();
- $this->error('操作失败');
- }
- //循环全部已读
- $list2 = Db::name('message_sys')->column('id');
- if(!empty($list2)){
- $newall = [];
- foreach($list2 as $key => $msg_id){
- $newall[] = [
- 'user_id' => $this->auth->id,
- 'msg_id' => $msg_id,
- ];
- }
- if(!empty($newall)){
- $rs = Db::name('user_messagesys')->insertAll($newall);
- if($rs === false){
- Db::rollback();
- $this->error('操作失败');
- }
- }
- }
- Db::commit();
- $this->success("获取成功!",$list);
- }
- }
|