BaseController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * 入口
  4. * @author wesmiler
  5. */
  6. namespace app\weixin\controller;
  7. use app\weixin\model\Fans;
  8. use app\weixin\model\Member;
  9. use app\weixin\model\Wechat;
  10. use app\weixin\service\PRedis;
  11. use cmf\controller\HomeBaseController;
  12. use think\Request;
  13. class BaseController extends HomeBaseController
  14. {
  15. public $userId = 0;
  16. public $userInfo = [];
  17. public $userType = 2;
  18. public $needRegProfile = false;
  19. public function __construct(Request $request = null)
  20. {
  21. parent::__construct($request);
  22. // AJAX请求 不验证授权
  23. $field = 'id,openid,user_login,mobile,real_name,user_status,agent_type,agent_status,is_reg_profile,user_nickname,avatar,is_heart,is_follow,freezing_choose';
  24. if($this->request->isAjax()){
  25. $this->userInfo = session('userInfo');
  26. $debug = config('weixin.debug');
  27. if(empty($this->userInfo) || $debug){
  28. // 调试模式
  29. $curOpenId = session('openid');
  30. $openid = $curOpenId? $curOpenId : config('weixin.openid');
  31. if($openid){
  32. $this->userInfo = Member::getInfo(['openid'=> $openid, 'user_type'=> 2],$field);
  33. }
  34. $wxInfo = Fans::getInfo(['openid' => $openid]);
  35. session('openid', $openid);
  36. session('wxInfo', $wxInfo);
  37. session('userInfo', $this->userInfo);
  38. }
  39. $this->userId = isset($this->userInfo['id'])? intval($this->userInfo['id']) : 0;
  40. $this->userType = isset($this->userInfo['user_type'])? intval($this->userInfo['user_type']) : 2;
  41. $userStatus = isset($this->userInfo['user_status'])? intval($this->userInfo['user_status']) : -1;
  42. if(empty($this->userId) || $userStatus == -1){
  43. showJson(1005,2103,['url'=> Wechat::makeRedirectUrl(url('/weixin/match/index','','',true))]);
  44. }
  45. // $realname = isset($this->userInfo['real_name'])? $this->userInfo['real_name'] : '';
  46. $mobile = isset($this->userInfo['mobile'])? $this->userInfo['mobile'] : '';
  47. $isReg = isset($this->userInfo['is_reg_profile'])? $this->userInfo['is_reg_profile'] : 0;
  48. if(empty($mobile) || $isReg != 1){
  49. $this->needRegProfile = true;
  50. session('needRegProfile', true);
  51. }else{
  52. session('needRegProfile', false);
  53. }
  54. return true;
  55. }
  56. // 调用微信授权校验
  57. $openid = '';
  58. $debug = config('weixin.debug');
  59. if(empty($debug)){
  60. $this->userInfo = session('userInfo');
  61. $openid = session('openid');
  62. $cacheKey = "weixin:auth:".$openid;
  63. $userStatus = isset($this->userInfo['user_status'])? intval($this->userInfo['user_status']) : -1;
  64. $agentStatus = isset($this->userInfo['agent_status'])? intval($this->userInfo['agent_status']) : -1;
  65. // PRedis::set('weixin:temp:'.$openid.'_'.get_client_ip(), ['openid'=> $openid, 'userInfo'=> $this->userInfo], 24 * 3600);
  66. if(empty($openid) || empty($this->userInfo) || $userStatus ==-1 || $agentStatus == -1 || !PRedis::get($cacheKey)){
  67. Wechat::init();
  68. if($openid){
  69. PRedis::set($cacheKey, session('userInfo'), 7 * 24 * 3600);
  70. }
  71. }
  72. }else{
  73. $curOpenId = session('openid');
  74. $openid = $curOpenId? $curOpenId : config('weixin.openid');
  75. $this->userInfo = session('userInfo');
  76. $agentStatus = isset($this->userInfo['agent_status'])? intval($this->userInfo['agent_status']) : -1;
  77. $cacheKey = "weixin:test:".$openid;
  78. if(empty($this->userInfo) || $agentStatus == -1 || !PRedis::get($cacheKey)){
  79. if($openid){
  80. $this->userInfo = Member::getInfo(['openid'=> $openid, 'user_type'=> 2],$field);
  81. }
  82. $wxInfo = Fans::getInfo(['openid' => $openid]);
  83. session('openid', $openid);
  84. session('wxInfo', $wxInfo);
  85. session('userInfo', $this->userInfo);
  86. if($openid){
  87. PRedis::set($cacheKey, session('userInfo'), 7 * 24 * 3600);
  88. }
  89. }
  90. $this->userId = isset($this->userInfo['id'])? $this->userInfo['id'] : 0;
  91. }
  92. // 验证信息
  93. // $realname = isset($this->userInfo['real_name'])? $this->userInfo['real_name'] : '';
  94. $mobile = isset($this->userInfo['mobile'])? $this->userInfo['mobile'] : '';
  95. $isReg = isset($this->userInfo['is_reg_profile'])? $this->userInfo['is_reg_profile'] : 0;
  96. if(empty($mobile) || $isReg != 1){
  97. $this->needRegProfile = true;
  98. session('needRegProfile', true);
  99. }else{
  100. session('needRegProfile', false);
  101. }
  102. $this->userType = isset($this->userInfo['user_type'])? intval($this->userInfo['user_type']) : 2;
  103. }
  104. /**
  105. * 验证用户状态
  106. */
  107. public function checkUserStatus(){
  108. // 验证用户是否冻结
  109. $userStatus = Member::where(['id'=> $this->userId, 'user_type'=> 2])->value('user_status');
  110. if($userStatus == -1) {
  111. redirect('https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=Mzg3ODEzNjMzMQ==&scene=124#wechat_redirect');
  112. exit;
  113. }else if($userStatus != 1){
  114. Wechat::redirectUrl(url('/weixin/page/custom', '', '', true));
  115. exit;
  116. }
  117. }
  118. }
  119. ?>