index.js
4.05 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
import wx from '../../utils/wx';
import Promise from '../../vendors/es6-promise';
import commonModel from '../../models/common';
import indexModel from '../../models/index/index';
// 获取应用实例
const app = getApp();
const router = global.router;
Page({
data: {
shopInfo: {},
shopBanner: '',
shopProductList: [],
decorFloors: []
},
onLoad: function() {
},
onShow: function() {
const shopId = app.getShopId();
if (shopId) {
this.getShopData(shopId);
this.getShopProducts(shopId);
} else {
commonModel.getBindShop({app_id: app.getAppId()}).then(res => {
if (res.code === 200 && res.data.shopId) {
const id = res.data.shopId;
app.globalData.unionShop = res.data;
app._setSync('unionShop', res.data);
this.getShopData(id);
this.getShopProducts(id);
} else {
return Promise.reject();
}
}).catch(() => {});
}
},
getShopData: function(shopId) {
this._shopInfo(shopId);
this._shopDecor(shopId);
this._shopFavCount(shopId);
},
getShopProducts: function() {
this._productList();
},
// 店铺信息
_shopInfo: function(shopId) {
const params = {
shop_id: shopId
};
indexModel.shopInfo(params)
.then(res => {
if (res.data.shop_name) {
let shopName = res.data.shop_name;
wx.setNavigationBarTitle({
title: shopName,
});
}
let shopInfo = res.data;
this.setData({
shopInfo: shopInfo
});
});
},
// 店铺装修
_shopDecor: function(shopId) {
const params = {
shop_id: shopId
};
indexModel.shopDecorator(params)
.then(res => {
if (res.code === 200) {
let floors = [];
let modules = res.data.modules;
if (modules && modules.length > 0) {
floors = modules.map(module => {
module.data = JSON.parse(module.module_data).data;
module.properties = JSON.parse(module.module_data).properties;
delete module.module_data;
return module;
});
const banner = floors[0];
if (banner && banner.module_type === 'ShopBanner') {
this.setData({
shopBanner: banner.data[0] && banner.data[0].pic
});
}
this.setData({
decorFloors: floors
});
}
}
})
.catch(() => {});
},
// 店铺收藏
_shopFavCount: function(shopId) {
indexModel.shopFavCount({favIds: shopId}).then(res => {
const favCount = res.data[0].approximateCount || '2.1w';
this.setData({
favCount
});
});
},
// 筛选变更
filterChange: function(e) {
let params;
let {curSort, gender} = e.detail;
this.data.order = curSort;
this.data.gender = gender;
params = {
gender,
order: curSort,
page: 1,
limit: 20
};
this.data.productList = [];
params.setScrollPos = true;
this._productList(params);
},
// 商品列表
_productList: function(params) {
params = params || {page: 1, pageNo: 20};
params.shop_id = app.getShopId();
indexModel.shopProductList(params).then(res => {
if (res.code === 200) {
const keyAdapter = {
skn: 'product_skn',
salesPrice: 'sales_price',
marketPrice: 'market_price',
productName: 'product_name',
defaultImages: 'default_images'
};
let list = [];
(res.data.product_list || []).forEach(product => {
let item = {};
Object.keys(keyAdapter).forEach(key => {
item[key] = product[keyAdapter[key]];
});
list.push(item);
});
this.setData({
currentPage: params.page,
productList: this.data.shopProductList.concat(list),
totalPage: res.data && res.data.page_total || 0
});
}
}).catch(() => {});
},
toSearch() {
router.go('productSearch');
}
});