From 2820f3e2c3a33c2ff815effa55dea8b29ca049ec Mon Sep 17 00:00:00 2001 From: b2894lxlx <517289602@qq.com> Date: Wed, 31 Jan 2024 17:15:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E7=89=87=E4=B8=8A=E4=BC=A0=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E4=B8=BAoss=E7=9B=B4=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/ossUpload.js | 73 +++++++++++++++++++++++ api/upload.js | 111 +++++++++++++++++++++++------------ package-lock.json | 11 ++++ package.json | 1 + pages/user/shop/addgoods.vue | 3 +- 5 files changed, 159 insertions(+), 40 deletions(-) create mode 100644 api/ossUpload.js diff --git a/api/ossUpload.js b/api/ossUpload.js new file mode 100644 index 0000000..434b77f --- /dev/null +++ b/api/ossUpload.js @@ -0,0 +1,73 @@ +import request from '@/utils/request/request' +import urlRequest from '@/utils/request' + +import storage from '@/utils/storage' +import dayjs from "dayjs" + +const ossRequest = new request({ + // 接口请求地址 + baseUrl: 'https://server.jimeigroup.cn/api/', + // 服务器本地上传文件地址 + fileUrl: 'https://server.jimeigroup.cn/api/', + // 服务器上传图片默认url + defaultUploadUrl: 'upload/image', + // 设置请求头(如果使用报错跨域问题,可能是content-type请求类型和后台那边设置的不一致) + header: { + 'content-type': 'application/json;charset=utf-8' + }, + // 请求超时时间, 单位ms(默认15000) + timeout: 15000, + // 默认配置(可不写) + config: { + // 是否自动提示错误 + isPrompt: true, + // 是否显示加载动画 + load: true, + // 是否使用数据工厂 + isFactory: true + } +}) + +export const ossUploadFile = async (file) => { + console.log(file) + // 获取临时凭证 + let sts = storage.get('sts'); + if (!sts) { + const stsRes = await ossRequest.get('oss/getPostForm') + sts = stsRes.data.data + storage.set('sts', sts, 60) + } + const {polocyBase64, signature} = sts; + var suffix = file.name.substring(file.name.lastIndexOf(".")); + const fileName = dayjs().format('YYYYMMDD') + '/' + uni.$u.guid() + suffix; + return new Promise((reso, rej) => { + uni.uploadFile({ + url: 'https://oss.jimeigroup.cn', // 开发者服务器的URL。 + filePath: file.path, + name: 'file', // 必须填file。 + formData: { + key: fileName, + 'policy': polocyBase64, + 'OSSAccessKeyId': 'LTAI5t8UTh8CTXEi2dYxobhj', + 'success_action_status': '200', //让服务端返回200,不然,默认会返回204 + 'signature': signature, + // 'x-oss-security-token': this.stsToken, + }, + success: () => { + urlRequest.post('upload/addFile', { + storage: 'aliyun', + domain: 'https://oss.jimeigroup.cn', + file_name: file.name, + file_path: fileName, + file_size: file.size, + file_ext: suffix + }).then(res => { + reso(res) + }) + }, + fail: err => { + rej(err) + } + }); + }) +} \ No newline at end of file diff --git a/api/upload.js b/api/upload.js index 4884148..b2afd17 100644 --- a/api/upload.js +++ b/api/upload.js @@ -1,57 +1,90 @@ import request from '@/utils/request' +import {ossUploadFile} from "./ossUpload"; // api地址 const api = { - image: 'upload/image', - goods_detail_image:'upload/image', + image: 'upload/image', + goods_detail_image: 'upload/image', } // 图片上传 export const goods_detail_image = (files) => { - // 文件上传大小, 2M - const maxSize = 1024 * 1024 * 20000 - // 执行上传 - return new Promise((resolve, reject) => { - request.urlFileUpload({ files, maxSize }) - .then(result => { - const fileIds = result.map(item => { - return item.data.fileInfo.preview_url + // 文件上传大小, 2M + const maxSize = 1024 * 1024 * 20000 + // 执行上传 + // return new Promise((resolve, reject) => { + // request.urlFileUpload({files, maxSize}) + // .then(result => { + // const fileIds = result.map(item => { + // return item.data.fileInfo.preview_url + // }) + // resolve(fileIds, result) + // }) + // .catch(reject) + // }) + return new Promise(resolve => { + const fileIds = [] + Promise.all( + files.map(async file => { + const {data: {fileInfo: {file_id}}} = await ossUploadFile(file) + fileIds.push(file_id) + }) + ).then(() => { + resolve(fileIds) }) - resolve(fileIds, result) - }) - .catch(reject) - }) + }) } // 图片上传 export const image = (files) => { - // 文件上传大小, 2M - const maxSize = 1024 * 1024 * 20000 - // 执行上传 - return new Promise((resolve, reject) => { - request.urlFileUpload({ files, maxSize }) - .then(result => { - const fileIds = result.map(item => { - return item.data.fileInfo.file_id + // 文件上传大小, 2M + const maxSize = 1024 * 1024 * 20000 + // 执行上传 + // return new Promise((resolve, reject) => { + // request.urlFileUpload({ files, maxSize }) + // .then(result => { + // const fileIds = result.map(item => { + // return item.data.fileInfo.file_id + // }) + // resolve(fileIds, result) + // }) + // .catch(reject) + // }) + return new Promise(resolve => { + const fileIds = [] + Promise.all( + files.map(async file => { + const {data: {fileInfo: {file_id}}} = await ossUploadFile(file) + fileIds.push(file_id) + }) + ).then(() => { + resolve(fileIds) }) - resolve(fileIds, result) - }) - .catch(reject) - }) + }) } - export const images = (files) => { - // 文件上传大小, 2M - const maxSize = 1024 * 1024 * 20000 - // 执行上传 - return new Promise((resolve, reject) => { - request.urlFileUpload({ files, maxSize }) - .then(result => { - const fileIds = result.map(item => { - return item.data.fileInfo + // 文件上传大小, 2M + const maxSize = 1024 * 1024 * 20000 + // 执行上传 + // return new Promise((resolve, reject) => { + // request.urlFileUpload({files, maxSize}) + // .then(result => { + // const fileIds = result.map(item => { + // return item.data.fileInfo + // }) + // resolve(fileIds, result) + // }) + // .catch(reject) + // }) + return new Promise(resolve => { + const fileIds = [] + Promise.all( + files.map(async file => { + const {data: {fileInfo: {file_id}}} = await ossUploadFile(file) + fileIds.push(file_id) + }) + ).then(() => { + resolve(fileIds) }) - resolve(fileIds, result) - }) - .catch(reject) - }) + }) } diff --git a/package-lock.json b/package-lock.json index 2a6f00e..3b80d43 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,6 +6,7 @@ "": { "dependencies": { "@climblee/uv-ui": "^1.1.19-4", + "dayjs": "^1.11.10", "weixin-js-sdk": "^1.6.5" } }, @@ -14,6 +15,11 @@ "resolved": "https://registry.npmjs.org/@climblee/uv-ui/-/uv-ui-1.1.19-4.tgz", "integrity": "sha512-ZHip40iETfDT6CxjER0U9rZLpc6nDG3rBAOvC6amfep0XTr46oE1WfImYzbSe3ypxI+WbY2elftSV9Y9NMRM5w==" }, + "node_modules/dayjs": { + "version": "1.11.10", + "resolved": "https://registry.npmmirror.com/dayjs/-/dayjs-1.11.10.tgz", + "integrity": "sha512-vjAczensTgRcqDERK0SR2XMwsF/tSvnvlv6VcF2GIhg6Sx4yOIt/irsr1RDJsKiIyBzJDpCoXiWWq28MqH2cnQ==" + }, "node_modules/weixin-js-sdk": { "version": "1.6.5", "resolved": "https://registry.npmjs.org/weixin-js-sdk/-/weixin-js-sdk-1.6.5.tgz", @@ -26,6 +32,11 @@ "resolved": "https://registry.npmjs.org/@climblee/uv-ui/-/uv-ui-1.1.19-4.tgz", "integrity": "sha512-ZHip40iETfDT6CxjER0U9rZLpc6nDG3rBAOvC6amfep0XTr46oE1WfImYzbSe3ypxI+WbY2elftSV9Y9NMRM5w==" }, + "dayjs": { + "version": "1.11.10", + "resolved": "https://registry.npmmirror.com/dayjs/-/dayjs-1.11.10.tgz", + "integrity": "sha512-vjAczensTgRcqDERK0SR2XMwsF/tSvnvlv6VcF2GIhg6Sx4yOIt/irsr1RDJsKiIyBzJDpCoXiWWq28MqH2cnQ==" + }, "weixin-js-sdk": { "version": "1.6.5", "resolved": "https://registry.npmjs.org/weixin-js-sdk/-/weixin-js-sdk-1.6.5.tgz", diff --git a/package.json b/package.json index b30e9d4..d4c05bb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { "dependencies": { "@climblee/uv-ui": "^1.1.19-4", + "dayjs": "^1.11.10", "weixin-js-sdk": "^1.6.5" } } diff --git a/pages/user/shop/addgoods.vue b/pages/user/shop/addgoods.vue index a2cb45a..aca3b52 100644 --- a/pages/user/shop/addgoods.vue +++ b/pages/user/shop/addgoods.vue @@ -900,7 +900,7 @@ app.$toast(result.message) setTimeout(() => { app.disabled = false - uni.navigateBack() + // uni.navigateBack() }, 1500) }) .catch(err => app.disabled = false) @@ -930,6 +930,7 @@ UploadApi.image(file.images) .then(fileIds => { app.formData[index].uploaded = fileIds + console.log(app.formData[index].uploaded, fileIds) resolve(fileIds) }) .catch(reject)