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

chore: 登录优化

上级 27ad6c41
...@@ -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-ep/user/getinfo') return httpRequest.get('/api/lms-ep/user/getinfo', params)
} }
// 退出登录 // 退出登录
...@@ -16,7 +16,6 @@ export function logout() { ...@@ -16,7 +16,6 @@ export function logout() {
return httpRequest.get('/api/passport/rest/logout') return httpRequest.get('/api/passport/rest/logout')
} }
// 获取营销数据 // 获取营销数据
export function getShare(data) { export function getShare(data) {
return httpRequest.get(`/api/lms-ep/v3/activities/share/${data.course_id}/${data.chapter_id}`) return httpRequest.get(`/api/lms-ep/v3/activities/share/${data.course_id}/${data.chapter_id}`)
......
...@@ -2,6 +2,7 @@ import * as api from './api/base.js' ...@@ -2,6 +2,7 @@ import * as api from './api/base.js'
App({ App({
store: { store: {
token: '',
pageshow: false, pageshow: false,
user: {} // 用户信息 user: {} // 用户信息
}, },
...@@ -13,6 +14,7 @@ App({ ...@@ -13,6 +14,7 @@ App({
source: 'https://webapp-pub.ezijing.com/weapp/share/my.ttf', source: 'https://webapp-pub.ezijing.com/weapp/share/my.ttf',
scopes: ['webview', 'native'] scopes: ['webview', 'native']
}) })
this.store.token = wx.getStorageSync('token')
}, },
onPageNotFound() { onPageNotFound() {
wx.redirectTo({ url: 'pages/login/index' }) wx.redirectTo({ url: 'pages/login/index' })
...@@ -31,8 +33,8 @@ App({ ...@@ -31,8 +33,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 })
...@@ -44,5 +46,25 @@ App({ ...@@ -44,5 +46,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' })
})
}, },
handleError(response) { handleError(response) {
if (response.msg === '账号不存在') { if (response.msg === '账号不存在') {
......
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 => {
...@@ -100,7 +104,8 @@ Page({ ...@@ -100,7 +104,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' })
})
}, },
handleError(response) { handleError(response) {
if (response.msg === '账号不存在') { if (response.msg === '账号不存在') {
......
...@@ -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) {
// 未登录 // 未登录
wx.redirectTo({ url: '/pages/login/index' }) if (!config.data.check && (res.data.status === 403 || res.data.status === 401)) {
return setTimeout(() => wx.redirectTo({ url: '/pages/login/index' }), 2000)
} }
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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论