SysMessageController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace Modules\Manage\Controllers;
  3. use App\Constants\CommonConstant;
  4. use App\Model\ManagerModel;
  5. use App\Repositories\Criterias\OrderLimitCriteria;
  6. use App\Repositories\Criterias\SysMessagePagesCriteria;
  7. use App\Repositories\SysMessageRepository;
  8. use Carbon\Carbon;
  9. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  10. use Modules\Manage\Responses\CommonPagesResponse;
  11. class SysMessageController extends BaseController
  12. {
  13. public function lists(ValidatorFactoryInterface $validatorFactory)
  14. {
  15. $inputData = $this->getJsonData();
  16. $rules = [
  17. 'page' => 'nullable|integer',
  18. 'page_size' => 'nullable|integer',
  19. 'uid' => 'nullable|string',
  20. 'subject' => 'nullable|string',
  21. 'struct_type' => 'nullable|string',
  22. ];
  23. $validator = $validatorFactory->make($inputData, $rules);
  24. if ($validator->fails()) {
  25. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  26. return json_fail($validator->errors()->first());
  27. }
  28. $orders = [
  29. ['created_at', 'DESC'],
  30. ];
  31. $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
  32. if ($pageSize > 500) {
  33. $pageSize = CommonConstant::PAGE_SIZE;
  34. }
  35. $page = format_page($inputData['page']);
  36. $lists = make(SysMessageRepository::class)->pushCriteria(new SysMessagePagesCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page);
  37. return new CommonPagesResponse($lists);
  38. }
  39. public function create(ValidatorFactoryInterface $validatorFactory)
  40. {
  41. $inputData = $this->getJsonData();
  42. $rules = [
  43. 'subject' => 'required|string|max:255',
  44. 'struct_type' => 'required|string',
  45. 'content' => 'nullable|array',
  46. 'expired_at' => 'nullable'
  47. ];
  48. $validator = $validatorFactory->make($inputData, $rules);
  49. if ($validator->fails()) {
  50. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  51. return json_fail($validator->errors()->first());
  52. }
  53. if (!empty($inputData['expired_at'])) {
  54. if ($inputData['expired_at'] == '0000-00-00 00:00:00') {
  55. return json_fail('过期日期有误');
  56. }
  57. $month1 = intval(substr($inputData['expired_at'], 5, 2));
  58. $day1 = intval(substr($inputData['expired_at'], 8, 2));
  59. $year1 = intval(substr($inputData['expired_at'], 0, 4));
  60. if (!checkdate($month1, $day1, $year1)) {
  61. return json_fail('过期时间格式有误');
  62. }
  63. if (Carbon::parse($inputData['expired_at']) < Carbon::now()) {
  64. return json_fail('过期日期必须是当前以后的时间');
  65. }
  66. }
  67. $obj = make(SysMessageRepository::class)->createOne($inputData);
  68. if ($obj) {
  69. return json_success(['uid' => $obj->uid]);
  70. } else {
  71. return json_fail('添加系统消息失败');
  72. }
  73. }
  74. public function modify(ValidatorFactoryInterface $validatorFactory)
  75. {
  76. $inputData = $this->getJsonData();
  77. $rules = [
  78. 'uid' => 'required',
  79. 'subject' => 'required|string|max:255',
  80. 'struct_type' => 'required|string',
  81. 'content' => 'nullable|array',
  82. 'expired_at' => 'nullable'
  83. ];
  84. $validator = $validatorFactory->make($inputData, $rules);
  85. if ($validator->fails()) {
  86. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  87. return json_fail($validator->errors()->first());
  88. }
  89. if (!empty($inputData['expired_at'])) {
  90. if ($inputData['expired_at'] == '0000-00-00 00:00:00') {
  91. return json_fail('过期时间有误');
  92. }
  93. $month1 = intval(substr($inputData['expired_at'], 5, 2));
  94. $day1 = intval(substr($inputData['expired_at'], 8, 2));
  95. $year1 = intval(substr($inputData['expired_at'], 0, 4));
  96. if (!checkdate($month1, $day1, $year1)) {
  97. return json_fail('过期时间格式有误');
  98. }
  99. if (Carbon::parse($inputData['expired_at']) < Carbon::now()) {
  100. return json_fail('过期日期必须是当前以后的时间');
  101. }
  102. }
  103. $resultInfo = make(SysMessageRepository::class)->updateOne($inputData['uid'], $inputData);
  104. if ($resultInfo['is_ok']) {
  105. $logSubject = '编辑一个系统消息:'. $resultInfo['new']->subject;
  106. $logSvalue = $inputData['uid'];
  107. $logDbData = [
  108. 'after_items' => [
  109. $resultInfo['new']->toArray(),
  110. ],
  111. 'before_items' => [
  112. $resultInfo['old']->toArray(),
  113. ],
  114. ];
  115. $this->log($logSubject, $logSvalue, $logDbData);
  116. return json_success(['uid' => $inputData['uid']]);
  117. } else {
  118. return json_fail('编辑系统消息失败');
  119. }
  120. }
  121. public function delete(ValidatorFactoryInterface $validatorFactory)
  122. {
  123. $inputData = $this->getJsonData();
  124. $rules = [
  125. 'uid' => 'required',
  126. ];
  127. $validator = $validatorFactory->make($inputData, $rules);
  128. if ($validator->fails()) {
  129. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  130. return json_fail($validator->errors()->first());
  131. }
  132. $uid = data_get($inputData, 'uid', '');
  133. $resultInfo = make(SysMessageRepository::class)->deleteOne($uid);
  134. if ($resultInfo['is_ok']) {
  135. $logSubject = '删除一个系统消息:'. $resultInfo['old']->subject;
  136. $logSvalue = $uid;
  137. $logDbData = [
  138. 'after_items' => [
  139. ],
  140. 'before_items' => [
  141. ],
  142. ];
  143. $this->log($logSubject, $logSvalue, $logDbData);
  144. return json_success(['uid' => $uid]);
  145. } else {
  146. return json_fail('删除系统消息失败');
  147. }
  148. }
  149. public function batch()
  150. {
  151. $inputData = $this->getJsonData();
  152. $operateName = $inputData['operate_name'] ?? '';
  153. switch ($operateName) {
  154. case 'delete':
  155. $itemPks = $inputData['item_pks'] ?? [];
  156. if (!empty($itemPks)) {
  157. $resultInfo = make(SysMessageRepository::class)->deleteMulti($itemPks);
  158. if ($resultInfo['success_uids']) {
  159. $strPks = implode(',', $resultInfo['success_uids']);
  160. $logSubject = '批量删除多个系统消息';
  161. $logSvalue = $strPks;
  162. $logDbData = [
  163. 'after_items' => [
  164. ],
  165. 'before_items' => [],
  166. ];
  167. $this->log($logSubject, $logSvalue, $logDbData);
  168. }
  169. }
  170. $data = [
  171. 'result' => 'success',
  172. 'pks' => $itemPks,
  173. ];
  174. return json_success($data);
  175. break;
  176. default:
  177. break;
  178. }
  179. return json_fail('未知操作');
  180. }
  181. }