| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- namespace App\Http\Controllers\admin;
- use App\Constants\HttpStatus;
- use App\Http\Controllers\Controller;
- use App\Services\KeyboardService;
- use Exception;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Validation\ValidationException;
- class Keyboard extends Controller
- {
- /**
- * @api {get} /admin/keyboard 回复列表
- * @apiGroup 回复管理
- *
- * @apiUse result
- * @apiUse header
- * @apiVersion 1.0.0
- *
- * @apiParam {int} [page=1]
- * @apiParam {int} [limit=10]
- * @apiParam {string} [button] 关键字
- *
- * @apiSuccess (data) {Object} data
- * @apiSuccess (data) {int} data.total 数量
- * @apiSuccess (data) {Object[]} data.data 列表
- * @apiSuccess (data) {int} data.data.id
- * @apiSuccess (data) {string} data.data.button 回复关键字
- * @apiSuccess (data) {string} data.data.replay 回复内容
- * @apiSuccess (data) {string} data.data.updated_at
- * @apiSuccess (data) {string} data.data.created_at
- */
- public function index()
- {
- try {
- request()->validate([
- 'title' => ['nullable', 'string'],
- 'permission_name' => ['nullable', 'string'],
- ]);
- $search = request()->all();
- $result = KeyboardService::paginate($search);
- } catch (ValidationException $e) {
- return $this->error(HttpStatus::VALIDATION_FAILED, '', $e->errors());
- } catch (Exception $e) {
- return $this->error(intval($e->getCode()));
- }
- return $this->success($result);
- }
- /**
- * @api {post} /admin/keyboard/submit 修改回复
- * @apiGroup 回复管理
- *
- * @apiUse result
- * @apiUse header
- * @apiVersion 1.0.0
- *
- * @apiParam {int} id 回复ID
- * @apiParam {string} button 关键字
- * @apiParam {string} reply 回复内容
- * @apiParam {array} buttons 操作按钮组
- * @apiExample {js} button 示例
- * //button 的数据格式如下
- * [
- * [ //第一行
- * { //第一行按钮 的第一个按钮
- * "text": "百度", //按钮文字
- * "url": "https://baidu.com" //按钮跳转的链接
- * },
- * { //第一行按钮 的第二个按钮
- * "text": "百度",
- * "url": "https://baidu.com"
- * }
- * //更多按钮...
- * ],
- * [ //第二行
- * {
- * "text": "百度",
- * "url": "https://baidu.com"
- * }
- * ]
- * //更多行...
- * ]
- */
- public function store(): JsonResponse
- {
- try {
- $id = request()->input('id', null);
- $validator = [
- 'id' => ['required', 'integer'],
- 'reply' => 'required|string',
- 'explain' => 'nullable|string',
- 'language' => 'required|string',
- 'reply' => 'required|string',
- 'image' => ['nullable', 'url'],
- 'buttons' => ['array'],
- ];
- if (empty($id)) {
- unset($validator['id']);
- $validator['button'] = ['required', 'string', 'min:1', 'max:100'];
- }
- $params = request()->validate($validator);
- $ret = KeyboardService::submit($params);
- if ($ret['code'] == KeyboardService::NOT) {
- throw new Exception($ret['msg'], HttpStatus::CUSTOM_ERROR);
- }
- } catch (ValidationException $e) {
- return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
- } catch (Exception $e) {
- if ($e->getCode() == HttpStatus::CUSTOM_ERROR) {
- return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
- }
- return $this->error(intval($e->getCode()));
- }
- return $this->success([], $ret['msg']);
- }
- /**
- * @api {post} /admin/keyboard/delete 删除回复
- * @apiGroup 回复管理
- *
- * @apiUse result
- * @apiUse header
- * @apiVersion 1.0.0
- *
- * @apiParam {int} id 回复ID
- */
- public function destroy(): JsonResponse
- {
- return $this->error(HttpStatus::CUSTOM_ERROR, '禁止删除');
- $id = request()->post('id');
- // 示例:通过 ID 删除回复
- $info = KeyboardService::findOne(['id' => $id]);
- if (!$info) {
- return $this->error(0, '数据不存在');
- }
- $info->delete();
- return $this->success([], '删除成功');
- }
- }
|