zhongping-miniprogram/miniprogram/components/uploadImages/uploadImages.ts

150 lines
4.4 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { uploadImg } from "../../apis/upload";
Component({
/**
* 组件的属性列表
*/
properties: {
isText: {
type: Boolean,
value: false
}
// count: { //最多选择图片的张数默认9张
// type: Number,
// value: 9
// },
// uploadUrl: { //图片上传的服务器路径
// type: String,
// value: ''
// },
// showUrl: { //图片的拼接路径
// type: String,
// value: ''
// }
},
/**
* 组件的初始数据
*/
data: {
detailPics: [], //上传的结果图片集合
},
ready: function () {
// console.log(this.data)
},
/**
* 组件的方法列表
*/
methods: {
uploadDetailImage: function (e) { //这里是选取图片的方法
this.setData({
detailPics: []
})
var that = this;
var pics = [];
var detailPics = that.data.detailPics;
if (detailPics.length >= that.data.count) {
wx.showToast({
title: '最多选择' + that.data.count + '张!',
})
return;
}
wx.chooseMedia({
count: 9,//that.data.count, // 最多可以选择的图片张数默认9
sizeType: ['original', 'compressed'], // original 原图compressed 压缩图,默认二者都有
sourceType: ['album', 'camera'], // album 从相册选图camera 使用相机,默认二者都有
success: function (res) {
console.log(res);
var imgs = res.tempFiles;
for (var i = 0; i < imgs.length; i++) {
pics.push(imgs[i])
}
console.log(imgs);
that.uploadimg({
url: 'https://www.chrivc.com/api/consumer/commonLogin/upload', //这里是你图片上传的接口
// url:'http://office.xianci.info:18085/consumer/commonLogin/upload',
path: pics, //这里是选取的图片的地址数组
});
},
})
},
//多张图片上传
uploadimg: function (data) {
console.log(data);
wx.showLoading({
title: '上传中...',
mask: true,
})
var that = this,
i = data.i ? data.i : 0,
success = data.success ? data.success : 0,
fail = data.fail ? data.fail : 0;
console.log(wx.getStorageSync("Authorization"));
wx.uploadFile({
url: data.url,
filePath: data.path[i].tempFilePath,
header: {
"Content-Type": "multipart/form-data",
"authorization": wx.getStorageSync("Authorization")
},
name: 'file',
formData: {},
success: (resp) => {
console.log('success');
wx.hideLoading();
success++;
var str = JSON.parse(resp.data).d
var detailPics = that.data.detailPics;
str = str.slice(0, str.lastIndexOf(':') + 1) + '附件_' + str.slice(str.lastIndexOf(':') + 1)
// console.log('str',str.slice(0, str.lastIndexOf(':') + 1) + '附件_' + str.slice(str.lastIndexOf(':') + 1));
// sources.lastIndexOf(':') + 1) + '附件_' + sources.slice(sources.lastIndexOf(':') + 1)
detailPics.push({ name: str.split(':')[2], url: str })
that.setData({
detailPics: detailPics
})
},
fail: (res) => {
fail++;
console.log('fail:' + i + "fail:" + fail);
},
complete: (res1) => {
console.log(res1,res1.data);
let t = JSON.parse(res1.data)
if(t.c!==200){
wx.showToast({
duration: 1500,
icon:'none',
title:t.m
})
}else{
i++;
if (i == data.path.length) { //当图片传完时,停止调用
console.log('执行完毕');
console.log('成功:' + success + " 失败:" + fail);
var myEventDetail = {
picsList: that.data.detailPics
} // detail对象提供给事件监听函数
// console.log(myEventDetail);
var myEventOption = {} // 触发事件的选项
that.triggerEvent('myevent', myEventDetail, myEventOption)//结果返回调用的页面
} else { //若图片还没有传完,则继续调用函数
data.i = i;
data.success = success;
data.fail = fail;
that.uploadimg(data);//递归,回调自己
}
}
}
});
},
}
})