封装好api接口

// 详细注册

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前端接口网页

https://api.it120.cc/doc.html#/%E5%89%8D%E7%AB%AFapi%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F/registerComplexUsingPOST_7

查找需要的参数

​ 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')))
  })
}

}
},

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值