info.nvue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 { 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. loadUserInfo() {
  81. const phone = uni.getStorageSync('phone') || '138****8888'
  82. const nickname = uni.getStorageSync('nickname') || '用户'
  83. const appid = uni.getStorageSync('appId') || ''
  84. const avatar = uni.getStorageSync('avatar') || '/static/144x144.png'
  85. this.phone = phone
  86. this.nickname = nickname
  87. this.appid = appid
  88. this.avatar = avatar
  89. },
  90. changeAvatar() {
  91. uni.chooseImage({
  92. count: 1,
  93. sizeType: ['compressed'],
  94. sourceType: ['album', 'camera'],
  95. success: (res) => {
  96. const tempPath = res.tempFilePaths[0]
  97. this.uploadAvatar(tempPath)
  98. }
  99. })
  100. },
  101. async uploadAvatar(filePath) {
  102. try {
  103. // 上传图片到 OSS
  104. const res = await uploadAvatarApi(filePath)
  105. const avatarUrl = res.url
  106. // 更新后端用户资料
  107. await updateProfile({ avatar: avatarUrl })
  108. // 更新本地存储和页面显示
  109. this.avatar = avatarUrl
  110. uni.setStorageSync('avatar', avatarUrl)
  111. this.$refs.ayToast.success('头像修改成功')
  112. } catch (e) {
  113. // 错误已由 http 层统一提示
  114. }
  115. },
  116. editNickname() {
  117. this.nicknameInput = this.nickname
  118. this.showNicknamePopup = true
  119. },
  120. closeNicknamePopup() {
  121. this.showNicknamePopup = false
  122. this.keyboardHeight = 0
  123. },
  124. onKeyboardHeight(e) {
  125. this.keyboardHeight = e.detail.height || 0
  126. },
  127. async saveNickname() {
  128. const val = this.nicknameInput.trim()
  129. if (!val) {
  130. this.$refs.ayToast.error('昵称不能为空')
  131. return
  132. }
  133. try {
  134. // 更新后端用户资料
  135. await updateProfile({ nickname: val })
  136. this.nickname = val
  137. uni.setStorageSync('nickname', val)
  138. this.showNicknamePopup = false
  139. this.keyboardHeight = 0
  140. this.$refs.ayToast.success('修改成功')
  141. } catch (e) {
  142. // 错误已由 http 层统一提示
  143. }
  144. }
  145. },
  146. onShow() {
  147. // 返回时刷新用户信息
  148. this.loadUserInfo()
  149. }
  150. }
  151. </script>
  152. <style>
  153. .page {
  154. flex: 1;
  155. background-color: #F5F6F8;
  156. }
  157. /* 分组 */
  158. .section {
  159. background-color: #FFFFFF;
  160. margin-top: 20rpx;
  161. border-radius: 20rpx;
  162. margin-left: 24rpx;
  163. margin-right: 24rpx;
  164. overflow: hidden;
  165. }
  166. /* 信息项 */
  167. .info-item {
  168. flex-direction: row;
  169. align-items: center;
  170. justify-content: space-between;
  171. height: 110rpx;
  172. padding-left: 32rpx;
  173. padding-right: 32rpx;
  174. border-bottom-width: 1px;
  175. border-bottom-color: #F0F0F0;
  176. border-bottom-style: solid;
  177. }
  178. .item-label {
  179. font-size: 30rpx;
  180. color: #333333;
  181. }
  182. .item-right {
  183. flex-direction: row;
  184. align-items: center;
  185. }
  186. .item-value {
  187. font-size: 30rpx;
  188. color: #999999;
  189. margin-right: 16rpx;
  190. }
  191. .item-arrow {
  192. width: 32rpx;
  193. height: 32rpx;
  194. }
  195. .avatar-preview {
  196. width: 80rpx;
  197. height: 80rpx;
  198. border-radius: 80rpx;
  199. margin-right: 16rpx;
  200. }
  201. /* 昵称修改弹窗 */
  202. .nickname-overlay {
  203. position: fixed;
  204. top: 0;
  205. left: 0;
  206. right: 0;
  207. bottom: 0;
  208. background-color: rgba(0, 0, 0, 0.4);
  209. justify-content: center;
  210. align-items: center;
  211. }
  212. .nickname-dialog {
  213. width: 600rpx;
  214. background-color: #FFFFFF;
  215. border-radius: 28rpx;
  216. align-items: center;
  217. overflow: hidden;
  218. }
  219. .dialog-title {
  220. font-size: 34rpx;
  221. color: #333333;
  222. font-weight: bold;
  223. margin-top: 48rpx;
  224. margin-bottom: 40rpx;
  225. }
  226. .input-box {
  227. width: 520rpx;
  228. height: 88rpx;
  229. background-color: #F5F6F8;
  230. border-radius: 16rpx;
  231. justify-content: center;
  232. padding-left: 24rpx;
  233. padding-right: 24rpx;
  234. margin-bottom: 48rpx;
  235. }
  236. .nickname-input {
  237. font-size: 30rpx;
  238. color: #333333;
  239. }
  240. .dialog-btns {
  241. flex-direction: row;
  242. align-self: stretch;
  243. border-top-width: 1px;
  244. border-top-color: #F0F0F0;
  245. border-top-style: solid;
  246. height: 96rpx;
  247. align-items: center;
  248. }
  249. .dialog-btn {
  250. flex: 1;
  251. height: 96rpx;
  252. justify-content: center;
  253. align-items: center;
  254. }
  255. .dialog-cancel {
  256. font-size: 32rpx;
  257. color: #999999;
  258. }
  259. .dialog-divider {
  260. width: 1px;
  261. height: 96rpx;
  262. background-color: #F0F0F0;
  263. }
  264. .dialog-confirm {
  265. font-size: 32rpx;
  266. color: #389588;
  267. font-weight: bold;
  268. }
  269. </style>