seven 1 місяць тому
батько
коміт
267dd80051

+ 143 - 0
app/Http/Controllers/admin/Keyboard.php

@@ -0,0 +1,143 @@
+<?php
+
+
+namespace App\Http\Controllers\admin;
+
+use App\Constants\HttpStatus;
+use App\Http\Controllers\Controller;
+use App\Services\KeyboardService;
+use Exception;
+
+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()
+    {
+        // try {
+            $params = request()->all();
+
+            $validator = [
+                // 'name' => 'required|string|max:50|alpha_dash',
+                // 'name' => 'required|string|max:50|alpha_dash|unique:keyboards,name',
+                'button' => 'required|nullable|string|max:100',
+                'reply' => 'required|nullable|string',
+            ];
+
+            request()->validate($validator);
+
+            $ret = KeyboardService::submit($params);
+            if ($ret['code'] == KeyboardService::NOT) {
+                return $this->error($ret['code'], $ret['msg']);
+            }
+        // } catch (ValidationException $e) {
+        //     return $this->error(HttpStatus::VALIDATION_FAILED, '', $e->errors());
+        // } catch (Exception $e) {
+        //     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()
+    {
+        $id = request()->post('id');
+        // 示例:通过 ID 删除回复
+        $info = KeyboardService::findOne(['id' => $id]);
+        if (!$info) {
+            return $this->error(0, '数据不存在');
+        }
+
+        $info->delete();
+
+        return $this->success([], '删除成功');
+    }
+
+}

+ 36 - 0
app/Models/Keyboard.php

@@ -0,0 +1,36 @@
+<?php
+
+
+
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Foundation\Auth\User as Authenticatable;
+use Illuminate\Notifications\Notifiable;
+use Laravel\Sanctum\HasApiTokens;
+
+/**
+ * Admin
+ * @mixin Builder
+ * @method static Builder|static where($column, $operator = null, $value = null, $boolean = 'and')
+ */
+class Keyboard extends Authenticatable
+{
+    use HasApiTokens, Notifiable;
+    protected $table = 'keyboard';
+    protected $hidden = ['created_at', 'updated_at'];
+    protected $fillable = ['button', 'reply', 'id' ,'buttons' ,'image'];
+    // protected $appends = ['menus_ids', 'menus_uris'];
+
+    protected function getCreatedAtAttribute($value)
+    {
+        return \Carbon\Carbon::parse($value)->setTimezone('Asia/Shanghai')->format('Y-m-d H:i:s');
+    }
+
+    protected function getUpdatedAtAttribute($value)
+    {
+        return \Carbon\Carbon::parse($value)->setTimezone('Asia/Shanghai')->format('Y-m-d H:i:s');
+    }
+
+}

+ 143 - 0
app/Services/KeyboardService.php

