BaseController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /*
  3. * @description:
  4. * @Date: 2022-09-26 17:52:37
  5. * @LastEditTime: 2022-09-26 20:28:17
  6. */
  7. declare(strict_types=1);
  8. namespace app;
  9. use app\model\SettingModel;
  10. use app\model\TokenModel;
  11. use app\model\UserModel;
  12. use think\App;
  13. use think\db\exception\DataNotFoundException;
  14. use think\db\exception\DbException;
  15. use think\db\exception\ModelNotFoundException;
  16. use think\facade\Config;
  17. use think\Model;
  18. /**
  19. * 控制器基础类
  20. */
  21. class BaseController
  22. {
  23. /**
  24. * Request实例
  25. * @var \think\Request
  26. */
  27. protected $request;
  28. /**
  29. * 应用实例
  30. * @var \think\App
  31. */
  32. protected $app;
  33. /**
  34. * 是否批量验证
  35. * @var bool
  36. */
  37. protected $batchValidate = false;
  38. /**
  39. * 控制器中间件
  40. * @var array
  41. */
  42. protected $middleware = [];
  43. /**
  44. * 构造方法
  45. * @access public
  46. * @param App $app 应用对象
  47. */
  48. private $SettingConfig = false;
  49. public $auth = false;
  50. public function __construct(App $app)
  51. {
  52. $this->app = $app;
  53. $this->request = $this->app->request;
  54. // 控制器初始化
  55. $this->initialize();
  56. }
  57. // 初始化
  58. protected function initialize()
  59. {
  60. if ($this->systemSetting('authCode', env('authCode', false), true)) {
  61. $this->auth = true;
  62. }
  63. if ($this->systemSetting("app_debug", '0') === '1') {
  64. $this->app->debug(true);
  65. Config::set([
  66. 'show_error_msg' => true,
  67. 'exception_tmpl' => app()->getThinkPath() . 'tpl/think_exception.tpl'
  68. ], 'app');
  69. }
  70. }
  71. //系统设置项
  72. protected function systemSetting($key = false, $def = false, $emptyReplace = false)
  73. {
  74. if ($this->SettingConfig === false) {
  75. $this->SettingConfig = SettingModel::Config();
  76. }
  77. if ($key) {
  78. if (isset($this->SettingConfig[$key])) {
  79. if ($emptyReplace && empty($this->SettingConfig[$key])) {
  80. return $def;
  81. }
  82. return $this->SettingConfig[$key];
  83. }
  84. return $def;
  85. }
  86. return $this->SettingConfig;
  87. }
  88. /**
  89. * @description :用户信息获取
  90. * @param false $must 是否强制验证,true则强制验证程序退出
  91. * @return TokenModel|array|bool|mixed|Model|void
  92. * @throws DataNotFoundException
  93. * @throws DbException
  94. * @throws ModelNotFoundException
  95. */
  96. protected function getUser(bool $must = false)
  97. {
  98. return UserModel::getUser($must);
  99. }
  100. //admin认证
  101. protected function getAdmin()
  102. {
  103. $user = $this->getUser(true);
  104. $info = UserModel::where('id', $user['user_id'])->where("manager", 1)->find();
  105. if ($info) {
  106. return $info;
  107. }
  108. $this->error('not permission')->send();
  109. exit();
  110. }
  111. protected function success($msg, $data = []): \think\response\Json
  112. {
  113. if (is_array($msg)) {
  114. return json(['msg' => "", "code" => 1, "data" => $msg]);
  115. }
  116. return json(['msg' => $msg, "code" => 1, "data" => $data]);
  117. }
  118. protected function error($msg, $data = []): \think\response\Json
  119. {
  120. if (is_array($msg)) {
  121. return json(['msg' => "", "code" => 0, "data" => $msg]);
  122. }
  123. return json(['msg' => $msg, "code" => 0, "data" => $data]);
  124. }
  125. }