// pages/login/login.js const app = getApp(); Page({ /** * 页面的初始数据 */ data: { back: true, code: '', }, login() { var that = this; wx.getUserProfile({ //获取用户信息。页面产生点击事件(例如 button 上 bindtap 的回调中)后才可调用,每次请求都会弹出授权窗口,用户同意后返回 userInfo,用于替换 wx.getUserInfo lang: 'zh_CN', desc: '用于在后台更好的识别您的身份', //声明获取用户个人信息后的用途 success(user) { //接口调用成功的回调函数 const data = { encryptedData: user.encryptedData, iv: user.iv, // signature: user.signature, // rawData: user.rawData, code: that.data.code, }; // console.log(data); app.request({ url: `login`, method: 'POST', data: data, success: async function (res) { // wx.setStorageSync('userInfo', res); app.put('userInfo', res, 86400); const userInfo = app.get('userInfo'); // 如果第一次登录就注册一个客户账号 app.request({ url: `login/addCustomer`, method: 'POST', data: { CustomerId: 0, CustomerGuid: 0, CustomerXcxOpenid: userInfo.openId, CustomerXcxName: userInfo.nickName, CustomerXcxImg: userInfo.avatarUrl, CustomerSex: userInfo.gender, }, success(res) { app.put('Authorization', res.data, 86400); const pages = getCurrentPages(); //获取小程序页面栈 const beforePage = pages[pages.length - 2]; //获取上个页面的实例对象 -3是上上一个页面 that.setData({ back: false, }); wx.navigateBack({ delta: 2, }); beforePage.onLoad(); // console.log(wx.getStorageSync('userInfo')); wx.showToast({ icon: 'success', title: '登录成功', }); }, }); }, }); }, fail() { wx.switchTab({ url: '/pages/home/home', }); }, }); }, /** * 生命周期函数--监听页面加载 */ onLoad() { var that = this; wx.login({ success(res) { that.setData({ code: res.code, }); }, }); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() {}, /** * 生命周期函数--监听页面显示 */ onShow() {}, /** * 生命周期函数--监听页面隐藏 */ onHide() {}, /** * 生命周期函数--监听页面卸载 */ onUnload() { var that = this; //设置缓存允许下次跳转登录页 wx.setStorage({ key: 'isshowLogin', data: false, }); if (that.data.back === true) { wx.reLaunch({ url: '/pages/home/home', }); } }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() {}, /** * 页面上拉触底事件的处理函数 */ onReachBottom() {}, /** * 用户点击右上角分享 */ onShareAppMessage() {}, });