BaseController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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",'');
  92. $token = $this->request->header("Token",'');
  93. if (!$id) {
  94. $id = $this->request->cookie('user_id', '');
  95. }
  96. if (!$token) {
  97. $token = $this->request->cookie('token', '');
  98. }
  99. if ($id && $token) {
  100. if ($this->user_temp) return $this->user_temp;
  101. $user = TokenModel::where("user_id", $id)->where('token', $token)->field("user_id,token,create_time")->find();
  102. if ($user) {
  103. if (time() > ($user['create_time'] + 60 * 60 * 24 * 15)) {//如果创建时间大于15天则删除
  104. $user->delete();
  105. } else {
  106. if ((time() - $user['create_time']) > (864000)) { //token定时15天清理一次,10-15天内如果使用了则重新计算时间
  107. $user->create_time = time();
  108. $user->save();
  109. }
  110. $this->user_temp = $user;
  111. return $user;
  112. }
  113. }
  114. }
  115. if ($must) {
  116. $this->error("请登录后操作")->send();
  117. exit();
  118. }
  119. return false;
  120. }
  121. //admin认证
  122. public function getAdmin()
  123. {
  124. $user = $this->getUser(true);
  125. $info = UserModel::where('id', $user['user_id'])->where("manager", 1)->find();
  126. if ($info) {
  127. return $info;
  128. }
  129. $this->error('not permission')->send();
  130. exit();
  131. }
  132. public function success($msg, $data = []): \think\response\Json
  133. {
  134. if (is_array($msg)) {
  135. return json(['msg' => "", "code" => 1, "data" => $msg]);
  136. }
  137. return json(['msg' => $msg, "code" => 1, "data" => $data]);
  138. }
  139. public function error($msg, $data = []): \think\response\Json
  140. {
  141. if (is_array($msg)) {
  142. return json(['msg' => "", "code" => 0, "data" => $msg]);
  143. }
  144. return json(['msg' => $msg, "code" => 0, "data" => $data]);
  145. }
  146. }