| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- <template>
- <view class="page">
- <!-- 头像区 -->
- <view class="section">
- <view class="info-item" @click="changeAvatar">
- <text class="item-label">头像</text>
- <view class="item-right">
- <image class="avatar-preview" :src="avatar" mode="aspectFill"></image>
- <image class="item-arrow" src="/static/my/arrowright.png" mode="aspectFill"></image>
- </view>
- </view>
- </view>
- <!-- 基本信息 -->
- <view class="section">
- <view class="info-item" @click="editNickname">
- <text class="item-label">昵称</text>
- <view class="item-right">
- <text class="item-value">{{nickname}}</text>
- <image class="item-arrow" src="/static/my/arrowright.png" mode="aspectFill"></image>
- </view>
- </view>
- <view class="info-item">
- <text class="item-label">手机号</text>
- <view class="item-right">
- <text class="item-value">{{phone}}</text>
- </view>
- </view>
- <view class="info-item">
- <text class="item-label">AppId</text>
- <view class="item-right">
- <text class="item-value">{{appid}}</text>
- </view>
- </view>
- </view>
- <!-- 修改昵称弹窗 -->
- <view class="nickname-overlay" v-if="showNicknamePopup" @click="closeNicknamePopup">
- <view class="nickname-dialog" @click.stop="" :style="{marginBottom: keyboardHeight + 'px'}">
- <text class="dialog-title">修改昵称</text>
- <view class="input-box">
- <input class="nickname-input" v-model="nicknameInput" placeholder="请输入昵称" placeholder-class="input-placeholder"
- maxlength="12" :adjust-position="false" :focus="showNicknamePopup"
- @keyboardheightchange="onKeyboardHeight" />
- </view>
- <view class="dialog-btns">
- <view class="dialog-btn" @click="closeNicknamePopup">
- <text class="dialog-cancel">取消</text>
- </view>
- <view class="dialog-divider"></view>
- <view class="dialog-btn" @click="saveNickname">
- <text class="dialog-confirm">确定</text>
- </view>
- </view>
- </view>
- </view>
- <ay-toast ref="ayToast" />
- </view>
- </template>
- <script>
- import ayToast from '@/components/ay-toast/ay-toast.nvue'
- import { getProfile, uploadAvatar as uploadAvatarApi, updateProfile } from '@/utils/api/user.js'
- export default {
- components: {
- ayToast
- },
- data() {
- return {
- avatar: '/static/144x144.png',
- nickname: '用户',
- phone: '',
- appid: '',
- showNicknamePopup: false,
- nicknameInput: '',
- keyboardHeight: 0
- }
- },
- onLoad() {
- this.loadUserInfo()
- },
- methods: {
- async loadUserInfo() {
- try {
- const profile = await getProfile()
- this.phone = profile.accountPhone || profile.phone || ''
- this.nickname = profile.nickname || '用户'
- this.appid = profile.appId || ''
- this.avatar = profile.avatar || '/static/144x144.png'
- uni.setStorageSync('phone', this.phone)
- uni.setStorageSync('nickname', this.nickname)
- uni.setStorageSync('appId', this.appid)
- uni.setStorageSync('avatar', this.avatar)
- uni.setStorageSync('current_manage_user', profile)
- uni.setStorageSync('current_manage_user_id', profile.profileId || profile.id)
- return
- } catch (e) {
- console.error('加载个人资料失败,使用本地缓存', e)
- }
- const phone = uni.getStorageSync('phone') || '138****8888'
- const nickname = uni.getStorageSync('nickname') || '用户'
- const appid = uni.getStorageSync('appId') || ''
- const avatar = uni.getStorageSync('avatar') || '/static/144x144.png'
- this.phone = phone
- this.nickname = nickname
- this.appid = appid
- this.avatar = avatar
- },
- changeAvatar() {
- uni.chooseImage({
- count: 1,
- sizeType: ['compressed'],
- sourceType: ['album', 'camera'],
- success: (res) => {
- const tempPath = res.tempFilePaths[0]
- this.uploadAvatar(tempPath)
- }
- })
- },
- async uploadAvatar(filePath) {
- try {
- // 上传图片到 OSS
- const res = await uploadAvatarApi(filePath)
- const avatarUrl = res.url
- // 更新后端用户资料
- await updateProfile({ avatar: avatarUrl })
- // 更新本地存储和页面显示
- this.avatar = avatarUrl
- uni.setStorageSync('avatar', avatarUrl)
- this.$refs.ayToast.success('头像修改成功')
- } catch (e) {
- // 错误已由 http 层统一提示
- }
- },
- editNickname() {
- this.nicknameInput = this.nickname
- this.showNicknamePopup = true
- },
- closeNicknamePopup() {
- this.showNicknamePopup = false
- this.keyboardHeight = 0
- },
- onKeyboardHeight(e) {
- this.keyboardHeight = e.detail.height || 0
- },
- async saveNickname() {
- const val = this.nicknameInput.trim()
- if (!val) {
- this.$refs.ayToast.error('昵称不能为空')
- return
- }
- try {
- // 更新后端用户资料
- await updateProfile({ nickname: val })
- this.nickname = val
- uni.setStorageSync('nickname', val)
- this.showNicknamePopup = false
- this.keyboardHeight = 0
- this.$refs.ayToast.success('修改成功')
- } catch (e) {
- // 错误已由 http 层统一提示
- }
- }
- },
- onShow() {
- // 返回时刷新用户信息
- this.loadUserInfo()
- }
- }
- </script>
- <style>
- .page {
- flex: 1;
- background-color: #F5F6F8;
- }
- /* 分组 */
- .section {
- background-color: #FFFFFF;
- margin-top: 20rpx;
- border-radius: 20rpx;
- margin-left: 24rpx;
- margin-right: 24rpx;
- overflow: hidden;
- }
- /* 信息项 */
- .info-item {
- flex-direction: row;
- align-items: center;
- justify-content: space-between;
- height: 110rpx;
- padding-left: 32rpx;
- padding-right: 32rpx;
- border-bottom-width: 1px;
- border-bottom-color: #F0F0F0;
- border-bottom-style: solid;
- }
- .item-label {
- font-size: 30rpx;
- color: #333333;
- }
- .item-right {
- flex-direction: row;
- align-items: center;
- }
- .item-value {
- font-size: 30rpx;
- color: #999999;
- margin-right: 16rpx;
- }
- .item-arrow {
- width: 32rpx;
- height: 32rpx;
- }
- .avatar-preview {
- width: 80rpx;
- height: 80rpx;
- border-radius: 80rpx;
- margin-right: 16rpx;
- }
- /* 昵称修改弹窗 */
- .nickname-overlay {
- position: fixed;
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- background-color: rgba(0, 0, 0, 0.4);
- justify-content: center;
- align-items: center;
- }
- .nickname-dialog {
- width: 600rpx;
- background-color: #FFFFFF;
- border-radius: 28rpx;
- align-items: center;
- overflow: hidden;
- }
- .dialog-title {
- font-size: 34rpx;
- color: #333333;
- font-weight: bold;
- margin-top: 48rpx;
- margin-bottom: 40rpx;
- }
- .input-box {
- width: 520rpx;
- height: 88rpx;
- background-color: #F5F6F8;
- border-radius: 16rpx;
- justify-content: center;
- padding-left: 24rpx;
- padding-right: 24rpx;
- margin-bottom: 48rpx;
- }
- .nickname-input {
- font-size: 30rpx;
- color: #333333;
- }
- .dialog-btns {
- flex-direction: row;
- align-self: stretch;
- border-top-width: 1px;
- border-top-color: #F0F0F0;
- border-top-style: solid;
- height: 96rpx;
- align-items: center;
- }
- .dialog-btn {
- flex: 1;
- height: 96rpx;
- justify-content: center;
- align-items: center;
- }
- .dialog-cancel {
- font-size: 32rpx;
- color: #999999;
- }
- .dialog-divider {
- width: 1px;
- height: 96rpx;
- background-color: #F0F0F0;
- }
- .dialog-confirm {
- font-size: 32rpx;
- color: #389588;
- font-weight: bold;
- }
- </style>
|