home.js
3.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
import wx from '../../utils/wx';
import event from '../../common/event';
import Yas from '../../common/yas';
import homeModel from '../../models/home/index';
import accountModel from '../../models/account/index';
import {getPhoneNumber, getUserInfoLogin} from '../../common/login';
// 获取应用实例
let app = getApp();
let router = global.router;
let yas;
Page({
data: {
productList: [],
userInfo: {},
isLogin: false,
hasUnionID: false,
infoNum: {}
},
getUserInfoLogin,
getPhoneNumber,
onLoad: function() {
event.on('user-login-success', this.loginSucess);
event.on('bind-auto-register-type-report', params => {
yas.report('YB_REGISTER_SUCCESS', params);
});
event.on('wx-union-id-update', () => {
console.log('update', !!app.globalData.unionID);
this.setData({
hasUnionID: !!app.globalData.unionID
});
});
this.setData({
hasUnionID: !!app.getUnionID()
});
if (app.getUid()) {
this.showUserInfo();
}
this.getInfoNum();
this.chooseForYouList();
yas = new Yas(app);
},
onShow: function() {
yas.pageOpenReport();
yas.report('YB_MAIN_TAB_C', {TAB_ID: 4});
},
loginSucess: function() {
this.showUserInfo();
this.getInfoNum();
this.chooseForYouList();
},
getInfoNum: function() {
if (app.getUid()) {
homeModel.infoNum()
.then(res => {
if (res.code === 200) {
this.data.infoNum = res.data;
this.setData({
infoNum: this.data.infoNum,
});
}
});
}
},
chooseForYouList: function() {
homeModel.productList({
page: 1,
limit: 20
}).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({
productList: list
});
}
});
},
toOrdersTap: function(e) {
if (!this.data.isLogin) {
return wx.showToast({
title: '请先完成登录/注册,再查看!',
icon: 'none',
duration: 2000
});
}
let type = parseInt(e.currentTarget.dataset.type);
const EVENTS = {
1: 'YB_MY_ORD', // 我的订单
2: 'YB_MY_TOPAY', // 待付款
3: 'YB_MY_TOSEND', // 待发货
4: 'YB_MY_TOREC', // 待收货
};
yas.report(EVENTS[type]);
router.go('orderList', {type});
},
toAddress: function() {
if (!this.data.isLogin) {
return wx.showToast({
title: '请先完成登录/注册,再查看!',
icon: 'none',
duration: 2000
});
}
router.go('address');
},
toService: function() {
router.go('service');
},
showUserInfo: function() {
const defaultAvatar = 'https://img10.static.yhbimg.com/headimg/2013/11/28/09/01cae078abe5fe320c88cdf4c220212688.gif';
accountModel.getProfile().then(res => {
wx.hideLoading();
this.setData({
isLogin: true,
userInfo: {
phoneNum: res.data.mobile,
nickName: res.data.nickname,
avatarUrl: res.data.head_ico || defaultAvatar
}
});
}).catch(() => {
wx.hideLoading();
});
}
});