// 详细注册
getwxre:(data)=>{
return request(‘user/wxapp/register/complex’,‘POST’,data,true)
},
// 登录
login_wx: (code) => {
return request(‘user/wxapp/login’,‘POST’, {
code,
type: 2
} ,true)
2.在外部封装一个utils文件里的一个auth.js文件
以及在template login文件夹下的组件
3.在我的页面的js文件中引用login文件夹下的组件
在我的页面的js文件 写入
引入外部的auth.js文件
const AUTH = require(’…/…/utils/auth’)
processLogin(e) {
调用auth.js文件 传入当前页面this
AUTH.register(this);
},
方法
4.在外部的auth.js 中调用
async function register(page) {
let _this = this;
使用wx.login方法
wx.login({
success: function (res) {
// 获取到code值
let code = res.code;
再使用wx.getuserinfo的方法
wx.getUserInfo({
success: function (res) {
获取的是用户的信息
console.log(‘utils文件夹auth文件用户信息再次获取’,res)
// 保存到本地
wx.setStorageSync(“urlinfo”,res.userInfo)
在api前端接口网页
查找需要的参数
let iv = res.iv;
let encryptedData = res.encryptedData;
// 下面开始调用注册接口
getwxre({
code: code,
encryptedData: encryptedData,
iv: iv,
}).then(function (res) {
_this.login(page);
})
}
})
}
})
}
在auth.js 的外部文件中调用登录
async function login(page){
const _this = this
再次用wx.login方法
wx.login({
success: function (res) {
先获取code的值再传入分装好的登录的接口
login_wx(res.code).then(function (res) {
// console.log(‘code3’,res)
判断后台传过来的另一个code值
if (res.code == 10000) {
// 去注册
// _this.register(page)
return;
}
if (res.code != 0) {
console.log('登录错误')
// 登录错误
wx.showModal({
title: '无法登录',
content: res.msg,
showCancel: false
})
return;
}
如果正确将数据保存到本地
wx.setStorageSync('token', res.data.token)
wx.setStorageSync('uid', res.data.uid)
if ( page ) {
page.onShow()
}
})
}
})
}
在我的页面的js文件中
// 退出登录
loginOut(){
删除的同时也会调用页面刷新的生命周期
this.onShow(true)
AUTH.loginOut()
},
执行 外部文件auth.js 的一个退出的方法
// 退出
function loginOut(){
就是删除本地用户信息
wx.removeStorageSync(‘token’)
wx.removeStorageSync(‘uid’)
wx.removeStorageSync(‘urlinfo’)
}
data 中的一个变量当登录时获取本地用户数据
同时将数据渲染到页面
当触发退出的时候
onShow(out){
this.setData({
wxlogin: true
})
// 登录信息删除和添加
if(this.data.wxlogin){
if(out){
this.setData({
apiUserInfoMap:''
})
}else{
this.setData({
apiUserInfoMap:JSON.parse(JSON.stringify(wx.getStorageSync('urlinfo')))
})
}
}
},