统一前后端代码格式化配置
This commit is contained in:
@@ -20,10 +20,7 @@ const CURRENT_DIR = path.dirname(fileURLToPath(import.meta.url))
|
||||
const PROJECT_ROOT = path.resolve(CURRENT_DIR, '..')
|
||||
const WORKSPACE_ROOT = path.resolve(PROJECT_ROOT, '../..')
|
||||
|
||||
loadEnvFiles([
|
||||
path.join(WORKSPACE_ROOT, '.env'),
|
||||
path.join(PROJECT_ROOT, '.env'),
|
||||
])
|
||||
loadEnvFiles([path.join(WORKSPACE_ROOT, '.env'), path.join(PROJECT_ROOT, '.env')])
|
||||
|
||||
const accessToken = String(process.env.KUASHOU_INDUSTRY_ACCESS_TOKEN || 'your_access_token').trim()
|
||||
const appKey = String(process.env.KUASHOU_INDUSTRY_APP_KEY || 'your_app_key').trim()
|
||||
@@ -31,7 +28,7 @@ const signSecret = String(process.env.KUASHOU_INDUSTRY_SIGN_SECRET || 'your_sign
|
||||
|
||||
const args = parseArgs(process.argv.slice(2))
|
||||
const mode = String(args.mode || 'send').trim() // send | consume | destroy
|
||||
const oid = String(args.oid || String(Date.now())).trim() // 快手要求纯数字 oid
|
||||
const oid = String(args.oid || String(Date.now())).trim() // 快手要求纯数字 oid
|
||||
const sendNum = normalizeInt(args.num, 3)
|
||||
const token = String(args.token || `test-token-${Date.now()}`).trim()
|
||||
const bodyAccessToken = String(args.accessToken || accessToken).trim()
|
||||
@@ -41,57 +38,60 @@ const KUAISHOU_OPEN_API = 'https://openapi.kwaixiaodian.com'
|
||||
const now = Date.now()
|
||||
const validEnd = now + 30 * 24 * 60 * 60 * 1000
|
||||
|
||||
const bizParams: JsonObject = mode === 'consume'
|
||||
? {
|
||||
oid,
|
||||
etickets: Array.from({ length: sendNum }, (_, i) => ({
|
||||
id: String(i + 1),
|
||||
num: 1,
|
||||
goodsValue: 10,
|
||||
})),
|
||||
status: 'CONSUMED',
|
||||
consumeType: 'consume',
|
||||
consumeTime: now,
|
||||
storeName: '测试门店',
|
||||
storeAddress: '测试地址',
|
||||
seriallNum: `SN${Date.now()}`,
|
||||
token,
|
||||
}
|
||||
: mode === 'destroy'
|
||||
? {
|
||||
oid,
|
||||
etickets: Array.from({ length: sendNum }, (_, i) => ({
|
||||
id: String(i + 1),
|
||||
num: 1,
|
||||
goodsValue: 100,
|
||||
})),
|
||||
reason: 'ETICKET_EXPIRED',
|
||||
token,
|
||||
}
|
||||
: {
|
||||
oid,
|
||||
sendType: 'VIRTUAL',
|
||||
etickets: Array.from({ length: sendNum }, (_, i) => ({
|
||||
id: String(i + 1),
|
||||
num: 1,
|
||||
validStartTime: now,
|
||||
validEndTime: validEnd,
|
||||
})),
|
||||
sendNum,
|
||||
token,
|
||||
}
|
||||
const bizParams: JsonObject =
|
||||
mode === 'consume'
|
||||
? {
|
||||
oid,
|
||||
etickets: Array.from({ length: sendNum }, (_, i) => ({
|
||||
id: String(i + 1),
|
||||
num: 1,
|
||||
goodsValue: 10,
|
||||
})),
|
||||
status: 'CONSUMED',
|
||||
consumeType: 'consume',
|
||||
consumeTime: now,
|
||||
storeName: '测试门店',
|
||||
storeAddress: '测试地址',
|
||||
seriallNum: `SN${Date.now()}`,
|
||||
token,
|
||||
}
|
||||
: mode === 'destroy'
|
||||
? {
|
||||
oid,
|
||||
etickets: Array.from({ length: sendNum }, (_, i) => ({
|
||||
id: String(i + 1),
|
||||
num: 1,
|
||||
goodsValue: 100,
|
||||
})),
|
||||
reason: 'ETICKET_EXPIRED',
|
||||
token,
|
||||
}
|
||||
: {
|
||||
oid,
|
||||
sendType: 'VIRTUAL',
|
||||
etickets: Array.from({ length: sendNum }, (_, i) => ({
|
||||
id: String(i + 1),
|
||||
num: 1,
|
||||
validStartTime: now,
|
||||
validEndTime: validEnd,
|
||||
})),
|
||||
sendNum,
|
||||
token,
|
||||
}
|
||||
|
||||
const method = mode === 'consume'
|
||||
? 'integration.callback.virtual.eticket.consume'
|
||||
: mode === 'destroy'
|
||||
? 'integration.callback.virtual.eticket.destroy'
|
||||
: 'integration.callback.virtual.eticket.send'
|
||||
const method =
|
||||
mode === 'consume'
|
||||
? 'integration.callback.virtual.eticket.consume'
|
||||
: mode === 'destroy'
|
||||
? 'integration.callback.virtual.eticket.destroy'
|
||||
: 'integration.callback.virtual.eticket.send'
|
||||
|
||||
const endpoint = mode === 'consume'
|
||||
? '/integration/callback/virtual/eticket/consume'
|
||||
: mode === 'destroy'
|
||||
? '/integration/callback/virtual/eticket/destroy'
|
||||
: '/integration/callback/virtual/eticket/send'
|
||||
const endpoint =
|
||||
mode === 'consume'
|
||||
? '/integration/callback/virtual/eticket/consume'
|
||||
: mode === 'destroy'
|
||||
? '/integration/callback/virtual/eticket/destroy'
|
||||
: '/integration/callback/virtual/eticket/send'
|
||||
|
||||
const paramStr = JSON.stringify(bizParams)
|
||||
|
||||
@@ -112,7 +112,9 @@ const modeLabel = mode === 'consume' ? '核销' : mode === 'destroy' ? '销毁'
|
||||
console.log(`# 电子凭证${modeLabel}回调 curl 命令`)
|
||||
console.log(`# oid=${oid} sendNum=${sendNum} access_token=${bodyAccessToken.slice(0, 10)}...`)
|
||||
console.log(`# 先测试网络连通性:`)
|
||||
console.log(`curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 https://openapi.kwaixiaodian.com/`)
|
||||
console.log(
|
||||
`curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 https://openapi.kwaixiaodian.com/`,
|
||||
)
|
||||
console.log('# 如果返回 000,说明网络不通,需要关闭代理或换服务器')
|
||||
console.log('')
|
||||
console.log('curl -X POST \\')
|
||||
@@ -130,10 +132,15 @@ console.log('')
|
||||
|
||||
// 本地签名校验(不调快手)
|
||||
console.log('# 本地签名校验(不会真正调用快手):')
|
||||
const localEndpoint = mode === 'consume' ? 'consume-code' : mode === 'destroy' ? 'destroy-code' : 'send-code'
|
||||
console.log(`curl -s -X POST http://127.0.0.1:3000/api/v1/open/kuaishou-industry/${localEndpoint} \\`)
|
||||
const localEndpoint =
|
||||
mode === 'consume' ? 'consume-code' : mode === 'destroy' ? 'destroy-code' : 'send-code'
|
||||
console.log(
|
||||
`curl -s -X POST http://127.0.0.1:3000/api/v1/open/kuaishou-industry/${localEndpoint} \\`,
|
||||
)
|
||||
console.log(" -H 'Content-Type: application/x-www-form-urlencoded' \\")
|
||||
console.log(` -d 'appkey=${appKey}&version=1×tamp=${now}&signMethod=MD5&access_token=&method=&sign=${sign}¶m=${encodeURIComponent(paramStr)}'`)
|
||||
console.log(
|
||||
` -d 'appkey=${appKey}&version=1×tamp=${now}&signMethod=MD5&access_token=&method=&sign=${sign}¶m=${encodeURIComponent(paramStr)}'`,
|
||||
)
|
||||
console.log('')
|
||||
|
||||
function signPayload(params: JsonObject, secret: string): string {
|
||||
@@ -141,7 +148,11 @@ function signPayload(params: JsonObject, secret: string): string {
|
||||
.filter(([k]) => k !== 'sign' && k !== 'signSecret')
|
||||
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
||||
const qs = entries.map(([k, v]) => `${k}=${stringify(v)}`).join('&')
|
||||
return crypto.createHash('md5').update(`${qs}&signSecret=${secret}`, 'utf8').digest('hex').toLowerCase()
|
||||
return crypto
|
||||
.createHash('md5')
|
||||
.update(`${qs}&signSecret=${secret}`, 'utf8')
|
||||
.digest('hex')
|
||||
.toLowerCase()
|
||||
}
|
||||
|
||||
function stringify(v: unknown): string {
|
||||
@@ -164,7 +175,10 @@ function parseArgs(raw: string[]): JsonObject {
|
||||
if (!arg) continue
|
||||
const n = arg.startsWith('--') ? arg.slice(2) : arg
|
||||
const eq = n.indexOf('=')
|
||||
if (eq < 0) { p[n] = true; continue }
|
||||
if (eq < 0) {
|
||||
p[n] = true
|
||||
continue
|
||||
}
|
||||
p[n.slice(0, eq).trim()] = n.slice(eq + 1).trim()
|
||||
}
|
||||
return p
|
||||
|
||||
Reference in New Issue
Block a user