common.js
6.62 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/**
* 页面公共逻辑和接口
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2015/11/23
*/
var $ = require('yoho-jquery');
var $body = $('body');
var _ChannelVary = {
boys: 1,
girls: 2,
kids: 3,
lifestyle: 4
};
function cookie(name) {
var re = new RegExp(name + '=([^;$]*)', 'i'),
matchPattern = '$1';
try {
return re.test(decodeURIComponent(document.cookie)) ? RegExp[matchPattern] : '';
} catch (e) {
return decodeURIComponent(re.test(document.cookie) ? RegExp[matchPattern] : '');
}
}
function setCookie(name, value, options) {
var expires = '',
path,
domain,
secure,
date;
if (typeof value !== 'undefined') {
options = options || {};
if (value === null) {
value = '';
options.expires = -1;
}
if (options.expires &&
(typeof options.expires === 'number' || options.expires.toUTCString)) {
if (typeof options.expires === 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString();
}
path = options.path ? '; path=' + options.path : '';
domain = options.domain ? '; domain=' + options.domain : '';
secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
}
}
function getUser() {
var c = cookie('_UID'),
user;
if (typeof c === 'undefined') {
return 0;
}
user = c.split('::');
if (typeof user === 'undefined' || user.length < 4) {
return 0;
}
return user;
}
function getUid() {
var user = getUser();
if (user === 0) {
return 0;
}
return user[1];
}
function getProfileName() {
var user = getUser();
if (user === 0) {
return 0;
}
return decodeURIComponent(user[0]);
}
function getShoppingKey() {
var c = cookie('_g');
if (typeof c === 'undefined') {
return '';
}
return JSON.parse(c).k;
}
// YAS统计代码
(function(w, d, s, j, f) {
var a = d.createElement(s);
var m = d.getElementsByTagName(s)[0];
w.YohoAcquisitionObject = f;
w[f] = function() {
w[f].p = arguments;
};
a.async = 1;
a.src = j;
m.parentNode.insertBefore(a, m);
}(window, document, 'script', (document.location.protocol === 'https:' ? 'https:' : 'http:') + '//' + // eslint-disable-line
'cdn.yoho.cn/yas-jssdk/' + window._yasVersion + '/yas.js', '_yas')); // eslint-disable-line
(function() {
var uid = getUid();
uid = uid === 0 ? '' : uid;
window._ozuid = uid; // 暴露ozuid
if (window._yas) {
window._yas(1 * new Date(), window._yasVersion, 'yohobuy_web', uid, '', '');
}
/* 【加入ocpcApi代码】: 判断当前页面地址若百度sem推广链接,即将推广链接存到cookies,支付成功后将推广链接上报百度 ocpcapi
* @ description: 收集百度sem推广链接
* @ author: huzhiming
* @ date: 2019-11-18 16:55:17
* @ version: v1.0.0
* 详情见文档:[http://ocpc.baidu.com/developer/d/guide/?iurl=api%2Fapi-doc%2Fapi-interface%2F]
*/
if (location.href.includes('bd_vid=')) {
setCookie('bd_vid_path', location.href, { path: '/', domain: '.yohobuy.com', expires: 90 });
}
}());
// window.resize在width<1180时适配成990
$(window).on('resize', function() {
var w = $(this).width();
// return top
if (w < 1360) {
$('.right-floating-layer').addClass('min');
} else {
$('.right-floating-layer').removeClass('min');
}
// body
if (w < 1180) {
$body.addClass('min-screen');
} else {
$body.removeClass('min-screen');
}
}).trigger('resize');
// 对特殊的情况进行处理
function queryString() {
var vars = {},
hash,
index,
i,
search = window.location.search,
hashes = search ? decodeURIComponent(search).slice(1).split('&') : [];
for (i = 0; i < hashes.length; i++) {
index = hashes[i].indexOf('=');
hash = hashes[i].substring(0, index);
vars[hash] = hashes[i].substring(++index);
}
return vars;
}
// 个人中心左侧栏头像加载失败后,显示默认头像
(function() {
$(window).load(function() {
var thumb = document.getElementById('user-thumb');
var img;
if (!thumb) {
return;
}
img = new Image();
img.src = thumb.src;
img.onerror = function() {
$(thumb).parent().append('<div class="default-user-thumb"></div>').end().remove();
};
});
}());
function signinUrl() {
return '//www.yohobuy.com/signin.html?refer=' + encodeURIComponent(window.location.href);
}
function registerUrl() {
return '//www.yohobuy.com/reg.html?refer=' + encodeURIComponent(window.location.href);
}
function jumpUrl(url) {
window.location.href = url;
}
function once(fn, context) {
var result;
return function() {
if (fn) {
result = fn.apply(context || this, arguments);
fn = null;
}
return result;
};
}
function isLogin() {
var uid = getUid();
if (uid) {
return true;
} else {
jumpUrl(signinUrl());
return false;
}
}
function isIE() {
return /msie|trident/i.test(navigator.userAgent);
}
function isIEVer(version) {
var ver = '';
var getVersion = function(regex) {
var ua = navigator.userAgent;
var match = ua && ua.match(regex);
return (match && match.length > 1 && match[1]) || '';
};
if (isIE()) {
ver = getVersion(/(?:msie |rv:)(\d+(\.\d+)?)/i);
return ver === version;
}
return false;
}
// 品友
function addPyEvent(eventName, val) {
if (typeof window.py === 'function') {
window.py('event', eventName, val);
} else {
setTimeout(function() {
addPyEvent(eventName, val);
}, 3000);
}
}
window.cookie = cookie;
window.setCookie = setCookie;
window.getUser = getUser;
window.getUid = getUid;
window.getProfileName = getProfileName;
window.getShoppingKey = getShoppingKey;
window.queryString = queryString;
window.signinUrl = signinUrl;
window.isLogin = isLogin;
window.registerUrl = registerUrl;
window.jumpUrl = jumpUrl;
window.isIE = isIE;
window.isIE8 = isIEVer('8.0');
window.once = once;
window.addPyEvent = addPyEvent;
window._ChannelVary = _ChannelVary;
require('./header');
require('./footer');