小程序开发之登录

小程序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));
        }
        
    }

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值