您的位置:首页 > 移动开发 > 微信开发

微信小程序的登录流程

2020-08-11 23:36 435 查看

在小程序中

  1. 在wxml文件
    通过button标签
<button open-type="getUserInfo" bindgetuserinfo="processLogin">允许</button>
  1. 在js文件
//引入auth.js
const AUTH = require('../../utils/auth')
processLogin(e) {
// console.log('我的页面获取用户信息:',e.detail.userInfo)
//this为该页面
//调用auth.js文件中的方法
AUTH.register(this);
},
  1. 在auth.js文件里
//引入接口
const {login_wx,getwxre,checkToken } =require('../http/wxw/api')
// 详细注册
//使用async 函数 是因为async后必定跟一个promise函数
//使用时可以使用.then()的方法
async function register(page) {
//变量声明this解决this指向问题
let _this = this;
wx.login({
success: function (res) {
// 获取到code值
let code = res.code; // 微信登录接口返回的 code 参数,下面注册接口需要用到
wx.getUserInfo({
success: function (res) {
//获取用户信息
console.log('utils文件夹auth文件用户信息再次获取',res)

let iv = res.iv;
let encryptedData = res.encryptedData;

// 下面开始调用注册接口使用的是分装好的接口
//接口问题在上一个文章中提到
getwxre({
code: code,
encryptedData: encryptedData,
iv: iv,

}).then(function (res) {
//调用该页面的login方法
_this.login(page);
})
}
})
}
})
}
// 登录
async function login(page){
const _this = this
wx.login({
success: function (res) {
//下面是登录的接口需要wx.login的方法传入code值
login_wx(res.code).then(function (res) {
//本地保存token信息和uid信息
wx.setStorageSync('token', res.data.token)
wx.setStorageSync('uid', res.data.uid)
if ( page ) {
//使页面刷新
page.onShow()
}
})
}
})
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: