| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- import Taro from '@tarojs/taro'
- import http from '@/http'
- import { ossSignatureUrl } from '@/http/api'
- import network from '@/class/Network';
- export async function uploadImageFile(imageInfo) {
- const signature = await _getSignature(1)
- if (!signature.accessId)
- return Promise.reject({ message: 'no signature accessId' })
- return _uploadFile(imageInfo.tempFilePath, signature)
- }
- function _getSignature(fileType) {
- return http.post(ossSignatureUrl, { fileType: fileType })
- .then((res: RequestType) => {
- const { data, code } = res
- if (code !== 0)
- return {}
- return data
- }).catch(err => {
- console.log(err)
- return {}
- })
- }
- function _uploadFile(fileTempPath, signature) {
- return new Promise((resolve, reject) => {
- Taro.uploadFile({
- filePath: fileTempPath,
- url: signature.host,
- name: 'file',
- formData: {
- key: signature.fileName,
- OSSAccessKeyId: signature.accessId,
- policy: signature.policy,
- signature: signature.signature,
- success_action_status: '200',
- name: fileTempPath,
- networkType: network.networkType
- },
- success() {
- resolve({
- fileUrl: signature.host + signature.fileName,
- objectKey: signature.fileName,
- host: signature.host,
- relativeUrl: signature.fileName
- })
- },
- fail(err) {
- reject(err)
- }
- })
- })
- }
|