選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

BaseController.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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($msg = [], $data = [])
  47. {
  48. if (is_array($msg)) {
  49. $res = [
  50. 'status' => 200,
  51. 'msg' => 'ok',
  52. 'data' => $msg,
  53. ];
  54. } else {
  55. if ($data) {
  56. $res = [
  57. 'status' => 200,
  58. 'msg' => $msg,
  59. 'data' => $data,
  60. ];
  61. } else {
  62. $res = [
  63. 'status' => 200,
  64. 'msg' => $msg,
  65. 'data' => [],
  66. ];
  67. }
  68. }
  69. return $res;
  70. }
  71. public function fail($msg = '', $data = [])
  72. {
  73. $res = [
  74. 'status' => 400,
  75. 'msg' => $msg,
  76. 'data' => $data,
  77. ];
  78. return $res;
  79. }
  80. /**
  81. * 验证数据
  82. * @access protected
  83. * @param array $data 数据
  84. * @param string|array $validate 验证器名或者验证规则数组
  85. * @param string|array $message 验证场景或者提示信息
  86. * @param bool $batch 是否批量验证
  87. * @return array|string|true
  88. * @throws ValidateException
  89. */
  90. final protected function validate(array $data, $validate, $message = null, bool $batch = false)
  91. {
  92. if (is_array($validate)) {
  93. $v = new Validate();
  94. $v->rule($validate);
  95. } else {
  96. if (strpos($validate, '.')) {
  97. // 支持场景
  98. list($validate, $scene) = explode('.', $validate);
  99. }
  100. $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
  101. $v = new $class();
  102. if (!empty($scene)) {
  103. $v->scene($scene);
  104. }
  105. if (is_string($message) && empty($scene)) {
  106. $v->scene($message);
  107. }
  108. }
  109. if (is_array($message))
  110. $v->message($message);
  111. // 是否批量验证
  112. if ($batch) {
  113. $v->batch(true);
  114. }
  115. return $v->failException(true)->check($data);
  116. }
  117. }