From d097ed3afee5db334fc8de80bc0ef4662f8a529a Mon Sep 17 00:00:00 2001 From: wanghongjun <1445693971@qq,com> Date: Wed, 30 Oct 2024 16:00:36 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=B4=E6=92=AD=E9=97=B4=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/liveroom/Index.php | 127 +++++++++++++++++++++++ app/admin/validate/IpValidate.php | 2 +- app/admin/validate/LiveRoomValidate.php | 42 ++++++++ app/admin/view/liveroom/index/add.html | 43 ++++++++ app/admin/view/liveroom/index/edit.html | 43 ++++++++ app/admin/view/liveroom/index/index.html | 10 ++ public/static/admin/js/liveroom/index.js | 54 ++++++++++ public/static/admin/js/user/ip.js | 6 +- 8 files changed, 323 insertions(+), 4 deletions(-) create mode 100644 app/admin/validate/LiveRoomValidate.php create mode 100644 app/admin/view/liveroom/index/add.html create mode 100644 app/admin/view/liveroom/index/edit.html create mode 100644 app/admin/view/liveroom/index/index.html create mode 100644 public/static/admin/js/liveroom/index.js diff --git a/app/admin/controller/liveroom/Index.php b/app/admin/controller/liveroom/Index.php index 469822b..c0f7a49 100644 --- a/app/admin/controller/liveroom/Index.php +++ b/app/admin/controller/liveroom/Index.php @@ -3,8 +3,10 @@ namespace app\admin\controller\liveroom; use app\admin\model\LiveRoom; +use app\admin\validate\LiveRoomValidate; use app\common\controller\AdminController; use think\App; +use think\exception\ValidateException; class Index extends AdminController { @@ -15,4 +17,129 @@ class Index extends AdminController $this->model = new LiveRoom(); } + public function index() + { + if ($this->request->isAjax()) { + if (input('selectFields')) { + return $this->selectList(); + } + list($page, $limit, $where) = $this->buildTableParames(); + $count = $this->model + ->where($where) + ->count(); + $list = $this->model + ->withoutField('update_time, delete_time') + ->where($where) + ->page($page, $limit) + ->order('id') + ->select() + ->each(function ($item) { + + return $item; + }); + $data = [ + 'code' => 0, + 'msg' => '', + 'count' => $count, + 'data' => $list + ]; + return json($data); + } + return $this->fetch(); + } + + /** + * @NodeAnotation(title="添加") + */ + public function add() + { + if ($this->request->isPost()) { + $post = $this->request->post(); + try { + validate(LiveRoomValidate::class)->scene('add')->check($post); + $exists = LiveRoomValidate::existsRoomId($post['room_id']); + if ($exists !== true) throw new ValidateException($exists); + $save = $this->model->save($post); + } catch (ValidateException $e) { + $this->error($e->getMessage()); + } catch (\Exception $e) { + $this->error('保存失败'); + } + $save ? $this->success('保存成功') : $this->error('保存失败'); + } + return $this->fetch(); + } + + /** + * @NodeAnotation(title="编辑") + */ + public function edit($id) + { + $row = $this->model->find($id); + empty($row) && $this->error('数据不存在'); + if ($this->request->isPost()) { + $post = $this->request->post(); + try { + validate(LiveRoomValidate::class)->scene('add')->check($post); + $exists = LiveRoomValidate::existsRoomId($post['room_id'], $id); + if ($exists !== true) throw new ValidateException($exists); + $save = $row->save($post); + } catch (ValidateException $e) { + $this->error($e->getMessage()); + } catch (\Exception $e) { + $this->error('保存失败'); + } + $save ? $this->success('保存成功') : $this->error('保存失败'); + } + $this->assign('row', $row); + return $this->fetch(); + } + + /** + * @NodeAnotation(title="删除") + */ + public function delete($id) + { + $this->checkPostRequest(); + $row = $this->model->whereIn('id', $id)->select(); + $row->isEmpty() && $this->error('数据不存在'); + try { + $save = false; + foreach ($row as $value) { + $save = $this->model->where('id', $value['id'])->useSoftDelete('delete_time', time())->delete(); + } + } catch (\Exception $e) { + $this->error('删除失败'); + } + $save ? $this->success('删除成功') : $this->error('删除失败'); + } + + /** + * @NodeAnotation(title="属性修改") + */ + public function modify() + { + $this->checkPostRequest(); + $post = $this->request->post(); + $rule = [ + 'id|ID' => 'require', + 'field|字段' => 'require', + 'value|值' => 'require', + ]; + $this->validate($post, $rule); + if (!in_array($post['field'], $this->allowModifyFields)) { + $this->error('该字段不允许修改:' . $post['field']); + } + $row = $this->model->find($post['id']); + empty($row) && $this->error('数据不存在'); + try { + $row->save([ + $post['field'] => $post['value'], + ]); + } catch (\Exception $e) { + $this->error($e->getMessage()); + } + $this->success('保存成功'); + } + } diff --git a/app/admin/validate/IpValidate.php b/app/admin/validate/IpValidate.php index 2cdd50f..7d41562 100644 --- a/app/admin/validate/IpValidate.php +++ b/app/admin/validate/IpValidate.php @@ -37,7 +37,7 @@ class IpValidate extends Validate { $IpModel = new IpModel(); $where = [['ip', '=', $ip], ['port', '=', $port]]; - if ($id > 0) $where[] = ['id', '!=', $id]; + if ($id > 0) $where[] = ['id', '<>', $id]; try { $existsRes = $IpModel->where($where)->find(); return $existsRes ? 'IP及端口已存在' : true; diff --git a/app/admin/validate/LiveRoomValidate.php b/app/admin/validate/LiveRoomValidate.php new file mode 100644 index 0000000..0a98fac --- /dev/null +++ b/app/admin/validate/LiveRoomValidate.php @@ -0,0 +1,42 @@ + 'require|number', + 'room_id|房间号' => 'require|alphaNum|max:50', + 'anchor_information|主播信息' => 'require', + 'link|直播间链接' => 'require|checkLink', + 'status|状态' => 'require|in:0,1', + ]; + + protected $message = []; + + protected $scene= [ + 'add' => ['room_id', 'anchor_information', 'link', 'status'], + 'edit' => ['id', 'room_id', 'anchor_information', 'link', 'status'] + ]; + + protected function checkLink($value, $rule, $data=[]) + { + if (strpos($value, 'http://') !== 0 && strpos($value, 'https://') !== 0) { + return '直播间链接填写错误'; + } + return true; + } + + public static function existsRoomId($room_id, $id = 0) + { + $where = [['room_id', "=", $room_id]]; + if ($id > 0) $where[] = ['id', '<>', $id]; + $res = LiveRoom::where($where)->find(); + return !$res ? true : '房间号已存在'; + } +} diff --git a/app/admin/view/liveroom/index/add.html b/app/admin/view/liveroom/index/add.html new file mode 100644 index 0000000..ad90233 --- /dev/null +++ b/app/admin/view/liveroom/index/add.html @@ -0,0 +1,43 @@ +