濮阳杆衣贸易有限公司

主頁 > 知識庫 > PHP實(shí)現(xiàn)微信小程序用戶授權(quán)的工具類示例

PHP實(shí)現(xiàn)微信小程序用戶授權(quán)的工具類示例

熱門標(biāo)簽:銀川ai電話機(jī)器人 地圖標(biāo)注風(fēng)向標(biāo) 安陽自動外呼系統(tǒng)價(jià)格是多少 芒果電銷機(jī)器人 上海公司外呼系統(tǒng)線路 臨沂智能電銷機(jī)器人軟件 浙江外呼電話系統(tǒng)軟件 十堰ai電話機(jī)器人效果怎么樣 電梯外呼線路板維修視頻

先準(zhǔn)備工作

1.申請一個(gè)小程序,申請地址:傳送門
2.仔細(xì)閱讀小程序的用戶授權(quán)登陸官方文檔: 《用戶授權(quán)登陸的流程》
3.仔細(xì)閱讀微信用戶數(shù)據(jù)解密的相關(guān)文檔: 《用戶數(shù)據(jù)解密說明文檔》
4.在小程序后臺配置好相應(yīng)的后端請求地址,路徑是:開發(fā)---->開發(fā)設(shè)置,如圖


5.小程序如果需要做多個(gè)小程序的打通,還需要在微信開放平臺綁定到開發(fā)者賬號下面, 如果不需要union_id請忽略

6.服務(wù)端準(zhǔn)備一個(gè)用戶授權(quán)的接口,假設(shè)接口鏈接為http://test.dev.com/user/authorization,此接口接受如下參數(shù)

  • code:微信登陸接口返回的登陸憑證,用戶獲取session_key
  • iv:微信小程序登陸接口返回的向量,用于數(shù)據(jù)解密
  • encrypted_data : 微信獲取用戶信息接口的返回的用戶加密數(shù)據(jù),用于后端的接口解析
  • signature加密數(shù)據(jù)

接口返回的數(shù)據(jù)如下

{
  "errcode": 200,
  "msg": "SUCCESS",
  "data": {
    "uid": 34098,
    "unionid": "xxx",
  }
}

6.建表

1)用戶表,其中比較重要的字段是union_id,因?yàn)槲覀兪怯卸鄠€(gè)小程序和公眾號,因此使用這個(gè)來區(qū)分唯一的用戶編號

