thinkphp5.1 easywechat4 微信第三方開放平臺

需求描述

  1. 當前商城(uid標識)受權第三方開發平臺.
  2. 網頁受權成功後跳轉到另外一個商城項目連接並帶上當前微信用戶信息和微信初始化驗證簽名.

第三方平臺受權

安裝easywechat4
$ composer require overtrue/wechat:~4.0 -vvv
引用
use EasyWeChat\Factory;
建立一個跳轉到微信掃二維碼受權頁面
/**
 * 開發平臺受權跳轉
 *
 * @return void
 */
public function accessView(){
    // 
    $uid = Request()->route('uid' , 0);
    $url = 'http://qgcloud.capsui.com/public/index/wxopen/config?uid=' . $uid;
    $this->assign('url' , $url);
    return $this->fetch();
}
跳轉方法(爲何我不寫到上一個方法呢 由於微信要求同一個地址)
/**
 * 開發平臺跳轉受權掃碼頁
 *
 * @return void
 */
public function config(){
    $uid = Request()->get('uid' , 0);
    $config = [
        'app_id'   => '開放平臺第三方平臺 APPID',
        'secret'   => '開放平臺第三方平臺 Secret',
        'token'    => '開放平臺第三方平臺 Token',
        'aes_key'  => '開放平臺第三方平臺 AES Key'
    ];
    $openPlatform = Factory::openPlatform($config);
    
    $url = $openPlatform->getPreAuthorizationUrl('http://qgcloud.capsui.com/public/index/wxopen/wxcallback?uid=' . $uid);

    $this->redirect($url);
}
受權回調(注意:掃碼確認受權後他第一次回調不會帶uid參數,)
引入 
use EasyWeChat\OpenPlatform\Server\Guard;
/**
 * 開發平臺受權回調
 *
 * @return void
 */
public function wxcallback(){
    // 這個表是記錄受權成功的
    //$Wxpublic   = new Wxpublic;
    // 這個表是記錄受權成功後傳過來所屬uid商城綁定appid
    //$ShopConfig = new ShopConfig;

    $get = Request()->param();
    
    $config = [
        'app_id'   => '開放平臺第三方平臺 APPID',
        'secret'   => '開放平臺第三方平臺 Secret',
        'token'    => '開放平臺第三方平臺 Token',
        'aes_key'  => '開放平臺第三方平臺 AES Key'
    ];
    $openPlatform = Factory::openPlatform($config);
    $server       = $openPlatform->server;

    
    // 處理受權成功事件-第一次回調
    // 閉包方法!裏面調用外面的方法請在use裏面填寫
    $server->push(function ($message) use ($openPlatform /*, $Wxpublic*/) {
        
        $authCode = $message['AuthorizationCode'];
        $res      = $openPlatform->handleAuthorize($authCode);

        if($res['authorization_info']['authorizer_refresh_token']){
            //受權成功記錄到數據庫
            //$Wxpublic->insert(['appid' => $res['authorization_info']['authorizer_appid'] , 'createtime' => time()]);
        }

    }, Guard::EVENT_AUTHORIZED);

    // 處理受權取消事件-第一次回調
    // 閉包方法!裏面調用外面的方法請在use裏面填寫
    $server->push(function ($message) use(/*$Wxpublic , $ShopConfig*/) {
        //處理數據庫邏輯
        //$Wxpublic::appid($message['AppId'])->delete();
        //$ShopConfig::appid($message['AppId'])->update(['token' => '']);
    }, Guard::EVENT_UNAUTHORIZED);
    
    // 第二次回調會帶一個受權code和自定義參數商城id(uid)
    if(isset($get['auth_code']) && isset($get['uid'])){
        
        $res      = $openPlatform->handleAuthorize($get['auth_code']);
        $appid    = $res['authorization_info']['authorizer_appid'];
        //數據庫邏輯
        //$isConfig = $Wxpublic::appid($appid)->count();
        
        //if($isConfig){
        //$add = $ShopConfig->where('uid' , $get['uid'])->update(['token' => $appid]);
        //}
    }

    return $server->serve();
}

第三方平臺 網頁受權&微信JSSDK初始化簽名生成

/**
 * 網頁受權調起
 *
 * @return void
 */
public function htmlAccess(){
    $appid = Request()->get('appid' , 0);
    
    $config = [
        'app_id'   => '開放平臺第三方平臺 APPID',
        'secret'   => '開放平臺第三方平臺 Secret',
        'token'    => '開放平臺第三方平臺 Token',
        'aes_key'  => '開放平臺第三方平臺 AES Key'
    ];
    $openPlatform = Factory::openPlatform($config);
    $data         = $openPlatform->getAuthorizer($appid);
    $appid        = $data['authorization_info']['authorizer_appid'];
    $refreshToken = $data['authorization_info']['authorizer_refresh_token'];

    $officialAccount = $openPlatform->officialAccount($appid , $refreshToken);
    $oauth           = $officialAccount->oauth;
    
    // 回調受權地址
    $url      = "http://qgcloud.capsui.com/public/index/wxopen/callbackOpenid";
    $response = $officialAccount->oauth->scopes(['snsapi_userinfo'])->redirect($url)->send();

}
網頁受權回調方法
/**
 * 網頁受權回調
 *
 * @return void
 */
public function callbackOpenid(){
    $appid = Request()->get('appid' , null);
    
    $config = [
        'app_id'   => '開放平臺第三方平臺 APPID',
        'secret'   => '開放平臺第三方平臺 Secret',
        'token'    => '開放平臺第三方平臺 Token',
        'aes_key'  => '開放平臺第三方平臺 AES Key'
    ];
    $openPlatform = Factory::openPlatform($config);
    $data         = $openPlatform->getAuthorizer($appid);
    
    $appid        = $data['authorization_info']['authorizer_appid'];
    $refreshToken = $data['authorization_info']['authorizer_refresh_token'];
    
    // 獲取微信用戶信息 如openid nickname等信息
    $officialAccount = $openPlatform->officialAccount($appid , $refreshToken);
    $oauth           = $officialAccount->oauth;
    $user            = $oauth->user();
    
    // 處理wxconfig初始化JSSDK
    $officialAccount->jssdk->setUrl('http://quguoshop.capsui.com/');
    $wxconfig = $officialAccount->jssdk->buildConfig(['chooseWXPay'], $debug = true, $beta = false, $json = true);

    $ShopConfig = new ShopConfig;
    $shopInfo   = $ShopConfig::appid($appid)->find();
    
    // 注意 這裏我是帶參數跳轉到其餘TP5項目裏面再用緩存處理一下
    $url = 'http://quguoshop.capsui.com/public/wxoauthCallback?data=' . json_encode($user->toArray()) . '&token=' . $shopInfo['id'] . '&wxconfig=' . $wxconfig;
    $this->redirect($url);
}
相關文章
相關標籤/搜索