import { UPLOAD_IMAGE_URL,SERIAL } from '../config.js' import { message } from './message.js' import { http } from '@/common/api/index.js'; export default class UploadImg { /** * [constructor description] * @param {[Array]} files [chooseImg选中的tempFilePaths] * @param {[Object]} json [success每上传成功一张调用 complete全部上传完成调用] * @return {[void]} [description] */ constructor(files,type, json) { console.log("结果",files); console.log("结果",type); console.log("结果",json); if (!Array.isArray(files)) { throw new Error('Class UploadImg parameter must be an array'); } let _this = this; _this.data = []; _this.fn = json.success; _this.complete = json.complete; _this.files = files; _this.fileLen = _this.files.length; _this.curIndex = 0; uni.showLoading({ title: global.i18n['上传中'] }); _this.upload(type); } upload(type) { let _this = this; http.post(global.apiUrls.post669b195ddee2f).then(res=>{ if(res.data.code==1){ let token = res.data.data.token uni.uploadFile({ url: 'https://htkactvi.lakala.com/registration/file/upload', filePath: this.files[this.curIndex], name: 'file', header: { 'user-token': global.token || '', 'Accept-Language': global.locale, 'Accept-Serial': SERIAL, 'Authorization':'bearer ' + token }, formData: { 'module': '', 'dir': '', imgType:type, sourcechnl:1, isOcr:true }, success: res => { let data = JSON.parse(res.data); console.log("返回结果",data); if(data.showUrl){ this.data.push(data.showUrl); if (this.fn) this.fn(this.data); }else{ message.info(global.i18n['图片上传失败,请重试']) } // if (data.code == 1) { // console.log("返回结果",data); // // } else { // message.info(global.i18n['图片上传失败,请重试']) // } }, complete: () => { this.curIndex++; // 这个图片执行完上传后,开始上传下一张 if (this.curIndex == this.fileLen) { // 当图片传完时,停止调用 this.complete(this.data); uni.hideLoading() } else { // 若图片还没有传完,则继续调用函数 this.upload(); } } }); }else{ message.info(res.data.msg) } }) } }