Kaynağa Gözat

修复后台页面设计转义问题

萤火科技 3 yıl önce
ebeveyn
işleme
a3fe54eace

+ 0 - 58
app/admin/controller/Controller.php

@@ -135,64 +135,6 @@ class Controller extends BaseController
         }
     }
 
-    /**
-     * 返回封装后的 API 数据到客户端
-     * @param int|null $status 状态码
-     * @param string $message
-     * @param array $data
-     * @return Json
-     */
-    protected function renderJson(int $status = null, string $message = '', array $data = []): Json
-    {
-        return json(compact('status', 'message', 'data'));
-    }
-
-    /**
-     * 返回操作成功json
-     * @param array|string $data
-     * @param string $message
-     * @return Json
-     */
-    protected function renderSuccess($data = [], string $message = 'success'): Json
-    {
-        if (is_string($data)) {
-            $message = $data;
-            $data = [];
-        }
-        return $this->renderJson(config('status.success'), $message, $data);
-    }
-
-    /**
-     * 返回操作失败json
-     * @param string $message
-     * @param array $data
-     * @return Json
-     */
-    protected function renderError(string $message = 'error', array $data = []): Json
-    {
-        return $this->renderJson(config('status.error'), $message, $data);
-    }
-
-    /**
-     * 获取post数据 (数组)
-     * @param $key
-     * @return mixed
-     */
-    protected function postData($key = null)
-    {
-        return $this->request->post(empty($key) ? '' : "{$key}/a");
-    }
-
-    /**
-     * 获取post数据 (数组)
-     * @param string $key
-     * @return mixed
-     */
-    protected function postForm(string $key = 'form')
-    {
-        return $this->postData($key);
-    }
-
     /**
      * 强制验证当前访问的控制器方法method
      * @throws BaseException

+ 0 - 59
app/api/controller/Controller.php

@@ -12,7 +12,6 @@ declare (strict_types=1);
 
 namespace app\api\controller;
 
-use think\response\Json;
 use cores\BaseController;
 use app\api\model\User as UserModel;
 use app\api\model\Store as StoreModel;
@@ -99,62 +98,4 @@ class Controller extends BaseController
     {
         return UserService::getCurrentLoginUser($isForce);
     }
-
-    /**
-     * 返回封装后的 API 数据到客户端
-     * @param int|null $status 状态码
-     * @param string $message
-     * @param array $data
-     * @return Json
-     */
-    protected function renderJson(int $status = null, string $message = '', array $data = []): Json
-    {
-        return json(compact('status', 'message', 'data'));
-    }
-
-    /**
-     * 返回操作成功json
-     * @param array|string $data
-     * @param string $message
-     * @return Json
-     */
-    protected function renderSuccess($data = [], string $message = 'success'): Json
-    {
-        if (is_string($data)) {
-            $message = $data;
-            $data = [];
-        }
-        return $this->renderJson(config('status.success'), $message, $data);
-    }
-
-    /**
-     * 返回操作失败json
-     * @param string $message
-     * @param array $data
-     * @return Json
-     */
-    protected function renderError(string $message = 'error', array $data = []): Json
-    {
-        return $this->renderJson(config('status.error'), $message, $data);
-    }
-
-    /**
-     * 获取post数据 (数组)
-     * @param $key
-     * @return mixed
-     */
-    protected function postData($key = null)
-    {
-        return $this->request->post(is_null($key) ? '' : $key . '/a');
-    }
-
-    /**
-     * 获取post数据 (数组)
-     * @param string $key
-     * @return mixed
-     */
-    protected function postForm(string $key = 'form')
-    {
-        return $this->postData($key);
-    }
 }

+ 8 - 80
app/store/controller/Controller.php

@@ -13,10 +13,9 @@ declare (strict_types=1);
 namespace app\store\controller;
 
 use cores\BaseController;
+use cores\exception\BaseException;
 use app\store\service\Auth as AuthService;
 use app\common\service\store\User as StoreUserService;
