editAvatar.vue 4.7 KB

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