userInfo.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <script>
  2. import { mapState } from 'vuex'
  3. import api from '@/api'
  4. export default {
  5. data() {
  6. return {
  7. tempImg: '',
  8. form: {
  9. avatar: '',
  10. nickname: ''
  11. }
  12. }
  13. },
  14. computed: {
  15. ...mapState({
  16. // 这里需使用普通函数,使 this 为vc
  17. user(state) {
  18. const { avatar, nickname } = state.user
  19. this.form.avatar = avatar
  20. this.form.nickname = nickname
  21. return state.user
  22. },
  23. token: 'token'
  24. })
  25. },
  26. methods: {
  27. selectAvatar() {
  28. uni.chooseImage({
  29. count: 1,
  30. sizeType: ['compressed'], // 只传压缩图
  31. crop: {
  32. width: 100,
  33. height: 100
  34. },
  35. success: res => (this.tempImg = res.tempFilePaths[0])
  36. })
  37. },
  38. async uploadImg() {
  39. try {
  40. let res = await api.uploadImg({ filePath: this.tempImg, formData: { type: 'avatar' } })
  41. let result = JSON.parse(res)
  42. this.form.avatar = result.data.base_url
  43. return true
  44. } catch (e) {
  45. return false
  46. }
  47. },
  48. async save() {
  49. // 存在新的头像 但未上传成功时拦截
  50. if (this.tempImg && !(await this.uploadImg())) return
  51. let params = JSON.parse(JSON.stringify(this.form))
  52. if (this.tempImg === '') delete params.avatar
  53. await api.editUserInfo(params)
  54. this.$common.toast('修改成功')
  55. setTimeout(() => {
  56. this.$common.back()
  57. }, 1000)
  58. }
  59. },
  60. mounted() {
  61. uni.$on('editName', name => (this.form.nickname = name))
  62. }
  63. }
  64. </script>
  65. <template>
  66. <view>
  67. <u-navbar title="个人资料" placeholder autoBack leftIconSize="40rpx"></u-navbar>
  68. <view class="bg-white flex flex-column flex-center pt-20 relative">
  69. <u-avatar :src="tempImg || form.avatar" size="132rpx" @click="selectAvatar"></u-avatar>
  70. <u-text
  71. text="点击修改头像"
  72. color="#8D9399"
  73. size="24rpx"
  74. align="center"
  75. margin="20rpx 0"
  76. ></u-text>
  77. <view class="badge flex flex-center" @click="selectAvatar">
  78. <u-icon name="camera" color="#fff" size="28rpx"></u-icon>
  79. </view>
  80. </view>
  81. <view class="radius bg-white w-m mt-10 group">
  82. <view class="item">
  83. <view class="title f28">昵称</view>
  84. <u-icon
  85. name="arrow-right"
  86. color="#8D9399"
  87. :label="form.nickname"
  88. labelPos="left"
  89. labelColor="#8D9399"
  90. labelSize="28rpx"
  91. @click="$jump({ url: '/pagesUser/userInfo/editName' })"
  92. ></u-icon>
  93. </view>
  94. <view class="item">
  95. <view class="title f28">ID</view>
  96. <view class="value">{{ user.id }}</view>
  97. </view>
  98. <view class="item">
  99. <view class="title f28">用户名</view>
  100. <view class="value">{{ user.user_name }}</view>
  101. </view>
  102. <view class="item">
  103. <view class="title f28">手机号码</view>
  104. <view class="value">{{ user.mobile }}</view>
  105. </view>
  106. </view>
  107. <u-button
  108. text="保存"
  109. color="var(--theme)"
  110. @click="save"
  111. shape="circle"
  112. customStyle="width: 650rpx;height: 98rpx; position:fixed;bottom:100rpx;left:50%;transform:translate(-50%)"
  113. ></u-button>
  114. </view>
  115. </template>
  116. <style lang="scss" scoped>
  117. .badge {
  118. position: absolute;
  119. width: 36rpx;
  120. height: 36rpx;
  121. background-color: #637bf7;
  122. border-radius: 50%;
  123. left: calc(50% + 40rpx);
  124. top: calc(50% - 70rpx);
  125. }
  126. .group {
  127. padding: 0 24rpx;
  128. .item {
  129. display: flex;
  130. justify-content: space-between;
  131. align-items: center;
  132. height: 98rpx;
  133. line-height: 98rpx;
  134. box-sizing: border-box;
  135. .title {
  136. color: #232323;
  137. font-weight: bold;
  138. font-size: 32rpx;
  139. }
  140. .value {
  141. color: #5d6167;
  142. font-size: 30rpx;
  143. }
  144. }
  145. }
  146. </style>