BaseController.php 3.7 KB

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