123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 工作台及基础
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\admin\controller;
  8. use app\admin\logic\MessageLogic;
  9. use think\Db;
  10. use think\Hook;
  11. use app\admin\model\Message as MessageModel;
  12. use think\Request;
  13. class Message extends ApiCommon
  14. {
  15. /**
  16. * 用于判断权限
  17. * @permission 无限制
  18. * @allow 登录用户可访问
  19. * @other 其他根据系统设置
  20. **/
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $action = [
  25. 'permission' => [],
  26. 'allow' => ['index', 'markedRead', 'messagelist', 'updatemessage','delete','readallmessage','clear','unreadcount'],
  27. ];
  28. Hook::listen('check_auth', $action);
  29. $request = Request::instance();
  30. $a = strtolower($request->action());
  31. if (!in_array($a, $action['permission'])) {
  32. parent::_initialize();
  33. }
  34. }
  35. /**
  36. * 系统信息
  37. */
  38. public function index()
  39. {
  40. $param = $this->param;
  41. $userInfo = $this->userInfo;
  42. $type = $param['type'] ?: 'all';
  43. if ($type != 'all' && !isset(MessageModel::$typeGroup[$type])) {
  44. return resultArray(['error' => '参数错误']);
  45. }
  46. $where = ['to_user_id' => $userInfo['id']];
  47. if ($type != 'all') {
  48. $where['type'] = ['IN', MessageModel::$typeGroup[$type]];
  49. }
  50. $order = [
  51. 'read_time' => 'ASC',
  52. 'send_time' => 'DESC',
  53. ];
  54. $page = $param['page'] ?: 1;
  55. $limit = $param['limit'] ?: 15;
  56. $data = MessageModel::where($where)
  57. ->order($order)
  58. ->paginate($limit)
  59. ->each(function ($val) {
  60. $val['relation_title'] = $val->relation_title;
  61. $val['from_user_id_info'] = $val->from_user_id_info;
  62. })
  63. ->toArray();
  64. return resultArray([
  65. 'data' => [
  66. 'list' => $data['data'],
  67. 'dataCount' => $data['total']
  68. ]
  69. ]);
  70. }
  71. /**
  72. * 阅读系统通知,修改状态为已读
  73. */
  74. public function markedRead()
  75. {
  76. $userInfo = $this->userInfo;
  77. $param = $this->param;
  78. $where = [
  79. 'to_user_id' => $userInfo['id'],
  80. 'message_id' => ['IN', (array)$param['message_id']],
  81. 'read_time' => 0,
  82. ];
  83. $res = MessageModel::where($where)->update(['read_time' => time()]);
  84. return resultArray(['data' => $res > 0]);
  85. }
  86. /**
  87. * 未读数
  88. */
  89. // public function unReadCount()
  90. // {
  91. // $data = [];
  92. // foreach (MessageModel::$typeGroup as $key => $val) {
  93. // $data[$key] = MessageModel::where(['type' => ['IN', $val]])->count();
  94. // }
  95. // $data['all'] = array_sum($data);
  96. // return resultArray(['data' => $data]);
  97. // }
  98. /**
  99. * 未读消息列表
  100. * @return \think\response\Json
  101. */
  102. public function messageList()
  103. {
  104. $param = $this->param;
  105. $userInfo = $this->userInfo;
  106. $param['user_id'] = $userInfo['id'];
  107. $messageLogic = new MessageLogic();
  108. $data = $messageLogic->getDataList($param);
  109. return resultArray(['data' => $data]);
  110. }
  111. /**
  112. *更新消息类型 已读未读
  113. */
  114. public function updateMessage()
  115. {
  116. $param = $this->param;
  117. $userInfo = $this->userInfo;
  118. $param['id']=$userInfo['id'];
  119. $messageLogic = new MessageLogic();
  120. $data = $messageLogic->endMessage($param);
  121. if(!$data){
  122. return resultArray(['data' => "操作失败!"]);
  123. }
  124. return resultArray(['data' => "操作成功!"]);
  125. }
  126. /**
  127. * 删除消息
  128. *
  129. * @throws \think\Exception
  130. * @throws \think\exception\PDOException
  131. */
  132. public function delete()
  133. {
  134. if (empty($this->param['message_id'])) return resultArray(['error' => '参数错误!']);
  135. $userInfo = $this->userInfo;
  136. $param = $this->param;
  137. $data['message_id'] = $param['message_id'];
  138. $data['user_id'] = $userInfo['id'];
  139. $messageLogic = new MessageLogic();
  140. if (!$messageLogic->delete($data)) return resultArray(['error' => '操作失败!']);
  141. return resultArray(['data' => '操作成功!']);
  142. }
  143. /**
  144. * 批量更新
  145. * @throws \think\Exception
  146. * @throws \think\exception\PDOException
  147. */
  148. public function readAllMessage(){
  149. $param = $this->param;
  150. $userInfo = $this->userInfo;
  151. $param['user_id'] = $userInfo['id'];
  152. $messageLogic = new MessageLogic();
  153. $data = $messageLogic->readAllMessage($param);
  154. return resultArray(['data' => "操作成功!"]);
  155. }
  156. /**
  157. * 批量删除
  158. * @throws \think\Exception
  159. * @throws \think\exception\PDOException
  160. */
  161. public function clear(){
  162. $param = $this->param;
  163. $userInfo = $this->userInfo;
  164. $param['user_id'] = $userInfo['id'];
  165. $messageLogic = new MessageLogic();
  166. $data = $messageLogic->clear($param);
  167. return resultArray(['data' => "操作成功!"]);
  168. }
  169. /**
  170. * 总数
  171. * @return \think\response\Json
  172. * @throws \think\Exception
  173. * @throws \think\exception\PDOException
  174. */
  175. public function unreadCount(){
  176. $param = $this->param;
  177. $userInfo = $this->userInfo;
  178. $param['user_id'] = $userInfo['id'];
  179. $messageLogic = new MessageLogic();
  180. $data = $messageLogic->unreadCount($param);
  181. return resultArray(['data' => $data]);
  182. }
  183. }