From 713817d0f18e0dc6a8c9b84546a505d750b4b3f4 Mon Sep 17 00:00:00 2001 From: sfmind Date: Thu, 21 Apr 2022 00:01:24 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=80=E5=87=BA=E7=99=BB=E5=BD=95=E3=80=81?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=AA=E4=BA=BA=E5=A4=B4=E5=83=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yudao-ui-app/common/api.js | 9 +++ yudao-ui-app/pages/profile/profile.vue | 88 ++++++++++++++++++++++++-- yudao-ui-app/pages/user/user.vue | 6 +- yudao-ui-app/store/index.js | 32 ++++++---- 4 files changed, 114 insertions(+), 21 deletions(-) diff --git a/yudao-ui-app/common/api.js b/yudao-ui-app/common/api.js index 34b56b7273..0ba9e579f1 100644 --- a/yudao-ui-app/common/api.js +++ b/yudao-ui-app/common/api.js @@ -3,6 +3,8 @@ const { http } = uni.$u /* login */ //使用手机 + 密码登录 export const passwordLogin = params => http.post('/app-api/member/login', params) +//退出登录 +export const logout = params => http.post('/app-api/member/logout', params) //发送手机验证码 export const sendSmsCode = params => http.post('/app-api/member/send-sms-code', params) //使用手机 + 验证码登录 @@ -10,6 +12,13 @@ export const smsLogin = params => http.post('/app-api/member/sms-login', params) //获取用户信息 export const getUserInfo = params => http.get('/app-api/member/user/get', params) +//修改用户头像 +export const updateAvatar = filePath => + http.upload('/app-api/member/user/update-avatar', { + name: 'avatarFile', + fileType: 'image', + filePath: filePath + }) /* index */ // 获取滚动图数据 diff --git a/yudao-ui-app/pages/profile/profile.vue b/yudao-ui-app/pages/profile/profile.vue index 2fceddcafc..c556b8c4f7 100644 --- a/yudao-ui-app/pages/profile/profile.vue +++ b/yudao-ui-app/pages/profile/profile.vue @@ -1,17 +1,95 @@ - + diff --git a/yudao-ui-app/pages/user/user.vue b/yudao-ui-app/pages/user/user.vue index 91f4328437..20b271d4c5 100644 --- a/yudao-ui-app/pages/user/user.vue +++ b/yudao-ui-app/pages/user/user.vue @@ -71,7 +71,9 @@ export default { ] } }, - onLoad() {}, + onLoad() { + this.$store.dispatch('obtainUserInfo') + }, methods: { loginOrJump(pageUrl) { if (!this.hasLogin) { @@ -87,7 +89,7 @@ export default { success: res => { if (res.confirm) { console.log('用户点击确定') - this.$store.commit('logout') + this.$store.dispatch('logout') } else if (res.cancel) { console.log('用户点击取消') } diff --git a/yudao-ui-app/store/index.js b/yudao-ui-app/store/index.js index ace23c08ea..604de14c15 100644 --- a/yudao-ui-app/store/index.js +++ b/yudao-ui-app/store/index.js @@ -1,6 +1,6 @@ import Vue from 'vue' import Vuex from 'vuex' -import { getUserInfo } from '@/common/api' +import { getUserInfo, logout } from '@/common/api' Vue.use(Vuex) // vue的插件机制 @@ -9,7 +9,7 @@ const store = new Vuex.Store({ state: { openExamine: false, // 是否开启审核状态。用于小程序、App 等审核时,关闭部分功能。TODO 芋艿:暂时没找到刷新的地方 token: uni.getStorageSync('token'), // 用户身份 Token - userInfo: {}, // 用户基本信息 + userInfo: uni.getStorageSync('userInfo'), // 用户基本信息 timerIdent: false // 全局 1s 定时器,只在全局开启一个,所有需要定时执行的任务监听该值即可,无需额外开启 TODO 芋艿:需要看看 }, getters: { @@ -38,25 +38,29 @@ const store = new Vuex.Store({ // 加载用户信息 this.dispatch('obtainUserInfo') }, - // 退出登录 - logout(state) { - // 清空 Token - state.token = '' + // 更新用户信息 + setUserInfo(state, data) { + state.userInfo = data + uni.setStorageSync('userInfo', data) + }, + // 清空 Token 和 用户信息 + clearLoginInfo(state) { uni.removeStorageSync('token') - // 清空用户信息 TODO 芋艿:这里 setTimeout 的原因是,上面可能还有一些 request 请求。后续得优化下 - setTimeout(() => { - state.userInfo = {} - }, 1100) + state.token = '' + uni.removeStorageSync('userInfo') + state.userInfo = {} } }, actions: { // 获得用户基本信息 async obtainUserInfo({ state, commit }) { const res = await getUserInfo() - commit('setStateAttr', { - key: 'userInfo', - val: res.data - }) + commit('setUserInfo', res.data) + }, + // 退出登录 + async logout({ state, commit }) { + commit('clearLoginInfo') + await logout() } } })