edit.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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, isLoggedIn as checkLoginStatus } 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. const loginStatus = checkLoginStatus()
  53. console.log('[编辑资料] 登录状态:', loginStatus)
  54. this.loadUserInfo()
  55. },
  56. onShow() {
  57. // 设置导航栏标题
  58. uni.setNavigationBarTitle({ title: '量化交易大师' })
  59. },
  60. methods: {
  61. /**
  62. * 加载用户信息
  63. */
  64. loadUserInfo() {
  65. const userInfo = getUserInfo()
  66. console.log('[编辑资料] 加载用户信息:', userInfo)
  67. if (userInfo) {
  68. this.avatarUrl = userInfo.avatar || '/static/images/head.png'
  69. this.nickname = userInfo.nickname || ''
  70. this.phone = userInfo.phone || ''
  71. this.originalAvatar = this.avatarUrl
  72. this.originalNickname = this.nickname
  73. }
  74. },
  75. /**
  76. * 选择头像
  77. */
  78. onChooseAvatar(e) {
  79. const { avatarUrl } = e.detail
  80. this.avatarUrl = avatarUrl
  81. console.log('选择头像:', avatarUrl)
  82. },
  83. /**
  84. * 保存资料
  85. */
  86. async handleSave() {
  87. if (!this.nickname || this.nickname.trim() === '') {
  88. uni.showToast({
  89. title: '请输入昵称',
  90. icon: 'none'
  91. })
  92. return
  93. }
  94. // 检查是否有修改
  95. if (this.avatarUrl === this.originalAvatar && this.nickname === this.originalNickname) {
  96. uni.showToast({
  97. title: '没有修改',
  98. icon: 'none'
  99. })
  100. return
  101. }
  102. try {
  103. uni.showLoading({ title: '保存中...' })
  104. // 上传头像(如果更换了头像且不是默认头像)
  105. let uploadedAvatarUrl = this.avatarUrl
  106. if (this.avatarUrl !== this.originalAvatar && !this.avatarUrl.startsWith('/static/')) {
  107. // TODO: 这里需要上传头像到服务器
  108. // 暂时直接使用微信临时路径
  109. uploadedAvatarUrl = this.avatarUrl
  110. }
  111. // 调用后端接口更新用户信息
  112. const result = await updateUserProfile({
  113. nickname: this.nickname,
  114. avatar: uploadedAvatarUrl
  115. })
  116. console.log('更新成功:', result)
  117. // 更新本地存储的用户信息
  118. const userInfo = getUserInfo()
  119. userInfo.nickname = this.nickname
  120. userInfo.avatar = uploadedAvatarUrl
  121. setUserInfo(userInfo)
  122. uni.hideLoading()
  123. uni.showToast({
  124. title: '保存成功',
  125. icon: 'success'
  126. })
  127. // 延迟返回
  128. setTimeout(() => {
  129. uni.navigateBack()
  130. }, 1500)
  131. } catch (error) {
  132. console.error('保存失败:', error)
  133. uni.hideLoading()
  134. uni.showToast({
  135. title: error.message || '保存失败',
  136. icon: 'none'
  137. })
  138. }
  139. }
  140. }
  141. }
  142. </script>
  143. <style scoped>
  144. .edit-profile-container {
  145. min-height: 100vh;
  146. background: #f5f6fb;
  147. padding: 30rpx;
  148. }
  149. .edit-header {
  150. text-align: center;
  151. padding: 40rpx 0;
  152. }
  153. .header-title {
  154. font-size: 40rpx;
  155. font-weight: bold;
  156. color: #222;
  157. }
  158. .edit-form {
  159. background: #fff;
  160. border-radius: 20rpx;
  161. padding: 40rpx;
  162. margin-bottom: 40rpx;
  163. }
  164. .form-item {
  165. display: flex;
  166. align-items: center;
  167. padding: 30rpx 0;
  168. border-bottom: 1rpx solid #f0f0f0;
  169. }
  170. .form-item:last-child {
  171. border-bottom: none;
  172. }
  173. .item-label {
  174. width: 150rpx;
  175. font-size: 32rpx;
  176. color: #333;
  177. font-weight: 500;
  178. }
  179. .avatar-btn {
  180. flex: 1;
  181. display: flex;
  182. align-items: center;
  183. justify-content: space-between;
  184. background: transparent;
  185. border: none;
  186. padding: 0;
  187. margin: 0;
  188. line-height: normal;
  189. }
  190. .avatar-btn::after {
  191. border: none;
  192. }
  193. .avatar-preview {
  194. width: 120rpx;
  195. height: 120rpx;
  196. border-radius: 50%;
  197. background: #f0f0f0;
  198. }
  199. .change-text {
  200. font-size: 28rpx;
  201. color: #5d55e8;
  202. }
  203. .item-input {
  204. flex: 1;
  205. height: 80rpx;
  206. font-size: 32rpx;
  207. color: #333;
  208. text-align: right;
  209. }
  210. .item-value {
  211. flex: 1;
  212. font-size: 32rpx;
  213. color: #999;
  214. text-align: right;
  215. }
  216. .action-area {
  217. padding: 0 40rpx;
  218. }
  219. .save-btn {
  220. width: 100%;
  221. height: 90rpx;
  222. background: linear-gradient(135deg, #5d55e8, #7568ff);
  223. color: #fff;
  224. font-size: 32rpx;
  225. border-radius: 45rpx;
  226. border: none;
  227. box-shadow: 0 12rpx 24rpx rgba(93, 85, 232, 0.4);
  228. line-height: 90rpx;
  229. }
  230. .save-btn:active {
  231. opacity: 0.9;
  232. }
  233. </style>