import { pathToBase64, base64ToPath } from "image-tools"; // 初始化OSS签名 /* let fileName = 'jpg' let fileType = 'LRD-ADMIN/audit' * fileType 文件存放路径 * fileName 文件类型 */ const initOssSign = function (ossData, imgData,flag) { return new Promise(async (resolve, reject) => { try { const { accessId, dir, host, policy, signature } = ossData.oss; const data = { success_action_status: "200", key: dir, OSSAccessKeyId: accessId, policy: policy, Signature: signature, }; if(flag){ const newFile = await imageCompress(imgData) const config = { url: host, dir: dir, formData: data, filePath: newFile.path, }; return resolve(config); }else{ imgBase64ToFilePath(imgData) .then((path) => { const config = { url: host, dir: dir, formData: data, filePath: path, }; return resolve(config); }) .catch((err) => { console.log(err, "报错了"); reject(err); }); } } catch (err) { console.log(err, "err"); uni.showToast({ icon: "error", title: "处理签名数据失败", }); } }); }; // 文件上传请求 export const UploadFile = function (ossData, imgData,flag) { // console.log(ossData, "ossDataweb8888"); return new Promise((resolve, reject) => { initOssSign(ossData, imgData,flag).then((config) => { const { url, filePath, dir, formData } = config; uni.showLoading({ title: "加载中...", }); uni.uploadFile({ url, //仅为示例,非真实的接口地址 filePath, name: "file", formData, success: (res) => { res.fileUrl = `${url}${dir}`; return resolve(res); }, fail: (err) => { uni.showToast({ icon: "none", title: "文件上传OSS失败", }); return reject(err); }, complete: () => { uni.hideLoading(); }, }); }); }); }; // Base64转本地图片路径 export const imgBase64ToFilePath = function (base64Img) { return new Promise((resolve, reject) => { base64ToPath(base64Img) .then((path) => { // console.log(path, '到这里了嘛') return resolve(path); }) .catch((err) => { return reject(err); }); }); }; /** 图片压缩 * 图片压缩递归,小于1M跳出 * */ const imageCompress = function (file) { return new Promise((resolve, reject) => { let { size, path } = file; let type = path.split(".")[1]; //大于1M进行压缩, if (size < 5 * 1000 * 1000) { resolve(file); return false; } uni.compressImage({ src: path, quality: 80, success: (res) => { let newPath = res.tempFilePath + type; let newName = res.tempFilePath.split("/")[res.tempFilePath.split("/").length - 1] + type; uni.getFileInfo({ filePath: res.tempFilePath, success: async (info) => { let newFile = { ...file, size: info.size, path: newPath, name: newName, tempFilePath: res.tempFilePath, }; resolve(await imageCompress(newFile)); }, }); }, fail:(err)=>reject(err) }); }); };