Message.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\service\CommonService;
  5. use app\common\service\MessageService;
  6. use think\exception\HttpResponseException;
  7. use think\Validate;
  8. class Message extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = ['*'];
  12. public function list()
  13. {
  14. try {
  15. $user_id = $this->auth->id ?? -1;
  16. $page = $this->request->get('page', 1);
  17. $page_size = $this->request->get('pageSize', 10);
  18. $where = [
  19. 'to_user_id' => $user_id
  20. ];
  21. $unreadCount = MessageService::where($where + ['is_read' => CommonService::STATUS_OFF])->count();
  22. $list = MessageService::getList($where, $page_size, 'id desc')->toArray();
  23. $list['unread_count'] = $unreadCount;
  24. $this->success('', $list);
  25. } catch (\Exception $e) {
  26. if ($e instanceof HttpResponseException) {
  27. throw $e;
  28. }
  29. $this->error($e->getMessage());
  30. }
  31. }
  32. /**
  33. * 详情
  34. * @return void
  35. */
  36. public function detail()
  37. {
  38. $user_id = $this->auth->id ?? -1;
  39. $id = $this->request->get('id', 0);
  40. $info = MessageService::getOne(['id' => $id, 'to_user_id' => $user_id]);
  41. $this->success('success', $info);
  42. }
  43. /**
  44. * @return void
  45. */
  46. public function setRead()
  47. {
  48. try {
  49. $user_id = $this->auth->id ?? -1;
  50. $id = $this->request->post('id', 0);
  51. $isAll = $this->request->post('isAll', 0);
  52. $where = ['id' => $id, 'to_user_id' => $user_id];
  53. if ($isAll) {
  54. unset($where['id']);
  55. }
  56. $res = MessageService::where($where)->update(['is_read' => CommonService::STATUS_ON]);
  57. if ($res === false) {
  58. exception('操作失败');
  59. }
  60. $this->success('操作成功');
  61. } catch (\Exception $e) {
  62. if ($e instanceof HttpResponseException) {
  63. throw $e;
  64. }
  65. $this->error($e->getMessage());
  66. }
  67. }
  68. }