当前位置:   article > 正文

微信小程序 账户系统使用unique_id 多平台账号通用_wechat uniqueid

wechat uniqueid

小程序端获取数据:使用session_key 去获取uniqueid。

app.js

  1. login: function () {
  2. var that = this;
  3. // 登录
  4. wx.login({
  5. success: res => {
  6. // 发送 res.code 到后台换取 openId, sessionKey
  7. wx.request({
  8. url: 'https://www******l/api/jscode2session.php',
  9. data: { code: res.code },
  10. method: 'POST',
  11. header: {
  12. 'content-type': 'application/x-www-form-urlencoded'
  13. },
  14. success: function (data) {
  15. that.getUserInfo(data.data.data.user_info.session_key);//解码获取unionID
  16. }
  17. })
  18. }
  19. })
  20. },
  21. getUserInfo:function(sessionKey){
  22. var that = this
  23. wx.getUserInfo({
  24. success: res => {
  25. res.sessionKey = sessionKey
  26. // 可以将 res 发送给后台解码出 unionId
  27. wx.request({
  28. url: 'https://*****/api/aes_data.php',
  29. data: res,
  30. method: 'POST',
  31. header: {
  32. 'content-type': 'application/x-www-form-urlencoded'
  33. },
  34. success: function (data) {
  35. //解码获取unionID
  36. that.globalData.userInfo = data.data
  37. }
  38. })
  39. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  40. // 所以此处加入 callback 以防止这种情况
  41. if (this.userInfoReadyCallback) {
  42. this.userInfoReadyCallback(res)
  43. }
  44. }
  45. })
  46. },

后台:

jscode2session.php   获取session_key

aes_data.php              使用session_key获取unique_id(使用微信提供的加密解密方式)

 

jscode2session.php 

  1. <?php
  2. include_once('config.php');
  3. $code = trim($_POST['code']); //小程序端传来的登陆code
  4. $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_code';
  5. $r = curlN($url);
  6. $r = json_decode($r,true);
  7. $arr = array(
  8. 'ret' => 1,
  9. 'msg' => '操作成功',
  10. 'data' => array(
  11. 'user_info' => $r,
  12. )
  13. );
  14. echo json_encode($arr);
  15. function curlN($url,$data=null)
  16. {
  17. $ch = curl_init();
  18. curl_setopt($ch, CURLOPT_URL, $url);
  19. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  20. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  21. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  22. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  23. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  24. $tmpInfo = curl_exec($ch);
  25. if (curl_errno($ch)) {
  26. return curl_error($ch);
  27. }
  28. curl_close($ch);
  29. return $tmpInfo;
  30. }
  31. ?>

aes_data.php

  1. <?php
  2. include_once('config.php');
  3. include_once('wxBizDataCrypt.php');
  4. $appid = $appid;
  5. $sessionKey = $_POST['sessionKey'];
  6. $encryptedData = $_POST['encryptedData'];
  7. $iv = $_POST['iv'];
  8. $pc = new WXBizDataCrypt($appid, $sessionKey);
  9. $errCode = $pc->decryptData($encryptedData, $iv, $data );
  10. if ($errCode == 0) {
  11. print($data . "\n");
  12. } else {
  13. print($errCode . "\n");
  14. }

wxBizDataCrypt.php

  1. <?php
  2. /**
  3. * 对微信小程序用户加密数据的解密示例代码.
  4. *
  5. * @copyright Copyright (c) 1998-2014 Tencent Inc.
  6. */
  7. include_once "errorCode.php";
  8. class WXBizDataCrypt
  9. {
  10. private $appid;
  11. private $sessionKey;
  12. /**
  13. * 构造函数
  14. * @param $sessionKey string 用户在小程序登录后获取的会话密钥
  15. * @param $appid string 小程序的appid
  16. */
  17. public function __construct( $appid, $sessionKey)
  18. {
  19. $this->sessionKey = $sessionKey;
  20. $this->appid = $appid;
  21. }
  22. /**
  23. * 检验数据的真实性,并且获取解密后的明文.
  24. * @param $encryptedData string 加密的用户数据
  25. * @param $iv string 与用户数据一同返回的初始向量
  26. * @param $data string 解密后的原文
  27. *
  28. * @return int 成功0,失败返回对应的错误码
  29. */
  30. public function decryptData( $encryptedData, $iv, &$data )
  31. {
  32. if (strlen($this->sessionKey) != 24) {
  33. return ErrorCode::$IllegalAesKey;
  34. }
  35. $aesKey=base64_decode($this->sessionKey);
  36. if (strlen($iv) != 24) {
  37. return ErrorCode::$IllegalIv;
  38. }
  39. $aesIV=base64_decode($iv);
  40. $aesCipher=base64_decode($encryptedData);
  41. $result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
  42. $dataObj=json_decode( $result );
  43. if( $dataObj == NULL )
  44. {
  45. return ErrorCode::$IllegalBuffer;
  46. }
  47. if( $dataObj->watermark->appid != $this->appid )
  48. {
  49. return ErrorCode::$IllegalBuffer;
  50. }
  51. $data = $result;
  52. return ErrorCode::$OK;
  53. }
  54. }

errorCode.php

  1. <?php
  2. /**
  3. * error code 说明.
  4. * <ul>
  5. * <li>-41001: encodingAesKey 非法</li>
  6. * <li>-41003: aes 解密失败</li>
  7. * <li>-41004: 解密后得到的buffer非法</li>
  8. * <li>-41005: base64加密失败</li>
  9. * <li>-41016: base64解密失败</li>
  10. * </ul>
  11. */
  12. class ErrorCode
  13. {
  14. public static $OK = 0;
  15. public static $IllegalAesKey = -41001;
  16. public static $IllegalIv = -41002;
  17. public static $IllegalBuffer = -41003;
  18. public static $DecodeBase64Error = -41004;
  19. }
  20. ?>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/525048
推荐阅读
  

闽ICP备14008679号