1.在app.vue文件中判斷小程序版本并存在緩存中,代碼如下:
const version = uni.getSystemInfoSync().SDKVersion
if (Routine.compareVersion(version, '2.21.2') >= 0) {
that.$Cache.set('MP_VERSION_ISNEW', true)
} else {
that.$Cache.set('MP_VERSION_ISNEW', false)
}
2./pages/users/user_info.vue頁面取出存儲的版本信息,代碼如下:
mp_is_new: this.$Cache.get('MP_VERSION_ISNEW') || false
上傳頭像那里判斷是否是新版本的小程序,如果不是則還是以前的邏輯,是的話用open-type="chooseAvatar",@chooseavatar="onChooseAvatar"獲取到臨時頭像,然后自己存儲頭像并上傳:
拿到臨時頭像上傳后再調用修改頭像接口,代碼如下:
onChooseAvatar(e) {
const {avatarUrl} = e.detail
this.$util.uploadImgs('upload/image', avatarUrl, (res) => {
this.userInfo.avatar = res.data.path
editAvatar({avatar:res.data.path}).then((res)=>{
that.$util.Tips({
title:res.message,
})
})
}, (err) => {
console.log(err)
})
},
本地上傳圖片的方法要按照下圖修改一下,文件路徑為:utils/util.js
uploadImgs(uploadUrl, filePath, successCallback, errorCallback) {
let that = this;
let inputName = 'pics';
uni.uploadFile({
url: HTTP_REQUEST_URL + '/api/' + uploadUrl + '/' + inputName,
filePath: filePath,
fileType: 'image',
name: 'pics',
formData: {
'filename': 'pics'
},
header: {
// #ifdef MP
"Content-Type": "multipart/form-data",
// #endif
[TOKENNAME]: 'Bearer ' + store.state.app.token
},
success: (res) => {
uni.hideLoading();
if (res.statusCode == 403) {
that.Tips({
title: res.data
});
} else {
let data = res.data ? JSON
.parse(res.data) : {};
if (data.status == 200) {
successCallback &&
successCallback(data)
} else {
errorCallback &&
errorCallback(data);
that.Tips({
title: data.message
});
}
}
},
fail: (err) => {
console.log(err)
uni.hideLoading();
that.Tips({
title: i18n.t(`上傳圖片失敗`)
});
}
})
},
這里就修改完成了,在需要重新打包上傳代碼到服務器上就可以了。