editAvatar.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <view class="content">
  3. <view @click="upload" class="picture">
  4. <image class="xj-image" v-if="!deptListurl.avatarUrl" src="@/static/images/common/tianjiazhaopian@2x.png">
  5. </image>
  6. <image class="xj-image" :src="deptListurl.avatarUrl" v-if="deptListurl.avatarUrl"></image>
  7. </view>
  8. <view @click="submit" class="submit">提交</view>
  9. <u-action-sheet :actions="$helper.imgTypeList" :title="$helper.imgType" :show="isShowimgType"
  10. @select="imgTypeSelect" :closeOnClickOverlay="true" :closeOnClickAction="true" @close="isShowimgType=false">
  11. </u-action-sheet>
  12. </view>
  13. </template>
  14. <script>
  15. import uploadImage from '@/components/ossutil/uploadFile.js';
  16. import {
  17. mapState
  18. } from 'vuex';
  19. var that
  20. export default {
  21. data() {
  22. return {
  23. isShowimgType: false,
  24. imgSrc: '',
  25. deptListurl: {
  26. avatarUrl: '',
  27. id: ''
  28. }
  29. }
  30. },
  31. onLoad() {
  32. that = this
  33. },
  34. computed: {
  35. ...mapState(['hasLogin', 'userInfo'])
  36. },
  37. methods: {
  38. upload() {
  39. this.isShowimgType = true
  40. },
  41. imgTypeSelect(val) {
  42. console.log(val)
  43. if (val.name == '相册') {
  44. uni.chooseImage({
  45. count: 1,
  46. sourceType: that.$helper.chooseImage.sourceType,
  47. success: function(res) {
  48. console.log(JSON.stringify(res.tempFilePaths));
  49. uploadImage(res.tempFilePaths[0], 'appData/',
  50. result => {
  51. // 上传成功
  52. console.log('图片地址', result)
  53. that.$request.baseRequest('get', '/BDAccessToken/getAuth', {
  54. apiKey: 'yNWLRW6unoUxo2KXiCZxOxGS',
  55. secretKey: 'E8hqzKWdFdTGnFnpAwZ7ixtL89SN3deR'
  56. }).then(res => {
  57. console.log(res)
  58. that.$request.baseRequest('post',
  59. '/commonUser/getUserDefinedBean', {
  60. accessToken: res.data,
  61. imagePath: result
  62. }).then(res1 => {
  63. that.deptListurl.avatarUrl = result
  64. })
  65. .catch(res => {
  66. uni.$u.toast(res.message);
  67. });
  68. })
  69. .catch(res => {
  70. uni.$u.toast(res.message);
  71. });
  72. }
  73. )
  74. }
  75. });
  76. } else {
  77. }
  78. },
  79. submit() {
  80. this.deptListurl.id = this.userInfo.id
  81. this.$request.baseRequest('post', '/commonUser/editUserInfo', this.deptListurl).then(res => {
  82. uni.showToast({
  83. title: '修改成功!',
  84. icon: 'success',
  85. duration: 2000,
  86. success() {
  87. setTimeout(() => {
  88. that.userInfo.avatarUrl = that.deptListurl.avatarUrl
  89. var _student = uni.getStorageSync('userInfo');
  90. _student.avatarUrl = that.deptListurl.avatarUrl;
  91. uni.setStorageSync('userInfo', _student);
  92. var name = 'userInfo';
  93. var value = _student;
  94. that.$store.commit('$uStore', {
  95. name,
  96. value
  97. });
  98. this.$refs.uToast.show({
  99. type: 'success',
  100. message: "修改成功!",
  101. })
  102. uni.navigateBack({
  103. delta: 1
  104. })
  105. }, 2000)
  106. }
  107. })
  108. })
  109. .catch(res => {
  110. uni.$u.toast(res.message);
  111. });
  112. }
  113. }
  114. }
  115. </script>
  116. <style>
  117. .content {
  118. background: white;
  119. padding: 20rpx;
  120. }
  121. .left-text {
  122. width: 290rpx;
  123. color: #333333;
  124. display: flex;
  125. align-items: center;
  126. }
  127. .picture {
  128. margin-top: 20rpx;
  129. background: #F5F6FA;
  130. width: 212rpx;
  131. height: 212rpx;
  132. border-radius: 10rpx;
  133. display: flex;
  134. flex-direction: column;
  135. justify-content: center;
  136. align-items: center;
  137. color: #6A7282;
  138. }
  139. .xj-image {
  140. width: 100rpx;
  141. height: 100rpx;
  142. }
  143. .submit {
  144. margin-top: 20rpx;
  145. background: #2772FB;
  146. border-radius: 50rpx;
  147. padding: 20rpx 0;
  148. color: white;
  149. text-align: center;
  150. }
  151. </style>