CarBrand.js
5.54 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
216
// packageMine/components/CarBrand/CarBrand.js
import mineApi from '~/api/mineApi/index'
import Util from '~/utils/util'
Component({
/**
* 组件的属性列表
*/
properties: {
popupShow: {
type: Boolean,
value: false,
observer: function (n) {
if (n) {
this.getBrandList()
}
}
},
selectedList: {
type: Array,
value: []
}
},
/**
* 组件的初始数据
*/
data: {
selectingList: []
},
/**
* 组件的方法列表
*/
methods: {
dealSelectedList(brandList) {
return new Promise(resolve => {
let { selectedList } = this.data
selectedList.forEach((item, idx) => {
let brandIdx = brandList.findIndex(k => k.id == item.id)
let brandChildList = brandList[brandIdx].children
item.label = item.name
item.children.forEach((subItem, subIdx) => {
let cIdx = brandChildList.findIndex(k => k.id == subItem.id)
brandList[brandIdx].children[cIdx].active = true
subItem.active = true
subItem.label = subItem.name
})
})
this.setData({
selectingList: Util.deepClone(selectedList)
})
resolve(brandList)
})
},
getBrandList() {
let data = wx.$storage.get('car_brand_list')
if (data) {
this.dealSelectedList(Util.deepClone(data)).then(brandlist => {
this.formatData(brandlist)
})
} else {
mineApi.getCarBrandList().then(data => {
wx.$storage.set('car_brand_list', data)
this.dealSelectedList(Util.deepClone(data)).then(brandlist => {
this.formatData(brandlist)
})
})
}
},
formatData(data) {
let letterList = Array.from(new Set([...data.map(e => e.letter)]))
let brandList = letterList.map(e => {
let list = []
data.map(i => {
if (e === i.letter) {
list.push(i)
}
})
return {
letter: e,
list
}
})
this.setCarData(brandList)
},
setCarData(brandList) {
wx.$api.showLoading('加载中...')
let count = 0
let settime = setInterval(() => {
if (null != brandList[count].list && brandList[count].list.length > 0) {
this.setData({
[`brandList[${count}]`]: brandList[count]
})
}
count++
if (count >= brandList.length) {
clearInterval(settime)
this.getLetter()
}
}, 10)
},
getLetter() {
wx.hideLoading()
let { brandList, letter } = this.data
let arr = []
if (brandList.length > 0) {
brandList.forEach(e => {
arr.push(e.letter)
})
this.setData({
arr,
letter,
flag: true
})
}
setTimeout(() => {
this.getSideBarInfo()
}, 500)
},
getSideBarInfo() {
let _this = this
wx.$api.getViewInfo('#letter-A', res => {
wx.$api.getViewInfo('#side-bar', _res => {
_this.setData({
letterHeight: res.height,
toTop: res.height + _res.top,
height: _res.bottom
})
})
})
},
dealSelectList(index, pidx, cidx) {
let { selectingList } = this.data
let pItem = Util.deepClone(this.data.brandList[index].list[pidx])
// 已选列表中,当前选中的品牌是否存在
let selectPItemId = selectingList.findIndex(item => item.id == pItem.id)
// 不存在该品牌,插入;存在该品牌,替换
if (selectPItemId == -1) {
selectingList.push(pItem)
this.setData({
selectingList
})
return true
}
this.setData({
[`selectingList[${selectPItemId}]`]: pItem
})
},
// 选中/取消子品牌
clickChild(e) {
let { index, pidx, cidx } = e.currentTarget.dataset
let childItem = this.data.brandList[index].list[pidx].children[cidx]
this.setData({
[`brandList[${index}].list[${pidx}].children[${cidx}].active`]: !childItem.active
})
this.dealSelectList(index, pidx, cidx)
},
handleConfirm() {
let { selectingList } = this.data
let _list = []
let idx = 0
selectingList.forEach(item => {
if (item.children.some(k => k.active == true)) {
_list.push({
id: item.id,
name: item.label,
logoUrl: item.logoUrl,
children: []
})
item.children.forEach(k => {
if (k.active) {
_list[idx].children.push({
id: k.id,
name: k.label
})
}
})
idx = idx + 1
}
})
console.log(_list, '_list处理后---')
return _list
},
start(e) {
let { letter } = e.target.dataset
this.setData({
showTip: true,
letter
})
},
move(e) {
let { letterHeight, showTip, height, toTop, arr } = this.data
let { y, num, isNum } = this
if (showTip) {
y = e.touches[0].clientY
num = Math.ceil((y - toTop) / letterHeight)
if (num < 0) num = 0
if (y < height && isNum != num) {
clearTimeout(this.time)
isNum = num
this.time = setTimeout(() => {
this.setData({
letter: arr[num]
})
}, 1000 / 60)
}
}
},
end(e) {
clearTimeout(this.time)
this.setData({
showTip: false
})
}
}
})