profile.vue 3.1 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 class="info">
  14. <u--input maxlength="10" :border="borderStyle" v-model="userInfo.nickname" @focus="borderStyle = 'bottom'" inputAlign="right" @blur="borderStyle = 'none'" @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. borderStyle: 'none'
  42. }
  43. },
  44. computed: {
  45. nameUpdateVisible: function () {
  46. return this.userInfo.nickname !== this.tempName
  47. }
  48. },
  49. onLoad() {
  50. this.loadUserInfoData()
  51. },
  52. methods: {
  53. loadUserInfoData() {
  54. getUserInfo().then(res => {
  55. this.userInfo = res.data
  56. this.tempName = this.userInfo.nickname
  57. })
  58. },
  59. handleAvatarClick() {
  60. uni.chooseImage({
  61. success: chooseImageRes => {
  62. const tempFilePaths = chooseImageRes.tempFilePaths
  63. updateAvatar(tempFilePaths[0]).then(res => {
  64. this.userInfo.avatar = res.data
  65. this.$store.commit('SET_USER_INFO', this.userInfo)
  66. })
  67. }
  68. })
  69. },
  70. handleNameChange(val) {
  71. let str = uni.$u.trim(val, 'all')
  72. this.$nextTick(() => {
  73. this.userInfo.nickname = str
  74. })
  75. },
  76. handleSaveBtnClick() {
  77. updateNickname({ nickname: this.userInfo.nickname }).then(res => {
  78. this.tempName = this.userInfo.nickname
  79. this.$store.commit('SET_USER_INFO', this.userInfo)
  80. uni.$u.toast('已保存')
  81. setTimeout(() => {
  82. uni.switchTab({
  83. url: '/pages/user/user'
  84. })
  85. }, 300)
  86. })
  87. }
  88. }
  89. }
  90. </script>
  91. <style lang="scss" scoped>
  92. .user-info {
  93. .info-item {
  94. padding: 20rpx 60rpx;
  95. border-bottom: $custom-border-style;
  96. @include flex-space-between;
  97. .label {
  98. font-size: 30rpx;
  99. }
  100. .info {
  101. @include flex-left;
  102. .value {
  103. font-size: 30rpx;
  104. }
  105. .btn {
  106. margin-left: 30rpx;
  107. }
  108. }
  109. .name-edit {
  110. @include flex-left;
  111. .edit-btn-group {
  112. @include flex;
  113. .edit-btn {
  114. margin-left: 20rpx;
  115. }
  116. }
  117. }
  118. }
  119. }
  120. .btn-group {
  121. padding: 0 30rpx;
  122. }
  123. </style>