Commit 26d10e0b3ce004d0376dc2abe0f81c07cd39c704

Authored by fengtao
1 parent bbe91817

fix:修改个人资料上传头像

Showing 1 changed file with 28 additions and 14 deletions
... ... @@ -49,7 +49,7 @@
49 49 <view class="item" v-if="info.data.isThirdLogin">
50 50 <button class="submit" size="default" @click="clearAccountFunc" :style="{ background: InfoColor }"><text class="un-bind-text">解绑</text></button>
51 51 </view>
52   - <view class="item" style="margin-right: 60rpx;" :style="[{ position: info.data.isThirdLogin ? '' : 'relative' }, { right: info.data.isThirdLogin ? '' : '-158rpx' }]">
  52 + <view class="item" style="margin-right: 60rpx;" :style="[{ position: info.data.isThirdLogin ? '' : 'relative' }, { right: info.data.isThirdLogin ? '' : '-190rpx' }]">
53 53 <button class="submit" size="default" @click="onSubmitFunc" :style="{ background: PrimaryColor }"><text class="un-bind-text">确认</text></button>
54 54 </view>
55 55 </view>
... ... @@ -72,6 +72,8 @@
72 72
73 73 <script>
74 74 import { mapMutations } from 'vuex';
  75 +import baseUrl from '@/config/baseUrl.js';
  76 +import { mapState } from 'vuex';
75 77
76 78 export default {
77 79 data() {
... ... @@ -116,6 +118,9 @@ export default {
116 118 this.openIds = getOpenId;
117 119 }
118 120 },
  121 + computed: {
  122 + ...mapState(['userInfo'])
  123 + },
119 124 methods: {
120 125 ...mapMutations(['setUserInfo']),
121 126 confrimBind(e) {
... ... @@ -144,8 +149,13 @@ export default {
144 149 this.showBind = true;
145 150 },
146 151 // 修改头像
147   - upAvatar() {
148   - var that = this;
  152 + async upAvatar() {
  153 + let token;
  154 + token = this.userInfo.isToken || uni.getStorageSync('userInfo').isToken || undefined;
  155 + // #ifdef H5
  156 + token = window.sessionStorage.getItem('userInfo').isToken;
  157 + // #endif
  158 + if (!token) return uni.$u.toast('请登录后上传图片');
149 159 uni.chooseImage({
150 160 count: 1,
151 161 sourceType: ['camera', 'album'],
... ... @@ -174,17 +184,21 @@ export default {
174 184 });
175 185 return;
176 186 }
177   - uni.$u.http
178   - .upload('/yt/oss/upload', {
179   - filePath: tempFilePaths[0],
180   - name: 'file'
181   - })
182   - .then(res => {
183   - if (res) {
184   - that.avatar = res.fileStaticUri;
185   - return uni.$u.toast('头像上传成功');
186   - }
187   - });
  187 + uni.uploadFile({
  188 + url: `${baseUrl.baseUrl}/yt/oss/upload`,
  189 + filePath: tempFilePaths[0],
  190 + name: 'file',
  191 + header: {
  192 + 'content-type': 'multipart/form-data',
  193 + Authorization: 'Bearer ' + token
  194 + },
  195 + formData: {},
  196 + success: res => {
  197 + let objImage = JSON.parse(res.data);
  198 + this.avatar = objImage.fileStaticUri;
  199 + uni.$u.toast('头像上传成功');
  200 + }
  201 + });
188 202 }
189 203 });
190 204 },
... ...