info-edit.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <template>
  2. <view class="user-info-edit">
  3. <nav-bar title="个人信息" @init="onInitNavbar"></nav-bar>
  4. <u-scroll-view>
  5. <form @submit="onSubmit">
  6. <van-cell-group>
  7. <van-cell title="头像" class="user-avatar-cell van-cell--clickable">
  8. <view class="right-icon" slot="right-icon">
  9. <button class="btn-choose" open-type="chooseAvatar" @chooseavatar="onChooseAvatar"></button>
  10. <view class="user-avator">
  11. <image v-if="avatar" :src="avatar" mode="aspectFill" />
  12. <image v-else src="https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132" mode="aspectFill" />
  13. </view>
  14. <van-button type="primary" plain>点击修改</van-button>
  15. </view>
  16. </van-cell>
  17. <van-field required type="nickname" name="nickname" :value="nick_name" placeholder="请输入呢称" :border="false" label="呢称" clearable @change="(e) => onFieldChange('nick_name', e)" />
  18. <van-field :value="company_name" placeholder="请输入公司" :border="false" label="公司" clearable @change="(e) => onFieldChange('company_name', e)" />
  19. <van-field :value="title" placeholder="请输入职业" :border="false" label="职业" clearable @change="(e) => onFieldChange('title', e)" />
  20. </van-cell-group>
  21. <view class="user-info-submit">
  22. <van-button type="primary" form-type="submit" :loading="saveLoading" size="large">保存</van-button>
  23. </view>
  24. </form>
  25. </u-scroll-view>
  26. </view>
  27. </template>
  28. <script>
  29. import NavBar from '@/components/layout/nav-bar'
  30. import UScrollView from '@/components/common/u-scroll-view'
  31. import VanCellGroup from '@/wxcomponents/vant/cell-group/index'
  32. import VanCell from '@/wxcomponents/vant/cell/index'
  33. import VanField from '@/wxcomponents/vant/field/index'
  34. import { updateInfo, uploadFile } from '@/api/user'
  35. export default {
  36. options: {
  37. styleIsolation: 'shared'
  38. },
  39. components: {
  40. NavBar,
  41. UScrollView,
  42. VanCellGroup,
  43. VanCell,
  44. VanField
  45. },
  46. computed: {
  47. },
  48. data() {
  49. return {
  50. redirectUrl: '',
  51. avatar: '',
  52. nick_name: '',
  53. title: '',
  54. company_name: '',
  55. saveLoading: false
  56. }
  57. },
  58. onLoad(options) {
  59. console.log(options)
  60. if (options.avatar) {
  61. this.avatar = decodeURIComponent(options.avatar)
  62. } else {
  63. this.avatar = ''
  64. }
  65. if (options.redirect) {
  66. this.redirectUrl = decodeURIComponent(options.redirect)
  67. } else {
  68. this.redirectUrl = ''
  69. }
  70. },
  71. created() {
  72. this.checkAuth('/pages/user/info-edit')
  73. },
  74. onShow() {
  75. this.$store.dispatch('getInfo').then(res => {
  76. this.nick_name = res.data.nick_name
  77. this.company_name = res.data.company_name
  78. this.title = res.data.title
  79. if (!this.avatar) {
  80. this.avatar = res.data.avatar
  81. }
  82. })
  83. },
  84. methods: {
  85. onChooseAvatar(e) {
  86. this.avatar = e.detail.avatarUrl
  87. },
  88. onSubmit(e) {
  89. this.saveLoading = true
  90. if (this.avatar.indexOf('http://tmp') === 0 || this.avatar.indexOf('wxfile') === 0) {
  91. uploadFile({
  92. filePath: this.avatar,
  93. }).then(res => {
  94. console.log("上传微信头像:", res)
  95. if (res.data) {
  96. this.avatar = res.data.url
  97. this.doUpdateInfo()
  98. }
  99. }).then(error => {
  100. this.saveLoading = false
  101. })
  102. } else {
  103. this.doUpdateInfo()
  104. }
  105. },
  106. onFieldChange(field, value) {
  107. this[field] = value
  108. },
  109. doUpdateInfo() {
  110. if (!this.nick_name) {
  111. this.showToast('请填写呢称')
  112. return
  113. }
  114. updateInfo({
  115. avatar: this.avatar,
  116. nick_name: this.nick_name,
  117. title: this.title,
  118. company_name: this.company_name
  119. }).then(() => {
  120. this.$store.dispatch('getInfo')
  121. this.saveLoading = false
  122. this.showSuccessToast('保存成功', () => {
  123. if (this.redirectUrl) {
  124. this.redirectTo(this.redirectUrl)
  125. } else {
  126. const pages = getCurrentPages()
  127. if (pages.length > 1) {
  128. const parentPagePath = pages[pages.length - 2].$page.fullPath
  129. if (parentPagePath === '/pages/user/info') {
  130. uni.navigateBack()
  131. return
  132. }
  133. }
  134. this.redirectTo('/pages/user/index')
  135. }
  136. })
  137. })
  138. }
  139. }
  140. }
  141. </script>
  142. <style lang="scss">
  143. .user-info-edit{
  144. .right-icon{
  145. position: relative;
  146. button::after {
  147. content: unset;
  148. }
  149. .btn-choose{
  150. position: absolute;
  151. background-color: transparent;
  152. width: 100%;
  153. height: 100%;
  154. top: 0;
  155. left: 0;
  156. z-index: 2;
  157. }
  158. }
  159. .user-info-submit{
  160. width: 400rpx;
  161. margin: 60rpx auto 0 auto;
  162. }
  163. .van-cell__title{
  164. max-width: 180rpx!important;
  165. min-width: 180rpx!important;
  166. margin-right: 24rpx!important;
  167. }
  168. .user-avatar-cell{
  169. .van-cell__title{
  170. flex: unset;
  171. }
  172. .right-icon{
  173. display: flex;
  174. justify-content: space-between;
  175. flex: 1;
  176. }
  177. }
  178. .user-avator{
  179. @include display-flex-center;
  180. width: 111rpx;
  181. height: 111rpx;
  182. border-radius: 50%;
  183. overflow: hidden;
  184. margin-right: 13rpx;
  185. .icon{
  186. color: #ffffff;
  187. opacity: 0.67;
  188. font-size: 70rpx;
  189. }
  190. }
  191. }
  192. </style>