BaseController.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace app\admin;
  3. use think\App;
  4. use app\BaseController as base;
  5. use think\facade\Db;
  6. /**
  7. * Class BaseController
  8. * @package app\admin
  9. * 基础控制器
  10. */
  11. class BaseController extends base{
  12. public $post=[];
  13. public $uid='';
  14. public $uname='';
  15. public $roleid='';
  16. public function __construct(App $app) {
  17. parent::__construct($app);
  18. $this->post =$this->request->post();
  19. $this->validateToken();
  20. }
  21. /**
  22. * @return \think\response\Json|void
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\DbException
  25. * @throws \think\db\exception\ModelNotFoundException
  26. * @throws \think\exception\DbException
  27. */
  28. public function validateToken(){
  29. $token = isset($this->post['token']) ? trim($this->post['token']) : "";
  30. if($token==""){
  31. return error_show(101,'token不能为空');
  32. }
  33. $effetc = VerifyTokens($token);
  34. if(!empty($effetc) && $effetc['code']!=0) {
  35. return error_show($effetc['code'], $effetc['message']);
  36. }
  37. $this->uid=isset($effetc['data']['user']['id']) ?$effetc['data']['user']['id']:"";
  38. $this->uname=isset($effetc['data']['user']['nickname']) ?$effetc['data']['user']['nickname']:"";
  39. $role = Db::name("user_role")->where(["uid"=>$this->uid,"is_del"=>0])->find();
  40. if($role['status']==0){
  41. return error_show(101,'账户已禁用');
  42. }
  43. $this->roleid=$role['roleid'];
  44. }
  45. }