187 lines
4.7 KiB
JavaScript
187 lines
4.7 KiB
JavaScript
// pages/login.js
|
||
const app = getApp();
|
||
const config = app.loadConfig()
|
||
const regeneratorRuntime = app.loadAsync()
|
||
const model = app.loadModel("index")
|
||
const http = app.loadHttp()
|
||
const { StorageSync } = app.loadModel("getSetting")
|
||
const storage = new StorageSync()
|
||
Page({
|
||
|
||
/**
|
||
* 页面的初始数据
|
||
*/
|
||
data: {
|
||
system: app.globalData.system, //设备相关信息
|
||
pageTitle: "登录", //头部标题
|
||
isBack: true,//显示返回
|
||
canIUse: wx.canIUse('button.open-type.getUserInfo'),
|
||
isLogin: false,
|
||
closbtn: '取消登陆'
|
||
},
|
||
onAuth() {
|
||
return new Promise(async (resolve,reject) => {
|
||
wx.showLoading({
|
||
title: '登录中',
|
||
})
|
||
let that = this
|
||
let inviteId = await storage.getSetting('inviteId')
|
||
// 一键预约页面保存的from在登陆时使用
|
||
let from = await storage.getSetting('from')
|
||
wx.login({
|
||
success(rel) {
|
||
if (rel.code) {
|
||
wx.getSetting({
|
||
success: (res) => {
|
||
if (res.authSetting['scope.userInfo']) {
|
||
wx.getUserInfo({
|
||
success: async (res) => {
|
||
let userInfo = {
|
||
nickname: res.userInfo.nickName,
|
||
avatar: res.userInfo.avatarUrl,
|
||
encryptedData: res.encryptedData,
|
||
iv: res.iv
|
||
}
|
||
if (!!inviteId) {
|
||
userInfo.scene = inviteId
|
||
}
|
||
if (!!from) {
|
||
userInfo.from = from
|
||
}
|
||
wx.request({
|
||
url: `${config.baseUrl}/login`, method: "POST", data: { code: rel.code, ...userInfo },
|
||
success(rel) {
|
||
storage.setStorage("token", rel.data.data.token)
|
||
that.setData({
|
||
isLogin: true,
|
||
closbtn: '返 回'
|
||
})
|
||
wx.hideLoading()
|
||
resolve()
|
||
}
|
||
})
|
||
}
|
||
})
|
||
} else {
|
||
wx.hideLoading()
|
||
wx.showToast({
|
||
title: '取消授权',
|
||
icon: 'none',
|
||
duration: 2000
|
||
})
|
||
}
|
||
}, fail() {
|
||
wx.hideLoading()
|
||
}
|
||
})
|
||
} else {
|
||
wx.hideLoading()
|
||
console.log('登录失败!' + res.errMsg)
|
||
}
|
||
}
|
||
})
|
||
})
|
||
},
|
||
// 手机回调
|
||
// 如果已经授权个人信息未授权手机号,那么办卡时将跳到登陆,授权手机号
|
||
async telNum(e) {
|
||
if (!!e.detail.iv) {
|
||
let res = await http.http.request2({ url: `${http.api.getTel}`, method: "POST", data: { encryptedData: e.detail.encryptedData, iv: e.detail.iv } })
|
||
if (res.data.status == 1) {
|
||
if (res.data.data == 1) {
|
||
await this.onAuth()
|
||
await this.personalInfo()
|
||
} else {
|
||
this.personalInfo()
|
||
}
|
||
}
|
||
}
|
||
},
|
||
onTap() {
|
||
wx.navigateBack({
|
||
delta: 1
|
||
})
|
||
},
|
||
// 获取个人信息
|
||
async personalInfo() {
|
||
let user = await http.http.request2({ url: `${http.api.personalInfo}`, method: "POST" })
|
||
app.globalData.userInfo = user.data.data
|
||
setTimeout(() => {
|
||
wx.navigateBack({
|
||
delta: 1
|
||
})
|
||
}, 1000)
|
||
},
|
||
// 判断本地是否有token ,如果没有token显示授权登陆
|
||
async isLogin() {
|
||
let that = this
|
||
let token = await storage.getSetting('token')
|
||
if (!!token) {
|
||
wx.getSetting({
|
||
success(res) {
|
||
if (res.authSetting['scope.userInfo']) {
|
||
that.setData({
|
||
isLogin: true,
|
||
closbtn: '返 回'
|
||
})
|
||
}
|
||
}
|
||
})
|
||
}
|
||
},
|
||
/**
|
||
* 生命周期函数--监听页面加载
|
||
*/
|
||
onLoad: function (options) {
|
||
this.isLogin()
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面初次渲染完成
|
||
*/
|
||
onReady: function () {
|
||
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面显示
|
||
*/
|
||
onShow: function () {
|
||
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面隐藏
|
||
*/
|
||
onHide: function () {
|
||
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面卸载
|
||
*/
|
||
onUnload: function () {
|
||
|
||
},
|
||
|
||
/**
|
||
* 页面相关事件处理函数--监听用户下拉动作
|
||
*/
|
||
onPullDownRefresh: function () {
|
||
|
||
},
|
||
|
||
/**
|
||
* 页面上拉触底事件的处理函数
|
||
*/
|
||
onReachBottom: function () {
|
||
|
||
},
|
||
|
||
/**
|
||
* 用户点击右上角分享
|
||
*/
|
||
onShareAppMessage: function () {
|
||
|
||
}
|
||
}) |