Message.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 系统消息
  7. */
  8. class Message extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = ['*'];
  12. //消息页信息
  13. public function index(){
  14. $rs = [
  15. 'msg_first' => Db::name('message')->where('user_id',$this->auth->id)->order('id desc')->find(),
  16. 'msg_unread_num' => Db::name('message')->where('user_id',$this->auth->id)->where('status',0)->count(),
  17. 'msgsys_first' => Db::name('message_sys')->order('id desc')->find(),
  18. 'dtmsg_unread_num' => Db::name('topic_dongtai_message')->where('user_id',$this->auth->id)->where('status',0)->count(),
  19. ];
  20. //系统消息未读数量
  21. $sys_ids = Db::name('message_sys')->column('id');
  22. $all_num = count($sys_ids);
  23. $read_num = Db::name('user_messagesys')->where('user_id',$this->auth->id)->where('msg_id','IN',$sys_ids)->count();
  24. $rs['msgsys_unread_num'] = $all_num > $read_num ? $all_num - $read_num : 0; //因公告被删除,未读公告没来得及删除,相减可能会负数
  25. $this->success(1,$rs);
  26. }
  27. //个人消息全部改为已读
  28. public function message_read(){
  29. Db::startTrans();
  30. //读取即为已读
  31. $map = [
  32. 'user_id' => $this->auth->id,
  33. 'status' => 0,
  34. ];
  35. $rs = Db::name('message')->where($map)->update(['status'=>1]);
  36. if($rs === false){
  37. Db::rollback();
  38. $this->error('操作失败');
  39. }
  40. //系统消息
  41. //全部未读
  42. $rs = Db::name('user_messagesys')->where('user_id',$this->auth->id)->delete();
  43. if($rs === false){
  44. Db::rollback();
  45. $this->error('操作失败');
  46. }
  47. //循环全部已读
  48. $list = Db::name('message_sys')->column('id');
  49. if(!empty($list)){
  50. $newall = [];
  51. foreach($list as $key => $msg_id){
  52. $newall[] = [
  53. 'user_id' => $this->auth->id,
  54. 'msg_id' => $msg_id,
  55. ];
  56. }
  57. if(!empty($newall)){
  58. $rs = Db::name('user_messagesys')->insertAll($newall);
  59. if($rs === false){
  60. Db::rollback();
  61. $this->error('操作失败');
  62. }
  63. }
  64. }
  65. Db::commit();
  66. $this->success();
  67. }
  68. //我的个人消息列表
  69. public function mylist(){
  70. $list = Db::name('message')->where('user_id',$this->auth->id)->autopage()->order('id desc')->select();
  71. //读取即为已读
  72. $map = [
  73. 'user_id' => $this->auth->id,
  74. 'status' => 0,
  75. ];
  76. Db::name('message')->where($map)->update(['status'=>1]);
  77. $this->success('success',$list);
  78. }
  79. /*
  80. * 获取系统消息列表
  81. */
  82. public function getmessagesys() {
  83. $list = Db::name('message_sys')->autopage()->order('id desc')->select();
  84. $list = list_domain_image($list,['image']);
  85. Db::startTrans();
  86. //系统消息
  87. //全部未读
  88. $rs = Db::name('user_messagesys')->where('user_id',$this->auth->id)->delete();
  89. if($rs === false){
  90. Db::rollback();
  91. $this->error('操作失败');
  92. }
  93. //循环全部已读
  94. $list2 = Db::name('message_sys')->column('id');
  95. if(!empty($list2)){
  96. $newall = [];
  97. foreach($list2 as $key => $msg_id){
  98. $newall[] = [
  99. 'user_id' => $this->auth->id,
  100. 'msg_id' => $msg_id,
  101. ];
  102. }
  103. if(!empty($newall)){
  104. $rs = Db::name('user_messagesys')->insertAll($newall);
  105. if($rs === false){
  106. Db::rollback();
  107. $this->error('操作失败');
  108. }
  109. }
  110. }
  111. Db::commit();
  112. $this->success("获取成功!",$list);
  113. }
  114. //系统消息详情
  115. public function messagesys_info(){
  116. $id = input('id',0);
  117. $info = Db::name('message_sys')->where('id',$id)->find();
  118. $info = info_domain_image($info,['image']);
  119. $this->success("获取成功!",$info);
  120. }
  121. }