info-edit.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <page-layout 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. </page-layout>
  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. import PageLayout from "@/components/layout/page-layout";
  36. export default {
  37. options: {
  38. styleIsolation: 'shared'
  39. },
  40. components: {
  41. PageLayout,
  42. NavBar,
  43. UScrollView,
  44. VanCellGroup,
  45. VanCell,
  46. VanField
  47. },
  48. computed: {
  49. },
  50. data() {
  51. return {
  52. redirectUrl: '',
  53. avatar: '',
  54. nick_name: '',
  55. title: '',
  56. company_name: '',
  57. saveLoading: false
  58. }
  59. },
  60. onLoad(options) {
  61. console.log(options)
  62. if (options.avatar) {
  63. this.avatar = decodeURIComponent(options.avatar)
  64. } else {
  65. this.avatar = ''
  66. }
  67. if (options.redirect) {
  68. this.redirectUrl = decodeURIComponent(options.redirect)
  69. } else {
  70. this.redirectUrl = ''
  71. }
  72. },
  73. created() {
  74. this.checkAuth('/pages/user/info-edit')
  75. },
  76. onShow() {
  77. this.$store.dispatch('getInfo').then(res => {
  78. this.nick_name = res.data.nick_name
  79. this.company_name = res.data.company_name
  80. this.title = res.data.title
  81. if (!this.avatar) {
  82. this.avatar = res.data.avatar
  83. }
  84. })
  85. },
  86. methods: {
  87. onChooseAvatar(e) {
  88. this.avatar = e.detail.avatarUrl
  89. },
  90. onSubmit(e) {
  91. this.saveLoading = true
  92. if (this.avatar.indexOf('http://tmp') === 0 || this.avatar.indexOf('wxfile') === 0) {
  93. uploadFile({
  94. filePath: this.avatar,
  95. }).then(res => {
  96. console.log("上传微信头像:", res)
  97. if (res.data) {
  98. this.avatar = res.data.url
  99. this.doUpdateInfo()
  100. }
  101. }).then(error => {
  102. this.saveLoading = false
  103. })
  104. } else {
  105. this.doUpdateInfo()
  106. }
  107. },
  108. onFieldChange(field, value) {
  109. this[field] = value
  110. },
  111. doUpdateInfo() {
  112. if (!this.nick_name) {
  113. this.showToast('请填写呢称')
  114. return
  115. }
  116. updateInfo({
  117. avatar: this.avatar,
  118. nick_name: this.nick_name,
  119. title: this.title,
  120. company_name: this.company_name
  121. }).then(() => {
  122. this.$store.dispatch('getInfo')
  123. this.saveLoading = false
  124. this.showSuccessToast('保存成功', () => {
  125. if (this.redirectUrl) {
  126. this.redirectTo(this.redirectUrl)
  127. } else {
  128. const pages = getCurrentPages()
  129. if (pages.length > 1) {
  130. const parentPagePath = pages[pages.length - 2].$page.fullPath
  131. if (parentPagePath === '/pages/user/info') {
  132. uni.navigateBack()
  133. return
  134. }
  135. }
  136. this.redirectTo('/pages/user/index')
  137. }
  138. })
  139. })
  140. }
  141. }
  142. }
  143. </script>
  144. <style lang="scss">
  145. .user-info-edit{
  146. .right-icon{
  147. position: relative;
  148. button::after {
  149. content: unset;
  150. }
  151. .btn-choose{
  152. position: absolute;
  153. background-color: transparent;
  154. width: 100%;
  155. height: 100%;
  156. top: 0;
  157. left: 0;
  158. z-index: 2;
  159. }
  160. }
  161. .user-info-submit{
  162. width: 400rpx;
  163. margin: 60rpx auto 0 auto;
  164. }
  165. .van-cell__title{
  166. max-width: 180rpx!important;
  167. min-width: 180rpx!important;
  168. margin-right: 24rpx!important;
  169. }
  170. .user-avatar-cell{
  171. .van-cell__title{
  172. flex: unset;
  173. }
  174. .right-icon{
  175. display: flex;
  176. justify-content: space-between;
  177. flex: 1;
  178. }
  179. }
  180. .user-avator{
  181. @include display-flex-center;
  182. width: 111rpx;
  183. height: 111rpx;
  184. border-radius: 50%;
  185. overflow: hidden;
  186. margin-right: 13rpx;
  187. .icon{
  188. color: #ffffff;
  189. opacity: 0.67;
  190. font-size: 70rpx;
  191. }
  192. }
  193. }
  194. </style>