| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- /**
- * 入口
- * @author wesmiler
- */
- namespace app\weixin\controller;
- use app\weixin\model\Fans;
- use app\weixin\model\Member;
- use app\weixin\model\Wechat;
- use app\weixin\service\PRedis;
- use cmf\controller\HomeBaseController;
- use think\Request;
- class BaseController extends HomeBaseController
- {
- public $userId = 0;
- public $userInfo = [];
- public $userType = 2;
- public $needRegProfile = false;
- public function __construct(Request $request = null)
- {
- parent::__construct($request);
- // AJAX请求 不验证授权
- $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';
- if($this->request->isAjax()){
- $this->userInfo = session('userInfo');
- $debug = config('weixin.debug');
- if(empty($this->userInfo) || $debug){
- // 调试模式
- $curOpenId = session('openid');
- $openid = $curOpenId? $curOpenId : config('weixin.openid');
- if($openid){
- $this->userInfo = Member::getInfo(['openid'=> $openid, 'user_type'=> 2],$field);
- }
- $wxInfo = Fans::getInfo(['openid' => $openid]);
- session('openid', $openid);
- session('wxInfo', $wxInfo);
- session('userInfo', $this->userInfo);
- }
-
- $this->userId = isset($this->userInfo['id'])? intval($this->userInfo['id']) : 0;
- $this->userType = isset($this->userInfo['user_type'])? intval($this->userInfo['user_type']) : 2;
- $userStatus = isset($this->userInfo['user_status'])? intval($this->userInfo['user_status']) : -1;
- if(empty($this->userId) || $userStatus == -1){
- showJson(1005,2103,['url'=> Wechat::makeRedirectUrl(url('/weixin/match/index','','',true))]);
- }
- // $realname = isset($this->userInfo['real_name'])? $this->userInfo['real_name'] : '';
- $mobile = isset($this->userInfo['mobile'])? $this->userInfo['mobile'] : '';
- $isReg = isset($this->userInfo['is_reg_profile'])? $this->userInfo['is_reg_profile'] : 0;
- if(empty($mobile) || $isReg != 1){
- $this->needRegProfile = true;
- session('needRegProfile', true);
- }else{
- session('needRegProfile', false);
- }
- return true;
- }
- // 调用微信授权校验
- $openid = '';
- $debug = config('weixin.debug');
- if(empty($debug)){
- $this->userInfo = session('userInfo');
- $openid = session('openid');
- $cacheKey = "weixin:auth:".$openid;
- $userStatus = isset($this->userInfo['user_status'])? intval($this->userInfo['user_status']) : -1;
- $agentStatus = isset($this->userInfo['agent_status'])? intval($this->userInfo['agent_status']) : -1;
- // PRedis::set('weixin:temp:'.$openid.'_'.get_client_ip(), ['openid'=> $openid, 'userInfo'=> $this->userInfo], 24 * 3600);
- if(empty($openid) || empty($this->userInfo) || $userStatus ==-1 || $agentStatus == -1 || !PRedis::get($cacheKey)){
- Wechat::init();
- if($openid){
- PRedis::set($cacheKey, session('userInfo'), 7 * 24 * 3600);
- }
- }
- }else{
- $curOpenId = session('openid');
- $openid = $curOpenId? $curOpenId : config('weixin.openid');
- $this->userInfo = session('userInfo');
- $agentStatus = isset($this->userInfo['agent_status'])? intval($this->userInfo['agent_status']) : -1;
- $cacheKey = "weixin:test:".$openid;
- if(empty($this->userInfo) || $agentStatus == -1 || !PRedis::get($cacheKey)){
- if($openid){
- $this->userInfo = Member::getInfo(['openid'=> $openid, 'user_type'=> 2],$field);
- }
- $wxInfo = Fans::getInfo(['openid' => $openid]);
- session('openid', $openid);
- session('wxInfo', $wxInfo);
- session('userInfo', $this->userInfo);
- if($openid){
- PRedis::set($cacheKey, session('userInfo'), 7 * 24 * 3600);
- }
- }
- $this->userId = isset($this->userInfo['id'])? $this->userInfo['id'] : 0;
- }
- // 验证信息
- // $realname = isset($this->userInfo['real_name'])? $this->userInfo['real_name'] : '';
- $mobile = isset($this->userInfo['mobile'])? $this->userInfo['mobile'] : '';
- $isReg = isset($this->userInfo['is_reg_profile'])? $this->userInfo['is_reg_profile'] : 0;
- if(empty($mobile) || $isReg != 1){
- $this->needRegProfile = true;
- session('needRegProfile', true);
- }else{
- session('needRegProfile', false);
- }
- $this->userType = isset($this->userInfo['user_type'])? intval($this->userInfo['user_type']) : 2;
- }
- /**
- * 验证用户状态
- */
- public function checkUserStatus(){
- // 验证用户是否冻结
- $userStatus = Member::where(['id'=> $this->userId, 'user_type'=> 2])->value('user_status');
- if($userStatus == -1) {
- redirect('https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=Mzg3ODEzNjMzMQ==&scene=124#wechat_redirect');
- exit;
- }else if($userStatus != 1){
- Wechat::redirectUrl(url('/weixin/page/custom', '', '', true));
- exit;
- }
- }
- }
- ?>
|