ASEMiniProgram/pages/index/index.js

241 lines
6.5 KiB
JavaScript
Raw Normal View History

2021-03-02 07:14:49 +00:00
// index.js
import {getDocument} from '../../api/document'
2021-03-20 17:48:56 +00:00
import {login, checkProfile, setProfile, getProfile} from '../../api/user'
2021-03-02 07:14:49 +00:00
// 获取应用实例
const app = getApp()
Page({
data: {
motto: 'Hello World',
userInfo: {},
userDocument: {},
2021-03-02 07:14:49 +00:00
hasUserInfo: false,
2021-03-20 17:48:56 +00:00
canIUse: true,
loading: true,
unrecoverable: false,
refresh: false,
unrecoverableInfo: '',
2021-03-02 07:14:49 +00:00
},
2021-03-20 17:48:56 +00:00
handleLogin() {
return new Promise((resolve, reject) => {
wx.login({
success: (res) => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
login(app.globalData.appID, res.code).then(res => {
console.log(res)
if(res.userBanned) {
return Promise.reject("用户被封禁")
} else {
app.globalData.login = true
// 设置登录时间
app.globalData.loginTime = Date.now()
// 设置登录信息
const userBaseInfo = {}
userBaseInfo.openid = res.openid
userBaseInfo.postmark = res.postmark
userBaseInfo.token = res.token
app.globalData.userBaseInfo = userBaseInfo
return Promise.resolve(userBaseInfo)
}
}).catch(err => {
if(err === "用户被封禁") {
this.setData({
loading: false,
unrecoverable: true
})
}
}).then((res) => {
// 获得并设置用户个人账号信息
return this.handleUserInfo()
})
}
})
2021-03-02 07:14:49 +00:00
})
},
2021-03-20 17:48:56 +00:00
handleUserInfo() {
let that = this
return new Promise((resolve, reject) => {
resolve(checkProfile())
}).then((uploaded) => {
if(!uploaded) {
console.log(app.globalData.userInfo)
if(app.globalData.userInfo) {
// 上传账户基本信息到服务器
return setProfile(app.globalData.userInfo).then((res) => {
that.data.hasUserInfo = true
return Promise.resolve(res)
}).catch(err => {
return Promise.reject(err)
})
} else {
// 获取账户基本信息
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
new Promise((resolve, reject) => {
// 可以将 res 发送给后台解码出 unionId
const userInfo = res.userInfo
resolve(userInfo)
}).then((res) => {
// 上传账户基本信息到服务器
return setProfile(res).then((res) => {
return Promise.resolve(res)
}).catch(err => {
console.log(err)
})
}).then(() => {
that.data.hasUserInfo = true
that.onLoad()
})
}
})
} else {
wx.navigateTo({
url: '../get-user-info/get-user-info',
events: {
// 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
acceptDataFromOpenedPage: function(data) {
console.log(data)
// 是否在跳转回来时刷新该页面
if(data === "refresh") {
this.setData({
refresh: true
})
}
},
someEvent: function(data) {
console.log(data)
}
},
})
}
}
})
}
2021-03-20 17:48:56 +00:00
} else {
return new Promise((resolve, reject) => {
return getProfile().then(res => {
// 设置账户基本信息
app.globalData.userInfo = res
that.data.hasUserInfo = true
resolve(res)
})
})
}
}).then(userInfo => {
console.log(userInfo)
// 检查账户基本信息是否设置到位
if(!this.data.hasUserInfo) {
return Promise.reject(this.data.hasUserInfo)
}
app.globalData.userInfo = userInfo
2021-03-02 07:14:49 +00:00
this.setData({
2021-03-20 17:48:56 +00:00
userInfo,
2021-03-02 07:14:49 +00:00
hasUserInfo: true
})
2021-03-20 17:48:56 +00:00
return Promise.resolve(this.data.hasUserInfo)
2021-03-20 17:48:56 +00:00
}).then((res) => {
// 获取与用户关联的档案
return getDocument(app.globalData.userBaseInfo.openid).then(res => {
console.log(res)
if(res === null) {
wx.navigateTo({
url: '../bind-document/bind-document'
2021-03-02 07:14:49 +00:00
})
} else {
app.globalData.userDocument = res
this.setData({
userDocument: app.globalData.userDocument
})
2021-03-02 07:14:49 +00:00
}
})
2021-03-20 17:48:56 +00:00
}).catch(err => {
console.log("账户基本信息未设置")
}).finally(() => {
this.setData({
loading: false
})
})
},
// 事件处理函数
bindViewTap() {
wx.navigateTo({
url: '../logs/logs'
})
},
onLoad() {
new Promise((resolve, reject) => {
return this.handleLogin()
});
2021-03-02 07:14:49 +00:00
},
2021-03-20 17:48:56 +00:00
onShow() {
if(this.data.refresh)
this.onLoad()
},
getLoginStatus(e) {
if(e.userBanned === true) {
this.setData({
loading: false,
unrecoverable: true
})
}
},
2021-03-02 07:14:49 +00:00
getUserInfo(e) {
console.log(e)
app.globalData.userInfo = e.detail.userInfo
this.setData({
userInfo: e.detail.userInfo,
hasUserInfo: true
})
},
bindAccountDocInfo() {
// 跳转
wx.navigateTo({
url: '/pages/account-doc-info/account-doc-info'
})
},
bindAccountRelationInfo() {
// 跳转
wx.navigateTo({
url: '/pages/account-relation-info/account-relation-info'
})
},
bindMyGrade() {
// 跳转
wx.navigateTo({
url: '/pages/my-grade/my-grade'
})
},
bindChildSupervisor() {
// 跳转
wx.navigateTo({
url: '/pages/child-supervisor/child-supervisor'
})
},
bindAnnouncement() {
// 跳转
wx.navigateTo({
url: '/pages/announcement/announcement'
})
},
bindMessage() {
// 跳转
wx.navigateTo({
url: '/pages/message/message'
})
},
2021-03-02 07:14:49 +00:00
})