profile.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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="50" shape="square" :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 class="info">
  14. <u--input maxlength="10" border="none" v-model="userInfo.nickname" inputAlign="right" @change="handleNameChange"></u--input>
  15. </view>
  16. </view>
  17. <view class="info-item">
  18. <view class="label">手机:</view>
  19. <view class="info">
  20. <view class="value">{{ userInfo.mobile }}</view>
  21. </view>
  22. </view>
  23. </view>
  24. <view v-if="nameUpdateVisible" class="btn-group">
  25. <u-button type="primary" text="保存" customStyle="margin-top: 50px" @click="handleSaveBtnClick"></u-button>
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. import { getUserInfo, updateAvatar, updateNickname } from '../../api/user'
  31. export default {
  32. data() {
  33. return {
  34. userInfo: {
  35. nickname: '',
  36. avatar: '',
  37. mobile: ''
  38. },
  39. avatarFiles: [],
  40. tempName: ''
  41. }
  42. },
  43. computed: {
  44. nameUpdateVisible: function () {
  45. return this.userInfo.nickname !== this.tempName
  46. }
  47. },
  48. onLoad() {
  49. this.loadUserInfoData()
  50. },
  51. methods: {
  52. loadUserInfoData() {
  53. getUserInfo().then(res => {
  54. this.userInfo = res.data
  55. this.tempName = this.userInfo.nickname
  56. })
  57. },
  58. handleAvatarClick() {
  59. uni.chooseImage({
  60. success: chooseImageRes => {
  61. const tempFilePaths = chooseImageRes.tempFilePaths
  62. updateAvatar(tempFilePaths[0]).then(res => {
  63. this.userInfo.avatar = res.data
  64. this.$store.commit('SET_USER_INFO', this.userInfo)
  65. })
  66. }
  67. })
  68. },
  69. handleNameChange(val) {
  70. let str = uni.$u.trim(val, 'all')
  71. this.$nextTick(() => {
  72. this.userInfo.nickname = str
  73. })
  74. },
  75. handleSaveBtnClick() {
  76. updateNickname({ nickname: this.userInfo.nickname }).then(res => {
  77. this.tempName = this.userInfo.nickname
  78. this.$store.commit('SET_USER_INFO', this.userInfo)
  79. uni.$u.toast('已保存')
  80. setTimeout(() => {
  81. uni.switchTab({
  82. url: '/pages/user/user'
  83. })
  84. }, 300)
  85. })
  86. }
  87. }
  88. }
  89. </script>
  90. <style lang="scss" scoped>
  91. .user-info {
  92. .info-item {
  93. padding: 20rpx 60rpx;
  94. border-bottom: $custom-border-style;
  95. @include flex-space-between;
  96. .label {
  97. font-size: 30rpx;
  98. }
  99. .info {
  100. @include flex-left;
  101. .value {
  102. font-size: 30rpx;
  103. }
  104. .btn {
  105. margin-left: 30rpx;
  106. }
  107. }
  108. .name-edit {
  109. @include flex-left;
  110. .edit-btn-group {
  111. @include flex;
  112. .edit-btn {
  113. margin-left: 20rpx;
  114. }
  115. }
  116. }
  117. }
  118. }
  119. .btn-group {
  120. padding: 0 30rpx;
  121. }
  122. </style>