BaseController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 function __construct(App $app)
  50. {
  51. $this->app = $app;
  52. $this->request = $this->app->request;
  53. // 控制器初始化
  54. $this->initialize();
  55. }
  56. // 初始化
  57. protected function initialize()
  58. {
  59. }
  60. //系统设置项
  61. public function Setting($key = false, $def = false, $emptyReplace = false)
  62. {
  63. if ($this->SettingConfig === false) {
  64. $this->SettingConfig = SettingModel::Config();
  65. }
  66. if ($key) {
  67. if (isset($this->SettingConfig[$key])) {
  68. if ($emptyReplace && empty($this->SettingConfig[$key])) {
  69. return $def;
  70. }
  71. return $this->SettingConfig[$key];
  72. }
  73. return $def;
  74. }
  75. return $this->SettingConfig;
  76. }
  77. /**
  78. * @description :用户信息获取
  79. * @param false $must 是否强制验证,true则强制验证程序退出
  80. * @return TokenModel|array|bool|mixed|Model|void
  81. * @throws DataNotFoundException
  82. * @throws DbException
  83. * @throws ModelNotFoundException
  84. */
  85. public function getUser(bool $must = false)
  86. {
  87. $id = $this->request->header("Userid", $this->request->cookie('Userid', ''));
  88. $token = $this->request->header("Token", $this->request->cookie('Token', ''));
  89. if ($id && $token) {
  90. if ($this->user_temp) return $this->user_temp;
  91. $user = TokenModel::where("user_id", $id)->where('token', $token)->field("user_id,token,create_time")->find();
  92. if ($user) {
  93. if (time() > ($user['create_time'] + 60 * 60 * 24 * 15)) {//如果创建时间大于15天则删除
  94. $user->delete();
  95. } else {
  96. if ((time() - $user['create_time']) > (864000)) { //token定时15天清理一次,10-15天内如果使用了则重新计算时间
  97. $user->create_time = time();
  98. $user->save();
  99. }
  100. $this->user_temp = $user;
  101. return $user;
  102. }
  103. }
  104. }
  105. if ($must) {
  106. $this->error("请登录后操作")->send();
  107. exit();
  108. }
  109. return false;
  110. }
  111. //admin认证
  112. public function getAdmin()
  113. {
  114. $user = $this->getUser(true);
  115. $info = UserModel::where('id', $user['user_id'])->where("manager", 1)->find();
  116. if ($info) {
  117. return $info;
  118. }
  119. $this->error('not permission')->send();
  120. exit();
  121. }
  122. public function success($msg, $data = []): \think\response\Json
  123. {
  124. if (is_array($msg)) {
  125. return json(['msg' => "", "code" => 1, "data" => $msg]);
  126. }
  127. return json(['msg' => $msg, "code" => 1, "data" => $data]);
  128. }
  129. public function error($msg, $data = []): \think\response\Json
  130. {
  131. if (is_array($msg)) {
  132. return json(['msg' => "", "code" => 0, "data" => $msg]);
  133. }
  134. return json(['msg' => $msg, "code" => 0, "data" => $data]);
  135. }
  136. }