123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
-
- namespace crmeb\basic;
-
- use think\exception\ValidateException;
- use think\Validate;
-
- /**
- * 控制器基础类
- */
- abstract class BaseController
- {
- protected $services;
- /**
- * 应用实例
- * @var \think\App
- */
- protected $app;
-
- /**
- * Request实例
- * @var \app\Request
- */
- protected $request;
-
- public function __construct()
- {
- $this->request = app('request');
- $this->initialize();
- }
-
- public function __z6uxyJQ4xYa5ee1mx5()
- {
- return true;
- }
-
- protected function initialize()
- {
-
- }
-
- public function checkAuthDecrypt()
- {
- return $this->success();
- }
-
- public function getAuth()
- {
- $res = [
- 'status' => 200,
- 'msg' => 'ok',
- 'data' => ['day' => 3666666, 'status' => -1],
- ];
- return $res;
- }
-
- public function success($data = [])
- {
- if (is_array($data)) {
- $res = [
- 'status' => 200,
- 'msg' => 'ok',
- 'data' => $data,
- ];
- } else {
- $res = [
- 'status' => 200,
- 'msg' => $data,
- 'data' => [],
- ];
- }
- return $res;
- }
-
- public function fail($msg = '', $data = [])
- {
- $res = [
- 'status' => 400,
- 'msg' => $msg,
- 'data' => $data,
- ];
- return $res;
- }
-
- /**
- * 验证数据
- * @access protected
- * @param array $data 数据
- * @param string|array $validate 验证器名或者验证规则数组
- * @param string|array $message 验证场景或者提示信息
- * @param bool $batch 是否批量验证
- * @return array|string|true
- * @throws ValidateException
- */
- final protected function validate(array $data, $validate, $message = null, bool $batch = false)
- {
- if (is_array($validate)) {
- $v = new Validate();
- $v->rule($validate);
- } else {
- if (strpos($validate, '.')) {
- // 支持场景
- list($validate, $scene) = explode('.', $validate);
- }
- $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
- $v = new $class();
- if (!empty($scene)) {
- $v->scene($scene);
- }
-
- if (is_string($message) && empty($scene)) {
- $v->scene($message);
- }
- }
-
- if (is_array($message))
- $v->message($message);
-
-
- // 是否批量验证
- if ($batch) {
- $v->batch(true);
- }
-
- return $v->failException(true)->check($data);
- }
-
- }
|