feat: add tested mobile presign contracts and form state.
This commit is contained in:
1 parent
702ff0e074
commit
b28ed3e280
8 files changed
+992
No files matched your search
@@ -0,0 +1,163 @@
|
||||
import { MARKET_MODULES, MARKET_ROLES } from '../market/routes.js'
|
||||
import { getMarketModule } from '../market/module-registry.js'
|
||||
|
||||
const RESOURCE_BY_MODULE = Object.freeze({
|
||||
presign: 'presign-orders',
|
||||
protocol: 'protocols',
|
||||
'payment-chase': 'payment-chase',
|
||||
payment: 'payments',
|
||||
sample: 'samples',
|
||||
freight: 'freights'
|
||||
})
|
||||
|
||||
function assertContext(context) {
|
||||
if (
|
||||
!MARKET_ROLES.includes(context?.marketRole) ||
|
||||
!MARKET_MODULES.includes(context?.module)
|
||||
) {
|
||||
throw new Error('市场页面配置无效')
|
||||
}
|
||||
}
|
||||
|
||||
export function buildMarketResourcePath(context, id) {
|
||||
assertContext(context)
|
||||
const base = `/mini/market/${context.marketRole}/${RESOURCE_BY_MODULE[context.module]}`
|
||||
const normalizedId = String(id ?? '').trim()
|
||||
return normalizedId ? `${base}/${encodeURIComponent(normalizedId)}` : base
|
||||
}
|
||||
|
||||
export function buildMarketFormOptionsPath(context) {
|
||||
assertContext(context)
|
||||
return `/mini/market/${context.marketRole}/${RESOURCE_BY_MODULE[context.module]}/form-options`
|
||||
}
|
||||
|
||||
export function buildMarketAttachmentPath(context) {
|
||||
assertContext(context)
|
||||
return `/mini/market/${context.marketRole}/${context.module}/attachments`
|
||||
}
|
||||
|
||||
export function buildMarketActionPath(context, action, id) {
|
||||
assertContext(context)
|
||||
const roleBase = `/mini/market/${context.marketRole}`
|
||||
const encodedId = encodeURIComponent(String(id ?? '').trim())
|
||||
if (context.module === 'presign' && encodedId) {
|
||||
if (action === 'delete') return buildMarketResourcePath(context, id)
|
||||
if (action === 'convert') return `${buildMarketResourcePath(context, id)}/convert`
|
||||
if (action === 'create-transfer') {
|
||||
return `${buildMarketResourcePath(context, id)}/transfers`
|
||||
}
|
||||
if (['edit-presign-transfer', 'delete-transfer'].includes(action)) {
|
||||
return `${roleBase}/presign-transfers/${encodedId}`
|
||||
}
|
||||
if (['submit-transfer', 'withdraw-transfer'].includes(action)) {
|
||||
const suffix = action === 'submit-transfer' ? 'submit' : 'withdraw'
|
||||
return `${roleBase}/presign-transfers/${encodedId}/${suffix}`
|
||||
}
|
||||
}
|
||||
if (context.module === 'payment') {
|
||||
if (action === 'register-transfer') return `${roleBase}/payment-transfers`
|
||||
if (['edit-transfer', 'submit', 'withdraw'].includes(action) && encodedId) {
|
||||
const suffix = action === 'edit-transfer' ? '' : `/${action}`
|
||||
return `${roleBase}/payment-transfers/${encodedId}${suffix}`
|
||||
}
|
||||
}
|
||||
if (context.module === 'sample' && ['storage-cert', 'insurance'].includes(action) && encodedId) {
|
||||
return `${buildMarketResourcePath(context, id)}/${action}`
|
||||
}
|
||||
if (context.module === 'freight' && ['link', 'unlink'].includes(action) && encodedId) {
|
||||
return `${buildMarketResourcePath(context, id)}/${action}`
|
||||
}
|
||||
throw new Error('不支持的市场业务操作')
|
||||
}
|
||||
|
||||
export async function fetchMarketList(context, params, { signal } = {}) {
|
||||
const { request } = await import('../utils/http.js')
|
||||
return request({
|
||||
url: buildMarketResourcePath(context),
|
||||
method: 'GET',
|
||||
data: params,
|
||||
signal
|
||||
})
|
||||
}
|
||||
|
||||
export async function fetchMarketDetail(context, id, { signal, params } = {}) {
|
||||
const { request } = await import('../utils/http.js')
|
||||
return request({
|
||||
url: buildMarketResourcePath(context, id),
|
||||
method: 'GET',
|
||||
data: params,
|
||||
signal
|
||||
})
|
||||
}
|
||||
|
||||
export async function fetchMarketFormOptions(context, params, { signal } = {}) {
|
||||
const { request } = await import('../utils/http.js')
|
||||
return request({ url: buildMarketFormOptionsPath(context), method: 'GET', data: params, signal })
|
||||
}
|
||||
|
||||
export async function saveMarketRecord(context, { id, data }, { signal } = {}) {
|
||||
const { request } = await import('../utils/http.js')
|
||||
return request({
|
||||
url: buildMarketResourcePath(context, id),
|
||||
method: id ? 'PUT' : 'POST',
|
||||
data,
|
||||
signal
|
||||
})
|
||||
}
|
||||
|
||||
export function marketActionMethod(action) {
|
||||
if (['delete', 'delete-transfer'].includes(action)) return 'DELETE'
|
||||
return ['edit-transfer', 'edit-presign-transfer', 'storage-cert', 'insurance'].includes(action)
|
||||
? 'PUT'
|
||||
: 'POST'
|
||||
}
|
||||
|
||||
export async function performMarketAction(context, action, { id, data } = {}, { signal } = {}) {
|
||||
const { request } = await import('../utils/http.js')
|
||||
return request({
|
||||
url: buildMarketActionPath(context, action, id),
|
||||
method: marketActionMethod(action),
|
||||
data,
|
||||
signal
|
||||
})
|
||||
}
|
||||
|
||||
export const deletePresignOrder = (context, id, options) =>
|
||||
performMarketAction(context, 'delete', { id }, options)
|
||||
|
||||
export const createPresignTransfer = (context, orderId, data, options) =>
|
||||
performMarketAction(context, 'create-transfer', { id: orderId, data }, options)
|
||||
|
||||
export const updatePresignTransfer = (context, transferId, data, options) =>
|
||||
performMarketAction(context, 'edit-presign-transfer', { id: transferId, data }, options)
|
||||
|
||||
export const deletePresignTransfer = (context, transferId, options) =>
|
||||
performMarketAction(context, 'delete-transfer', { id: transferId }, options)
|
||||
|
||||
export const submitPresignTransfer = (context, transferId, options) =>
|
||||
performMarketAction(context, 'submit-transfer', { id: transferId }, options)
|
||||
|
||||
export const withdrawPresignTransfer = (context, transferId, options) =>
|
||||
performMarketAction(context, 'withdraw-transfer', { id: transferId }, options)
|
||||
|
||||
export async function uploadMarketAttachment(context, file, { signal } = {}) {
|
||||
const { request } = await import('../utils/http.js')
|
||||
const data = new FormData()
|
||||
data.append('file', file)
|
||||
return request({ url: buildMarketAttachmentPath(context), method: 'POST', data, signal })
|
||||
}
|
||||
|
||||
export async function fetchFreightSampleOptions(context, params, { signal } = {}) {
|
||||
const { request } = await import('../utils/http.js')
|
||||
return request({
|
||||
url: `${buildMarketResourcePath(context)}/sample-options`,
|
||||
method: 'GET',
|
||||
data: params,
|
||||
signal
|
||||
})
|
||||
}
|
||||
|
||||
export function marketModuleContract(context) {
|
||||
assertContext(context)
|
||||
return getMarketModule(context.module)
|
||||
}
|
||||
Reference in new issue
Block a user