BaseController.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 $level='';
  17. public $novalidate=['GetLast',"performance","productTh","productPerformance"];
  18. public function __construct(App $app) {
  19. parent::__construct($app);
  20. $this->post =$this->request->post();
  21. if(!in_array($this->request->action(),$this->novalidate)){
  22. $vali = $this->validateToken();
  23. if($vali['code']!=0){
  24. error_show($vali['code'],$vali['message'])->send();
  25. die();
  26. }
  27. }
  28. }
  29. /**
  30. * @return array
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. * @throws \think\exception\DbException
  35. */
  36. public function validateToken(){
  37. $token = isset($this->post['token']) ? trim($this->post['token']) : "";
  38. $companyNo = isset($this->post['relaComNo']) ? trim($this->post['relaComNo']) : "";
  39. if($token==""){
  40. return ["code"=>101,"message"=>"参数token不能为空"];
  41. }
  42. $effetc = VerifyTokens($token);
  43. if(!empty($effetc) && $effetc['code']!=0) {
  44. return ["code"=>$effetc['code'],"message"=>$effetc['message']];
  45. }
  46. $this->uid=$effetc['data']['id']??"";
  47. $this->uname=$effetc['data']['nickname']??"";
  48. $where=[];
  49. if($companyNo!=""){
  50. $where=["companyNo"=>$companyNo];
  51. }
  52. $role = Db::name("user_role")->where(["uid"=>$this->uid,"is_del"=>0])->where($where)->findOrEmpty();
  53. if(empty($role)){
  54. return ["code"=>101,"message"=>"账户已禁用"];
  55. }
  56. $this->roleid=$role['roleid'];
  57. $level =Db::name("role")->where(["id"=>$role['roleid']])->find();
  58. if($role['status']==0 || $level['status']==0 ){
  59. $this->level='';
  60. }else{
  61. $this->level=$level['level']>1 ?2 :($level['level']??1);
  62. }
  63. return ["code"=>0,"message"=>"验证通过"];
  64. }
  65. }