84 lines
5.4 KiB
Plaintext
84 lines
5.4 KiB
Plaintext
import { API_BASE_URL, getSession, clearSession, uploadJson, requestJson } from './api.uts'
|
|||
|
|
import { agreementRecordIdValid, sameConsentSession } from './agreements-route.uts'
|
||
|
|
|
||
|
|
export const documentCategory = (name: string): string => {
|
||
|
|
const names = ['身份证正面', '身份证反面', '银行卡', '收入证明', '营业执照', '银行流水', '经营证明']
|
||
|
|
const categories = ['id-front', 'id-back', 'bank-card', 'income', 'business-license', 'bank-statement', 'business-proof']
|
||
|
|
const index = names.indexOf(name)
|
||
|
|
return index < 0 ? '' : categories[index]
|
||
|
|
}
|
||
|
|
export type OwnedDocument = { name: string, documentId: string, consentId: string, deletePending: boolean }
|
||
|
|
export async function fetchOwnedDocuments(): Promise<OwnedDocument[]> {
|
||
|
|
const sent = owner()
|
||
|
|
const data = await requestJson('/consents', 'GET', { scope: 'documents', page: 1, pageSize: 1 })
|
||
|
|
if (!sameOwner(sent)) throw new Error('登录状态已变化,请重新操作')
|
||
|
|
const rows = data.getArray<UTSJSONObject>('documents')
|
||
|
|
if (rows == null) throw new Error('已保存照片读取未能确认,请重新读取')
|
||
|
|
const names = ['身份证正面', '身份证反面', '银行卡', '收入证明', '营业执照', '银行流水', '经营证明']
|
||
|
|
const items: OwnedDocument[] = []; const ids: string[] = []
|
||
|
|
rows.forEach((row) => {
|
||
|
|
const id = row.getString('documentId') ?? ''; const consentId = row.getString('consentId') ?? ''
|
||
|
|
const category = row.getString('category') ?? ''; const pending = row.getBoolean('deletePending')
|
||
|
|
const name = names.find((item) => documentCategory(item) == category) ?? ''
|
||
|
|
if (!agreementRecordIdValid(id) || !agreementRecordIdValid(consentId) || name.length == 0 || pending == null
|
||
|
|
|| ids.includes(id) || row['path'] != null || row['url'] != null || row['storage_key'] != null)
|
||
|
|
throw new Error('已保存照片响应无效,请重新读取')
|
||
|
|
ids.push(id); items.push({ name, documentId: id, consentId, deletePending: pending })
|
||
|
|
})
|
||
|
|
return items
|
||
|
|
}
|
||
|
|
function owner(): UTSJSONObject {
|
||
|
|
const current = getSession()
|
||
|
|
if (current == null) throw new Error('请先登录')
|
||
|
|
return current
|
||
|
|
}
|
||
|
|
function sameOwner(sent: UTSJSONObject): boolean {
|
||
|
|
const current = getSession()
|
||
|
|
return sameConsentSession(sent.getString('token') ?? '', sent.getString('userId') ?? '', current?.getString('token') ?? '', current?.getString('userId') ?? '')
|
||
|
|
}
|
||
|
|
export async function uploadDocument(path: string, category: string, consentId: string): Promise<string> {
|
||
|
|
const sent = owner()
|
||
|
|
if (path.length == 0 || !['id-front', 'id-back', 'bank-card', 'income', 'business-license', 'bank-statement', 'business-proof'].includes(category) || !agreementRecordIdValid(consentId))
|
||
|
|
throw new Error('照片类别或同意记录无效')
|
||
|
|
const data = await uploadJson('/documents', path, { category, consentId }, '保存结果未确认,重试可能另存一份照片')
|
||
|
|
if (!sameOwner(sent)) throw new Error('登录状态已变化,请重新操作')
|
||
|
|
const id = data.getString('documentId') ?? ''; const size = data.getNumber('byteSize') ?? 0
|
||
|
|
if (!agreementRecordIdValid(id) || data.getString('category') != category || data.getString('mimeType') != 'image/png'
|
||
|
|
|| !Number.isInteger(size) || size <= 0 || size > 5242880 || data['url'] != null || data['path'] != null)
|
||
|
|
throw new Error('保存回执未能确认,请手动重试;可能另存一份照片')
|
||
|
|
return id
|
||
|
|
}
|
||
|
|
export function releaseDocumentTemp(path: string): void {
|
||
|
|
if (path.length == 0) return
|
||
|
|
// 不保存到相册或持久缓存。原生端尽力移除下载临时文件;H5对象URL由运行时管理。
|
||
|
|
// #ifndef WEB
|
||
|
|
uni.getFileSystemManager().unlink({ filePath: path, fail: () => {} })
|
||
|
|
// #endif
|
||
|
|
}
|
||
|
|
export function downloadDocument(id: string): Promise<string> {
|
||
|
|
return new Promise<string>((resolve, reject) => {
|
||
|
|
let sent: UTSJSONObject
|
||
|
|
try { sent = owner(); if (!agreementRecordIdValid(id)) throw new Error('资料编号无效') }
|
||
|
|
catch (error) { reject(error); return }
|
||
|
|
uni.downloadFile({ url: API_BASE_URL + '/documents/' + id + '/content', timeout: 10000,
|
||
|
|
header: { Authorization: 'Bearer ' + (sent.getString('token') ?? ''), 'X-Requested-With': 'XMLHttpRequest' },
|
||
|
|
success: (result) => {
|
||
|
|
if (!sameOwner(sent)) { releaseDocumentTemp(result.tempFilePath); reject(new Error('登录状态已变化,请重新操作')); return }
|
||
|
|
if (result.statusCode != 200 || result.tempFilePath.length == 0) {
|
||
|
|
releaseDocumentTemp(result.tempFilePath)
|
||
|
|
if (result.statusCode == 401) clearSession()
|
||
|
|
reject(new Error('资料读取失败,请重新登录或稍后重试')); return
|
||
|
|
}
|
||
|
|
resolve(result.tempFilePath)
|
||
|
|
}, fail: () => { reject(new Error('资料读取失败,请检查网络后重试')) }
|
||
|
|
})
|
||
|
|
})
|
||
|
|
}
|
||
|
|
export async function deleteDocument(id: string): Promise<void> {
|
||
|
|
const sent = owner()
|
||
|
|
if (!agreementRecordIdValid(id)) throw new Error('资料编号无效')
|
||
|
|
const data = await requestJson('/documents/' + id + '/delete', 'POST', {})
|
||
|
|
if (!sameOwner(sent)) throw new Error('登录状态已变化,请重新操作')
|
||
|
|
if (data.getString('documentId') != id || data.getBoolean('deleted') != true) throw new Error('删除未能确认,请对同一资料重试')
|
||
|
|
}
|