很详细 获取微信小程序 open_id sessionKey 以及 微信步数

直接上代码:
这里面直接包含从前端到后端获取的
后端类处理请求和解密。
没appid 和 secret 的先去申请

我的框架 yii1.1
php5.6
nginx 忘了 也懒得看
1,index。js

//index.js
//获取应用实例
const app = getApp()
Page({
  data: {

  },
  //登录获取code
  login: function () {
    wx.login({
      success: function (res) {
        console.log(res.code)
      if(res.code) {
        wx.getWeRunData({
          success(ret){
            let encryptedData = ret.encryptedData 
            console.log(ret);
           // console.log("getWeRunData",encryptedData)加密的步数
         wx.request({
          url: 'http://abcd.com/test/weixin', //后端接口地址
          data: { 
            code: res.code,
            encryptedData: encryptedData,
            iv: ret.iv,
           },
          header: {
            'content-type': 'application/json' //默认值
          },
          success: function (res) {
            console.log(res.data)
          }
        })
          }
        })
      } else {
        that.alert("huoqushibaii"+res.errMsg);
      }
      }
    })
  }
})

2,index.wxml


3,//app.js

App({})```

4后端 php

class TestController extends BaseController{

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

//测试微信获取步数
//获取 open_id sessionkey
public function actionWeixin()
{
    //声明CODE,获取小程序传过来的CODE
    $code = $_GET["code"];
    //配置appid
    $appid = 'wxa9asdasdafasf88a07e6a78478e';//我乱写的 用自己之前申请了的

    //配置appscret
    $secret = 'e82f5661asda6dda21a sc5b3534adsc571bf5783f';//我乱写的 用自己之前申请了的

    //api接口
    $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
    //获取GET请求

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
    curl_setopt($curl, CURLOPT_URL, $url);
    $res = curl_exec($curl);
    curl_close($curl);

    $res =  json_decode($res);
    //这里包含了sessionKey 和 openId
    $sessionKey = $res->session_key; 
   // $openId =  $res->open_id; 
    $encryptedData=$_GET["encryptedData"];//加密的步数

    $iv = $_GET["iv"];//解密初始向量

    //$pc = new WXBizDataCrypt($appid, $sessionKey);
    //$errCode = $pc->decryptData($encryptedData, $iv, $data );
    $errCode = $this->_decryptData($sessionKey,$appid,$encryptedData,$iv,$data);
    if ($errCode == 0) {
        print($data . "\n");
    } else {
        print($errCode . "\n");
    }

}


//解密
public function _decryptData($sessionKey,$appid, $encryptedData, $iv, &$data )
{
    if (strlen($sessionKey) != 24) {
        return self::$IllegalAesKey;
    }
    $aesKey=base64_decode($sessionKey);


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

    $aesCipher=base64_decode($encryptedData);

    $result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);

    $dataObj=json_decode( $result );
    if( $dataObj  == NULL )
    {
        return self::$IllegalBuffer;
    }
    if( $dataObj->watermark->appid != $appid )
    {
        return self::$IllegalBuffer;
    }
    $data = $result;
    return self::$OK;
}

}

很详细 获取微信小程序 open_id sessionKey 以及 微信步数_第1张图片

你可能感兴趣的:(其他)