editAvatar.vue 4.6 KB

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