BaseController.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. $vali = $this->validateToken();
  20. if($vali['code']!=0){
  21. error_show($vali['code'],$vali['message'])->send();
  22. die();
  23. }
  24. }
  25. /**
  26. * @return array
  27. * @throws \think\db\exception\DataNotFoundException
  28. * @throws \think\db\exception\DbException
  29. * @throws \think\db\exception\ModelNotFoundException
  30. * @throws \think\exception\DbException
  31. */
  32. public function validateToken(){
  33. $token = isset($this->post['token']) ? trim($this->post['token']) : "";
  34. if($token==""){
  35. return ["code"=>101,"message"=>"参数token不能为空"];
  36. }
  37. $effetc = VerifyTokens($token);
  38. if(!empty($effetc) && $effetc['code']!=0) {
  39. return ["code"=>$effetc['code'],"message"=>$effetc['message']];
  40. // return error_show($effetc['code'], $effetc['message']);
  41. }
  42. $this->uid=isset($effetc['data']['user']['id']) ?$effetc['data']['user']['id']:"";
  43. $this->uname=isset($effetc['data']['user']['nickname']) ?$effetc['data']['user']['nickname']:"";
  44. $role = Db::name("user_role")->where(["uid"=>$this->uid,"is_del"=>0])->find();
  45. if($role['status']==0){
  46. return ["code"=>101,"message"=>"账户已禁用"];
  47. //return error_show(101,'账户已禁用');
  48. }
  49. $this->roleid=$role['roleid'];
  50. return ["code"=>0,"message"=>"验证通过"];
  51. }
  52. }