|
|
@@ -103,82 +103,62 @@ export default {
|
|
|
this.loading = true
|
|
|
var self = this
|
|
|
|
|
|
- uni.login({
|
|
|
- provider: 'weixin',
|
|
|
- success: function(loginRes) {
|
|
|
- wechatLogin(loginRes.code, {
|
|
|
- nickname: self.nickname || '用户',
|
|
|
- avatar: self.avatar || ''
|
|
|
- }).then(function(res) {
|
|
|
- // 保存 token 和用户信息
|
|
|
- uni.setStorageSync('token', res.data.token)
|
|
|
- uni.setStorageSync('userId', res.data.userId)
|
|
|
- uni.setStorageSync('role', res.data.role)
|
|
|
- uni.setStorageSync('familyId', res.data.familyId)
|
|
|
- uni.setStorageSync('currentRole', res.data.role)
|
|
|
- uni.setStorageSync('isSwitchedChild', false)
|
|
|
- uni.setStorageSync('isSwitchedTeacher', false)
|
|
|
- // 保存 openid,用于后续自动登录
|
|
|
- if (res.data.openid) {
|
|
|
- uni.setStorageSync('openid', res.data.openid)
|
|
|
- }
|
|
|
- uni.setStorageSync('userInfo', {
|
|
|
- nickname: res.data.nickname,
|
|
|
- userId: res.data.userId
|
|
|
- })
|
|
|
-
|
|
|
- // 存储成长规划师审核状态
|
|
|
- if (res.data.teacherStatus) {
|
|
|
- uni.setStorageSync('teacherStatus', res.data.teacherStatus)
|
|
|
- }
|
|
|
- if (res.data.teacherRejectReason) {
|
|
|
- uni.setStorageSync('teacherRejectReason', res.data.teacherRejectReason)
|
|
|
- }
|
|
|
-
|
|
|
- // 保存 token 和用户信息(同步写入,立即持久化)
|
|
|
- uni.setStorageSync('token', res.data.token)
|
|
|
- uni.setStorageSync('userId', res.data.userId)
|
|
|
- uni.setStorageSync('role', res.data.role)
|
|
|
- uni.setStorageSync('familyId', res.data.familyId)
|
|
|
- uni.setStorageSync('currentRole', res.data.role)
|
|
|
- uni.setStorageSync('isSwitchedChild', false)
|
|
|
- uni.setStorageSync('isSwitchedTeacher', false)
|
|
|
- // 保存 openid,用于后续自动登录
|
|
|
- if (res.data.openid) {
|
|
|
- uni.setStorageSync('openid', res.data.openid)
|
|
|
- }
|
|
|
- uni.setStorageSync('userInfo', {
|
|
|
- nickname: res.data.nickname,
|
|
|
- userId: res.data.userId
|
|
|
- })
|
|
|
-
|
|
|
- // 存储成长规划师审核状态
|
|
|
- if (res.data.teacherStatus) {
|
|
|
- uni.setStorageSync('teacherStatus', res.data.teacherStatus)
|
|
|
- }
|
|
|
- if (res.data.teacherRejectReason) {
|
|
|
- uni.setStorageSync('teacherRejectReason', res.data.teacherRejectReason)
|
|
|
- }
|
|
|
-
|
|
|
- uni.showToast({ title: '登录成功', icon: 'success' })
|
|
|
+ // 超时保护:10秒强制结束登录流程,避免 uni.login 挂死时按钮 loading 卡死
|
|
|
+ var loginTimeout = new Promise(function(_, reject) {
|
|
|
+ setTimeout(function() {
|
|
|
+ reject(new Error('微信登录超时'))
|
|
|
+ }, 10000)
|
|
|
+ })
|
|
|
|
|
|
- // 立即跳转到对应页面(不等 setTimeout,直接在 Promise 链内)
|
|
|
- if (res.data.needsRoleSelect) {
|
|
|
- uni.redirectTo({
|
|
|
- url: '/pages/user-edit/user-edit?token=' + res.data.token + '&userId=' + res.data.userId + '&needsRoleSelect=true'
|
|
|
- })
|
|
|
- } else {
|
|
|
- self.handlePostLoginRouting()
|
|
|
- }
|
|
|
- }).catch(function(e) {
|
|
|
- uni.showToast({ title: e.message || '登录失败', icon: 'none' })
|
|
|
- self.loading = false
|
|
|
+ Promise.race([loginTimeout, new Promise(function(resolve, reject) {
|
|
|
+ uni.login({
|
|
|
+ provider: 'weixin',
|
|
|
+ success: function(loginRes) { resolve(loginRes) },
|
|
|
+ fail: function() { reject(new Error('微信登录失败')) }
|
|
|
+ })
|
|
|
+ })]).then(function(loginRes) {
|
|
|
+ wechatLogin(loginRes.code, {
|
|
|
+ nickname: self.nickname || '用户',
|
|
|
+ avatar: self.avatar || ''
|
|
|
+ }).then(function(res) {
|
|
|
+ // 保存 token 和用户信息(只写一次)
|
|
|
+ uni.setStorageSync('token', res.data.token)
|
|
|
+ uni.setStorageSync('userId', res.data.userId)
|
|
|
+ uni.setStorageSync('role', res.data.role)
|
|
|
+ uni.setStorageSync('familyId', res.data.familyId)
|
|
|
+ uni.setStorageSync('currentRole', res.data.role)
|
|
|
+ uni.setStorageSync('isSwitchedChild', false)
|
|
|
+ uni.setStorageSync('isSwitchedTeacher', false)
|
|
|
+ if (res.data.openid) {
|
|
|
+ uni.setStorageSync('openid', res.data.openid)
|
|
|
+ }
|
|
|
+ uni.setStorageSync('userInfo', {
|
|
|
+ nickname: res.data.nickname,
|
|
|
+ userId: res.data.userId
|
|
|
})
|
|
|
- },
|
|
|
- fail: function() {
|
|
|
- uni.showToast({ title: '微信登录失败', icon: 'none' })
|
|
|
+ if (res.data.teacherStatus) {
|
|
|
+ uni.setStorageSync('teacherStatus', res.data.teacherStatus)
|
|
|
+ }
|
|
|
+ if (res.data.teacherRejectReason) {
|
|
|
+ uni.setStorageSync('teacherRejectReason', res.data.teacherRejectReason)
|
|
|
+ }
|
|
|
+
|
|
|
+ uni.showToast({ title: '登录成功', icon: 'success' })
|
|
|
+
|
|
|
+ if (res.data.needsRoleSelect) {
|
|
|
+ uni.redirectTo({
|
|
|
+ url: '/pages/user-edit/user-edit?token=' + res.data.token + '&userId=' + res.data.userId + '&needsRoleSelect=true'
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ self.handlePostLoginRouting()
|
|
|
+ }
|
|
|
+ }).catch(function(e) {
|
|
|
+ uni.showToast({ title: e.message || '登录失败', icon: 'none' })
|
|
|
self.loading = false
|
|
|
- }
|
|
|
+ })
|
|
|
+ }).catch(function(e) {
|
|
|
+ uni.showToast({ title: e.message || '微信登录失败', icon: 'none' })
|
|
|
+ self.loading = false
|
|
|
})
|
|
|
},
|
|
|
handlePostLoginRouting() {
|
|
|
@@ -250,9 +230,10 @@ export default {
|
|
|
if (this.redirectUrl) {
|
|
|
const redirect = this.redirectUrl
|
|
|
this.redirectUrl = ''
|
|
|
- uni.reLaunch({ url: redirect })
|
|
|
+ // 使用 redirectTo 支持非 tabBar 页面,避免 switchTab 无法跳转到 discover 等非 tabBar 页
|
|
|
+ uni.redirectTo({ url: redirect })
|
|
|
} else {
|
|
|
- uni.reLaunch({ url: '/pages/index/index' })
|
|
|
+ uni.switchTab({ url: '/pages/index/index' })
|
|
|
}
|
|
|
}
|
|
|
}
|