Api.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace addons\notice\controller;
  3. use app\admin\library\Auth;
  4. use think\Cache;
  5. class Api extends \app\common\controller\Api
  6. {
  7. protected $noNeedRight = ['*'];
  8. protected $noNeedLogin = ['cache'];
  9. // 未读消息数量
  10. public function unread()
  11. {
  12. $user = $this->auth->getUser();
  13. $count = \app\admin\model\notice\Notice::where('to_id', $user['id'])
  14. ->where('platform', 'user')
  15. ->where('type','msg')
  16. ->order('id', 'desc')
  17. ->whereNull('readtime')
  18. ->count();
  19. $this->success('', $count);
  20. }
  21. // 我的站内消息
  22. public function index()
  23. {
  24. $user = $this->auth->getUser();
  25. $list = \app\admin\model\notice\Notice::where('to_id', $user['id'])
  26. ->where('platform', 'user')
  27. ->where('type','msg')
  28. ->order('id', 'desc')
  29. ->paginate();
  30. $is = true;
  31. if ($is) {
  32. \app\admin\model\notice\Notice::where('id', 'in',array_column($list->items(), 'id'))
  33. ->update(['readtime' => time()]);
  34. }
  35. $this->success('', $list);
  36. }
  37. // 标记为已读
  38. public function mark()
  39. {
  40. $user = $this->auth->getUser();
  41. $where = [];
  42. if (input('id')) {
  43. $where['id'] = input('id');
  44. }
  45. $count = \app\admin\model\notice\Notice::where('to_id', $user['id'])
  46. ->where($where)
  47. ->where('platform', 'user')
  48. ->where('type','msg')
  49. ->order('id', 'desc')
  50. ->whereNull('readtime')
  51. ->update(['readtime' => time()]);
  52. $this->success('', $count);
  53. }
  54. // 获取最新一条未读数据
  55. public function statistical()
  56. {
  57. $user = $this->auth->getUserInfo();
  58. $statisticalTime = Cache::get('notice_user_statistical_time_'.$user['id'], 0);
  59. $new = \app\admin\model\notice\Notice::where('to_id', $user['id'])
  60. ->where('platform', 'user')
  61. ->where('type','msg')
  62. ->order('id', 'desc')
  63. ->where('createtime','>', $statisticalTime)
  64. ->whereNull('readtime')
  65. ->find();
  66. if ($new) {
  67. Cache::set('notice_user_statistical_time_'.$user['id'], time());
  68. }
  69. $data = [
  70. 'num' => \app\admin\model\notice\Notice::where('to_id', $user['id'])
  71. ->where('platform', 'user')
  72. ->where('type','msg')
  73. ->order('id', 'desc')
  74. ->whereNull('readtime')
  75. ->count()
  76. ,
  77. 'new' => $new,
  78. ];
  79. $this->success('', $data );
  80. }
  81. // 缓存最后提示站内消息时间
  82. public function cache()
  83. {
  84. if (!$this->request->isPost()) {
  85. $this->error('请求方式错误');
  86. }
  87. $type = input('module', 'admin');
  88. $time = input('time');
  89. if ($type == 'admin') {
  90. $adminAuth = Auth::instance();
  91. if (!$adminAuth->isLogin()) {
  92. $this->error('未登录');
  93. }
  94. Cache::set('notice_admin_statistical_time_'.$adminAuth->id, $time);
  95. }
  96. if ($type == 'index') {
  97. $auth = $this->auth;
  98. if (!$auth->isLogin()) {
  99. $this->error('未登录');
  100. }
  101. Cache::set('notice_user_statistical_time_'.$auth->id, $time);
  102. }
  103. $this->success('ok');
  104. }
  105. }