BaseController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. return true;
  23. // AJAX请求 不验证授权
  24. $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';
  25. if($this->request->isAjax()){
  26. $this->userInfo = session('userInfo');
  27. $debug = config('weixin.debug');
  28. if(empty($this->userInfo) || $debug){
  29. // 调试模式
  30. $curOpenId = session('openid');
  31. $openid = $curOpenId? $curOpenId : config('weixin.openid');
  32. if($openid){
  33. $this->userInfo = Member::getInfo(['openid'=> $openid, 'user_type'=> 2],$field);
  34. }
  35. $wxInfo = Fans::getInfo(['openid' => $openid]);
  36. session('openid', $openid);
  37. session('wxInfo', $wxInfo);
  38. session('userInfo', $this->userInfo);
  39. }
  40. $this->userId = isset($this->userInfo['id'])? intval($this->userInfo['id']) : 0;
  41. $this->userType = isset($this->userInfo['user_type'])? intval($this->userInfo['user_type']) : 2;
  42. $userStatus = isset($this->userInfo['user_status'])? intval($this->userInfo['user_status']) : -1;
  43. if(empty($this->userId) || $userStatus == -1){
  44. showJson(1005,2103,['url'=> Wechat::makeRedirectUrl(url('/weixin/match/index','','',true))]);
  45. }
  46. // $realname = isset($this->userInfo['real_name'])? $this->userInfo['real_name'] : '';
  47. $mobile = isset($this->userInfo['mobile'])? $this->userInfo['mobile'] : '';
  48. $isReg = isset($this->userInfo['is_reg_profile'])? $this->userInfo['is_reg_profile'] : 0;
  49. if(empty($mobile) || $isReg != 1){
  50. $this->needRegProfile = true;
  51. session('needRegProfile', true);
  52. }else{
  53. session('needRegProfile', false);
  54. }
  55. return true;
  56. }
  57. // 调用微信授权校验
  58. $openid = '';
  59. $debug = config('weixin.debug');
  60. PRedis::set("weixin:temp:access_".get_client_ip(),['openid'=>session('openid'),'info'=>session('userInfo'),'date'=>date('Y-m-d H:i:s')],600);
  61. if(empty($debug)){
  62. $this->userInfo = session('userInfo');
  63. $openid = session('openid');
  64. $cacheKey = "weixin:auth:".$openid;
  65. $userStatus = isset($this->userInfo['user_status'])? intval($this->userInfo['user_status']) : -1;
  66. $agentStatus = isset($this->userInfo['agent_status'])? intval($this->userInfo['agent_status']) : -1;
  67. // PRedis::set('weixin:temp:'.$openid.'_'.get_client_ip(), ['openid'=> $openid, 'userInfo'=> $this->userInfo], 24 * 3600);
  68. if(empty($openid) || empty($this->userInfo) || $userStatus ==-1 || $agentStatus == -1 || !PRedis::get($cacheKey)){
  69. Wechat::init();
  70. if($openid){
  71. PRedis::set($cacheKey, session('userInfo'), 7 * 24 * 3600);
  72. }
  73. }
  74. }else{
  75. $curOpenId = session('openid');
  76. $openid = $curOpenId? $curOpenId : config('weixin.openid');
  77. $this->userInfo = session('userInfo');
  78. $agentStatus = isset($this->userInfo['agent_status'])? intval($this->userInfo['agent_status']) : -1;
  79. $cacheKey = "weixin:test:".$openid;
  80. if(empty($this->userInfo) || $agentStatus == -1 || !PRedis::get($cacheKey)){
  81. if($openid){
  82. $this->userInfo = Member::getInfo(['openid'=> $openid, 'user_type'=> 2],$field);
  83. }
  84. $wxInfo = Fans::getInfo(['openid' => $openid]);
  85. session('openid', $openid);
  86. session('wxInfo', $wxInfo);
  87. session('userInfo', $this->userInfo);
  88. if($openid){
  89. PRedis::set($cacheKey, session('userInfo'), 7 * 24 * 3600);
  90. }
  91. }
  92. $this->userId = isset($this->userInfo['id'])? $this->userInfo['id'] : 0;
  93. }
  94. // 验证信息
  95. // $realname = isset($this->userInfo['real_name'])? $this->userInfo['real_name'] : '';
  96. $mobile = isset($this->userInfo['mobile'])? $this->userInfo['mobile'] : '';
  97. $isReg = isset($this->userInfo['is_reg_profile'])? $this->userInfo['is_reg_profile'] : 0;
  98. if(empty($mobile) || $isReg != 1){
  99. $this->needRegProfile = true;
  100. session('needRegProfile', true);
  101. }else{
  102. session('needRegProfile', false);
  103. }
  104. $this->userType = isset($this->userInfo['user_type'])? intval($this->userInfo['user_type']) : 2;
  105. }
  106. /**
  107. * 验证用户状态
  108. */
  109. public function checkUserStatus(){
  110. // 验证用户是否冻结
  111. $userStatus = Member::where(['id'=> $this->userId, 'user_type'=> 2])->value('user_status');
  112. if($userStatus == -1) {
  113. redirect('https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=Mzg3ODEzNjMzMQ==&scene=124#wechat_redirect');
  114. exit;
  115. }else if($userStatus != 1){
  116. Wechat::redirectUrl(url('/weixin/page/custom', '', '', true));
  117. exit;
  118. }
  119. }
  120. }
  121. ?>