MessageController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace app\admin\controller\client;
  3. use app\controller\Curd;
  4. use app\model\ClientMessage;
  5. use app\model\Member;
  6. use support\exception\BusinessException;
  7. use support\Request;
  8. use support\Response;
  9. class MessageController extends Curd
  10. {
  11. public function __construct()
  12. {
  13. $this->model = new ClientMessage();
  14. }
  15. public function select(Request $request): Response
  16. {
  17. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  18. $order = $request->get('order', 'desc');
  19. $field = $field ?? 'client_message_addtimes';
  20. $query = $this->doSelect($where, $field, $order);
  21. return $this->doFormat($query, $format, $limit);
  22. }
  23. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  24. {
  25. $model = $this->model->with(['member', 'cert','info']);
  26. foreach ($where as $column => $value) {
  27. if (is_array($value)) {
  28. if ($value[0] === 'like' || $value[0] === 'not like') {
  29. $model = $model->where($column, $value[0], "%$value[1]%");
  30. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  31. $model = $model->where($column, $value[0], $value[1]);
  32. } elseif ($value[0] == 'in' && !empty($value[1])) {
  33. $valArr = $value[1];
  34. if (is_string($value[1])) {
  35. $valArr = explode(",", trim($value[1]));
  36. }
  37. $model = $model->whereIn($column, $valArr);
  38. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  39. $valArr = $value[1];
  40. if (is_string($value[1])) {
  41. $valArr = explode(",", trim($value[1]));
  42. }
  43. $model = $model->whereNotIn($column, $valArr);
  44. } elseif ($value[0] == 'null') {
  45. $model = $model->whereNull($column);
  46. } elseif ($value[0] == 'not null') {
  47. $model = $model->whereNotNull($column);
  48. } elseif ($value[0] !== '' || $value[1] !== '') {
  49. $model = $model->whereBetween($column, $value);
  50. }
  51. } else {
  52. $model = $model->where($column, $value);
  53. }
  54. }
  55. if ($field) {
  56. $model = $model->orderBy($field, $order);
  57. }
  58. return $model;
  59. }
  60. public function afterQuery($items)
  61. {
  62. foreach ($items as &$item) {
  63. if (!empty($item['client_message_body_json'])) {
  64. $bodyJson = json_decode($item['client_message_body_json'], true);
  65. if (isset($bodyJson['content'])) {
  66. $item['client_message_body_json'] = $bodyJson['content'];
  67. }
  68. }
  69. }
  70. return $items;
  71. }
  72. public function insert(Request $request): Response
  73. {
  74. if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
  75. return json_fail($this->validateClass->getError());
  76. }
  77. try {
  78. $params = $request->post();
  79. $memberIds = $params['join_client_message_recv_member_id'];
  80. foreach ($memberIds as $memberId){
  81. $params['join_client_message_send'] = 'SYSTEM';
  82. $params['join_client_message_recv_member_id'] = $memberId;
  83. $data = $this->insertInput($params);
  84. $this->doInsert($data);
  85. }
  86. } catch (BusinessException $customException) {
  87. return json_fail($customException->getMessage());
  88. } catch (\Exception $e) {
  89. dump($e->getMessage());
  90. return json_fail('数据写入失败11');
  91. }
  92. return json_success('success');
  93. }
  94. protected function insertInput($params): array
  95. {
  96. $data = $this->inputFilter($params);
  97. $title = self::$classify[$data['client_message_classify']] ?? '';
  98. $data['client_message_header_json'] = json_encode(['title'=>$title]);
  99. if (!empty($data['client_message_sendtime'])) {
  100. $data['client_message_sendtime'] = date('Y-m-d H:i:s', strtotime($data['client_message_sendtime']));
  101. }
  102. if (!empty($data['client_message_header_json']) && !is_json($data['client_message_header_json'])) {
  103. $data['client_message_header_json'] = json_encode(['header' => $data['client_message_header_json']]);
  104. }
  105. if (!empty($data['client_message_body_json']) && !is_json($data['client_message_body_json'])) {
  106. $data['client_message_body_json'] = json_encode(['content' => $data['client_message_body_json']]);
  107. }
  108. return $data;
  109. }
  110. /**
  111. * 群发
  112. */
  113. public function groupInsert(Request $request)
  114. {
  115. try {
  116. $params = $request->post();
  117. $memberIds = Member::where('member_status','ACTIVED')->pluck('member_id');
  118. foreach ($memberIds as $memberId){
  119. $params['join_client_message_send'] = 'SYSTEM';
  120. $params['join_client_message_recv_member_id'] = $memberId;
  121. $data = $this->insertInput($params);
  122. $this->doInsert($data);
  123. }
  124. } catch (BusinessException $customException) {
  125. return json_fail($customException->getMessage());
  126. } catch (\Exception $e) {
  127. dump($e->getMessage());
  128. return json_fail('数据写入失败11');
  129. }
  130. return json_success('success');
  131. }
  132. protected function updateInput(Request $request): array
  133. {
  134. $params = $request->post();
  135. $primary_key = $this->model->getKeyName();
  136. $id = $request->post($primary_key);
  137. $data = $this->inputFilter($params);
  138. $model = $this->model->find($id);
  139. if (!$model) {
  140. throw new BusinessException('记录不存在', 2);
  141. }
  142. $headerJson = [];
  143. if(!empty($model->client_message_header_json)){
  144. $headerJson = json_decode($model->client_message_header_json,true);
  145. }
  146. $headerJson['title'] = self::$classify[$data['client_message_classify']] ?? '';
  147. $data['client_message_header_json'] = json_encode($headerJson);
  148. if(!empty($params['header_title'])){
  149. $headerJson['title'] = $params['header_title'];
  150. $data['client_message_header_json'] = json_encode($headerJson);
  151. }
  152. if (!empty($data['client_message_datetimes'])) {
  153. $data['client_message_sendtime'] = date('Y-m-d H:i:s', strtotime($data['client_message_sendtime']));
  154. }
  155. if (!empty($data['client_message_header_json']) && !is_json($data['client_message_header_json'])) {
  156. $data['client_message_header_json'] = json_encode(['header' => $data['client_message_header_json']]);
  157. }
  158. if (!empty($data['client_message_body_json']) && !is_json($data['client_message_body_json'])) {
  159. $data['client_message_body_json'] = json_encode(['content' => $data['client_message_body_json']]);
  160. }
  161. unset($data[$primary_key]);
  162. return [$id, $data];
  163. }
  164. public static $classify = [
  165. 'WARNING' => '预警消息',
  166. 'REMIND' => '提醒消息',
  167. 'NOTICE' =>'系统通知',
  168. 'APPOINTMENT'=>'预约(服务)消息',
  169. 'ORDER'=>'订单消息',
  170. 'CHAT'=>'未读消息'
  171. ];
  172. }