index.js
6.09 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// tabRepairOrder/components/servicePopup/index.js
const IMG_URL =
'https://img02.glsx.com.cn/weapp/resource/dj-car-boutique-work-wechat/'
import api from '../../api'
Component({
/**
* 组件的属性列表
*/
properties: {
show: {
type: Boolean,
observer: function (n) {
if (n) {
let { selected } = this.data
this.setData(
{
searchValue: '',
selectedData: wx.$util.deepClone(selected)
},
() => {
this.getList(true)
}
)
}
}
},
selected: {
type: Array,
observer: function (n) {}
},
selectGoodsList: {
type: Array,
observer: function (n) {}
}
},
/**
* 组件的初始数据
*/
data: {
IMG_URL,
servicesList: [],
copyServicesList: [],
selectedData: []
},
/**
* 组件的方法列表
*/
methods: {
handelSearchInput(e) {
this.setData({
searchValue: e.detail.value
})
},
updateNumber(id, value, isBlur = false) {
let { servicesList, copyServicesList } = this.data
const finalValue = isBlur ? (value < 1 ? 1 : value) : value
const updateList = list => {
return list?.map(item => ({
...item,
detailList: item?.detailList?.map(child =>
child.id === id ? { ...child, useNum: finalValue } : child
)
}))
}
this.setData({
servicesList: updateList(servicesList),
copyServicesList: updateList(copyServicesList)
})
},
handleCountInput(e) {
let { value } = e.detail
let { target } = e.currentTarget.dataset
value = value.replace(/[^\d]/g, '')
this.updateNumber(target.id, value, false)
},
handleCountBlur(e) {
let { value } = e.detail
let { target } = e.currentTarget.dataset
value = value.replace(/[^\d]/g, '')
this.updateNumber(target.id, value, true)
},
// 获取服务项目数据
getList(firstFlag) {
let { searchValue, selectGoodsList } = this.data
const subOrderCodeList =
selectGoodsList?.map(item => item.subOrderCode) || []
api
.getServiceItemList({ serviceItemsName: searchValue, subOrderCodeList })
.then(res => {
let { code, data, message } = res
if (code == 0) {
let { selectedData } = this.data
const selectedIds =
selectedData?.reduce((set, item) => {
item?.detailList?.forEach(({ serviceItemsId }) =>
set.add(serviceItemsId)
)
return set
}, new Set()) ?? new Set()
const selectedItems =
selectedData?.reduce((arr, item) => {
item?.detailList?.forEach(({ serviceItemsId, useNum }) => {
// 检查是否已存在相同的id
const existingItem = arr.find(
obj => obj.id === serviceItemsId
)
if (!existingItem) {
arr.push({
id: serviceItemsId,
useNum: useNum
})
}
})
return arr
}, []) ?? []
data?.forEach(val => {
val.title = `${val.categoryName}-${val.itemName}`
val.detailList = val.serviceItemList?.map(item => ({
...item,
serviceItemsId: item.id,
active: selectedIds.has(item.id),
useNum: selectedItems?.find(v => v.id == item.id)?.useNum
}))
})
this.setData({
servicesList: data
})
if (firstFlag) {
this.setData({
copyServicesList: wx.$util.deepClone(data || [])
})
}
} else {
wx.$util.showToast(message)
}
})
},
search() {
this.getList()
},
// 服务项目选择
handleChecked(e) {
let { target } = e.currentTarget.dataset
let { servicesList, copyServicesList } = this.data
servicesList?.map(val => {
val?.detailList?.map(v => {
if (v.id == target.id) {
v.active = !v.active
v.useNum = 1
}
return v
})
return val
})
copyServicesList?.map(val => {
val?.detailList?.map(v => {
if (v.id == target.id) {
v.active = !v.active
v.useNum = 1
}
return v
})
return val
})
const filteredServicesList =
copyServicesList
?.map(val => {
if (!val?.detailList) return null
return {
...val,
detailList: val.detailList.filter(v => v.active === true)
}
})
.filter(val => val && val.detailList.length > 0) || []
this.setData({
servicesList,
copyServicesList,
selectedData: filteredServicesList
})
},
// 展开收起服务项目
handleShowItem(e) {
let { index } = e.currentTarget.dataset
let { servicesList } = this.data
this.setData({
[`servicesList[${index}].showItemFlag`]:
!servicesList[index].showItemFlag
})
},
cancel() {
this.triggerEvent('cancel')
},
confirm() {
let { servicesList, copyServicesList } = this.data
const result = copyServicesList
.map(item => ({
...item,
detailList: item?.detailList.filter(child => child.active)
}))
.filter(item => item.detailList?.length > 0)
.map(val => ({
...val,
useNum: val.useNum < 1 ? 1 : val.useNum // 兜底处理 当输入框为空值或0时默认赋值1 ,防止失去焦点赋值导致数据错误
}))
const activeNum = result.reduce((total, item) => {
return total + (item?.detailList?.length || 0)
}, 0)
if (activeNum <= 0) return wx.$util.showToast('请选择服务项目')
this.triggerEvent('confirm', result)
}
}
})