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. public $uid=0;
  32. public $uname='';
  33. public $roleid=0;
  34. public $level=0;
  35. public $post='';
  36. public function __construct(App $app) {
  37. parent::__construct($app);
  38. $this->post=$this->request->param();
  39. if(!in_array($this->request->pathinfo(),$this->novalidate)&&!in_array('*', $this->novalidate)){
  40. $this->validateToken($this->request->param());
  41. }
  42. }
  43. /**
  44. * @param string $message
  45. * @param int $code
  46. * @param null $data
  47. */
  48. public function error($message='',$code=1003,$data=null){
  49. $this->result($message,$data,$code);
  50. }
  51. /**
  52. * @param string $msg
  53. * @param null $data
  54. * @param int $code
  55. * @param string|null $type
  56. * @param array $header
  57. * @param array $options
  58. */
  59. private function result(string $msg, $data = null, int $code = 0, string $type = 'json', array $header = [], array
  60. $options = [])
  61. {
  62. $result = [
  63. 'code' => $code,
  64. 'message' => $msg,
  65. 'data' => $data,
  66. ];
  67. $code = 200;
  68. if (isset($header['statuscode'])) {
  69. $code = $header['statuscode'];
  70. unset($header['statuscode']);
  71. }
  72. $response = Response::create($result, $type, $code)->header($header)->options($options);
  73. throw new HttpResponseException($response);
  74. }
  75. /**
  76. * @param string $message
  77. * @param int $code
  78. * @param null $data
  79. */
  80. public function success($message='',$data=null,$code=0){
  81. $this->result($message,$data,$code);
  82. }
  83. /**
  84. * @param $request 校验用户信息
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\DbException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. * @throws \think\exception\DbException
  89. */
  90. public function validateToken($request){
  91. $token = $request['token']?? '';
  92. $companyNo = $request['relaComNo'] ?? '';
  93. if($token=='') $this->error('参数token不能为空',101);
  94. $effetc = VerifyTokens($token);
  95. if(!empty($effetc) && $effetc['code']!=0)$this->error($effetc['message'],$effetc['code']);
  96. $this->uid=$effetc['data']['id']??'';
  97. $this->uname=$effetc['data']['nickname']??'';
  98. $where=[];
  99. if($companyNo!=''){
  100. $where=['companyNo'=>$companyNo];
  101. }
  102. $userrole = \app\admin\model\UserRole::where(['uid'=>$this->uid,'is_del'=>0])->where($where)
  103. ->findOrEmpty();
  104. if($userrole->isEmpty()){
  105. $this->error("账户已禁用",101);
  106. }
  107. $this->roleid=$userrole->roleid;
  108. $role =\app\admin\model\Role::where(['id'=>$userrole->roleid])->findOrEmpty();
  109. if($role->status==0 || $userrole->status==0 ){
  110. $this->level=0;
  111. }else $this->level = $effetc['data']['level'];
  112. }
  113. //供应商公司存在操作账户,请用供应商账户操作
  114. public function NoAction(){
  115. $pathinfo =$this->request->pathinfo();
  116. $relaComNo =$this->request->param('relaComNo');
  117. if (in_array($this->level, [2, 3])) {
  118. if (in_array($pathinfo, $this->supperAction) && $this->level == 2) {
  119. if ($relaComNo == '') $this->error('关联公司不能为空');
  120. $companyinfo = UserHandle('/hqInfo', ['code' => $relaComNo]);
  121. if ($companyinfo['code'] != 0)$this->error($companyinfo['message'],$companyinfo['code']);
  122. if (!empty($companyinfo['data']) && $companyinfo['data']['relation_code'] != '') {
  123. $db= UserHandle('/userCompanyBasicList', ['companyNo' =>$companyinfo['data']['relation_code']]);
  124. if ($db['code'] != 0) $this->error($db['message'],$db['code']);
  125. if (!empty($db['data']) && $db['data']['count'] > 0) {
  126. $this->error("供应商公司存在操作账户,请用供应商账户操作");
  127. }
  128. }
  129. }
  130. }
  131. }
  132. }