info.nvue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <template>
  2. <view class="page">
  3. <!-- 头像区 -->
  4. <view class="section">
  5. <view class="info-item" @click="changeAvatar">
  6. <text class="item-label">头像</text>
  7. <view class="item-right">
  8. <image class="avatar-preview" :src="avatar" mode="aspectFill"></image>
  9. <image class="item-arrow" src="/static/my/arrowright.png" mode="aspectFill"></image>
  10. </view>
  11. </view>
  12. </view>
  13. <!-- 基本信息 -->
  14. <view class="section">
  15. <view class="info-item" @click="editNickname">
  16. <text class="item-label">昵称</text>
  17. <view class="item-right">
  18. <text class="item-value">{{nickname}}</text>
  19. <image class="item-arrow" src="/static/my/arrowright.png" mode="aspectFill"></image>
  20. </view>
  21. </view>
  22. <view class="info-item">
  23. <text class="item-label">手机号</text>
  24. <view class="item-right">
  25. <text class="item-value">{{phone}}</text>
  26. </view>
  27. </view>
  28. <view class="info-item">
  29. <text class="item-label">AppId</text>
  30. <view class="item-right">
  31. <text class="item-value">{{appid}}</text>
  32. </view>
  33. </view>
  34. </view>
  35. <!-- 修改昵称弹窗 -->
  36. <view class="nickname-overlay" v-if="showNicknamePopup" @click="closeNicknamePopup">
  37. <view class="nickname-dialog" @click.stop="" :style="{marginBottom: keyboardHeight + 'px'}">
  38. <text class="dialog-title">修改昵称</text>
  39. <view class="input-box">
  40. <input class="nickname-input" v-model="nicknameInput" placeholder="请输入昵称" placeholder-class="input-placeholder"
  41. maxlength="12" :adjust-position="false" :focus="showNicknamePopup"
  42. @keyboardheightchange="onKeyboardHeight" />
  43. </view>
  44. <view class="dialog-btns">
  45. <view class="dialog-btn" @click="closeNicknamePopup">
  46. <text class="dialog-cancel">取消</text>
  47. </view>
  48. <view class="dialog-divider"></view>
  49. <view class="dialog-btn" @click="saveNickname">
  50. <text class="dialog-confirm">确定</text>
  51. </view>
  52. </view>
  53. </view>
  54. </view>
  55. <ay-toast ref="ayToast" />
  56. </view>
  57. </template>
  58. <script>
  59. import ayToast from '@/components/ay-toast/ay-toast.nvue'
  60. import { getProfile, uploadAvatar as uploadAvatarApi, updateProfile } from '@/utils/api/user.js'
  61. export default {
  62. components: {
  63. ayToast
  64. },
  65. data() {
  66. return {
  67. avatar: '/static/144x144.png',
  68. nickname: '用户',
  69. phone: '',
  70. appid: '',
  71. showNicknamePopup: false,
  72. nicknameInput: '',
  73. keyboardHeight: 0
  74. }
  75. },
  76. onLoad() {
  77. this.loadUserInfo()
  78. },
  79. methods: {
  80. async loadUserInfo() {
  81. try {
  82. const profile = await getProfile()
  83. this.phone = profile.accountPhone || profile.phone || ''
  84. this.nickname = profile.nickname || '用户'
  85. this.appid = profile.appId || ''
  86. this.avatar = profile.avatar || '/static/144x144.png'
  87. uni.setStorageSync('phone', this.phone)
  88. uni.setStorageSync('nickname', this.nickname)
  89. uni.setStorageSync('appId', this.appid)
  90. uni.setStorageSync('avatar', this.avatar)
  91. uni.setStorageSync('current_manage_user', profile)
  92. uni.setStorageSync('current_manage_user_id', profile.profileId || profile.id)
  93. return
  94. } catch (e) {
  95. console.error('加载个人资料失败,使用本地缓存', e)
  96. }
  97. const phone = uni.getStorageSync('phone') || '138****8888'
  98. const nickname = uni.getStorageSync('nickname') || '用户'
  99. const appid = uni.getStorageSync('appId') || ''
  100. const avatar = uni.getStorageSync('avatar') || '/static/144x144.png'
  101. this.phone = phone
  102. this.nickname = nickname
  103. this.appid = appid
  104. this.avatar = avatar
  105. },
  106. changeAvatar() {
  107. uni.chooseImage({
  108. count: 1,
  109. sizeType: ['compressed'],
  110. sourceType: ['album', 'camera'],
  111. success: (res) => {
  112. const tempPath = res.tempFilePaths[0]
  113. this.uploadAvatar(tempPath)
  114. }
  115. })
  116. },
  117. async uploadAvatar(filePath) {
  118. try {
  119. // 上传图片到 OSS
  120. const res = await uploadAvatarApi(filePath)
  121. const avatarUrl = res.url
  122. // 更新后端用户资料
  123. await updateProfile({ avatar: avatarUrl })
  124. // 更新本地存储和页面显示
  125. this.avatar = avatarUrl
  126. uni.setStorageSync('avatar', avatarUrl)
  127. this.$refs.ayToast.success('头像修改成功')
  128. } catch (e) {
  129. // 错误已由 http 层统一提示
  130. }
  131. },
  132. editNickname() {
  133. this.nicknameInput = this.nickname
  134. this.showNicknamePopup = true
  135. },
  136. closeNicknamePopup() {
  137. this.showNicknamePopup = false
  138. this.keyboardHeight = 0
  139. },
  140. onKeyboardHeight(e) {
  141. this.keyboardHeight = e.detail.height || 0
  142. },
  143. async saveNickname() {
  144. const val = this.nicknameInput.trim()
  145. if (!val) {
  146. this.$refs.ayToast.error('昵称不能为空')
  147. return
  148. }
  149. try {
  150. // 更新后端用户资料
  151. await updateProfile({ nickname: val })
  152. this.nickname = val
  153. uni.setStorageSync('nickname', val)
  154. this.showNicknamePopup = false
  155. this.keyboardHeight = 0
  156. this.$refs.ayToast.success('修改成功')
  157. } catch (e) {
  158. // 错误已由 http 层统一提示
  159. }
  160. }
  161. },
  162. onShow() {
  163. // 返回时刷新用户信息
  164. this.loadUserInfo()
  165. }
  166. }
  167. </script>
  168. <style>
  169. .page {
  170. flex: 1;
  171. background-color: #F5F6F8;
  172. }
  173. /* 分组 */
  174. .section {
  175. background-color: #FFFFFF;
  176. margin-top: 20rpx;
  177. border-radius: 20rpx;
  178. margin-left: 24rpx;
  179. margin-right: 24rpx;
  180. overflow: hidden;
  181. }
  182. /* 信息项 */
  183. .info-item {
  184. flex-direction: row;
  185. align-items: center;
  186. justify-content: space-between;
  187. height: 110rpx;
  188. padding-left: 32rpx;
  189. padding-right: 32rpx;
  190. border-bottom-width: 1px;
  191. border-bottom-color: #F0F0F0;
  192. border-bottom-style: solid;
  193. }
  194. .item-label {
  195. font-size: 30rpx;
  196. color: #333333;
  197. }
  198. .item-right {
  199. flex-direction: row;
  200. align-items: center;
  201. }
  202. .item-value {
  203. font-size: 30rpx;
  204. color: #999999;
  205. margin-right: 16rpx;
  206. }
  207. .item-arrow {
  208. width: 32rpx;
  209. height: 32rpx;
  210. }
  211. .avatar-preview {
  212. width: 80rpx;
  213. height: 80rpx;
  214. border-radius: 80rpx;
  215. margin-right: 16rpx;
  216. }
  217. /* 昵称修改弹窗 */
  218. .nickname-overlay {
  219. position: fixed;
  220. top: 0;
  221. left: 0;
  222. right: 0;
  223. bottom: 0;
  224. background-color: rgba(0, 0, 0, 0.4);
  225. justify-content: center;
  226. align-items: center;
  227. }
  228. .nickname-dialog {
  229. width: 600rpx;
  230. background-color: #FFFFFF;
  231. border-radius: 28rpx;
  232. align-items: center;
  233. overflow: hidden;
  234. }
  235. .dialog-title {
  236. font-size: 34rpx;
  237. color: #333333;
  238. font-weight: bold;
  239. margin-top: 48rpx;
  240. margin-bottom: 40rpx;
  241. }
  242. .input-box {
  243. width: 520rpx;
  244. height: 88rpx;
  245. background-color: #F5F6F8;
  246. border-radius: 16rpx;
  247. justify-content: center;
  248. padding-left: 24rpx;
  249. padding-right: 24rpx;
  250. margin-bottom: 48rpx;
  251. }
  252. .nickname-input {
  253. font-size: 30rpx;
  254. color: #333333;
  255. }
  256. .dialog-btns {
  257. flex-direction: row;
  258. align-self: stretch;
  259. border-top-width: 1px;
  260. border-top-color: #F0F0F0;
  261. border-top-style: solid;
  262. height: 96rpx;
  263. align-items: center;
  264. }
  265. .dialog-btn {
  266. flex: 1;
  267. height: 96rpx;
  268. justify-content: center;
  269. align-items: center;
  270. }
  271. .dialog-cancel {
  272. font-size: 32rpx;
  273. color: #999999;
  274. }
  275. .dialog-divider {
  276. width: 1px;
  277. height: 96rpx;
  278. background-color: #F0F0F0;
  279. }
  280. .dialog-confirm {
  281. font-size: 32rpx;
  282. color: #389588;
  283. font-weight: bold;
  284. }
  285. </style>