// 上传文件
function uploadFile(url, filePath, name, formData, cb) {
wx.uploadFile({
url: ‘https://www.lenfshion.com/projectmng/‘ + url,
filePath: filePath,
name: name,
header: {
‘content-type‘: ‘multipart/form-data‘
}, // 设置请求的 header
formData: formData, // HTTP 请求中其他额外的 form data
success: function (res) {
if (res.statusCode == 200 && !res.data.result_code) {
return typeof cb == "function" && cb(res.data)
} else {
return typeof cb == "function" && cb(false)
}
},
fail: function () {
return typeof cb == "function" && cb(false)
}
})
}
changeImage: function () {
var that = this;
wx.chooseImage({
count: 1, // 默认9
sizeType: [‘original‘, ‘compressed‘], // 可以指定是原图还是压缩图,默认二者都有
sourceType: [‘album‘, ‘camera‘], // 可以指定来源是相册还是相机,默认二者都有
success: function (res) {
// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片,只有一张图片获取下标为0
var tempFilePaths = res.tempFilePaths[0];
that.setData({
actionSheetHidden: !that.data.actionSheetHidden
});
// that.setData({
// infoFace: tempFilePaths,
// actionSheetHidden: !that.data.actionSheetHidden
// });
// 图片截取
wx.navigateTo({
url: ‘../upload/upload?src=‘+tempFilePaths+‘‘
})
//
}
})
},
// 查看原图
viewImage: function () {
var that = this;
wx.previewImage({
current: ‘‘, // 当前显示图片的http链接
urls: [that.data.infoFace] // 需要预览的图片http链接列表
})
},
// 更换头像
actionSheetHidden: true, // 是否显示底部可选菜单
actionSheetItems: [
{ bindtap: ‘changeImage‘, txt: ‘修改头像‘ },
{ bindtap: ‘viewImage‘, txt: ‘查看头像‘ }
] // 底部可选菜单
// 点击头像 显示底部菜单
clickImage: function () {
var that = this;
that.setData({
actionSheetHidden: !that.data.actionSheetHidden
})
},
// 点击其他区域 隐藏底部菜单
actionSheetbindchange: function () {
var that = this;
that.setData({
actionSheetHidden: !that.data.actionSheetHidden
})
},
<action-sheet hidden="{{actionSheetHidden}}" bindchange="actionSheetbindchange">
<block wx:for="{{actionSheetItems}}" wx:key="unique">
<action-sheet-item bindtap="{{item.bindtap}}">{{item.txt}}</action-sheet-item>
</block>
<action-sheet-cancel class="cancel">取消</action-sheet-cancel>
</action-sheet>
if (this.data.isChooseImg){
uploadFile(‘appupload/upload‘, that.data.infoFace, ‘img‘, {}, function (res) {
if (null != res) {
var obj = JSON.parse(res);
if (obj) {
faceImg = obj.pd.files[0];
if (faceImg) {
that.setData({
infoFace: imgUrl + faceImg,
isChooseImg:false
})
}
}
} else {
// 显示消息提示框
wx.showToast({
title: ‘上传失败‘,
icon: ‘error‘,
duration: 2000
})
}
});
}