提交 c505e1a3 authored 作者: 王鹏飞's avatar 王鹏飞

chore: 登录优化

上级 3d64ae30
......@@ -7,8 +7,8 @@ export function getPageShowStatus() {
}
// 获取用户信息
export function getUser() {
return httpRequest.get('/api/lms/user/getinfo')
export function getUser(params) {
return httpRequest.get('/api/lms/user/getinfo', params)
}
// 退出登录
......
......@@ -2,10 +2,13 @@ import * as api from './api/base.js'
App({
store: {
token: '',
pageshow: false,
user: {} // 用户信息
},
onLaunch: function () {},
onLaunch() {
this.store.token = wx.getStorageSync('token')
},
onPageNotFound() {
wx.redirectTo({ url: 'pages/login/index' })
},
......@@ -23,8 +26,8 @@ App({
})
},
// 获取用户信息
getUser() {
return api.getUser().then(response => {
getUser(params) {
return api.getUser(params).then(response => {
this.store.user = response
wx.setStorage({ key: 'uid', data: response.id })
wx.setStorage({ key: 'sid', data: response.student_info.id })
......@@ -36,5 +39,25 @@ App({
return api.logout().then(response => {
wx.removeStorageSync('token')
})
},
setToken(token) {
this.store.token = token
try {
wx.setStorageSync('token', token)
} catch (e) {
wx.showToast({ title: 'Token 存储失败', icon: 'error' })
console.log(e)
}
},
// 检查权限
async checkAccess(token) {
if (token) this.setToken(token)
try {
const user = await this.getUser({ check: true })
return !!user?.id
} catch (error) {
console.log(error)
return false
}
}
})
// pages/learnSystem/home/home.js
const CourseApi = require('../../../apiService/CourseApi.js')
const app = getApp()
Page({
/**
......@@ -53,8 +52,6 @@ Page({
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
// 获取用户信息
app.getUser()
wx.showLoading({ title: '页面加载中...', mask: true })
CourseApi.getlearnFindList((_) => {
this.setData({ 'find[0].arrItem': _ })
......
import * as api from './api.js'
const app = getApp()
Page({
data: {
......@@ -34,8 +35,9 @@ Page({
},
// 登录成功
handleSuccess(data) {
wx.setStorage({ key: 'token', data: data.TGC })
wx.switchTab({ url: '/pages/learnSystem/home/home' })
app.checkAccess(data.TGC).then(isAuth => {
if (isAuth) wx.switchTab({ url: '/pages/learnSystem/home/home' })
})
},
handlePassword() {
wx.navigateTo({ url: '/pages/login/password' })
......
import * as api from '../../api'
const app = getApp()
Component({
/**
* 组件的属性列表
......@@ -42,8 +44,9 @@ Component({
},
// 登录成功
handleSuccess(data) {
wx.setStorage({ key: 'token', data: data.TGC })
wx.switchTab({ url: '/pages/learnSystem/home/home' })
app.checkAccess(data.TGC).then(isAuth => {
if (isAuth) wx.switchTab({ url: '/pages/learnSystem/home/home' })
})
}
}
})
import * as api from '../../api'
const app = getApp()
Component({
/**
* 组件的属性列表
......@@ -43,8 +45,9 @@ Component({
},
// 登录成功
handleSuccess(data) {
wx.setStorage({ key: 'token', data: data.TGC })
wx.switchTab({ url: '/pages/learnSystem/home/home' })
app.checkAccess(data.TGC).then(isAuth => {
if (isAuth) wx.switchTab({ url: '/pages/learnSystem/home/home' })
})
},
// 发送验证码
handleSendCode() {
......
import * as api from './api'
const app = getApp()
Page({
/**
......@@ -13,6 +14,9 @@ Page({
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
app.checkAccess().then(isLogin => {
if (isLogin) wx.switchTab({ url: '/pages/learnSystem/home/home' })
})
// 授权过的自动登录
// wx.getSetting({
// success: res => {
......@@ -63,22 +67,22 @@ Page({
},
// 微信登录
wechatLogin() {
// 获取code
// 获取code
const p1 = this.getCode()
// 获取用户信息
const p2 = this.getUserInfo()
Promise.all([p1, p2]).then(([code, user]) => {
const { encryptedData, iv } = user
api.wechatLogin({ encryptedData, iv, code, identity: 'sofia-mini' }).then(response => {
if (response.code === 0) {
this.handleSuccess(response.data)
} else if (response.code === 2) {
// 绑定手机号
this.setData({ show: true, unionid: response.data.unionid })
} else {
wx.showToast({ title: response.msg, icon: 'none' })
}
})
api.wechatLogin({ encryptedData, iv, code, identity: 'sofia-mini' }).then(response => {
if (response.code === 0) {
this.handleSuccess(response.data)
} else if (response.code === 2) {
// 绑定手机号
this.setData({ show: true, unionid: response.data.unionid })
} else {
wx.showToast({ title: response.msg, icon: 'none' })
}
})
})
},
// 手机号登录
......@@ -91,7 +95,8 @@ Page({
},
// 登录成功
handleSuccess(data) {
wx.setStorage({ key: 'token', data: data.TGC })
wx.switchTab({ url: '/pages/learnSystem/home/home' })
app.checkAccess(data.TGC).then(isAuth => {
if (isAuth) wx.switchTab({ url: '/pages/learnSystem/home/home' })
})
}
})
import * as api from './api.js'
const app = getApp()
Page({
data: {
......@@ -35,8 +36,9 @@ Page({
},
// 登录成功
handleSuccess(data) {
wx.setStorage({ key: 'token', data: data.TGC })
wx.switchTab({ url: '/pages/learnSystem/home/home' })
app.checkAccess(data.TGC).then(isAuth => {
if (isAuth) wx.switchTab({ url: '/pages/learnSystem/home/home' })
})
},
// 发送验证码
handleSendCode() {
......
......@@ -69,7 +69,8 @@ const httpRequest = new Request()
httpRequest.defaults.baseURL = apiBaseURL
httpRequest.defaults.headers.tenant = tenant
httpRequest.beforeRequest = function (config) {
config.headers.token = wx.getStorageSync('token')
const app = getApp()
config.headers.token = app ? app.store.token : wx.getStorageSync('token')
return config
}
// 成功处理
......@@ -82,12 +83,12 @@ httpRequest.defaults.fail = (res, config) => {
wx.showToast({ title: '接口请求失败,请检查网络设置', icon: 'none' })
return res
}
if (res.data.status === 403 || res.data.status === 401) {
// 未登录
wx.redirectTo({ url: '/pages/login/index' })
return
// 未登录
if (!config.data.check && (res.data.status === 403 || res.data.status === 401)) {
setTimeout(() => wx.redirectTo({ url: '/pages/login/index' }), 2000)
}
wx.showToast({ title: res.data.message, icon: 'none' })
return res.data
}
// 导出
module.exports = httpRequest
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论