MessageController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace app\admin\controller\client;
  3. use app\controller\Curd;
  4. use app\model\ClientMessage;
  5. use support\exception\BusinessException;
  6. use support\Request;
  7. use support\Response;
  8. class MessageController extends Curd
  9. {
  10. public function __construct()
  11. {
  12. $this->model = new ClientMessage();
  13. }
  14. public function select(Request $request): Response
  15. {
  16. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  17. $order = $request->get('order', 'desc');
  18. $field = $field ?? 'client_message_addtimes';
  19. $query = $this->doSelect($where, $field, $order);
  20. return $this->doFormat($query, $format, $limit);
  21. }
  22. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  23. {
  24. $model = $this->model->with(['member', 'cert']);
  25. foreach ($where as $column => $value) {
  26. if (is_array($value)) {
  27. if ($value[0] === 'like' || $value[0] === 'not like') {
  28. $model = $model->where($column, $value[0], "%$value[1]%");
  29. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  30. $model = $model->where($column, $value[0], $value[1]);
  31. } elseif ($value[0] == 'in' && !empty($value[1])) {
  32. $valArr = $value[1];
  33. if (is_string($value[1])) {
  34. $valArr = explode(",", trim($value[1]));
  35. }
  36. $model = $model->whereIn($column, $valArr);
  37. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  38. $valArr = $value[1];
  39. if (is_string($value[1])) {
  40. $valArr = explode(",", trim($value[1]));
  41. }
  42. $model = $model->whereNotIn($column, $valArr);
  43. } elseif ($value[0] == 'null') {
  44. $model = $model->whereNull($column);
  45. } elseif ($value[0] == 'not null') {
  46. $model = $model->whereNotNull($column);
  47. } elseif ($value[0] !== '' || $value[1] !== '') {
  48. $model = $model->whereBetween($column, $value);
  49. }
  50. } else {
  51. $model = $model->where($column, $value);
  52. }
  53. }
  54. if ($field) {
  55. $model = $model->orderBy($field, $order);
  56. }
  57. return $model;
  58. }
  59. public function afterQuery($items)
  60. {
  61. foreach ($items as &$item) {
  62. if (!empty($item['client_message_body_json'])) {
  63. $bodyJson = json_decode($item['client_message_body_json'], true);
  64. if (isset($bodyJson['content'])) {
  65. $item['client_message_body_json'] = $bodyJson['content'];
  66. }
  67. }
  68. }
  69. return $items;
  70. }
  71. public function insert(Request $request): Response
  72. {
  73. if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
  74. return json_fail($this->validateClass->getError());
  75. }
  76. try {
  77. $params = $request->post();
  78. $memberIds = $params['join_client_message_recv_member_id'];
  79. foreach ($memberIds as $memberId){
  80. $params['join_client_message_send'] = 'SYSTEM';
  81. $params['join_client_message_recv_member_id'] = $memberId;
  82. $data = $this->insertInput($params);
  83. $this->doInsert($data);
  84. }
  85. } catch (BusinessException $customException) {
  86. return json_fail($customException->getMessage());
  87. } catch (\Exception $e) {
  88. dump($e->getMessage());
  89. return json_fail('数据写入失败11');
  90. }
  91. return json_success('success');
  92. }
  93. protected function insertInput($params): array
  94. {
  95. $data = $this->inputFilter($params);
  96. if (!empty($data['client_message_sendtime'])) {
  97. $data['client_message_sendtime'] = date('Y-m-d H:i:s', strtotime($data['client_message_sendtime']));
  98. }
  99. if (!empty($data['client_message_header_json']) && !is_json($data['client_message_header_json'])) {
  100. $data['client_message_header_json'] = json_encode(['header' => $data['client_message_header_json']]);
  101. }
  102. if (!empty($data['client_message_body_json']) && !is_json($data['client_message_body_json'])) {
  103. $data['client_message_body_json'] = json_encode(['content' => $data['client_message_body_json']]);
  104. }
  105. return $data;
  106. }
  107. protected function updateInput(Request $request): array
  108. {
  109. $primary_key = $this->model->getKeyName();
  110. $id = $request->post($primary_key);
  111. $data = $this->inputFilter($request->post());
  112. $model = $this->model->find($id);
  113. if (!$model) {
  114. throw new BusinessException('记录不存在', 2);
  115. }
  116. if (!empty($data['client_message_datetimes'])) {
  117. $data['client_message_sendtime'] = date('Y-m-d H:i:s', strtotime($data['client_message_sendtime']));
  118. }
  119. if (!empty($data['client_message_header_json']) && !is_json($data['client_message_header_json'])) {
  120. $data['client_message_header_json'] = json_encode(['header' => $data['client_message_header_json']]);
  121. }
  122. if (!empty($data['client_message_body_json']) && !is_json($data['client_message_body_json'])) {
  123. $data['client_message_body_json'] = json_encode(['content' => $data['client_message_body_json']]);
  124. }
  125. unset($data[$primary_key]);
  126. return [$id, $data];
  127. }
  128. }