抖音小程序授权获取到了个人信息,昵称+头像等,当需要上传抖音的头像的时候,直接使用uni.uploadFile或者tt.uploadFile是不行的,因为在字节小程序中,由于安全策略的限制,你不能直接获取用户头像的文件路径,因此无法直接使用文件路径进行上传操作。所以这里先下载了再上传
具体如下
// 抖音修改个人头像
getTtuserinfo(){
let that = this
tt.getUserProfile({
success(res) {
const userInfo = res.userInfo;
const avatarUrl = userInfo.avatarUrl;
that.ttuploadAvatarToServer(avatarUrl)
},
fail(resd) {
console.log("getUserProfile 调用失败", resd);
},
});
},
// 抖音下载头像,再上传服务器
ttuploadAvatarToServer(avatarData){
// 先下载
tt.downloadFile({
url: avatarData,
header: {
"content-type": "application/json",
},
success: (res) => {
// res.tempFilePath 便是图片路径
tt.uploadFile({
url: "https://XXXX.XXX.XX/api/common/upload",
filePath: res.tempFilePath,
name: "file",
header: {
token: uni.getStorageSync('token')
},
success: async (resx) => {
var _res = JSON.parse(resx.data)
if (_res.code == 0) {
uni.showToast({
title: _res.msg,
icon: "none"
})
} else {
// 更新数据
await this.updata(resx)
}
},
fail: (rese) => {
console.log("tt.uploadFile fail", rese);
}
});
},
fail: (res) => {
console.log("downloadFile fail", res);
},
});
},
就酱~~~