index.js
6.53 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// index.js
import wx from '../../utils/wx';
import event from '../../common/event';
import {getLoginButtonType} from '../../common/login';
import {parse} from '../../vendors/query-stringify';
import accountModel from '../../models/account/index';
import Promise from '../../vendors/es6-promise';
import config from '../../common/config';
import Yas from '../../common/yas';
import helper from '../../utils/helper';
let app = getApp();
let yas;
let router = global.router;
Page({
data: {
loginButtonType: '', // 获取登录button-type
userInfo: {
phoneNum: '',
avatarUrl: '',
defaultAvatar: false,
nickName: '',
wechat: ''
},
coinNum: 0,
isBound: false,
myQrCodeUrl: 'https://m.yohobuy.com/home/newQrcode?needLogin=1',
loginText: '微信登录',
loginTips: '',
now_test_uid_index: 1,
containerHeight: '1006rpx',
h5PageUrl: [
{
title: '如何获得限购码?',
url: 'https://activity.yoho.cn/feature/247.html?nodownload=1'
}
],
limitCodes: {
codes: [],
invalidCodes: []
},
isShow: 0,
referer: '',
needBind: 1,
showLogin: 0
},
onLoad: function(options) {
let that = this;
if (options.referer) {
console.log('referer:', options.referer);
let referer = decodeURIComponent(options.referer);
if (referer.indexOf('/') !== 0) {
referer = '/' + referer;
}
this.setData({
referer: referer
});
}
if (options.login || options.login === 0) {
this.setData({
needBind: (options.login === '0' || !options.login) ? 0 : true
});
}
if (app.getUid()) {
setTimeout(() => {
this.setData({ // 用来防止总是闪出登录界面后再显示限购码
showLogin: 1
});
}, 1000);
} else {
this.setData({ // 用来防止总是闪出登录界面后再显示限购码
showLogin: 1
});
}
yas = new Yas(app); // 实例化埋点
yas.pageOpenReport();
event.on('user-login-success', this.showBindUserInfo);
event.on('user-login-callback', this.loginCallback.bind(this));
event.on('change-login-status', params => {
that.setData({
loginText: params.text || '微信登录',
loginTips: params.tips || ''
});
});
},
onShow: function() {
let that = this;
setTimeout(() => {
app = app || getApp();
that.showBindUserInfo();
}, app ? 0 : 500);
},
onReady: function() {
},
changeLoginStatus: function(params) {
console.log(params);
},
loginCallback: function(res) {
this.setData({
loginButtonType: getLoginButtonType()
});
this.goReferer(); // 如果不需要绑定手机则返回前一页
},
showBindUserInfo: function() {
let that = this;
this.setData({
loginButtonType: getLoginButtonType()
});
if (app && app.getUid() && app.getUserInfo().wechat) {
return Promise.all([
accountModel.getProfile(),
accountModel.getCoinTotal(),
accountModel.getLimitCodes()
]).then(res => {
let defaultAvatar = (app.globalData.userInfo.wechat &&
app.globalData.userInfo.wechat.avatarUrl) ||
'../../static/images/icons/default-avatar.png';
let resUserInfo = res[0].data || {};
console.log('resUserInfo:', resUserInfo);
console.log('limitInfo:', res[2]);
let user = Object.assign({}, this.data.userInfo, app.globalData.userInfo, {
nickName: resUserInfo.nickname,
avatarUrl: resUserInfo.head_ico || defaultAvatar,
phoneNum: resUserInfo.mobile,
defaultAvatar: (resUserInfo.head_ico || defaultAvatar) === defaultAvatar,
});
app.setUserInfo(user);// 把用户信息写入storage
that.setData({
isBound: true,
userInfo: user,
coinNum: res[1].data && res[1].data.total || 0
});
that.setLimitCodeList(res[2]);
that.goBack(); // 如果有referer则返回前一个页面
}).catch({});
}
},
outLogin: function() {
return wx.showModal({
title: '',
content: '确认退出?',
confirmText: '退出',
confirmColor: '#000'
}).then(res1 => {
if (res1.confirm) {
app.clearUserSession();
// app._setSync('disableAutoLogin', true);
wx.reLaunch({
url: '/pages/index/index'
});
}
});
},
setLimitCodeList: function(res) { // 处理限购码列表的数据
let that = this;
let invalidCodes = [];
let codes = [];
res.data.invalidLimitCodeProducts.forEach((item, index) => {
item.defaultUrl = helper.image(item.defaultUrl, 220, 138, 1);
invalidCodes.push(item);
});
res.data.limitCodeProducts.forEach((item, index) => {
item.defaultUrl = helper.image(item.defaultUrl, 220, 138, 1);
codes.push(item);
});
this.setData({
limitCodes: {
invalidCodes: invalidCodes,
codes: codes
},
isShow: 1
});
// 通过顶部和底部按钮的高度,计算中间限购码部分的高度
let sysInfo = wx.getSystemInfoSync();
let windowHeight = sysInfo.windowHeight;
let query = wx.createSelectorQuery();
query.select('.user-info-bar').boundingClientRect();
query.select('.out-login').boundingClientRect();
query.exec(result => {
let topHeight = result[0] && result[0].height || 0;
let bottomHeight = result[1] && result[1].height || 0;
let containerHeight = (windowHeight - topHeight - bottomHeight - 20 - 30) + 'px';
that.setData({
containerHeight: containerHeight
});
});
},
navigateToh5Page: function(e) {
console.log(e);
let type = e.currentTarget.dataset.type || 0;
let type_obj = this.data.h5PageUrl[type];
let url = type_obj.url + '&title=' + type_obj.title;
router.goUrl(url);
},
navigateToLimitListPage: function() {
router.go('home');
},
navigateToDetail: function(e) {
let limitProductCode = e.currentTarget.dataset.code;
router.go('productDetail', { limitProductCode });
},
goReferer: function() { // 无需绑定的获取用户信息后返回
if (this.data.referer && !this.data.needBind) {
router.goUrl(`${config.MINI_APP_DOMAIN}${this.data.referer}`);
this.setData({
referer: '',
needBind: 1
});
}
},
goBack: function() {
if (this.data.referer) {
router.goUrl(`${config.MINI_APP_DOMAIN}${this.data.referer}`);
this.setData({
referer: '',
needBind: 1
});
}
}
});