authContext.mjs
4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const METHODS = {
login: 'dj.api.smartcarlife.sms.login',
listAccounts: 'dj.api.smartcarlife.workWechat.listSelectAccount',
selectAccount: 'dj.api.smartcarlife.workWechat.selectAccountLogin',
listStores: 'dj.api.scenemerchantref.query4SRefByYqShopByLogin',
selectScene: 'dj.api.smartcarlife.selectMerchantScene'
}
const isSuccess = response => response?.code === 0 || response?.code === '0'
const requireSuccess = (method, response) => {
if (!isSuccess(response)) {
const code = response?.code ?? 'missing'
throw new Error(`ROP business call failed: ${method} (code ${code})`)
}
return response.data
}
const listFrom = value => {
if (Array.isArray(value)) return value
if (!value || typeof value !== 'object') return []
for (const key of ['list', 'records', 'rows', 'dataList', 'pageData']) {
if (Array.isArray(value[key])) return value[key]
}
return []
}
const displayName = item =>
String(
item?.merchantName ?? item?.yqMerchantName ?? item?.shopMerchantName ?? ''
).trim()
const stableId = item =>
String(item?.uuid ?? item?.merchantId ?? item?.shopMerchantId ?? item?.id ?? '')
const selectExact = (items, expectedName, label) => {
const candidates = listFrom(items)
const matches = candidates.filter(item => displayName(item) === expectedName)
if (matches.length === 1) return matches[0]
const names = [...new Set(candidates.map(displayName).filter(Boolean))].sort(
(left, right) => left.localeCompare(right, 'zh-CN')
)
if (matches.length === 0) {
throw new Error(
`No exact ${label} match for "${expectedName}". Candidates: ${
names.join(', ') || '(none)'
}`
)
}
const ids = matches.map(stableId).sort().join(', ')
throw new Error(`Duplicate exact ${label} matches for "${expectedName}": ${ids}`)
}
const createContext = ({ sessionId, park, store, user, scene, sceneType }) => {
const context = {
park: { id: park.merchantId, name: displayName(park), accountId: park.uuid },
store: { id: store.merchantId, name: displayName(store) },
user,
scene,
sceneType
}
Object.defineProperty(context, 'sessionId', {
value: sessionId,
enumerable: false,
writable: false,
configurable: false
})
return context
}
export async function loginAndSelectContext(client, config) {
const loginResponse = await client.post(METHODS.login, {
mobile: config.mobile,
code: config.code,
source: 'MINIAPP',
productCode: 'PC00c2'
})
const loginData = requireSuccess(METHODS.login, loginResponse)
let sessionId = loginData?.sessionId
if (!sessionId) {
throw new Error(`ROP response missing session for ${METHODS.login}`)
}
const accountResponse = await client.post(METHODS.listAccounts, { sessionId })
const accountData = requireSuccess(METHODS.listAccounts, accountResponse)
const accountList =
listFrom(accountData?.loginUserInfoList).length > 0
? accountData.loginUserInfoList
: loginData?.loginUserInfoList
const park = selectExact(accountList, config.yqName, 'park account')
if (!park.uuid) {
throw new Error(`Exact park account is missing uuid: ${config.yqName}`)
}
const selectedAccountResponse = await client.post(METHODS.selectAccount, {
id: park.uuid,
sessionId
})
const selectedAccountData = requireSuccess(
METHODS.selectAccount,
selectedAccountResponse
)
sessionId = selectedAccountData?.sessionId || sessionId
const user = listFrom(selectedAccountData?.loginUserInfoList)[0] || park
const storeResponse = await client.post(METHODS.listStores, {
merchantId: park.merchantId,
staffUUID: user.uuid || park.uuid,
sessionId
})
const storeData = requireSuccess(METHODS.listStores, storeResponse)
const store = selectExact(storeData, config.storeName, 'store')
const sceneResponse = await client.post(METHODS.selectScene, {
merchantId: store.merchantId,
merchantName: displayName(store),
sceneType: config.sceneType,
sessionId
})
const scene = requireSuccess(METHODS.selectScene, sceneResponse)
return createContext({
sessionId,
park,
store,
user,
scene,
sceneType: config.sceneType
})
}
export { METHODS, listFrom, selectExact }