Keyboard.php 4.0 KB

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