BaseController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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\Model;
  17. /**
  18. * 控制器基础类
  19. */
  20. class BaseController
  21. {
  22. /**
  23. * Request实例
  24. * @var \think\Request
  25. */
  26. protected $request;
  27. /**
  28. * 应用实例
  29. * @var \think\App
  30. */
  31. protected $app;
  32. /**
  33. * 是否批量验证
  34. * @var bool
  35. */
  36. protected $batchValidate = false;
  37. /**
  38. * 控制器中间件
  39. * @var array
  40. */
  41. protected $middleware = [];
  42. /**
  43. * 构造方法
  44. * @access public
  45. * @param App $app 应用对象
  46. */
  47. protected $user_temp = false;
  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->Setting('authCode', env('authCode', false), true)) {
  61. $this->auth = true;
  62. }
  63. }
  64. //系统设置项
  65. public function Setting($key = false, $def = false, $emptyReplace = false)
  66. {
  67. if ($this->SettingConfig === false) {
  68. $this->SettingConfig = SettingModel::Config();
  69. }
  70. if ($key) {
  71. if (isset($this->SettingConfig[$key])) {
  72. if ($emptyReplace && empty($this->SettingConfig[$key])) {
  73. return $def;
  74. }
  75. return $this->SettingConfig[$key];
  76. }
  77. return $def;
  78. }
  79. return $this->SettingConfig;
  80. }
  81. /**
  82. * @description :用户信息获取
  83. * @param false $must 是否强制验证,true则强制验证程序退出
  84. * @return TokenModel|array|bool|mixed|Model|void
  85. * @throws DataNotFoundException
  86. * @throws DbException
  87. * @throws ModelNotFoundException
  88. */
  89. public function getUser(bool $must = false)
  90. {
  91. $id = $this->request->header("Userid", $this->request->cookie('Userid', ''));
  92. $token = $this->request->header("Token", $this->request->cookie('Token', ''));
  93. if ($id && $token) {
  94. if ($this->user_temp) return $this->user_temp;
  95. $user = TokenModel::where("user_id", $id)->where('token', $token)->field("user_id,token,create_time")->find();
  96. if ($user) {
  97. if (time() > ($user['create_time'] + 60 * 60 * 24 * 15)) {//如果创建时间大于15天则删除
  98. $user->delete();
  99. } else {
  100. if ((time() - $user['create_time']) > (864000)) { //token定时15天清理一次,10-15天内如果使用了则重新计算时间
  101. $user->create_time = time();
  102. $user->save();
  103. }
  104. $this->user_temp = $user;
  105. return $user;
  106. }
  107. }
  108. }
  109. if ($must) {
  110. $this->error("请登录后操作")->send();
  111. exit();
  112. }
  113. return false;
  114. }
  115. //admin认证
  116. public function getAdmin()
  117. {
  118. $user = $this->getUser(true);
  119. $info = UserModel::where('id', $user['user_id'])->where("manager", 1)->find();
  120. if ($info) {
  121. return $info;
  122. }
  123. $this->error('not permission')->send();
  124. exit();
  125. }
  126. public function success($msg, $data = []): \think\response\Json
  127. {
  128. if (is_array($msg)) {
  129. return json(['msg' => "", "code" => 1, "data" => $msg]);
  130. }
  131. return json(['msg' => $msg, "code" => 1, "data" => $data]);
  132. }
  133. public function error($msg, $data = []): \think\response\Json
  134. {
  135. if (is_array($msg)) {
  136. return json(['msg' => "", "code" => 0, "data" => $msg]);
  137. }
  138. return json(['msg' => $msg, "code" => 0, "data" => $data]);
  139. }
  140. }