DROP TABLE IF EXISTS `jz_wxa_user`;
CREATE TABLE `jz_wxa_user` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `uid` bigint(18) DEFAULT NULL,
 `openid` varchar(255) CHARACTER SET utf8 DEFAULT NULL COMMENT 'openid',
 `user_name` varchar(100) CHARACTER SET utf8mb4 DEFAULT '',
 `nick_name` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '用戶昵稱',
 `sex` enum('0','1') CHARACTER SET utf8 DEFAULT '1' COMMENT '性別',
 `avatar` varchar(255) CHARACTER SET utf8 DEFAULT NULL COMMENT '用戶頭像',
 `province` varchar(100) CHARACTER SET utf8 DEFAULT NULL COMMENT '省份',
 `city` varchar(100) CHARACTER SET utf8 DEFAULT NULL COMMENT '城市',
 `country` varchar(100) CHARACTER SET utf8 DEFAULT NULL COMMENT '國家',
 `wx_union_id` varchar(255) CHARACTER SET utf8 DEFAULT NULL COMMENT '公眾平臺的唯一id',
 `from_url` varchar(255) CHARACTER SET utf8 DEFAULT NULL COMMENT '來源url',
 `created_at` timestamp NULL DEFAULT NULL,
 `updated_at` timestamp NULL DEFAULT NULL,
 `from_appid` varchar(30) COLLATE utf8mb4_unicode_ci DEFAULT 'wx95fc895bebd3743b' COMMENT '來源appid',
 `wx_header` varchar(150) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '微信頭像',
 `gh_openid` varchar(60) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '微信公眾號openid',
 `phone` varchar(30) CHARACTER SET utf8 DEFAULT '' COMMENT '手機(jī)號碼',
 PRIMARY KEY (`id`),
 KEY `idx_uid_union_id` (`uid`,`wx_union_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

實(shí)現(xiàn)步驟

用戶授權(quán)時(shí)序圖

關(guān)鍵代碼

小程序端

小程序端的獲取用戶信息流程

1)調(diào)用login方法獲取code
2)調(diào)用getUserInfo方法獲取用戶的加密數(shù)據(jù)
3)調(diào)用后端的用戶授權(quán)接口將用戶信息保存到服務(wù)端
4)保存后端接口返回的uid和unionid到localstorage中,作為全局參數(shù)

獲取用戶的授權(quán)信息

getUid:function(cf){
  var that = this
  wx.login({
   success: function (ress) {
    var code = ress.code 
    wx.getUserInfo({ 
     withCredentials: true,     
     success: function (res) {
      that.globalData.userInfo = res.userInfo;
      that.authorize(code, res.signature, res.iv, res.rawData, res.encryptedData, cf)
     }
    })
   }
  })
 },
 authorize: function (code, signature, iv, rawData, encryptedData, cf) {
  var that =this
  var dataobj = {
   code: code,
   signature: signature,
   iv: iv,
   raw_data: rawData,
   encrypted_data: encryptedData
  }
  console.log("code:",code)
  var param = JSON.stringify(dataobj)
  param = that.Encrypt(param)
  var url = that.data.API_DOMAIN2 + "/user/authorization?param=" + param
  wx.request({
   url: url,
   method: "GET",
   header: {
    'content-type': 'application/json'
   },
   success: function (res) {
    if (res.data.errcode == 200) {
     wx.hideToast()    
     wx.setStorage({
      key: "uid",
      data: res.data.data.uid,
      success: function () {
       if (cf) {
        typeof cf == "function"  cf(res.data.data.uid)
       }
      }
     })
    } else {
     that.exceptionHandle('uid', url, res.data.errcode, res.data.msg)
    }
   }
  })
 },

服務(wù)端

入口方法

/**
   * api接口開發(fā)
   * 獲取詳情的接口
   * @param $uid 用戶編號
   * @param $iv 向量
   * @param $encryptedData 微信加密的數(shù)據(jù)
   * @param $rawData 判斷是否為今天
   * @param $signature 簽名
   * @return array
   */
  public static function authorization($appid,$appsecret,$code,$iv,$encryptedData,$rawData,$signature){
    $result = self::decodeWxData($appid,$appsecret,$code,$iv,$encryptedData);
    if($result['errcode'] != 200){
      return $result;
    }
    //處理微信授權(quán)的邏輯
    $wxUserData = $result['data'];
    error_log("authorization data=============>");
    error_log(json_encode($wxUserData));
    $uid = WxaUserService::regWxaUser($wxUserData);
    $data['uid'] = $uid['uid'];
    $data['unionid'] = $uid['unionid'];
    $result['data'] = $data;
    return $result;
  }
  
  /**
   * 解密微信的數(shù)據(jù)
   * @param $code wx.login接口返回的code
   * @param $iv wx.getUserInfo接口或者wx.getWeRunData返回的iv
   * @param $encryptedData wx.getUserInfo接口或者wx.getWeRunData返回的加密數(shù)據(jù)
   * @return array
   */
  public static function decodeWxData($appid,$appsecret,$code,$iv,$encryptedData){
    $sessionKeyUrl = sprintf('%s?appid=%ssecret=%sjs_code=%sgrant_type=authorization_code',config('param.wxa_user_info_session_key_url'),$appid,$appsecret,$code);
    $rtnJson = curlRequest($sessionKeyUrl);
    $data = json_decode($rtnJson,true);
    error_log('authorization wx return data========>');
    error_log($rtnJson);
    if(isset($data['errcode'])){
      return $data;
    }
    $sessionKey = $data['session_key'];
    $wxHelper = new WxBizDataHelper($appid,$sessionKey,$encryptedData,$iv);
    $data['errcode'] = 200;
    $data['data'] = [];
    if(!$wxData = $wxHelper->getData()){
      $data['errcode'] = -1;
    }else{
      error_log('current wx return data is =========>'.json_encode($wxData));
      $data['data'] = $wxData;
    }
    return $data;
  }

保存用戶信息的方法

 /**
   * 保存用戶信息的方法
   * @param $wxaUserData
   * @param $regFromGh 表示是否從公眾號進(jìn)行注冊
   */
  public function regWxaUser($wxaUserData,$regFromGh = false)
  {
    $value = $wxaUserData['unionId'];
    $key = getCacheKey('redis_key.cache_key.zset_list.lock') . $value;
    $newExpire = RedisHelper::getLock($key);
    $data = $this->storeWxaUser($wxaUserData,$regFromGh);
    RedisHelper::releaseLock($key, $newExpire);
    return $data;
  }
  
  /**
   * 保存信息
   * @param $wxaUserData
   * @return mixed
   */
  public function storeWxaUser($wxaUserData,$regFromGh = false)
  {
    $wxUnionId = $wxaUserData['unionId'];
    if (!$user = $this->getByWxUnionId($wxUnionId)) {
      $getAccountDataStartTime = time();
      //這里是因?yàn)樾枰y(tǒng)一賬戶獲取uid,所以這個(gè)是用戶中心的接口,如果沒有這個(gè)流程,則直接使用數(shù)據(jù)
      if($accountData = AccountCenterHelper::regWxaUser($wxaUserData)){
        $getAccountDataEndTime = time();
        $accountRegTime = $getAccountDataEndTime - $getAccountDataStartTime;
        error_log("reg user spend time is ===================>" . $accountRegTime);
        $user = [
          'uid' => $accountData['uid'],
          'user_name' => $accountData['user_name'],
          'nick_name' => $wxaUserData['nickName'],
          'sex' => $accountData['sex'],
          'wx_union_id' => $accountData['wx_union_id'],
          'avatar' => isset($accountData['avatar'])?$accountData['avatar']:"",
          'from_appid' => $accountData['from_appid'],
          'province' => $wxaUserData['province'],
          'city' => $wxaUserData['city'],
          'country' => $wxaUserData['country'],
          'openid' => $wxaUserData['openId'],
          'wx_header' => isset($wxaUserData['avatarUrl'])?$wxaUserData['avatarUrl']:"",
          'gh_openid' => $regFromGh?$wxaUserData['openId']:"",
        ];
        error_log("insert data=============>" . json_encode($user));
        $user = $this->store($user);
        $regApiUserEndTime = time();
        error_log(" reg api user spend time================>" . ($regApiUserEndTime - $getAccountDataEndTime));
        error_log(" after insert data=============>" . json_encode($user));
      }
    }else{
      if(!$user['wx_header']){
        $updateData = [
          'id' => $user['id'],
          'uid' => $user['uid'],
          'wx_header' => $wxaUserData['avatarUrl'],
        ];
        $this->update($updateData);
      }
      //同步用戶的openid
      if($wxaUserData['openId'] != $user['openid']){
        $updateData = [
          'id' => $user['id'],
          'uid' => $user['uid'],
          'openid' => $wxaUserData['openId'],
        ];
        $this->update($updateData);
      }
    }
    $data['uid'] = $user['uid'];
    $data['unionid'] = $wxUnionId;
    return $data;
  }

根據(jù)unionid獲取用戶信息

  /**
   * 根據(jù)unionid獲取用戶信息
   */
  public function getByWxUnionId($unionId)
  {
    $cacheKey = getCacheKey('redis_key.cache_key.wxa_user.info') . $unionId;
    $value = $this->remember($cacheKey, function () use ($unionId) {
      $userInfo = WxaUser::where('wx_union_id', $unionId)->first();
      $userInfo = $this->compactUserInfo($userInfo);
      return $userInfo;
    });
    return $value;
  }

WxBizDataHelper工具類

?php
/**
 * Created by PhpStorm.
 * User: Auser
 * Time: 11:17
 */

namespace App\Http\Base\Wx;


class WxBizDataHelper
{

  private $appid;
  private $seesionKey ;
  private $encryptedData;
  private $iv;
  public function __construct($appid, $sessionKey,$encryptedData, $iv)
  {
    $this->appid = $appid;
    $this->seesionKey = $sessionKey;
    $this->encryptedData = $encryptedData;
    $this->iv = $iv;
  }

  public function getData(){
    $pc = new WXBizDataCrypt($this->appid, $this->seesionKey);
    $json = '';
    $errCode = $pc->decryptData($this->encryptedData, $this->iv, $json);
    $data = [];
    if ($errCode == 0) {
      $data = json_decode($json,true);
    }
    return $data;
  }


}

WXBizDataCrypt工具類

?php
/**
 * Created by PhpStorm.
 * User: Auser
 * Time: 10:38
 */

namespace App\Http\Base\Wx;

use App\Http\Base\Wx\Prpcrypt;
use App\Http\Base\Wx\ErrorCode;
use App\Http\Base\Wx\PKCS7Encoder;
class WXBizDataCrypt
{

  private $appid;
  private $sessionKey;

  /**
   * 構(gòu)造函數(shù)
   * @param $sessionKey string 用戶在小程序登錄后獲取的會話密鑰
   * @param $appid string 小程序的appid
   */
  public function __construct( $appid, $sessionKey)
  {
    $this->sessionKey = $sessionKey;
    $this->appid = $appid;
  }


  /**
   * 檢驗(yàn)數(shù)據(jù)的真實(shí)性,并且獲取解密后的明文.
   * @param $encryptedData string 加密的用戶數(shù)據(jù)
   * @param $iv string 與用戶數(shù)據(jù)一同返回的初始向量
   * @param $data string 解密后的原文
   *
   * @return int 成功0,失敗返回對應(yīng)的錯(cuò)誤碼
   */
  public function decryptData( $encryptedData, $iv, $data )
  {
    if (strlen($this->sessionKey) != 24) {
      return ErrorCode::$IllegalAesKey;
    }
    $aesKey=base64_decode($this->sessionKey);


    if (strlen($iv) != 24) {
      return ErrorCode::$IllegalIv;
    }
    $aesIV=base64_decode($iv);

    $aesCipher=base64_decode($encryptedData);

    $pc = new Prpcrypt($aesKey);
    $result = $pc->decrypt($aesCipher,$aesIV);

    if ($result[0] != 0) {
      return $result[0];
    }

    $dataObj=json_decode( $result[1] );
    if( $dataObj == NULL )
    {
      return ErrorCode::$IllegalBuffer;
    }
    if( $dataObj->watermark->appid != $this->appid )
    {
      return ErrorCode::$IllegalBuffer;
    }
    $data = $result[1];
    return ErrorCode::$OK;
  }

}

Prpcrypt工具類

?php
/**
 * Created by PhpStorm.
 * User: Auser
 * Time: 10:55
 */

namespace App\Http\Base\Wx;

class Prpcrypt
{
  public $key;

  public function __construct($key)
  {
    $this->key = $key;
  }

  /**
   * 對密文進(jìn)行解密
   * @param string $aesCipher 需要解密的密文
   * @param string $aesIV 解密的初始向量
   * @return string 解密得到的明文
   */
  public function decrypt($aesCipher, $aesIV)
  {

    try {
      $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
      mcrypt_generic_init($module, $this->key, $aesIV);
      //解密
      $decrypted = mdecrypt_generic($module, $aesCipher);
      mcrypt_generic_deinit($module);
      mcrypt_module_close($module);
    } catch (Exception $e) {
      return array(ErrorCode::$IllegalBuffer, null);
    }


    try {
      $result = PKCS7Encoder2::decode($decrypted);
    } catch (Exception $e) {
      //print $e;
      return array(ErrorCode::$IllegalBuffer, null);
    }
    return array(0, $result);
  }
}

ErrorCode狀態(tài)代碼類

?php
/**
 * Created by PhpStorm.
 * User: Auser
 * Time: 10:33
 */

namespace App\Http\Base\Wx;


class ErrorCode
{
  public static $OK = 0;
  public static $IllegalAesKey = -41001;
  public static $IllegalIv = -41002;
  public static $IllegalBuffer = -41003;
  public static $DecodeBase64Error = -41004;

}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 微信小程序開發(fā)之獲取用戶手機(jī)號碼(php接口解密)
  • 微信小程序發(fā)送訂閱消息的方法(php 為例)
  • 基于PHP實(shí)現(xiàn)微信小程序客服消息功能
  • 微信小程序?qū)W習(xí)筆記之表單提交與PHP后臺數(shù)據(jù)交互處理圖文詳解
  • PHP小程序支付功能完整版【基于thinkPHP】
  • PHP后臺實(shí)現(xiàn)微信小程序登錄
  • 微信小程序調(diào)用PHP后臺接口 解析純html文本
  • 微信小程序圖片選擇、上傳到服務(wù)器、預(yù)覽(PHP)實(shí)現(xiàn)實(shí)例
  • 微信小程序 PHP后端form表單提交實(shí)例詳解
  • PHP:微信小程序 微信支付服務(wù)端集成實(shí)例詳解及源碼下載
  • PHP小程序后臺部署運(yùn)行 LNMP+WNMP的方法

標(biāo)簽:荊門 徐州 遵義 吐魯番 常州 寧夏 遂寧 武威

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP實(shí)現(xiàn)微信小程序用戶授權(quán)的工具類示例》,本文關(guān)鍵詞  PHP,實(shí)現(xiàn),微信,小,程序,用戶,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PHP實(shí)現(xiàn)微信小程序用戶授權(quán)的工具類示例》相關(guān)的同類信息!
  • 本頁收集關(guān)于PHP實(shí)現(xiàn)微信小程序用戶授權(quán)的工具類示例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    渭南市| 吉木乃县| 乌审旗| 温泉县| 宜兴市| 汪清县| 读书| 贞丰县| 汉川市| 繁峙县| 永城市| 黄骅市| 龙胜| 来安县| 宜兰县| 凤山市| 石景山区| 砚山县| 临猗县| 浠水县| 吴旗县| 五华县| 云浮市| 神农架林区| 鄂尔多斯市| 新蔡县| 万全县| 静宁县| 共和县| 措美县| 临桂县| 通化市| 延川县| 张家港市| 宜兰县| 荆州市| 丽江市| 化德县| 鞍山市| 陆河县| 无锡市|