Keyboard.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. request()->validate([
  38. 'title' => ['nullable', 'string'],
  39. 'permission_name' => ['nullable', 'string'],
  40. ]);
  41. $search = request()->all();
  42. $result = KeyboardService::paginate($search);
  43. } catch (ValidationException $e) {
  44. return $this->error(HttpStatus::VALIDATION_FAILED, '', $e->errors());
  45. } catch (Exception $e) {
  46. return $this->error(intval($e->getCode()));
  47. }
  48. return $this->success($result);
  49. }
  50. /**
  51. * @api {post} /admin/keyboard/submit 修改回复
  52. * @apiGroup 回复管理
  53. *
  54. * @apiUse result
  55. * @apiUse header
  56. * @apiVersion 1.0.0
  57. *
  58. * @apiParam {int} id 回复ID
  59. * @apiParam {string} button 关键字
  60. * @apiParam {string} reply 回复内容
  61. * @apiParam {array} buttons 操作按钮组
  62. */
  63. public function store(): JsonResponse
  64. {
  65. try {
  66. $id = request()->input('id');
  67. $validator = [
  68. 'id' => ['required', 'integer'],
  69. 'reply' => 'required|string',
  70. 'explain' => 'nullable|string',
  71. 'language' => 'required|string',
  72. 'image' => ['nullable', 'url'],
  73. 'buttons' => ['array'],
  74. 'buttons.*' => ['array', 'min:1'],
  75. 'buttons.*.*.text' => ['required', 'string', 'min:1'],
  76. ];
  77. if (empty($id)) {
  78. unset($validator['id']);
  79. $validator['button'] = ['required', 'string', 'min:1', 'max:100'];
  80. }
  81. $params = request()->validate($validator);
  82. $ret = KeyboardService::submit($params);
  83. if ($ret['code'] == BaseService::NOT) {
  84. throw new Exception($ret['msg'], HttpStatus::CUSTOM_ERROR);
  85. }
  86. } catch (ValidationException $e) {
  87. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  88. } catch (Exception $e) {
  89. if ($e->getCode() == HttpStatus::CUSTOM_ERROR) {
  90. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  91. }
  92. return $this->error(intval($e->getCode()));
  93. }
  94. return $this->success([], $ret['msg']);
  95. }
  96. /**
  97. * @api {post} /admin/keyboard/delete 删除回复
  98. * @apiGroup 回复管理
  99. *
  100. * @apiUse result
  101. * @apiUse header
  102. * @apiVersion 1.0.0
  103. *
  104. * @apiParam {int} id 回复ID
  105. */
  106. public function destroy(): JsonResponse
  107. {
  108. $id = request()->post('id');
  109. // 示例:通过 ID 删除回复
  110. $info = KeyboardService::findOne(['id' => $id]);
  111. if (!$info) {
  112. return $this->error(HttpStatus::CUSTOM_ERROR, '数据不存在');
  113. }
  114. if ($info->group_id != 1) return $this->error(HttpStatus::CUSTOM_ERROR, '系统配置禁止删除');
  115. $info->delete();
  116. return $this->success([], '删除成功');
  117. }
  118. }