brands.js
3.44 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
import { ACTIVITY_HOST, API_HOST } from '../../libs/config';
import { GET, POST } from '../../libs/request';
import { jumpByUrl } from '../../libs/urlRoute';
let app = getApp();
let bannerW = 640;
let bannerH = 310;
let itemWidth = Math.ceil((app.globalData.systemInfo.windowWidth - 40) / 3);
let itemHeight = itemWidth - 40;
const windowHeight = app.globalData.systemInfo.windowHeight;
const scrollHeight = windowHeight;
Page({
/**
* 页面的初始数据
*/
data: {
scrollHeight,
itemWidth,
itemHeight,
scrollToView: '',
brandsList: [],
all_list: [],
all_list_key: [],
hotKeyword: {},
searching: false,
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
let res = wx.getSystemInfoSync();
let scrollHeight = res.screenHeight;
if (!scrollHeight) {
scrollHeight = res.windowHeight;
}
this.setData({
scrollHeight,
});
this.fetchBrandList();
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
},
/**
* 获取品牌列表
*/
fetchBrandList: function () {
let that = this;
GET(ACTIVITY_HOST + '/api/yohood/getBrandList', {})
.then(data => {
let brandsList = data_letter_sort(data.data);
that.setData({
brandsList,
});
})
.catch(error => {
});
},
activityItemTapped: function (event) {
let brandItem = event.currentTarget.dataset.brandItem;
let jumpUrl = brandItem.url;
jumpByUrl(jumpUrl, 'brands');
},
brandItemTapped: function(event) {
let brandItem = event.currentTarget.dataset.brandItem;
wx.navigateTo({
url: './brandDetail?brandId=' + brandItem.id + '&brandName=' + brandItem.name
});
},
handlerAlphaTap(e) {
let { ap } = e.target.dataset;
this.setData({ scrollToView: ap });
},
dismissSearchView(e) {
this.setData({ searching: false });
},
showSearchView(e) {
this.setData({ searching: true });
},
handlerMove(){
},
})
function data_letter_sort(data) {
var all_list_old = data;
var letter_reg = /^[A-Z]$/;
var list = new Array();
for (var i = 0; i < all_list_old.length; i++) {
// 添加 # 分组,用来 存放 首字母不能 转为 大写英文的 数据
// list['#'] = new Array();
// 首字母
let barndName = all_list_old[i].name || '#';
let letter = barndName.substr(0, 1);
// 是否 大写 英文 字母
if (!letter_reg.test(letter)) {
letter = '#';
}
// 创建 字母 分组
if (!(letter in list)) {
list[letter] = new Array();
}
// 字母 分组 添加 数据
list[letter].push(all_list_old[i]);
}
// 转换 格式 进行 排序;
var result = new Array();
for (var key in list) {
result.push({
letter: key,
list: list[key]
});
}
result.sort(function (x, y) {
return x.letter.charCodeAt(0) - y.letter.charCodeAt(0);
});
// # 号分组 放最后
var last_arr = result[0];
result.splice(0, 1);
result.push(last_arr);
// 转换 数据 {} 格式
// var json_sort = {}
// for (var i = 0; i < result.length; i++) {
// json_sort[result[i].letter] = result[i].list;
// }
return result;
}
function isEmptyObject(obj) {
for (var key in obj) {
return false;
}
return true;
}