editAvatar.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. // sourceType: that.$helper.chooseImage.sourceType,
  48. success: function(res) {
  49. uploadImage('image',res.tempFilePaths[0], 'appData/',
  50. result => {
  51. // 上传成功
  52. that.$request.baseRequest('get', '/BDAccessToken/getAuth', {
  53. apiKey: 'yNWLRW6unoUxo2KXiCZxOxGS',
  54. secretKey: 'E8hqzKWdFdTGnFnpAwZ7ixtL89SN3deR'
  55. }).then(res => {
  56. that.$request.baseRequest('post',
  57. '/commonUser/getUserDefinedBean', {
  58. accessToken: res.data,
  59. imagePath: result
  60. }).then(res1 => {
  61. that.deptListurl.avatarUrl = result
  62. })
  63. .catch(res => {
  64. uni.$u.toast(res.message);
  65. });
  66. })
  67. .catch(res => {
  68. uni.$u.toast(res.message);
  69. });
  70. }
  71. )
  72. }
  73. });
  74. // } else {
  75. // uni.chooseImage({
  76. // count: 1,
  77. // sourceType: ['camera'],
  78. // success: function(res) {
  79. // console.log(JSON.stringify(res.tempFilePaths));
  80. // uploadImage(res.tempFilePaths[0], 'appData/',
  81. // result => {
  82. // // 上传成功
  83. // console.log('图片地址', result)
  84. // that.$request.baseRequest('get', '/BDAccessToken/getAuth', {
  85. // apiKey: 'yNWLRW6unoUxo2KXiCZxOxGS',
  86. // secretKey: 'E8hqzKWdFdTGnFnpAwZ7ixtL89SN3deR'
  87. // }).then(res => {
  88. // console.log(res)
  89. // that.$request.baseRequest('post',
  90. // '/commonUser/getUserDefinedBean', {
  91. // accessToken: res.data,
  92. // imagePath: result
  93. // }).then(res1 => {
  94. // that.deptListurl.avatarUrl = result
  95. // })
  96. // .catch(res => {
  97. // uni.$u.toast(res.message);
  98. // });
  99. // })
  100. // .catch(res => {
  101. // uni.$u.toast(res.message);
  102. // });
  103. // }
  104. // )
  105. // }
  106. // });
  107. // }
  108. },
  109. submit() {
  110. this.deptListurl.id = this.userInfo.id
  111. this.$request.baseRequest('post', '/commonUser/editUserInfo', this.deptListurl).then(res => {
  112. uni.showToast({
  113. title: '修改成功!',
  114. icon: 'success',
  115. duration: 2000,
  116. success() {
  117. setTimeout(() => {
  118. that.userInfo.avatarUrl = that.deptListurl.avatarUrl
  119. var _student = uni.getStorageSync('userInfo');
  120. _student.avatarUrl = that.deptListurl.avatarUrl;
  121. uni.setStorageSync('userInfo', _student);
  122. var name = 'userInfo';
  123. var value = _student;
  124. that.$store.commit('$uStore', {
  125. name,
  126. value
  127. });
  128. that.$refs.uToast.show({
  129. type: 'success',
  130. message: "修改成功!",
  131. })
  132. uni.navigateBack({
  133. delta: 1
  134. })
  135. }, 2000)
  136. }
  137. })
  138. })
  139. .catch(res => {
  140. uni.$u.toast(res.message);
  141. });
  142. }
  143. }
  144. }
  145. </script>
  146. <style>
  147. .content {
  148. background: white;
  149. padding: 20rpx;
  150. }
  151. .left-text {
  152. width: 290rpx;
  153. color: #333333;
  154. display: flex;
  155. align-items: center;
  156. }
  157. .picture {
  158. margin-top: 20rpx;
  159. background: #F5F6FA;
  160. width: 212rpx;
  161. height: 212rpx;
  162. border-radius: 10rpx;
  163. display: flex;
  164. flex-direction: column;
  165. justify-content: center;
  166. align-items: center;
  167. color: #6A7282;
  168. }
  169. .xj-image {
  170. width: 100rpx;
  171. height: 100rpx;
  172. }
  173. .submit {
  174. margin-top: 20rpx;
  175. background: #2772FB;
  176. border-radius: 50rpx;
  177. padding: 20rpx 0;
  178. color: white;
  179. text-align: center;
  180. }
  181. </style>