-use app\common\exception\BaseException;
-use think\response\Json;
 
 /**
  * 商户后台控制器基类
@@ -148,98 +147,27 @@ class Controller extends BaseController
         return getStoreId();
     }
 
-    /**
-     * 返回封装后的 API 数据到客户端
-     * @param int|null $status 状态码
-     * @param string $message
-     * @param array $data
-     * @return Json
-     */
-    protected function renderData(int $status = null, string $message = '', array $data = []): Json
-    {
-        is_null($status) && $status = config('status.success');
-        return json(compact('status', 'message', 'data'));
-    }
-
-    /**
-     * 返回操作成功json
-     * @param array|string $data
-     * @param string $message
-     * @return Json
-     */
-    protected function renderSuccess($data = [], string $message = 'success'): Json
-    {
-        if (is_string($data)) {
-            $message = $data;
-            $data = [];
-        }
-        return $this->renderData(config('status.success'), $message, $data);
-    }
-
-    /**
-     * 返回操作失败json
-     * @param string $message
-     * @param array $data
-     * @return Json
-     */
-    protected function renderError(string $message = 'error', array $data = []): Json
-    {
-        return $this->renderData(config('status.error'), $message, $data);
-    }
-
-    /**
-     * 获取post数据 (数组)
-     * @param $key
-     * @return mixed
-     */
-    protected function postData($key = null)
-    {
-        return $this->request->post(empty($key) ? '' : "{$key}/a");
-    }
-
-    /**
-     * 获取post数据 (数组)
-     * @param string $key
-     * @return mixed
-     */
-    protected function postForm(string $key = 'form')
-    {
-        return $this->postData($key);
-    }
-
-    /**
-     * 获取post数据 (数组)
-     * @param $key
-     * @return mixed
-     */
-    protected function getData($key = null)
-    {
-        return $this->request->get(is_null($key) ? '' : $key);
-    }
-
     /**
      * 强制验证当前访问的控制器方法method
-     * @return bool
-     * @throws BaseException
+     * @return void
+     * @throws \cores\exception\BaseException
      */
-    private function checkMethodRules(): bool
+    private function checkMethodRules(): void
     {
         if (!isset($this->methodRules[$this->action])) {
-            return true;
+            return;
         }
         $methodRule = $this->methodRules[$this->action];
         $currentMethod = $this->request->method();
         if (empty($methodRule)) {
-            return true;
+            return;
         }
         if (is_array($methodRule) && in_array($currentMethod, $methodRule)) {
-            return true;
+            return;
         }
         if (is_string($methodRule) && $methodRule == $currentMethod) {
-            return true;
+            return;
         }
         throwError('illegal request method');
-        return false;
     }
-
 }

+ 17 - 17
app/store/controller/Page.php

@@ -12,6 +12,7 @@ declare (strict_types=1);
 
 namespace app\store\controller;
 
+use think\response\Json;
 use app\store\model\Page as PageModel;
 
 /**
@@ -23,12 +24,12 @@ class Page extends Controller
 {
     /**
      * 页面列表
-     * @return array
+     * @return Json
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    public function list()
+    public function list(): Json
     {
         $model = new PageModel;
         $list = $model->getList($this->request->param());
@@ -37,9 +38,9 @@ class Page extends Controller
 
     /**
      * 页面设计默认数据
-     * @return array
+     * @return Json
      */
