edit.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <template>
  2. <view class="edit-profile-container">
  3. <view class="edit-header">
  4. <text class="header-title">编辑资料</text>
  5. </view>
  6. <view class="edit-form">
  7. <!-- 头像 -->
  8. <view class="form-item">
  9. <text class="item-label">头像</text>
  10. <button class="avatar-btn" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
  11. <image class="avatar-preview" :src="avatarUrl" mode="aspectFill"></image>
  12. <text class="change-text">点击更换</text>
  13. </button>
  14. </view>
  15. <!-- 昵称 -->
  16. <view class="form-item">
  17. <text class="item-label">昵称</text>
  18. <input
  19. class="item-input"
  20. type="nickname"
  21. v-model="nickname"
  22. placeholder="请输入昵称"
  23. maxlength="20"
  24. />
  25. </view>
  26. <!-- 手机号(只读) -->
  27. <view class="form-item" v-if="phone">
  28. <text class="item-label">手机号</text>
  29. <text class="item-value">{{ phone }}</text>
  30. </view>
  31. </view>
  32. <!-- 保存按钮 -->
  33. <view class="action-area">
  34. <button class="save-btn" @click="handleSave">保存</button>
  35. </view>
  36. </view>
  37. </template>
  38. <script>
  39. import { getUserInfo, setUserInfo } from '@/utils/auth.js'
  40. import { updateUserProfile } from '@/utils/api.js'
  41. export default {
  42. data() {
  43. return {
  44. avatarUrl: '/static/images/head.png',
  45. nickname: '',
  46. phone: '',
  47. originalAvatar: '',
  48. originalNickname: ''
  49. }
  50. },
  51. onLoad() {
  52. this.loadUserInfo()
  53. },
  54. methods: {
  55. /**
  56. * 加载用户信息
  57. */
  58. loadUserInfo() {
  59. const userInfo = getUserInfo()
  60. if (userInfo) {
  61. this.avatarUrl = userInfo.avatar || '/static/images/head.png'
  62. this.nickname = userInfo.nickname || ''
  63. this.phone = userInfo.phone || ''
  64. this.originalAvatar = this.avatarUrl
  65. this.originalNickname = this.nickname
  66. }
  67. },
  68. /**
  69. * 选择头像
  70. */
  71. onChooseAvatar(e) {
  72. const { avatarUrl } = e.detail
  73. this.avatarUrl = avatarUrl
  74. console.log('选择头像:', avatarUrl)
  75. },
  76. /**
  77. * 保存资料
  78. */
  79. async handleSave() {
  80. if (!this.nickname || this.nickname.trim() === '') {
  81. uni.showToast({
  82. title: '请输入昵称',
  83. icon: 'none'
  84. })
  85. return
  86. }
  87. // 检查是否有修改
  88. if (this.avatarUrl === this.originalAvatar && this.nickname === this.originalNickname) {
  89. uni.showToast({
  90. title: '没有修改',
  91. icon: 'none'
  92. })
  93. return
  94. }
  95. try {
  96. uni.showLoading({ title: '保存中...' })
  97. // 上传头像(如果更换了头像且不是默认头像)
  98. let uploadedAvatarUrl = this.avatarUrl
  99. if (this.avatarUrl !== this.originalAvatar && !this.avatarUrl.startsWith('/static/')) {
  100. // TODO: 这里需要上传头像到服务器
  101. // 暂时直接使用微信临时路径
  102. uploadedAvatarUrl = this.avatarUrl
  103. }
  104. // 调用后端接口更新用户信息
  105. const result = await updateUserProfile({
  106. nickname: this.nickname,
  107. avatar: uploadedAvatarUrl
  108. })
  109. console.log('更新成功:', result)
  110. // 更新本地存储的用户信息
  111. const userInfo = getUserInfo()
  112. userInfo.nickname = this.nickname
  113. userInfo.avatar = uploadedAvatarUrl
  114. setUserInfo(userInfo)
  115. uni.hideLoading()
  116. uni.showToast({
  117. title: '保存成功',
  118. icon: 'success'
  119. })
  120. // 延迟返回
  121. setTimeout(() => {
  122. uni.navigateBack()
  123. }, 1500)
  124. } catch (error) {
  125. console.error('保存失败:', error)
  126. uni.hideLoading()
  127. uni.showToast({
  128. title: error.message || '保存失败',
  129. icon: 'none'
  130. })
  131. }
  132. }
  133. }
  134. }
  135. </script>
  136. <style scoped>
  137. .edit-profile-container {
  138. min-height: 100vh;
  139. background: #f5f6fb;
  140. padding: 30rpx;
  141. }
  142. .edit-header {
  143. text-align: center;
  144. padding: 40rpx 0;
  145. }
  146. .header-title {
  147. font-size: 40rpx;
  148. font-weight: bold;
  149. color: #222;
  150. }
  151. .edit-form {
  152. background: #fff;
  153. border-radius: 20rpx;
  154. padding: 40rpx;
  155. margin-bottom: 40rpx;
  156. }
  157. .form-item {
  158. display: flex;
  159. align-items: center;
  160. padding: 30rpx 0;
  161. border-bottom: 1rpx solid #f0f0f0;
  162. }
  163. .form-item:last-child {
  164. border-bottom: none;
  165. }
  166. .item-label {
  167. width: 150rpx;
  168. font-size: 32rpx;
  169. color: #333;
  170. font-weight: 500;
  171. }
  172. .avatar-btn {
  173. flex: 1;
  174. display: flex;
  175. align-items: center;
  176. justify-content: space-between;
  177. background: transparent;
  178. border: none;
  179. padding: 0;
  180. margin: 0;
  181. line-height: normal;
  182. }
  183. .avatar-btn::after {
  184. border: none;
  185. }
  186. .avatar-preview {
  187. width: 120rpx;
  188. height: 120rpx;
  189. border-radius: 50%;
  190. background: #f0f0f0;
  191. }
  192. .change-text {
  193. font-size: 28rpx;
  194. color: #5d55e8;
  195. }
  196. .item-input {
  197. flex: 1;
  198. height: 80rpx;
  199. font-size: 32rpx;
  200. color: #333;
  201. text-align: right;
  202. }
  203. .item-value {
  204. flex: 1;
  205. font-size: 32rpx;
  206. color: #999;
  207. text-align: right;
  208. }
  209. .action-area {
  210. padding: 0 40rpx;
  211. }
  212. .save-btn {
  213. width: 100%;
  214. height: 90rpx;
  215. background: linear-gradient(135deg, #5d55e8, #7568ff);
  216. color: #fff;
  217. font-size: 32rpx;
  218. border-radius: 45rpx;
  219. border: none;
  220. box-shadow: 0 12rpx 24rpx rgba(93, 85, 232, 0.4);
  221. line-height: 90rpx;
  222. }
  223. .save-btn:active {
  224. opacity: 0.9;
  225. }
  226. </style>