Keyboard.php 4.5 KB

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