import {OssSign} from './api' import { base64ToPath } from './image-tools' // 初始化OSS签名 /* let fileName = 'jpg' let fileType = 'LRD-ADMIN/audit' * fileType 文件存放路径 * fileName 文件类型 */ const initOssSign = async function (fileName, fileType, imgData) { return new Promise(async (resolve, reject) => { try { const res = await OssSign({ fileType, fileName }) if (res.success) { const { oss } = res.result const { accessId, dir, host, policy, signature } = oss const data = { success_action_status: '200', key: dir, OSSAccessKeyId: accessId, policy: policy, Signature: signature, } 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) { uni.showToast({ icon: 'error', title: '处理签名数据失败', }) } }) } // 文件上传请求 export const UploadFile = function (fileName, fileType, imgData) { return new Promise((resolve, reject) => { initOssSign(fileName, fileType, imgData).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) }) }) }