-    public function defaultData()
+    public function defaultData(): Json
     {
         $model = new PageModel;
         return $this->renderSuccess([
@@ -51,9 +52,9 @@ class Page extends Controller
     /**
      * 页面详情
      * @param int $pageId
-     * @return array
+     * @return Json
      */
-    public function detail(int $pageId)
+    public function detail(int $pageId): Json
     {
         $detail = PageModel::detail($pageId);
         return $this->renderSuccess(compact('detail'));
@@ -61,12 +62,12 @@ class Page extends Controller
 
     /**
      * 新增页面
-     * @return array|mixed
+     * @return Json
      */
-    public function add()
+    public function add(): Json
     {
         $model = new PageModel;
-        if (!$model->add($this->postForm())) {
+        if (!$model->add($this->postForm('form', false))) {
             return $this->renderError($model->getError() ?: '添加失败');
         }
         return $this->renderSuccess('添加成功');
@@ -75,12 +76,12 @@ class Page extends Controller
     /**
      * 编辑页面
      * @param int $pageId
-     * @return array|mixed
+     * @return Json
      */
-    public function edit(int $pageId)
+    public function edit(int $pageId): Json
     {
         $model = PageModel::detail($pageId);
-        if (!$model->edit($this->postForm())) {
+        if (!$model->edit($this->postForm('form', false))) {
             return $this->renderError($model->getError() ?: '更新失败');
         }
         return $this->renderSuccess('更新成功');
@@ -89,9 +90,9 @@ class Page extends Controller
     /**
      * 删除页面
      * @param int $pageId
-     * @return array
+     * @return Json
      */
-    public function delete(int $pageId)
+    public function delete(int $pageId): Json
     {
         // 帮助详情
         $model = PageModel::detail($pageId);
@@ -104,9 +105,9 @@ class Page extends Controller
     /**
      * 设置默认首页
      * @param int $pageId
-     * @return array
+     * @return Json
      */
-    public function setHome(int $pageId)
+    public function setHome(int $pageId): Json
     {
         // 帮助详情
         $model = PageModel::detail($pageId);
@@ -115,5 +116,4 @@ class Page extends Controller
         }
         return $this->renderSuccess('设置成功');
     }
-
 }

+ 65 - 5
cores/BaseController.php

@@ -14,6 +14,7 @@ namespace cores;
 
 use think\App;
 use think\Validate;
+use think\response\Json;
 use think\exception\ValidateException;
 
 /**
@@ -25,25 +26,25 @@ abstract class BaseController
      * Request实例
      * @var \think\Request
      */
-    protected $request;
+    protected \think\Request  $request;
 
     /**
      * 应用实例
-     * @var \think\App
+     * @var App
      */
-    protected $app;
+    protected App $app;
 
     /**
      * 是否批量验证
      * @var bool
      */
-    protected $batchValidate = false;
+    protected bool $batchValidate = false;
 
     /**
      * 控制器中间件
      * @var array
      */
-    protected $middleware = [];
+    protected array $middleware = [];
 
     /**
      * 构造方法
@@ -101,4 +102,63 @@ abstract class BaseController
         return $v->failException(true)->check($data);
     }
 
+    /**
+     * 返回封装后的 API 数据到客户端
+     * @param int|null $status
+     * @param string $message
+     * @param array $data
+     * @return Json
+     */
+    protected final function renderJson(int $status = null, string $message = '', array $data = []): Json
+    {
+        return json(compact('status', 'message', 'data'));
+    }
+
+    /**
+     * 返回操作成功json
+     * @param array|string $data
+     * @param string $message
+     * @return Json
+     */
+    protected final function renderSuccess($data = [], string $message = 'success'): Json
+    {
+        if (is_string($data)) {
+            $message = $data;
+            $data = [];
+        }
+        return $this->renderJson(config('status.success'), $message, $data);
+    }
+
+    /**
+     * 返回操作失败json
+     * @param string $message
+     * @param array $data
+     * @return Json
+     */
+    protected final function renderError(string $message = 'error', array $data = []): Json
+    {
+        return $this->renderJson(config('status.error'), $message, $data);
+    }
+
+    /**
+     * 获取post数据 (数组)
+     * @param null $key
+     * @param bool $filter
+     * @return mixed
+     */
+    protected final function postData($key = null, bool $filter = false)
+    {
+        return $this->request->post(empty($key) ? '' : "{$key}/a", null, $filter ? '' : null);
+    }
+
+    /**
+     * 获取post数据 (数组)
+     * @param string|null $key
+     * @param bool $filter
+     * @return mixed
+     */
+    protected final function postForm(?string $key = 'form', bool $filter = true)
+    {
+        return $this->postData(empty($key) ? 'form' : $key, $filter);
+    }
 }