You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BaseController.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace crmeb\basic;
  3. use think\exception\ValidateException;
  4. use think\Validate;
  5. /**
  6. * 控制器基础类
  7. */
  8. abstract class BaseController
  9. {
  10. protected $services;
  11. /**
  12. * 应用实例
  13. * @var \think\App
  14. */
  15. protected $app;
  16. /**
  17. * Request实例
  18. * @var \app\Request
  19. */
  20. protected $request;
  21. public function __construct()
  22. {
  23. $this->request = app('request');
  24. $this->initialize();
  25. }
  26. public function __z6uxyJQ4xYa5ee1mx5()
  27. {
  28. return true;
  29. }
  30. protected function initialize()
  31. {
  32. }
  33. public function checkAuthDecrypt()
  34. {
  35. return $this->success();
  36. }
  37. public function getAuth()
  38. {
  39. $res = [
  40. 'status' => 200,
  41. 'msg' => 'ok',
  42. 'data' => ['day' => 3666666, 'status' => -1],
  43. ];
  44. return $res;
  45. }
  46. public function success($data = [])
  47. {
  48. if (is_array($data)) {
  49. $res = [
  50. 'status' => 200,
  51. 'msg' => 'ok',
  52. 'data' => $data,
  53. ];
  54. } else {
  55. $res = [
  56. 'status' => 200,
  57. 'msg' => $data,
  58. 'data' => [],
  59. ];
  60. }
  61. return $res;
  62. }
  63. public function fail($msg = '', $data = [])
  64. {
  65. $res = [
  66. 'status' => 400,
  67. 'msg' => $msg,
  68. 'data' => $data,
  69. ];
  70. return $res;
  71. }
  72. /**
  73. * 验证数据
  74. * @access protected
  75. * @param array $data 数据
  76. * @param string|array $validate 验证器名或者验证规则数组
  77. * @param string|array $message 验证场景或者提示信息
  78. * @param bool $batch 是否批量验证
  79. * @return array|string|true
  80. * @throws ValidateException
  81. */
  82. final protected function validate(array $data, $validate, $message = null, bool $batch = false)
  83. {
  84. if (is_array($validate)) {
  85. $v = new Validate();
  86. $v->rule($validate);
  87. } else {
  88. if (strpos($validate, '.')) {
  89. // 支持场景
  90. list($validate, $scene) = explode('.', $validate);
  91. }
  92. $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
  93. $v = new $class();
  94. if (!empty($scene)) {
  95. $v->scene($scene);
  96. }
  97. if (is_string($message) && empty($scene)) {
  98. $v->scene($message);
  99. }
  100. }
  101. if (is_array($message))
  102. $v->message($message);
  103. // 是否批量验证
  104. if ($batch) {
  105. $v->batch(true);
  106. }
  107. return $v->failException(true)->check($data);
  108. }
  109. }