Commit d4e185ac authored by 黄奎's avatar 黄奎

11

parent a7d2884d
<template>
<view class="upImg row justify-center items-center" @click="chooseImg">
<u-icon name="plus" color="#5B5B5B" size="42"/>
</view>
<view class="upImg row justify-center items-center" @click="chooseImg">
<u-icon name="plus" color="#5B5B5B" size="42" />
</view>
</template>
<script>
export default {
props: [],
components: { },
data() {
return {
}
},
onLoad(options) {
},
onShow() {
export default {
props: [],
components: {},
data() {
return {
},
mounted() {
},
methods: {
chooseImg() {
let that = this;
uni.chooseImage({
count: 1, //默认9
sizeType: ["original", "compressed"], //可以指定是原图还是压缩图,默认二者都有
sourceType: ["album"], //从相册选择
success: function (res) {
wx.getFileSystemManager().readFile({
filePath: res.tempFilePaths[0],
encoding: "base64", //编码格式
success: (ans) => {
uni.showLoading({
title: "上传中",
});
uni.getImageInfo({
src: res.tempFilePaths[0],
success: function(image) {
that.upFile(image.path, (uploadRes) => {
let BusinessLicense = JSON.parse(
uploadRes.data
);
let infor = {
url: '',
name: ''
}
const tempFilePath = BusinessLicense.FileName;
infor.name = tempFilePath;
infor.url = that.hostErpFile+BusinessLicense.FilePath
that.$emit('onSuccess', infor);
},err=>{
uni.hideLoading()
});
console.log("res.tempFilePaths[0]",res.tempFilePaths[0]);
}
});
},
});
},
});
},
//图片上传
upFile(filePath,resCall) {
let that = this;
let MallBaseId = uni.getStorageSync("mall_UserInfo").MallBaseId ? uni.getStorageSync("mall_UserInfo").MallBaseId : 1;
let path = "/Upload/Temporary/"
let action = this.hostErpUpload+`/Upload/Index?filePath=${path}&ocr=1`
uni.uploadFile({
url: action,
filePath: filePath,
name: 'file',
formData: {
user: 'tesdt'
},
success: (uploadFileRes) => {
if (uploadFileRes.statusCode == 200) {
uni.hideLoading()
resCall(uploadFileRes);
}else{
wx.showToast({
title: uploadFileRes.message,
icon: 'none',
duration: 1000
})
}
}
},
onLoad(options) {
},
fail: function(err) {
console.log(err)
wx.showToast({
title: '上传失败!'+JSON.stringify(err),
icon: 'none',
duration: 1000
})
}
});
},
}
}
},
onShow() {
},
mounted() {
},
methods: {
chooseImg() {
const that = this;
uni.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success(res) {
const src = res.tempFilePaths[0];
const ext = src.split('.').pop().toLowerCase();
if (ext === 'png') {
// PNG文
that.UploadFileMethod(src);
} else {
//其他格式(jpg、jpeg、webp)
uni.compressImage({
src,
quality: 75,
success(compRes) {
that.UploadFileMethod(compRes.tempFilePath);
},
fail(err) {
console.log(ext + '格式compressImage失败,使用原图上传', err);
that.UploadFileMethod(src);
}
});
}
}
});
},
UploadFileMethod(filePath) {
const that = this;
uni.showLoading({
title: "上传中",
});
that.upFile(filePath, (uploadRes) => {
let BusinessLicense = JSON.parse(
uploadRes.data
);
let infor = {
url: '',
name: ''
}
const tempFilePath = BusinessLicense.FileName;
infor.name = tempFilePath;
infor.url = that.hostErpFile + BusinessLicense.FilePath
that.$emit('onSuccess', infor);
}, err => {
uni.hideLoading()
});
},
//图片上传
upFile(filePath, resCall) {
let that = this;
let MallBaseId = uni.getStorageSync("mall_UserInfo").MallBaseId ? uni.getStorageSync("mall_UserInfo")
.MallBaseId : 1;
let path = "/Upload/Temporary/"
let action = this.hostErpUpload + `/Upload/Index?filePath=${path}`
uni.uploadFile({
url: action,
filePath: filePath,
name: 'file',
formData: {
user: 'tesdt'
},
success: (uploadFileRes) => {
if (uploadFileRes.statusCode == 200) {
uni.hideLoading()
resCall(uploadFileRes);
} else {
wx.showToast({
title: uploadFileRes.message,
icon: 'none',
duration: 1000
})
}
},
fail: function(err) {
console.log(err)
wx.showToast({
title: '上传失败!' + JSON.stringify(err),
icon: 'none',
duration: 1000
})
}
});
},
}
}
</script>
<style lang="scss" scoped>
@import url("@/asset/css/flex.css");
.upImg{
width: 208rpx;
height: 208rpx;
border-radius: 10rpx;
border: 1rpx solid #ededed;
}
</style>
@import url("@/asset/css/flex.css");
.upImg {
width: 208rpx;
height: 208rpx;
border-radius: 10rpx;
border: 1rpx solid #ededed;
}
</style>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment