PHP微信实现主从,绑定多个微信公众平台

实现微信公众号:

PHP微信实现主从,绑定多个微信公众平台_第1张图片
Paste_Image.png

PHP微信实现主从,绑定多个微信公众平台
客户需求:

微信红包一个公众号限制100个,但是用户有时候可能超过这个需求,那么这个时候就需要去绑定多个微信公众号,并且是同一个用户去实现,也就是说我们要拿到多个OPENID

来看几个文件

数据库配置文件


PHP微信实现主从,绑定多个微信公众平台_第2张图片
Paste_Image.png

项目结构

PHP微信实现主从,绑定多个微信公众平台_第3张图片
Paste_Image.png



检查授权核心控制器

/**

namespace WxShop\Controller;

use Common\Helper\OrderHelper;
use Common\WeChat\TpWeChat;
use WxShop\Api\User;

class WxShopController extends BaseController
{

protected $_user;
protected $_page_list = 20;
protected $_openIds = [];
protected $_wxSlaveConfigs;
protected $_wxMainConfig;
protected $userRandom;
protected $_wxMainTool;

public function __construct()
{
    $this->userRandom = session("user_random");//标记一个用户得唯一标识
    if ($this->userRandom) {
        session("user_random", OrderHelper::createOrderNo());//生成一个
        $this->userRandom = session("user_random");
    }
    $this->_wxMainConfig = M('wx_config')->where(['enable' => 1, 'is_main' => 1])->find();//主
    $this->_wxSlaveConfigs = M('wx_config')->where(['enable' => 1, 'is_main' => 0])->select();//从
    if (!$this->_wxMainConfig) {
        exit("SYSTEM CONFIG ERR  Author:QQ 1716771371");
    }
    $this->_wxMainTool = new TpWeChat($this->_wxMainConfig);
    parent::__construct();
}

protected function _initialize()
{
    $this->_user['id'] = 6;
    if (!$this->_checkWxBrowser()) {
        // exit();
    }
    //检查用户权限
    $this->_checkOauth();
}

/**
 * 检测用户授权
 */
final  private function _checkOauth()
{
    //获取一个主微信公众号,去拉取用户信息  START
    $wxTool = new TpWeChat($this->_wxMainConfig);
    $wxAccessOauth = session("access_oauth_id_{$this->_wxMainConfig['id']}");
    if (!$wxAccessOauth) {
        //记录上一次的URL
        session("c_name", CONTROLLER_NAME);
        session("a_name", ACTION_NAME);
        $goUrl = $wxTool->getOauthRedirect(WeChat_CallBack, "wx_{$this->_wxMainConfig['id']}", "snsapi_userinfo");
        redirect($goUrl);
        exit();
    }
    $user = S("user_main_{$wxAccessOauth['openid']}");//缓存中取出用户信息
    if (!$user) {
        //检测用户是否存在
        $user = $this->checkUserExist($wxAccessOauth, $wxAccessOauth['openid'], $this->_wxMainConfig['id']);
        S("user_main_{$wxAccessOauth['openid']}", $user);
    }
    $this->_user = $user;
    session("user_main_id", $user['id']);

    //主微信好已经获取

    //检测,所有公众号用户基础授权,从微信
    foreach ($this->_wxSlaveConfigs as $item) {
        //获取
        $tmpAccessOauth = null;
        $wxTmpTool = null;
        $tmpUser = null;
        $wxTmpTool = new TpWeChat($item);
        $tmpAccessOauth = session("access_oauth_id_{$item['id']}");
        if (!$tmpAccessOauth) {

            //记录上一次的URL
            session("c_name", CONTROLLER_NAME);
            session("a_name", ACTION_NAME);

            $goUrl = $wxTmpTool->getOauthRedirect(WeChat_CallBack, "wx_{$item['id']}", "snsapi_base");
            redirect($goUrl);
            exit();
        }
        $tmpUser = S("user_{$item['id']}_{$tmpAccessOauth['openid']}");//缓存中取出用户信息
        if (!$tmpUser) {
            $tmpUser = $this->checkSlaveExist($tmpAccessOauth['openid'], $item['id'], $user['id']);
            S("user_{$item['id']}_{$tmpAccessOauth['openid']}", $tmpUser);
        }
    }
}

private function checkSlaveExist($openid, $wxId, $uid)
{
    $oauth = User::getWeConfigUser($openid, $wxId);
    if (!$oauth) {
        User::addWxOpenUser($openid, $uid, time(), $wxId, 0);
    }
    return $oauth;
}

private function checkUserExist($accessToken, $openid, $wxId)
{
    //查询是否存在用户
    $oauth = User::getWeUser($openid, $wxId);
    if (!$oauth) {
        $userInfo = $this->_getUserProfile($accessToken['access_token'], $openid);
        $playMember['username'] = "wx_" . OrderHelper::createOrderNo();
        $playMember['nickname'] = $userInfo['nickname'] . '';
        $playMember['open_id'] = $openid;
        $playMember['head_img_url'] = $userInfo['headimgurl'] . '';
        $playMember['create_time'] = time();
        $playMember['sex'] = $userInfo['sex'] . '';
        $playMember['score'] = 0;//默认赠送积分
        $playMember['money'] = 0;
        $playMember['history_money'] = 0;
        $playMember['status'] = 1;
        $playMember['del_status'] = 0;
        $playMember['password'] = md5("123!@#!");
        if ($playMember['nickname'] == '') {
            $playMember['nickname'] = rand(1000000, 9999999) . date("mdHsm") . "-微信用户";
        }
        if ($userInfo['unionid'] != '') {
            $userInfo['unionid'] = '';
        }
        $addRet = User::addWeUser($playMember);//添加到微信
        //添加微信用户信息
        $addWxRet = User::addWxOpenUser($openid, $addRet, $playMember['create_time'], $wxId, $userInfo['unionid']);
        if ($addRet && $addWxRet) {

        }
    } else {
        return $oauth;
    }
}

private function _getUserProfile($access_token, $openid)
{
    return $this->_wxMainTool->getOauthUserinfo($access_token, $openid);
}

}




数据结果


![Paste_Image.png](http://upload-images.jianshu.io/upload_images/3100692-e0008e2c548917aa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)



![Paste_Image.png](http://upload-images.jianshu.io/upload_images/3100692-24a5115cb4d87b1d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

你可能感兴趣的:(PHP微信实现主从,绑定多个微信公众平台)