AuthController.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\outapi\controller;
  12. use crmeb\basic\BaseController;
  13. use think\facade\Validate;
  14. /**
  15. * 基类 所有控制器继承的类
  16. * Class AuthController
  17. * @package app\controller\out
  18. * @method success($msg = 'ok', array $data = [])
  19. * @method fail($msg = 'error', array $data = [])
  20. */
  21. class AuthController extends BaseController
  22. {
  23. /**
  24. * 当前对外接口ID
  25. * @var
  26. */
  27. protected $outId;
  28. /**
  29. * 当前对外接口信息
  30. * @var
  31. */
  32. protected $outInfo;
  33. /**
  34. * 当前对外接口权限
  35. * @var array
  36. */
  37. protected $auth = [];
  38. /**
  39. * 初始化
  40. */
  41. protected function initialize()
  42. {
  43. $this->outId = $this->request->outId();
  44. $this->outInfo = $this->request->outInfo();
  45. $this->auth = $this->outInfo['rule'] ?? [];
  46. }
  47. /**
  48. * 验证数据
  49. * @param array $data
  50. * @param $validate
  51. * @param null $message
  52. * @param bool $batch
  53. * @return bool
  54. */
  55. final protected function validate(array $data, $validate, $message = null, bool $batch = false)
  56. {
  57. if (is_array($validate)) {
  58. $v = new Validate();
  59. $v->rule($validate);
  60. } else {
  61. if (strpos($validate, '.')) {
  62. // 支持场景
  63. list($validate, $scene) = explode('.', $validate);
  64. }
  65. $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
  66. $v = new $class();
  67. if (!empty($scene)) {
  68. $v->scene($scene);
  69. }
  70. if (is_string($message) && empty($scene)) {
  71. $v->scene($message);
  72. }
  73. }
  74. if (is_array($message))
  75. $v->message($message);
  76. // 是否批量验证
  77. if ($batch) {
  78. $v->batch(true);
  79. }
  80. return $v->failException(true)->check($data);
  81. }
  82. }