Message.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. Db::startTrans();
  84. //系统消息
  85. //全部未读
  86. $rs = Db::name('user_messagesys')->where('user_id',$this->auth->id)->delete();
  87. if($rs === false){
  88. Db::rollback();
  89. $this->error('操作失败');
  90. }
  91. //循环全部已读
  92. $list2 = Db::name('message_sys')->column('id');
  93. if(!empty($list2)){
  94. $newall = [];
  95. foreach($list2 as $key => $msg_id){
  96. $newall[] = [
  97. 'user_id' => $this->auth->id,
  98. 'msg_id' => $msg_id,
  99. ];
  100. }
  101. if(!empty($newall)){
  102. $rs = Db::name('user_messagesys')->insertAll($newall);
  103. if($rs === false){
  104. Db::rollback();
  105. $this->error('操作失败');
  106. }
  107. }
  108. }
  109. Db::commit();
  110. $this->success("获取成功!",$list);
  111. }
  112. }