import { message } from './message.js' import { host, SERIAL, API_VERSION, SERVER_TYPE } from '../config.js' import { http } from '@/common/api/index.js'; import dayjs from '@/libs/day.js' const getUUID = () => { const S4 = () => ((((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)) return (S4() + S4() + '' + S4() + '' + S4() + '' + S4() + '' + S4() + S4() + S4()) + new Date().getTime() } // 获取文件后缀名 const getSuffix = (fileName) => { let pos = fileName.lastIndexOf('.') return pos != -1 ? fileName.substring(pos) : '' } /* 判断url路径处理 */ function posUrl(url) { let newUrl = '' if (/(http|https):\/\/([\w.]+\/?)\S*/.test(url)) { newUrl = url } else { newUrl = `${host}/api/${url}` } return newUrl } // 赛诸葛上传 class SaiZhuGeUploadFiles { /** * [constructor description] * @param {[Array]} files [chooseImg选中的tempFilePaths、chooseVideo选中的rempFilePath] * @param {[Object]} json [success每上传成功一张调用 complete全部上传完成调用] * @return {[void]} [description] */ constructor(files, json) { if (!Array.isArray(files)) { throw new Error('Class UploadFileToOSS parameter must be an array'); } this.data = []; this.fn = json.success; this.complete = json.complete; this.files = files; this.fileLen = this.files.length; this.curIndex = 0; this.resData = false; uni.showLoading({ title: '加载中', mask: true }) if (!this.resData) this.getUploadConfig() } async getUploadConfig() { try { let { data: { code, msg, data } } = await http.post(global.apiUrls.queryAutograph, { member_id: API_VERSION.replace(/\D/g, "") }) if (code != 1) { uni.hideLoading() message.info(msg) return } this.resData = data this.uploadFile() } catch (error) { uni.hideLoading() message.info(msg) } } uploadFile() { let resData = this.resData const fileInfo = this.files[this.curIndex] let allowUploadFile = this.uploadVerification(fileInfo, resData.is_public, +resData.size) if (allowUploadFile !== true) { uni.hideLoading() return message.info('文件超出大小限制') } // deploy 1 本地服务器上传 否则 上传至oss if (resData.deploy == 1) this.uploadFileToLocal(resData) else this.uploadFileToOSS(resData) } // oss 上传验证 uploadVerification(file, isPublic = 1, size = 2048) { const fileName = file.name let isSizt = isPublic === 1 ? file.size / 1024 < size : true if (!isSizt) return false return true } // 本地上传 uploadFileToLocal(resData) { let fileInfo = this.files[this.curIndex] uni.uploadFile({ url: posUrl(global.apiUrls.UPLOAD_IMAGE_URL), filePath: fileInfo.path, name: 'file', // #ifdef MP-DINGTALK fileType: "image", // #endif header: { 'user-token': global.token || '', 'Accept-Language': global.locale, 'Accept-Serial': SERIAL, }, formData: { 'module': '', 'dir': resData.dir || '' }, success: res => { let data = JSON.parse(res.data); if (data.code == 1) { this.data.push({ name: fileInfo.name, ...data.data[0] }); if (this.fn) this.fn(this.data); } else { message.info('上传失败,请重试') } }, complete: () => { this.curIndex++; // 这个图片执行完上传后,开始上传下一张 if (this.curIndex >= this.fileLen) { // 当图片传完时,停止调用 this.complete(this.data); uni.hideLoading() } else { // 若图片还没有传完,则继续调用函数 this.uploadFile(); } } }); } // oss上传 uploadFileToOSS(ossData) { const fileInfo = this.files[this.curIndex] let joint = getUUID() let fileName = `${joint}${getSuffix(fileInfo.name ?? fileInfo.path)}` let filePath = `${ossData.dir}${dayjs(new Date()).format('YYYY-MM-DD')}/${fileName}` let fileNetworkPath = `${ossData.host}/${filePath}` uni.uploadFile({ url: ossData.host, filePath: fileInfo.path, // #ifdef MP-DINGTALK fileType: "image", // #endif name: 'file', formData: { key: filePath, // 文件名 policy: ossData.policy, // 后台获取超时时间 OSSAccessKeyId: ossData.accessid, // 后台获取临时ID success_action_status: 200, // 让服务端返回200,不然,默认会返回204 signature: ossData.signature // 后台获取签名 }, success: res => { console.log(res); if (res.statusCode == 200) { this.data.push({ id: fileNetworkPath, name: fileInfo.name || fileName, path: fileNetworkPath, thumb: fileNetworkPath + '?x-oss-process=video/snapshot,t_1000,m_fast', }) if (this.fn) this.fn(this.data); } else { message.info('上传失败,请重试') } }, fail(err) { console.log(err); }, complete: () => { this.curIndex++; // 当前文件执行完上传后,开始上传下一张 if (this.curIndex >= this.fileLen) { // 当文件传完时,停止调用 this.complete(this.data); uni.hideLoading() } else { // 若文件还没有传完,则继续调用函数 this.uploadFile(); } } }) } } // 普通上传 class OrdinaryUploadFiles { /** * [constructor description] * @param {[Array]} files [chooseImg选中的tempFilePaths] * @param {[Object]} json [success每上传成功一张调用 complete全部上传完成调用] * @return {[void]} [description] */ constructor(files, 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.url = json.url ? host + '/api' + json.url : host + '/api' + global.apiUrls.UPLOAD_IMAGE_URL; _this.complete = json.complete; _this.files = files; _this.formData = json.formData || {}; _this.fileLen = _this.files.length; _this.curIndex = 0; uni.showLoading({ // title: '上传中' title: global.i18n.t('上传中') }); _this.upload(); } upload() { uni.uploadFile({ url: this.url, filePath: this.files[this.curIndex].path, name: 'file', header: { 'user-token': global.token || '', 'Accept-Language': global.locale, 'Accept-Serial': SERIAL, }, formData: { 'module': '', 'dir': '', ...this.formData }, success: res => { let data = JSON.parse(res.data); if (data.code == 1) { let file = Array.isArray(data.data) ? data.data[0] : data.data file.thumb = file.path + '?x-oss-process=video/snapshot,t_1000,m_fast' this.data.push(file); if (this.fn) this.fn(this.data); } else { message.info(global.i18n.t('上传失败,请重试')) } }, complete: () => { this.curIndex++; // 这个图片执行完上传后,开始上传下一张 if (this.curIndex >= this.fileLen) { // 当图片传完时,停止调用 this.complete(this.data); uni.hideLoading() } else { // 若图片还没有传完,则继续调用函数 this.upload(); } } }); } } let UploadFiles = null switch (SERVER_TYPE) { case 0://赛诸葛 UploadFiles = SaiZhuGeUploadFiles break; case 1://java UploadFiles = OrdinaryUploadFiles break; default: UploadFiles = OrdinaryUploadFiles break; } export default UploadFiles