小程序js:
- //app.js
- App({
- onLaunch: function () {
- //调用API从本地缓存中获取数据
- var logs = wx.getStorageSync('logs') || []
- logs.unshift(Date.now())
- wx.setStorageSync('logs', logs)
- wx.login({
- success: function(res) {
- if (res.code) {
- //发起网络请求
- wx.getUserInfo({
- success: function(ures) {
- var userInfo = ures.userInfo
- var nickName = userInfo.nickName
- var avatarUrl = userInfo.avatarUrl
- var gender = userInfo.gender //性别 0:未知、1:男、2:女
- var province = userInfo.province
- var city = userInfo.city
- var country = userInfo.country
- wx.request({
- url: 'https://m.xxx.com/index.PHP',
- data: {
- m: 'api',
- c: 'wechat' ,
- a: 'xcx_do_auth',
- code: res.code,
- rawData: ures.rawData,
- encryptData: ures.encryptData,
- encryptedData: ures.encryptedData,
- iv: ures.iv,
- signature: ures.signature
- },
- header: {
- "Content-Type": "application/x-www-form-urlencoded"
- },
- success: function(lres) {
- console.log(lres.data)
- }
- })
- //request
- }
- })
- //getUserInfo
- } else {
- console.log('获取用户登录态失败!' + res.errMsg)
- }
- //login
- }
- });
- wx.checkSession({
- success: function(){
- //登录态未过期
- },
- fail: function(){
- //登录态过期
- wx.login()
- }
- });
- },
- globalData:{
- userInfo:null,
- ajaxUrl: "https://m.xxx.com/index.php",
- baseUrl: "https://www.xxx.com/",
- code : null,
- pageNum : 1,
- }
- })
php
- //获取session_key
- function xcx_openid_session_key($code)
- {
- require_once ROOT_PATH.'/weixin/wxHelper.php';
- $wxHelper = new wxHelper();
- //小程序配置参数
- $xcxConfig = include_once ROOT_PATH.'/weixin/xcxConfig.php';
- //crul
- $result = $wxHelper->httpGet("https://api.weixin.qq.com/sns/jscode2session?appid={$xcxConfig['AppId']}&secret={$xcxConfig['AppSecret']}&js_code=$code&grant_type=authorization_code");
- $result = json_decode($result, true);
- if(!empty($result['openid'])){
- $res['AppId'] = $xcxConfig['AppId'];
- $res['AppSecret'] = $xcxConfig['AppSecret'];
- $res['session_key'] = $result['session_key'];
- $res['openid'] = $result['openid'];
- }else{
- $res['errcode'] = 1;
- $res['errmsg'] = 'no openid';
- }
- return $res;
- }
//登录
- public function xcx_do_auth()
- {
- $msg = array();
- $code = $_REQUEST['code'];
- $rawData = $_REQUEST['rawData'];
- $signature = $_REQUEST['signature'];
- $encryptData = $_REQUEST['encryptData'];
- $encryptedData = $_REQUEST['encryptedData'];
- $iv = $_REQUEST['iv'];
- //验证code
- if(empty($code))
- {
- $msg['error'] = '106';
- $msg['msg'] = 'lack of the param code';
- die(json_encode($msg));
- }
- //请求微信服务器,获取openid和session_key
- $result = xcx_openid_session_key($code);
- if(empty($result['errmsg'])){
- $rawData2 = str_replace("\\", "", $rawData);
- $rawData3 = $rawData2.$result['session_key'];
- $signature2 = sha1($rawData3);
- if($signature == $signature2){
- include_once(ROOT_PATH."/weixin/wxBizDataCrypt.php");
- $pc = new WXBizDataCrypt($result['AppId'], $result['session_key']);
- $lastData = $pc->decryptData($encryptedData, $iv, $data );
- if (empty($lastData['errorMsg'])) {
- $lastDataArr = json_decode($lastData, true);
- $lastDataArr2['openid'] = $lastDataArr['openId'];
- $lastDataArr2['nickName'] = $lastDataArr['nickName'];
- $lastDataArr2['sex'] = $lastDataArr['gender'];
- $lastDataArr2['language'] = $lastDataArr['language'];
- $lastDataArr2['city'] = $lastDataArr['city'];
- $lastDataArr2['province'] = $lastDataArr['province'];
- $lastDataArr2['country'] = $lastDataArr['country'];
- $lastDataArr2['headimgurl'] = $lastDataArr['avatarUrl'];
- $lastDataArr2['unionid'] = $lastDataArr['unionId'];
- self::update_xcxweixin_user($lastDataArr2); //更新用户信息
- die($_SESSION['user_id']);
- } else {
- $msg['error'] = '106';
- $msg['msg'] = 'lack of the param unionId';
- die(json_encode($msg, true));
- }
- }else{
- $msg['error'] = '106';
- $msg['msg'] = 'lack of the param signature';
- die(json_encode($msg));
- }
- }else{
- $msg['error'] = '106';
- $msg['msg'] = 'lack of the param openid';
- die(json_encode($msg));
- }
- }