Base.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller;
  4. use app\BaseController;use think\App;use think\exception\HttpResponseException;use think\Response;
  5. class Base extends BaseController
  6. {
  7. protected $novalidate=[
  8. 'systemlast',
  9. 'login',
  10. 'stats',
  11. 'departstat',
  12. 'departEveryDay',
  13. 'departEveryMonth',
  14. 'companyEveryMonth',
  15. 'dzqrd',
  16. 'companyEvery',
  17. 'wechat_getinfo',
  18. 'wechat_getconfig',
  19. 'totalstat'
  20. ];
  21. public $supperAction=[
  22. 'payadd',#对账新建
  23. 'paysave',#对账编辑
  24. 'paydel',#对账删除
  25. 'invadd',#回票申请新建
  26. 'invAddBatchByImport',#回票申请批量
  27. 'invdel',#回票申请删除
  28. 'hpinvreturn', #回票申请退票
  29. 'stagereturn',#付款申请退款
  30. ];
  31. protected $uid=0;
  32. protected $uname='';
  33. protected $roleid=0;
  34. protected $level=0;
  35. public function __construct(App $app) {
  36. parent::__construct($app);
  37. if(!in_array($this->request->pathinfo(),$this->novalidate)&&!in_array('*', $this->novalidate)){
  38. $this->validateToken($this->request->param());
  39. }
  40. }
  41. /**
  42. * @param string $message
  43. * @param int $code
  44. * @param null $data
  45. */
  46. public function error($message='',$code=1003,$data=null){
  47. $this->result($message,$data,$code);
  48. }
  49. /**
  50. * @param string $msg
  51. * @param null $data
  52. * @param int $code
  53. * @param string|null $type
  54. * @param array $header
  55. * @param array $options
  56. */
  57. private function result(string $msg, $data = null, int $code = 0, string $type = 'json', array $header = [], array
  58. $options = [])
  59. {
  60. $result = [
  61. 'code' => $code,
  62. 'message' => $msg,
  63. 'data' => $data,
  64. ];
  65. $code = 200;
  66. if (isset($header['statuscode'])) {
  67. $code = $header['statuscode'];
  68. unset($header['statuscode']);
  69. }
  70. $response = Response::create($result, $type, $code)->header($header)->options($options);
  71. throw new HttpResponseException($response);
  72. }
  73. /**
  74. * @param string $message
  75. * @param int $code
  76. * @param null $data
  77. */
  78. public function success($message='',$data=null,$code=0){
  79. $this->result($message,$data,$code);
  80. }
  81. /**
  82. * @param $request 校验用户信息
  83. * @throws \think\db\exception\DataNotFoundException
  84. * @throws \think\db\exception\DbException
  85. * @throws \think\db\exception\ModelNotFoundException
  86. * @throws \think\exception\DbException
  87. */
  88. public function validateToken($request){
  89. $token = $request['token']?? '';
  90. $companyNo = $request['relaComNo'] ?? '';
  91. if($token=='') $this->error('参数token不能为空','101');
  92. $effetc = VerifyTokens($token);
  93. if(!empty($effetc) && $effetc['code']!=0)$this->error($effetc['message'],$effetc['code']);
  94. $this->uid=$effetc['data']['id']??'';
  95. $this->uname=$effetc['data']['nickname']??'';
  96. $where=[];
  97. if($companyNo!=''){
  98. $where=['companyNo'=>$companyNo];
  99. }
  100. $userrole = \app\admin\model\UserRole::where(['uid'=>$this->uid,'is_del'=>0])->where($where)
  101. ->findOrEmpty();
  102. if($userrole->isEmpty()){
  103. $this->error("账户已禁用",'101');
  104. }
  105. $this->roleid=$userrole->roleid;
  106. $role =\app\admin\model\Role::where(['id'=>$userrole->roleid])->findOrEmpty();
  107. if($role->status==0 || $userrole->status==0 ){
  108. $this->level=0;
  109. }else $this->level = $effetc['data']['level'];
  110. }
  111. //供应商公司存在操作账户,请用供应商账户操作
  112. public function NoAction(){
  113. $pathinfo =$this->request->pathinfo();
  114. $relaComNo =$this->request->param('relaComNo');
  115. if (in_array($this->level, [2, 3])) {
  116. if (in_array($pathinfo, $this->supperAction) && $this->level == 2) {
  117. if ($relaComNo == '') $this->error('关联公司不能为空');
  118. $companyinfo = UserHandle('/hqInfo', ['code' => $relaComNo]);
  119. if ($companyinfo['code'] != 0)$this->error($companyinfo['message'],$companyinfo['code']);
  120. if (!empty($companyinfo['data']) && $companyinfo['data']['relation_code'] != '') {
  121. $db= UserHandle('/userCompanyBasicList', ['companyNo' =>$companyinfo['data']['relation_code']]);
  122. if ($db['code'] != 0) $this->error($db['message'],$db['code']);
  123. if (!empty($db['data']) && $db['data']['count'] > 0) {
  124. $this->error("供应商公司存在操作账户,请用供应商账户操作");
  125. // return error_show(1004,"供应商公司存在操作账户,请用供应商账户操作");
  126. }
  127. }
  128. }
  129. }
  130. }
  131. }