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

chore: 登录优化

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