Commit 713817d0f18e0dc6a8c9b84546a505d750b4b3f4
1 parent
5e19beee
退出登录、修改个人头像
Showing
4 changed files
with
114 additions
and
21 deletions
yudao-ui-app/common/api.js
| ... | ... | @@ -3,6 +3,8 @@ const { http } = uni.$u |
| 3 | 3 | /* login */ |
| 4 | 4 | //使用手机 + 密码登录 |
| 5 | 5 | export const passwordLogin = params => http.post('/app-api/member/login', params) |
| 6 | +//退出登录 | |
| 7 | +export const logout = params => http.post('/app-api/member/logout', params) | |
| 6 | 8 | //发送手机验证码 |
| 7 | 9 | export const sendSmsCode = params => http.post('/app-api/member/send-sms-code', params) |
| 8 | 10 | //使用手机 + 验证码登录 |
| ... | ... | @@ -10,6 +12,13 @@ export const smsLogin = params => http.post('/app-api/member/sms-login', params) |
| 10 | 12 | |
| 11 | 13 | //获取用户信息 |
| 12 | 14 | export const getUserInfo = params => http.get('/app-api/member/user/get', params) |
| 15 | +//修改用户头像 | |
| 16 | +export const updateAvatar = filePath => | |
| 17 | + http.upload('/app-api/member/user/update-avatar', { | |
| 18 | + name: 'avatarFile', | |
| 19 | + fileType: 'image', | |
| 20 | + filePath: filePath | |
| 21 | + }) | |
| 13 | 22 | |
| 14 | 23 | /* index */ |
| 15 | 24 | // 获取滚动图数据 | ... | ... |
yudao-ui-app/pages/profile/profile.vue
| 1 | 1 | <template> |
| 2 | - <view class="container"> 个人资料 </view> | |
| 2 | + <view class="container"> | |
| 3 | + <view class="user-info"> | |
| 4 | + <view class="info-item"> | |
| 5 | + <view class="label">头像:</view> | |
| 6 | + <view class="info" @click="handleAvatarClick"> | |
| 7 | + <u-avatar size="60" :src="userInfo.avatar"></u-avatar> | |
| 8 | + <u-icon class="btn" name="arrow-right"></u-icon> | |
| 9 | + </view> | |
| 10 | + </view> | |
| 11 | + <view class="info-item"> | |
| 12 | + <view class="label">昵称:</view> | |
| 13 | + <view class="info"> | |
| 14 | + <view class="value">{{ userInfo.nickname }}</view> | |
| 15 | + <u-icon class="btn" name="edit-pen"></u-icon> | |
| 16 | + </view> | |
| 17 | + </view> | |
| 18 | + <view class="info-item"> | |
| 19 | + <view class="label">手机:</view> | |
| 20 | + <view class="info"> | |
| 21 | + <view class="value">{{ userInfo.mobile }}</view> | |
| 22 | + <u-icon class="btn" name="edit-pen"></u-icon> | |
| 23 | + </view> | |
| 24 | + </view> | |
| 25 | + </view> | |
| 26 | + </view> | |
| 3 | 27 | </template> |
| 4 | 28 | |
| 5 | 29 | <script> |
| 30 | +import { getUserInfo, updateAvatar } from '../../common/api' | |
| 31 | + | |
| 6 | 32 | export default { |
| 7 | 33 | data() { |
| 8 | 34 | return { |
| 9 | - title: '' | |
| 35 | + userInfo: { | |
| 36 | + nickname: '', | |
| 37 | + avatar: '', | |
| 38 | + mobile: '' | |
| 39 | + }, | |
| 40 | + avatarFiles: [] | |
| 10 | 41 | } |
| 11 | 42 | }, |
| 12 | - onLoad() {}, | |
| 13 | - methods: {} | |
| 43 | + onLoad() { | |
| 44 | + this.loadUserInfoData() | |
| 45 | + }, | |
| 46 | + methods: { | |
| 47 | + loadUserInfoData() { | |
| 48 | + getUserInfo() | |
| 49 | + .then(res => { | |
| 50 | + this.userInfo = res.data | |
| 51 | + }) | |
| 52 | + .catch(err => { | |
| 53 | + //console.log(err) | |
| 54 | + }) | |
| 55 | + }, | |
| 56 | + handleAvatarClick() { | |
| 57 | + uni.chooseImage({ | |
| 58 | + success: chooseImageRes => { | |
| 59 | + const tempFilePaths = chooseImageRes.tempFilePaths | |
| 60 | + console.log(tempFilePaths) | |
| 61 | + updateAvatar(tempFilePaths[0]) | |
| 62 | + .then(res => { | |
| 63 | + console.log(res) | |
| 64 | + }) | |
| 65 | + .catch(err => { | |
| 66 | + //console.log(err) | |
| 67 | + }) | |
| 68 | + } | |
| 69 | + }) | |
| 70 | + } | |
| 71 | + } | |
| 14 | 72 | } |
| 15 | 73 | </script> |
| 16 | 74 | |
| 17 | -<style lang="scss" scoped></style> | |
| 75 | +<style lang="scss" scoped> | |
| 76 | +.user-info { | |
| 77 | + .info-item { | |
| 78 | + padding: 20rpx 60rpx; | |
| 79 | + border-bottom: $custom-border-style; | |
| 80 | + @include flex-space-between; | |
| 81 | + .label { | |
| 82 | + font-size: 30rpx; | |
| 83 | + } | |
| 84 | + .info { | |
| 85 | + @include flex-right; | |
| 86 | + .value { | |
| 87 | + font-size: 30rpx; | |
| 88 | + } | |
| 89 | + .btn { | |
| 90 | + margin-left: 30rpx; | |
| 91 | + } | |
| 92 | + } | |
| 93 | + } | |
| 94 | +} | |
| 95 | +</style> | ... | ... |
yudao-ui-app/pages/user/user.vue
| ... | ... | @@ -71,7 +71,9 @@ export default { |
| 71 | 71 | ] |
| 72 | 72 | } |
| 73 | 73 | }, |
| 74 | - onLoad() {}, | |
| 74 | + onLoad() { | |
| 75 | + this.$store.dispatch('obtainUserInfo') | |
| 76 | + }, | |
| 75 | 77 | methods: { |
| 76 | 78 | loginOrJump(pageUrl) { |
| 77 | 79 | if (!this.hasLogin) { |
| ... | ... | @@ -87,7 +89,7 @@ export default { |
| 87 | 89 | success: res => { |
| 88 | 90 | if (res.confirm) { |
| 89 | 91 | console.log('用户点击确定') |
| 90 | - this.$store.commit('logout') | |
| 92 | + this.$store.dispatch('logout') | |
| 91 | 93 | } else if (res.cancel) { |
| 92 | 94 | console.log('用户点击取消') |
| 93 | 95 | } | ... | ... |
yudao-ui-app/store/index.js
| 1 | 1 | import Vue from 'vue' |
| 2 | 2 | import Vuex from 'vuex' |
| 3 | -import { getUserInfo } from '@/common/api' | |
| 3 | +import { getUserInfo, logout } from '@/common/api' | |
| 4 | 4 | |
| 5 | 5 | Vue.use(Vuex) // vue的插件机制 |
| 6 | 6 | |
| ... | ... | @@ -9,7 +9,7 @@ const store = new Vuex.Store({ |
| 9 | 9 | state: { |
| 10 | 10 | openExamine: false, // 是否开启审核状态。用于小程序、App 等审核时,关闭部分功能。TODO 芋艿:暂时没找到刷新的地方 |
| 11 | 11 | token: uni.getStorageSync('token'), // 用户身份 Token |
| 12 | - userInfo: {}, // 用户基本信息 | |
| 12 | + userInfo: uni.getStorageSync('userInfo'), // 用户基本信息 | |
| 13 | 13 | timerIdent: false // 全局 1s 定时器,只在全局开启一个,所有需要定时执行的任务监听该值即可,无需额外开启 TODO 芋艿:需要看看 |
| 14 | 14 | }, |
| 15 | 15 | getters: { |
| ... | ... | @@ -38,25 +38,29 @@ const store = new Vuex.Store({ |
| 38 | 38 | // 加载用户信息 |
| 39 | 39 | this.dispatch('obtainUserInfo') |
| 40 | 40 | }, |
| 41 | - // 退出登录 | |
| 42 | - logout(state) { | |
| 43 | - // 清空 Token | |
| 44 | - state.token = '' | |
| 41 | + // 更新用户信息 | |
| 42 | + setUserInfo(state, data) { | |
| 43 | + state.userInfo = data | |
| 44 | + uni.setStorageSync('userInfo', data) | |
| 45 | + }, | |
| 46 | + // 清空 Token 和 用户信息 | |
| 47 | + clearLoginInfo(state) { | |
| 45 | 48 | uni.removeStorageSync('token') |
| 46 | - // 清空用户信息 TODO 芋艿:这里 setTimeout 的原因是,上面可能还有一些 request 请求。后续得优化下 | |
| 47 | - setTimeout(() => { | |
| 48 | - state.userInfo = {} | |
| 49 | - }, 1100) | |
| 49 | + state.token = '' | |
| 50 | + uni.removeStorageSync('userInfo') | |
| 51 | + state.userInfo = {} | |
| 50 | 52 | } |
| 51 | 53 | }, |
| 52 | 54 | actions: { |
| 53 | 55 | // 获得用户基本信息 |
| 54 | 56 | async obtainUserInfo({ state, commit }) { |
| 55 | 57 | const res = await getUserInfo() |
| 56 | - commit('setStateAttr', { | |
| 57 | - key: 'userInfo', | |
| 58 | - val: res.data | |
| 59 | - }) | |
| 58 | + commit('setUserInfo', res.data) | |
| 59 | + }, | |
| 60 | + // 退出登录 | |
| 61 | + async logout({ state, commit }) { | |
| 62 | + commit('clearLoginInfo') | |
| 63 | + await logout() | |
| 60 | 64 | } |
| 61 | 65 | } |
| 62 | 66 | }) | ... | ... |