@@ -0,0 +1,143 @@
+<?php
+
+
+namespace App\Services;
+
+use App\Services\BaseService;
+use App\Models\Keyboard;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Collection;
+use Illuminate\Support\Facades\Cache;
+
+
+/**
+ * 菜单
+ */
+class KeyboardService extends BaseService
+{
+    /**
+     * @description: 模型
+     * @return {string}
+     */
+    public static function model(): string
+    {
+        return Keyboard::class;
+    }
+
+    /**
+     * @description: 枚举
+     * @return {*}
+     */
+    public static function enum(): string
+    {
+        return '';
+    }
+
+    /**
+     * @description: 获取查询条件
+     * @param {array} $search 查询内容
+     * @return {array}
+     */
+    public static function getWhere(array $search = []): array
+    {
+        $where = [];
+       
+        if (isset($search['id']) && !empty($search['id'])) {
+            $where[] = ['id', '=', $search['id']];
+        }
+        if (isset($search['button']) && !empty($search['button'])) {
+            $where[] = ['button', '=', $search['button']];
+        }
+       
+        return $where;
+    }
+
+    /**
+     * @description: 查询单条数据
+     * @param array $search
+     * @return \App\Models\Coin|null
+     */
+    public static function findOne(array $search): ?Keyboard
+    {
+        return self::model()::where(self::getWhere($search))->first();
+    }
+
+    /**
+     * @description: 查询所有数据
+     * @param array $search
+     * @return \Illuminate\Database\Eloquent\Collection
+     */
+    public static function findAll(array $search = [])
+    {
+        
+        return self::model()::where(self::getWhere($search))->get();
+        
+    }
+
+    /**
+     * @description: 分页查询
+     * @param array $search
+     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
+     */
+    public static function paginate(array $search = [])
+    {
+        $limit = isset($search['limit']) ? $search['limit'] : 15;
+        $paginator = self::model()::where(self::getWhere($search))
+            // ->orderBy("sort", 'asc')
+            ->paginate($limit);
+        foreach($paginator->items() as $item){
+            if(isset($item->buttons) && !empty($item->buttons)){
+                $item->buttons = json_decode($item->buttons,true);
+            }else{
+                $item->buttons = [];
+            }
+        }
+        return ['total' => $paginator->total(), 'data' => $paginator->items()];
+    }
+
+    /**
+     * @description: 
+     * @param {*} $params
+     * @return {*}
+     */    
+    public static function submit($params = [])
+    {
+        $result = false;
+        $msg['code'] = self::NOT;
+        $msg['msg'] = '';
+        if(isset($params['buttons']) && !empty($params['buttons'])){
+            if(is_array($params['buttons'])){
+                $params['buttons'] = json_encode($params['buttons'],JSON_UNESCAPED_UNICODE);
+            }
+        }else{
+            $params['buttons'] = json_encode([],JSON_UNESCAPED_UNICODE);
+        }
+        
+        // 2. 判断是否是更新
+        if (!empty($params['id'])) {
+            // 更新
+            $info = self::findOne(['id'=>$params['id']] );
+            if (!$info) {
+                $msg['msg'] = '数据不存在!';
+            }else{
+                $result = $info->update($params);
+                $id = $params['id'];
+            }
+
+        } else {
+            // 创建
+            $result = $info = self::model()::create($params);
+            $id = $result->id;
+        }
+        
+       
+        if($result){
+           $msg['code'] = self::YES;
+           $msg['msg'] = '设置成功';
+        }else{
+            $msg['msg'] = empty($msg['msg']) ?'操作失败':$msg['msg'];
+        }
+
+        return $msg;
+    }
+}

+ 8 - 0
routes/admin.php

@@ -16,6 +16,7 @@ use App\Http\Controllers\admin\Role;
 use App\Http\Controllers\admin\Game;
 use App\Http\Controllers\admin\GameplayRule;
 use App\Http\Controllers\admin\Issue;
+use App\Http\Controllers\admin\Keyboard;
 
 
 Route::post('/login', [Admin::class, 'login']);
@@ -42,6 +43,13 @@ Route::middleware(['admin.jwt'])->group(function () {
         Route::post('/submit', [Admin::class, 'store']);
         Route::post('/delete', [Admin::class, 'destroy']);
 
+        // 关键字回复路由
+        Route::prefix('/keyboard')->group(function () {
+            Route::get('/', [Keyboard::class, 'index']);
+            Route::post('/submit', [Keyboard::class, 'store']);
+            Route::post('/delete', [Keyboard::class, 'destroy']);
+        });
+
         // 期数路由
         Route::prefix('/issue')->group(function () {
             Route::get('/', [Issue::class, 'index']);

+ 16 - 0
sql/sql_prod.sql

@@ -468,5 +468,21 @@ ALTER TABLE `bot_issues`
 ADD COLUMN `combo`  varchar(20) NULL AFTER `updated_at`,
 ADD COLUMN `extreme`  varchar(20) NULL COMMENT '极值' AFTER `combo`;
 
+
+-- ----------------------------
+-- Table structure for bot_keyboard
+-- ----------------------------
+DROP TABLE IF EXISTS `bot_keyboard`;
+CREATE TABLE `bot_keyboard` (
+  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
+  `button` varchar(50) DEFAULT NULL COMMENT '按钮',
+  `image` varchar(255) DEFAULT NULL COMMENT '图片',
+  `reply` text COMMENT '回复',
+  `buttons` text COMMENT '按钮组',
+  `created_at` datetime DEFAULT NULL,
+  `updated_at` datetime DEFAULT NULL,
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
 SET FOREIGN_KEY_CHECKS = 1;