微信小程序app.js调用wx.login

只为梳理一下自己调用微信登陆的思路
先做一个简单的封装,将接口地址存到变量中,方便调用。
在utils中建立一个文件constant.js

var rqcfg = {
  domin: 'https://abc.com',   			 	
  rqurl: 'https://abc.com/applet.php?',  //示例接口
  version:'1.0.0',  
  appid: 'wx11111111111111',
  suid:''
};
module.exports = { 
  rqcfg: rqcfg
}

在app.js中引用该文件

var rqcfg = require('./utils/constant.js');
App({
onLaunch: function () {
		rqcfg.rqcfg.rqurl = rqcfg.rqcfg.rqurl + 'suid=' + suid + '&appid=' + appid;
        this.globalData.requestUrl = rqcfg.rqcfg.rqurl;    //后台接口地址
        this.globalData.appid = appid;
        this.wechatSq();        
  },
  //全局数据
    globalData: {
    requestUrl:'',   
    domin: rqcfg.rqcfg.domin,        //域名地址    
    isnewPlay: false,
    plumSession:''						
  },
   //微信登陆
  wechatSq: function (obj) {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    //调用api
    wx.login({
      success: function (res) {   
        var code = res.code;             //登录凭证(code)
        if (res.code) {              
          var data = {
            map: 'applet_member_info',   //登陆获取用户信息接口
            code: code,
            slient: 1,    //0不是静默方式授权,1是静默方式授权
          }     
          //传递code到服务器端
          wx.request({            //get方式传递数据,将接口地址与要传递的数据写在data中
            url: rqcfg.rqcfg.rqurl,
            data: data,
            success: function (res) {
              if (res.data.ec == 200) {       //调用接口成功
                console.log("获取用户信息成功");
                that.globalData.requestUrl = rqcfg.rqcfg.rqurl + '&plum_session_applet=' + res.data.data.plum_session_applet; 
                
               //plum_session_applet为后台生成的用户id,将用户id拼接到接口后,方便后续使用
               //存入缓存中备用
                 wx.setStorage ({
                  key: "plumSession",
                  data: res.data.data.plum_session_applet,
                  success: function () { 
                    console.log('成功')
                    that.globalData.plumSession = res.data.data.plum_session_applet;    
                  }
                })
                //由于这里是网络请求,可能会在 Page.onLoad 之后才返回,
                //若是idnex.js检测不到that.globalData.plumSession ,
                //会再次调用wx.login接口,浪费资源
            	//所以先检测回调函数是否被创建,若未被创建代表index.js的onLoad还未执行,
            	//直接设置 that.globalData.plumSession ,
            	//若回调函数已经创建,表示idnex的onLoad函数已经执行调用回调函数将plumSession传递
                if (that.plumSessionCallback) {
                  that.plumSessionCallback(res.data.data.plum_session_applet);
                }else{
                  that.globalData.plumSession = res.data.data.plum_session_applet;
                }
                //到此时已经登陆成功了  
              } else {
                console.log(res.data);
                console.log("获取用户信息失败");
              }
            },
            complete: function () {
              console.log("执行完成");
              wx.hideLoading();
            }
          });
        } else {            //若没有获取到登陆code
          wx.showToast({
            title: '获取用户登录态失败!' + res.errMsg,
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '用户code获取失败!' + res.errMsg,
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
})

为了防止app.js中登陆失败,在index.js中再次检测plumSession是否存在

//index.js
   onShow: function () {
    var that = this;
    console.log(app.globalData.plumSession); //wx.login是异步函数、此时可能获取不到值
    if (!app.globalData.plumSession) {    //未检测到  that.globalData.plumSession
      //创建 app.plumSessionCallback 的回调函数,将plumSession作为函数参数,app.js中检测到函数被创建,
      app.plumSessionCallback = plumSession => {         
        if (plumSession!=''){
         console.log('查询到登录信息');
        }else{
          console.log('未查询到登录信息');
          app.wechatSq(that);
        }
      }
  }
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值