profile.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <view class="container">
  3. <view class="user-info">
  4. <view class="info-item">
  5. <view class="label">头像:</view>
  6. <view class="info" @click="handleAvatarClick">
  7. <u-avatar size="60" :src="userInfo.avatar"></u-avatar>
  8. <u-icon class="btn" name="arrow-right"></u-icon>
  9. </view>
  10. </view>
  11. <view class="info-item">
  12. <view class="label">昵称:</view>
  13. <view v-if="!nameEditOn" class="info">
  14. <view class="value">{{ userInfo.nickname }}</view>
  15. <u-icon
  16. class="btn"
  17. name="edit-pen"
  18. @click="
  19. tempName = userInfo.nickname
  20. nameEditOn = true
  21. "
  22. ></u-icon>
  23. </view>
  24. <view v-else class="name-edit">
  25. <u--input maxlength="10" border="bottom" v-model="tempName"></u--input>
  26. <view class="edit-btn-group">
  27. <u-tag class="edit-btn" text="保存" plain size="mini" type="primary" @click="handleSaveBtnClick"></u-tag>
  28. <u-tag
  29. class="edit-btn"
  30. text="取消"
  31. plain
  32. size="mini"
  33. type="info"
  34. @click="
  35. tempName = ''
  36. nameEditOn = false
  37. "
  38. ></u-tag>
  39. </view>
  40. </view>
  41. </view>
  42. <view class="info-item">
  43. <view class="label">手机:</view>
  44. <view class="info">
  45. <view class="value">{{ userInfo.mobile }}</view>
  46. <u-icon class="btn" name="edit-pen"></u-icon>
  47. </view>
  48. </view>
  49. </view>
  50. </view>
  51. </template>
  52. <script>
  53. import { getUserInfo, updateAvatar, updateNickname } from '../../api/user'
  54. export default {
  55. data() {
  56. return {
  57. userInfo: {
  58. nickname: '',
  59. avatar: '',
  60. mobile: ''
  61. },
  62. avatarFiles: [],
  63. nameEditOn: false,
  64. tempName: ''
  65. }
  66. },
  67. onLoad() {
  68. this.loadUserInfoData()
  69. },
  70. methods: {
  71. loadUserInfoData() {
  72. getUserInfo().then(res => {
  73. this.userInfo = res.data
  74. })
  75. },
  76. handleAvatarClick() {
  77. uni.chooseImage({
  78. success: chooseImageRes => {
  79. const tempFilePaths = chooseImageRes.tempFilePaths
  80. updateAvatar(tempFilePaths[0]).then(res => {
  81. this.userInfo.avatar = res.data
  82. this.$store.commit('SET_USER_INFO', this.userInfo)
  83. })
  84. }
  85. })
  86. },
  87. handleSaveBtnClick() {
  88. updateNickname({ nickname: this.tempName }).then(res => {
  89. this.nameEditOn = false;
  90. this.userInfo.nickname = this.tempName
  91. this.$store.commit('SET_USER_INFO', this.userInfo)
  92. })
  93. }
  94. }
  95. }
  96. </script>
  97. <style lang="scss" scoped>
  98. .user-info {
  99. .info-item {
  100. padding: 20rpx 60rpx;
  101. border-bottom: $custom-border-style;
  102. @include flex-space-between;
  103. .label {
  104. font-size: 30rpx;
  105. }
  106. .info {
  107. @include flex-right;
  108. .value {
  109. font-size: 30rpx;
  110. }
  111. .btn {
  112. margin-left: 30rpx;
  113. }
  114. }
  115. .name-edit {
  116. @include flex-left;
  117. .edit-btn-group {
  118. @include flex;
  119. .edit-btn {
  120. margin-left: 20rpx;
  121. }
  122. }
  123. }
  124. }
  125. }
  126. </style>