1.页面样式:
<view class='avatar_div' bindtap="chooseimage">
<image class="userinfo-avatar" src="{{avatar}}" mode="aspectFill">
</image>
<image class="widget-image" src='../../images/avatar.png' mode='aspectFill'></image>
</view>
2.js写法:
data: {
staffData: {},
avatar: '',
tempFilePaths: '',
fileData: {}
},
chooseimage: function() {
var _this = this;
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: function(res) {
_this.setData({
tempFilePaths: res.tempFilePaths
})
var tempFilePaths = res.tempFilePaths;
wx.uploadFile({
url: '文件服务器上传文件路径',
filePath: tempFilePaths[0],
name: 'file',
formData: {
'user': 'test'
},
header: {
'appid': app.appid
},
success: function(res) {
var data = JSON.parse(res.data)
if (data.errCode == 0) {
_this.setData({
fileData: data.data,
avatar: data.data.accessUrl
})
}
}
})
}
})
/*延时3秒调用接口*/
const timer = setInterval(() => {
var staffId = this.data.staffData.staffId;
var fileId = this.data.fileData.fileId;
var accessUrl = this.data.avatar;
http.post("/updateAvatar", {
staffId: staffId,
accessUrl: accessUrl,
fileId: fileId
}, function (err, res) {
if (res.errCode == 0) {
const toast = Toast.success('修改成功');
clearInterval(timer);
Toast.clear();
}
})
}, 3000)
}