Message.php 4.1 KB

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