my_user_detail.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <view class="modifyWrapper">
  3. <form>
  4. <view class="uni-forms-item">
  5. <button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
  6. <image class="avatar" :src="userinfo.avatar"></image>
  7. </button>
  8. </view>
  9. <view class="uni-forms-item">
  10. <input type="text" class="alias-input" maxlength="6" v-model="userinfo.alias" />
  11. </view>
  12. <view class="uni-forms-item">
  13. <button form-type="submit" class="alias-submit" type="primary" @click="formSubmit">确认修改</button>
  14. </view>
  15. <view class="uni-forms-item">
  16. <button class="alias-submit" type="primary" @click="forExit">退出登录</button>
  17. </view>
  18. </form>
  19. </view>
  20. </template>
  21. <script>
  22. import {
  23. mapMutations,
  24. mapState
  25. } from 'vuex'
  26. export default {
  27. computed: {
  28. ...mapState('m_user', ['userinfo'])
  29. },
  30. methods: {
  31. ...mapMutations('m_user', ['updateUserInfo', 'updateToken']),
  32. // 选择头像
  33. onChooseAvatar(e) {
  34. const {
  35. avatarUrl
  36. } = e.detail
  37. this.userinfo.avatar = avatarUrl
  38. },
  39. formSubmit() {
  40. // 上传头像
  41. wx.uploadFile({
  42. url: uni.$http.baseUrl + '/file/uploading',
  43. filePath: this.userinfo.avatar,
  44. name: 'file',
  45. header: {
  46. 'token': uni.getStorageSync('token')
  47. },
  48. success: res => {
  49. const result = JSON.parse(res.data)
  50. this.userinfo.avatar = result.data.url
  51. // 表单提交
  52. this.sendFormData()
  53. },
  54. fail: res => {
  55. if (res.errMsg === 'uploadFile:fail createUploadTask:fail file not found') {
  56. // 表单提交
  57. this.sendFormData()
  58. } else {
  59. uni.$showMsg('修改头像失败,请重新选择')
  60. }
  61. }
  62. })
  63. },
  64. // 表单提交
  65. sendFormData() {
  66. wx.request({
  67. url: uni.$http.baseUrl + '/ucenter/mini-program-openid-uid/wxAlias',
  68. method: 'GET',
  69. data: {
  70. alias: this.userinfo.alias,
  71. avatar: this.userinfo.avatar
  72. },
  73. header: {
  74. 'token': uni.getStorageSync('token') // 默认值
  75. },
  76. success: res1 => {
  77. uni.$showMsg('修改成功!', 3000)
  78. // 更新成功
  79. // 将数据更新到vuex中
  80. this.updateUserInfo(this.userinfo)
  81. }
  82. })
  83. },
  84. forExit() {
  85. this.updateToken("")
  86. // 跳转个人信息页面
  87. uni.switchTab({
  88. url: '/pages/my/my'
  89. })
  90. }
  91. }
  92. }
  93. </script>
  94. <!-- 设置页面背景 -->
  95. <style lang="scss">
  96. page{
  97. height: 100%;
  98. padding: 0 20rpx;
  99. // background-color: #FFF;
  100. }
  101. </style>
  102. <style lang="scss" scoped>
  103. .modifyWrapper{
  104. width: 95%;
  105. height: 100%;
  106. }
  107. .alias-input {
  108. height: 100rpx;
  109. font-size: 40rpx;
  110. // border: 1px solid #aaa;
  111. border-radius: 30rpx;
  112. padding-left: 10rpx;
  113. margin-top: 20rpx;
  114. background-color: white;
  115. }
  116. .alias-submit {
  117. margin-top: 50rpx;
  118. width: 300rpx;
  119. border-radius: 50rpx;
  120. background-color: #35b882;
  121. }
  122. </style>