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