Keyboard.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Services\BaseService;
  6. use App\Services\KeyboardService;
  7. use Exception;
  8. use Illuminate\Http\JsonResponse;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Validation\ValidationException;
  11. class Keyboard extends Controller
  12. {
  13. /**
  14. * @api {get} /admin/keyboard 回复列表
  15. * @apiGroup 回复管理
  16. *
  17. * @apiUse result
  18. * @apiUse header
  19. * @apiVersion 1.0.0
  20. *
  21. * @apiParam {int} [page=1]
  22. * @apiParam {int} [limit=10]
  23. * @apiParam {string} [button] 关键字
  24. *
  25. * @apiSuccess (data) {Object} data
  26. * @apiSuccess (data) {int} data.total 数量
  27. * @apiSuccess (data) {Object[]} data.data 列表
  28. * @apiSuccess (data) {int} data.data.id
  29. * @apiSuccess (data) {string} data.data.button 回复关键字
  30. * @apiSuccess (data) {string} data.data.replay 回复内容
  31. * @apiSuccess (data) {string} data.data.updated_at
  32. * @apiSuccess (data) {string} data.data.created_at
  33. */
  34. public function index()
  35. {
  36. try {
  37. $search = request()->validate([
  38. 'page' => ['nullable', 'integer', 'min:1'],
  39. 'limit' => ['nullable', 'integer', 'min:1'],
  40. 'button' => ['nullable', 'string'],
  41. 'language' => ['nullable', 'string', 'in:zh,vi,en'],
  42. 'explain' => ['nullable', 'string'],
  43. 'group_id' => ['nullable', 'integer', 'in:0,1'],
  44. ]);
  45. $result = KeyboardService::paginate($search);
  46. } catch (ValidationException $e) {
  47. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  48. } catch (Exception $e) {
  49. return $this->error($e->getCode(), $e->getMessage());
  50. }
  51. return $this->success($result);
  52. }
  53. /**
  54. * @api {post} /admin/keyboard/submit 修改回复
  55. * @apiGroup 回复管理
  56. *
  57. * @apiUse result
  58. * @apiUse header
  59. * @apiVersion 1.0.0
  60. *
  61. * @apiParam {int} id 回复ID
  62. * @apiParam {string} button 关键字
  63. * @apiParam {string} reply 回复内容
  64. * @apiParam {array} buttons 操作按钮组
  65. */
  66. public function store(): JsonResponse
  67. {
  68. try {
  69. $id = request()->input('id');
  70. $validator = [
  71. 'id' => ['required', 'integer'],
  72. 'reply' => ['required', 'string'],
  73. 'explain' => ['nullable', 'string', 'max:28'],
  74. 'language' => ['required', 'string', 'in:zh,vi,en'],
  75. 'image' => ['nullable', 'url'],
  76. 'button' => ['nullable', 'string', 'max:100'],
  77. 'buttons' => ['array'],
  78. 'buttons.*' => ['array', 'min:1'],
  79. 'buttons.*.*.text' => ['required', 'string', 'min:1'],
  80. 'buttons.*.*.url' => ['required', 'string', 'min:1'],
  81. ];
  82. if (empty($id)) {
  83. unset($validator['id']);
  84. $validator['button'] = ['required', 'string', 'min:1', 'max:100'];
  85. }
  86. $params = request()->validate($validator);
  87. $ret = KeyboardService::submit($params);
  88. if ($ret['code'] == BaseService::NOT) {
  89. throw new Exception($ret['msg'], HttpStatus::CUSTOM_ERROR);
  90. }
  91. } catch (ValidationException $e) {
  92. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  93. } catch (Exception $e) {
  94. if ($e->getCode() == HttpStatus::CUSTOM_ERROR) {
  95. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  96. }
  97. return $this->error(intval($e->getCode()));
  98. }
  99. return $this->success([], $ret['msg']);
  100. }
  101. /**
  102. * @api {post} /admin/keyboard/delete 删除回复
  103. * @apiGroup 回复管理
  104. *
  105. * @apiUse result
  106. * @apiUse header
  107. * @apiVersion 1.0.0
  108. *
  109. * @apiParam {int} id 回复ID
  110. */
  111. public function destroy(): JsonResponse
  112. {
  113. $id = request()->post('id');
  114. // 示例:通过 ID 删除回复
  115. $info = KeyboardService::findOne(['id' => $id]);
  116. if (!$info) {
  117. return $this->error(HttpStatus::CUSTOM_ERROR, '数据不存在');
  118. }
  119. if ($info->group_id != 1) return $this->error(HttpStatus::CUSTOM_ERROR, '系统配置禁止删除');
  120. $info->delete();
  121. return $this->success([], '删除成功');
  122. }
  123. }