common.js
3.04 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
/**
* 页面公共逻辑
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2015/10/21
*/
var $ = require('yoho.zepto');
function cookie(name) {
var cookies = document.cookie,
cookieVal,
offset;
if (document.cookie && document.cookie !== '') {
offset = cookies.indexOf(name + '=');
if (offset > -1) {
offset += name.length + 1;
cookieVal = decodeURIComponent($.trim(cookies.substring(offset, cookies.indexOf(';', offset))));
}
}
return cookieVal;
}
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 getShoppingKey() {
var c = cookie('_g');
if (typeof c === 'undefined') {
return '';
}
return JSON.parse(c).k;
}
//页面通用底部位置及status设置
(function () {
var $footer = $('#yoho-footer'),
$op = $footer.children('.op-row');
var user = getUser();
if ($('body').height() < $(window).height()) {
$footer.addClass('bottom');
}
if (user === 0) {
//未登录
$op.prepend(
'<a href="http://m.yohobuy.com/signin.html">登录</a>' +
'<span class="sep-line">|</span>' +
'<a href="http://m.yohobuy.com/reg.html">注册</a>'
);
} else {
//已登录
$op.prepend(
'Hi,' +
'<a class="user-name" href="http://m.yohobuy.com/home?tmp=' + Math.random() + '">' + user[0] + '</a>' +
'<a href="http://m.yohobuy.com/passport/signout/index?token=' + user[3] + '">退出</a>'
);
}
$footer.removeClass('hide');
}());
//暴露公共接口
window.cookie = cookie;
window.setCookie = setCookie;
window.getUser = getUser;
window.getUid = getUid;
window.getShoppingKey = getShoppingKey;