5 changed files with 159 additions and 40 deletions
@ -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) |
||||
|
} |
||||
|
}); |
||||
|
}) |
||||
|
} |
@ -1,57 +1,90 @@ |
|||||
import request from '@/utils/request' |
import request from '@/utils/request' |
||||
|
import {ossUploadFile} from "./ossUpload"; |
||||
|
|
||||
// api地址
|
// api地址
|
||||
const 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) => { |
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) => { |
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) => { |
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) |
|
||||
}) |
|
||||
|
}) |
||||
} |
} |
||||
|
@ -1,6 +1,7 @@ |
|||||
{ |
{ |
||||
"dependencies": { |
"dependencies": { |
||||
"@climblee/uv-ui": "^1.1.19-4", |
"@climblee/uv-ui": "^1.1.19-4", |
||||
|
"dayjs": "^1.11.10", |
||||
"weixin-js-sdk": "^1.6.5" |
"weixin-js-sdk": "^1.6.5" |
||||
} |
} |
||||
} |
} |
||||
|
Loading…
Reference in new issue