99 lines
2.1 KiB
TypeScript
99 lines
2.1 KiB
TypeScript
import Taro from '@tarojs/taro'
|
|
import { request } from '@/lib/request'
|
|
import { applyCorpInit } from '@/lib/session'
|
|
import { buildFileUrl, getFileExtension, pickPresignResult, QINIU_UPLOAD_URL } from '@/lib/qiniu'
|
|
|
|
export function getUserConfig (params: Record<string, any> = {}) {
|
|
return request({
|
|
url: '/api/user/config',
|
|
method: 'GET',
|
|
params,
|
|
})
|
|
}
|
|
|
|
export function initCorp (params: Record<string, any> = {}) {
|
|
return request({
|
|
url: '/api/h5/corp',
|
|
method: 'GET',
|
|
params,
|
|
}).then((res) => {
|
|
applyCorpInit(res)
|
|
return res
|
|
})
|
|
}
|
|
|
|
export function getCompanies (keyword: string) {
|
|
return request({
|
|
url: '/api/h5/company',
|
|
method: 'GET',
|
|
params: { keyword },
|
|
})
|
|
}
|
|
|
|
export function report (data: {
|
|
contact: string
|
|
content: string
|
|
images?: string[]
|
|
type: string
|
|
[property: string]: any
|
|
}) {
|
|
return request({
|
|
url: '/api/user/feedback',
|
|
method: 'POST',
|
|
data,
|
|
})
|
|
}
|
|
|
|
export function presign (ext = '') {
|
|
return request({
|
|
url: '/api/presign',
|
|
method: 'GET',
|
|
params: {
|
|
ext,
|
|
scene: 'process',
|
|
},
|
|
})
|
|
}
|
|
|
|
export async function uploadImage (filePath: string, onProgress?: (percent: number) => void) {
|
|
const ext = getFileExtension(filePath)
|
|
onProgress?.(0)
|
|
const presignResponse = await presign(ext)
|
|
const { id, token, key, domain } = pickPresignResult(presignResponse)
|
|
if (!id || !token || !key || !domain) {
|
|
throw new Error('获取上传凭证失败')
|
|
}
|
|
|
|
const uploaded = await new Promise<{ key?: string }>((resolve, reject) => {
|
|
const task = Taro.uploadFile({
|
|
url: QINIU_UPLOAD_URL,
|
|
filePath,
|
|
name: 'file',
|
|
formData: {
|
|
token,
|
|
key,
|
|
},
|
|
success (res) {
|
|
try {
|
|
const body = typeof res.data === 'string' ? JSON.parse(res.data || '{}') : res.data
|
|
resolve(body || {})
|
|
} catch {
|
|
resolve({})
|
|
}
|
|
},
|
|
fail (error) {
|
|
reject(error)
|
|
},
|
|
})
|
|
task.onProgressUpdate?.((event) => {
|
|
onProgress?.(Math.min(100, Math.max(0, event.progress)))
|
|
})
|
|
})
|
|
|
|
onProgress?.(100)
|
|
return {
|
|
id,
|
|
url: buildFileUrl(domain, uploaded.key || key),
|
|
}
|
|
}
|