123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- declare(strict_types=1);
- namespace app;
- use app\model\SettingModel;
- use app\model\TokenModel;
- use app\model\UserModel;
- use think\App;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\Exception;
- use think\facade\Config;
- use think\Model;
- class BaseController
- {
-
- protected $request;
-
- protected $app;
-
- protected $batchValidate = false;
-
- protected $middleware = [];
-
- private $SettingConfig = false;
- public $auth = false;
- public function __construct(App $app)
- {
- $this->app = $app;
- $this->request = $this->app->request;
-
- $this->initialize();
- }
-
- protected function initialize()
- {
- if ($this->systemSetting('authCode', env('authCode', false), true)) {
- $this->auth = true;
- }
- if ($this->systemSetting("app_debug", '0') === '1') {
- $this->app->debug(true);
- Config::set([
- 'show_error_msg' => true,
- 'exception_tmpl' => app()->getThinkPath() . 'tpl/think_exception.tpl'
- ], 'app');
- }
- }
-
- protected function systemSetting($key = false, $def = false, $emptyReplace = false)
- {
- if ($this->SettingConfig === false) {
- $this->SettingConfig = SettingModel::Config();
- }
- if ($key) {
- if (isset($this->SettingConfig[$key])) {
- if ($emptyReplace && empty($this->SettingConfig[$key])) {
- return $def;
- }
- return $this->SettingConfig[$key];
- }
- return $def;
- }
- return $this->SettingConfig;
- }
-
- protected function getUser(bool $must = false)
- {
- return UserModel::getUser($must);
- }
-
- protected function getAdmin()
- {
- $user = $this->getUser(true);
- $info = UserModel::where('id', $user['user_id'])->where("manager", 1)->find();
- if ($info) {
- return $info;
- }
- $this->error('not permission')->send();
- exit();
- }
- protected function success($msg, $data = []): \think\response\Json
- {
- if (is_array($msg)) {
- return json(['msg' => "", "code" => 1, "data" => $msg]);
- }
- return json(['msg' => $msg, "code" => 1, "data" => $data]);
- }
- protected function error($msg, $data = []): \think\response\Json
- {
- if (is_array($msg)) {
- return json(['msg' => "", "code" => 0, "data" => $msg]);
- }
- return json(['msg' => $msg, "code" => 0, "data" => $data]);
- }
- }
|