type file = {
|
file: string,
|
url: string
|
}
|
type Images = 'coverImage'|'images';
|
// const app = getApp<IAppOption>();
|
Component({
|
/**
|
* 组件的属性列表
|
*/
|
options: {
|
styleIsolation:"shared",
|
addGlobalClass:true,
|
multipleSlots: true // 在组件定义时的选项中启用多slot支持
|
},
|
properties: {
|
url:{
|
type: String,
|
value: ""
|
},
|
fileDeleteUrl:{
|
type: String,
|
value: ""
|
},
|
description:{
|
type: String,
|
value: "建议尺寸:500x500像素,72分辨率"
|
},
|
previewSize:{
|
type: String,
|
value: "500rpx"
|
},
|
imageFit:{
|
type: String,
|
value: "aspectFill"
|
},
|
hasSlot:{
|
type:Boolean,
|
value: false
|
},
|
multiple:{
|
type:Boolean,
|
value: false
|
},
|
imgList:{
|
type:Array,
|
value: []
|
},
|
isMustUpload:{
|
type:Boolean,
|
value: true
|
}
|
},
|
|
/**
|
* 组件的初始数据
|
*/
|
data: {
|
imgList: <any>[],
|
coverImage:'',
|
images:''
|
},
|
lifetimes:{
|
ready(){
|
console.log(this.data.imgList);
|
|
}
|
},
|
observers: {
|
'imgList': function(val) {
|
console.log('监听图片列表imgList',val,this.data.imgList)
|
if (val.length>0) {
|
if (this.data.multiple){
|
this.getImgUnid('images');
|
} else {
|
this.getImgUnid('coverImage');
|
}
|
}
|
|
}
|
},
|
/**
|
* 组件的方法列表
|
*/
|
methods: {
|
async uploadSingleFile(event:any) {
|
const { file } = event.detail;
|
console.log(event,file);
|
// 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式
|
let [err,result] = await this.uploadFile({
|
file,
|
url: this.data.url,
|
formData:{
|
// isCoverImage:true,
|
// formid:710802
|
}
|
});
|
if (err){
|
console.log(err);
|
return;
|
}
|
console.log(result);
|
let imgList = this.data.imgList;
|
imgList.push({ ...result });
|
this.setData({ imgList });
|
console.log('单张上传后imgList。。。',imgList);
|
this.getImgUnid('coverImage');
|
// this.triggerEvent('getCoverImage',{
|
// imgList
|
// })
|
|
},
|
async uploadMultipleFile(event:any) {
|
const { file } = event.detail;
|
console.log(event,file);
|
let imgList = this.data.imgList;
|
let unid = this.data.imgList[0]&&this.data.imgList[0].unid||'';
|
let url = this.data.url;
|
for (let i = 0; i < file.length; i++) {
|
if (unid){
|
url+=`&unid=${unid}`;
|
}
|
console.log(url);
|
// 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式
|
let [err,result] = await this.uploadFile({
|
file:file[i],
|
url,
|
formData:{
|
// isCoverImage:true,
|
// formid:710802
|
}
|
});
|
if (err){
|
console.log(err);
|
return;
|
}
|
console.log(result);
|
imgList.push({ ...result });
|
unid = result.unid;
|
}
|
|
this.setData({ imgList });
|
console.log('多张上传后imgList。。。',imgList);
|
this.getImgUnid('images');
|
if (this.data.isMustUpload){
|
this.checkImages("images", "请至少上传一张图片")
|
}
|
|
// this.triggerEvent('getCoverImage',{
|
// imgList
|
// })
|
|
},
|
async deleteFile(event:any){
|
let {imgList,fileDeleteUrl} = this.data;
|
let {index} = event.detail;
|
console.log(index,'dd');
|
let [err,result] = await wx.$utils.to(
|
wx.$http.request({
|
url: fileDeleteUrl,
|
data:{
|
unid:imgList[index].unid,
|
seq:imgList[index].seq,
|
}
|
})
|
);
|
if (err){
|
console.log(err);
|
return;
|
}
|
console.log(result);
|
if (result.state === "success"){
|
imgList.splice(index, 1);
|
this.setData({
|
imgList
|
})
|
}
|
console.log('删除后imgList。。。',imgList);
|
if (this.data.multiple){
|
this.getImgUnid('images');
|
} else {
|
this.getImgUnid('coverImage');
|
}
|
},
|
async uploadFile(param:any){
|
let [err,result] = await wx.$utils.to(
|
wx.$http.uploadFile({
|
url:param.url,
|
formData:param.formData,
|
filePath:param.file.url
|
})
|
)
|
return [err,result];
|
},
|
|
getImgList(){
|
return this.data.imgList;
|
},
|
// getSingleImgUnid(){
|
// let unid:string|boolean|undefined;
|
// if (this.data.imgList[0]){
|
// unid = this.data.imgList[0].unid+';'+this.data.imgList[0].seq;
|
// } else {
|
// unid = undefined;
|
// }
|
// this.setData({
|
// coverImg:unid
|
// });
|
// console.log('封面图unid',unid);
|
// return unid;
|
// },
|
getImgUnid(type:string){
|
let unid:string|boolean|undefined;
|
if (!this.data.imgList[0]){
|
unid = undefined;
|
this.setData({
|
[type]:unid
|
});
|
console.log(type,'图unid',unid);
|
return unid;
|
}
|
unid = this.data.imgList[0].unid+';';
|
let seq = this.data.imgList.reduce((total:string,current:any)=>{
|
console.log('total:',total,'current.seq:',current.seq);
|
return total+';'+current.seq
|
},' ')
|
let res = unid+seq.replace(' ;','').replace(new RegExp(' ', 'gm'), '')
|
this.setData({
|
[type]:res
|
});
|
console.log(type,'图unid+seq',res);
|
return res;
|
},
|
checkImages(images:Images, msg:string) {
|
if (!this.data[images]) {
|
this.setData({
|
[images + "Error"]: msg,
|
});
|
return false;
|
}
|
this.setData({
|
[images + "Error"]: "",
|
});
|
return true;
|
// this.handleLiveTime()
|
},
|
getCoverImageUnid(){
|
return this.data.coverImage;
|
},
|
getImagesUnid(){
|
return this.data.images;
|
},
|
clearImagesData(){
|
this.setData({
|
imgList:[],
|
images:'',
|
coverImage:''
|
})
|
}
|
// async deleteFile(param:any){
|
// let [err,result] = await wx.$utils.to(
|
// wx.$http.uploadFile({
|
// url:param.url,
|
// formData:param.formData,
|
// filePath:param.file.url
|
// })
|
// )
|
// return [err,result];
|
// }
|
}
|
})
|