sideSelect.helpers.js
3.16 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
/**
* @file SideSelectPopup 纯逻辑 — dataSource 分支与 prop 模式门店高亮
* 供 SideSelect/SideSelectByPage 合并后单测;组件内 setData 前调用。
*/
/**
* 是否分页自拉门店列表
* @param {string} dataSource
* @returns {boolean}
*/
export function shouldUsePageDataSource(dataSource) {
return dataSource === 'page'
}
/**
* show=true 时重置的 UI 状态(prop/page 共用)
* @param {boolean} show
* @returns {object|null}
*/
export function getInitialPageState(show) {
if (!show) return null
return {
searchValue: '',
edit: false,
customSearchFlag: false,
showSelect: false,
nothing: false,
pageNum: 1
}
}
/**
* prop 模式:对 storeList 计算 actived 高亮
* @param {object[]} storeList
* @param {object} userInfo
* @param {number} subIndex
* @param {string} sceneName
* @param {boolean} isSuper
* @returns {object[]}
*/
export function mapStoreActived(storeList, userInfo, subIndex, sceneName, isSuper) {
return (storeList || []).map((val, index) => {
const item = {
...val,
actived: false,
index,
sceneName: val.sceneName || sceneName
}
if (userInfo?.sceneType == 1) {
if (isSuper && userInfo?.shopMerchantId == val.merchantId) item.actived = true
else if (!isSuper && val.merchantId == userInfo?.shopMerchantId) item.actived = true
} else {
const matchDefault4S =
(userInfo?.sceneType == 0 &&
userInfo?.default4SStoreInfo?.merchantId == val.merchantId) ||
(userInfo?.sceneType == 3 &&
userInfo?.default4SStoreInfo_crowdFunding?.merchantId == val.merchantId) ||
(userInfo?.sceneType == 2 &&
userInfo?.default4SStoreInfo_newCarConversion?.merchantId == val.merchantId)
if (isSuper && matchDefault4S) item.actived = true
else if (!isSuper && val.merchantId == userInfo?.shopMerchantId) item.actived = true
}
return item
})
}
/**
* 按属性去重(page 模式门店列表)
* @param {object[]} arr
* @param {string} prop
* @returns {object[]}
*/
export function uniqueByProperty(arr, prop) {
const map = new Map()
;(arr || []).forEach(obj => {
map.set(obj[prop], obj)
})
return Array.from(map.values())
}
/**
* page 模式 API 返回列表后处理:prepend「全部」、补当前店、高亮
* @param {object[]} tempList
* @param {object} userInfo
* @param {string} sceneName
* @param {string} searchValue
* @returns {object[]}
*/
export function finalizePageModeStoreList(tempList, userInfo, sceneName, searchValue) {
let list = [...(tempList || [])]
if (!searchValue) {
if (!list.some(v => v.merchantName === '全部')) {
list.unshift({ merchantName: '全部' })
}
if (
userInfo?.shopMerchantId &&
!list.some(v => v.merchantId === userInfo.shopMerchantId)
) {
list.push({
merchantId: userInfo.shopMerchantId,
merchantName: userInfo.shopMerchantName
})
}
}
list = uniqueByProperty(list, 'merchantId')
return list.map(v => ({
...v,
actived:
userInfo?.shopMerchantId === v.merchantId ||
(!userInfo?.shopMerchantId && !v.merchantId),
sceneName: v.sceneName || sceneName
}))
}