BaseController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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");
  88. $token = $this->request->header("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")->cache('user.' . $id, 3600)->find();
  92. if ($user) {
  93. if ((time() - $user['create_time']) > (864000)) { //token定时15天清理一次,10-15天内如果使用了则重新计算时间
  94. $user->create_time = time();
  95. $user->save();
  96. }
  97. $this->user_temp = $user;
  98. return $user;
  99. }
  100. }
  101. if ($must) {
  102. $this->error("请登录后操作")->send();
  103. exit();
  104. }
  105. return false;
  106. }
  107. //admin认证
  108. public function getAdmin()
  109. {
  110. $user = $this->getUser(true);
  111. $info = UserModel::where('id', $user['user_id'])->where("manager", 1)->find();
  112. if ($info) {
  113. return $info;
  114. }
  115. $this->error('not permission')->send();
  116. exit();
  117. }
  118. public function success($msg, $data = []): \think\response\Json
  119. {
  120. if (is_array($msg)) {
  121. return json(['msg' => "", "code" => 1, "data" => $msg]);
  122. }
  123. return json(['msg' => $msg, "code" => 1, "data" => $data]);
  124. }
  125. public function error($msg, $data = []): \think\response\Json
  126. {
  127. if (is_array($msg)) {
  128. return json(['msg' => "", "code" => 0, "data" => $msg]);
  129. }
  130. return json(['msg' => $msg, "code" => 0, "data" => $data]);
  131. }
  132. }