util.js
4.3 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
import yoho from 'yoho';
import interceptClick from 'common/intercept-click';
import Modal from 'common/modal';
import cookie from 'yoho-cookie';
const getImgHost = function(url, bucket = 'goodsimg') {
let urlArr = url.split('/'),
num = urlArr[urlArr.length - 1].substr(1, 1),
domain = `static.yhbimg.com/${bucket}`;
url = domain + url;
if (num === '1') {
return '//img11.' + url;
} else {
return '//img12.' + url;
}
};
const visibilitychange = function() {
document.addEventListener('visibilitychange', function() {
if (yoho.isApp && !document.hidden) {
yoho.showLoading(false);
}
});
};
const getImgUrl = function(src, width = 300, height = 300, mode = 2) {
return src ? src.replace(/(\{width}|\{height}|\{mode})/g, function($0) {
const dict = {
'{width}': width,
'{height}': height,
'{mode}': mode || 2
};
return dict[$0];
}).replace(/https?:/, '') + '/interlace/1' : '';
};
const replaceHttp = function(src) {
return src.replace(/https?:/, '');
};
// 退换货 申请 成功, 打开 modal
const applySuccuss = function(type, applyId) {
yoho.store.set('orderDetail', true);
const config = {
exchange: {
name: '换货',
detailUrl: `/me/return/exchange/detail/${applyId}`
},
refund: {
name: '退货',
detailUrl: `/me/return/refund/detail/${applyId}`
}
};
const kind = config[type];
const goStatusPage = function() {
const header = Object.assign({}, interceptClick.defaultTitleMap[1]);
header.left.action = location.origin + '/me/return';
header.title.des = `${kind.name}状态`;
return yoho.goNewPage({
header: header,
url: location.origin + kind.detailUrl,
backThrough: '1'
});
};
const modal = new Modal({
styleClass: 'return-success-modal',
title: `${kind.name}申请已提交,请等待审核.....`,
buttons: [{
text: '返回订单',
handler: function() {
this.hide();
yoho.goBack();
}
}, {
text: '查看进度',
handler: function() {
this.hide();
goStatusPage();
}
}]
});
modal.show();
};
const getChannel = function() {
let channel = [
{
id: 0,
en: 'men'
},
{
id: 1,
en: 'women'
}
];
yoho.ready(function() {
yoho.getChannel({}, function(val) {
const opt = {
path: '/'
};
val = val - 1;
cookie.set('_Channel', channel[val].en, opt);
cookie.set('_ChannelIndex', val, opt);
});
});
};
const miniVersion = function (miniVersion) {
let appVersion = cookie.get('app_version');
let miniVersions = miniVersion.split('.');
if (appVersion && typeof appVersion === 'string') {
let versions = appVersion.split('.');
if (versions.length === 3 &&
(+versions[0] > +miniVersions[0] ||
(+versions[0] === +miniVersions[0] && +versions[1] > +miniVersions[1]) ||
(+versions[0] === +miniVersions[0] && +versions[1] === +miniVersions[1] && +versions[2] >= +miniVersions[2]))) {
return true;
}
}
return false;
};
const debounce = function(idle, action) { // 函数去抖动,超过一定时间才会执行,如果周期内触发,重置计时器
let last;
return function() {
let args = arguments;
if (last) {
clearTimeout(last);
}
last = setTimeout(() => {
action.apply(this, args);
}, idle);
};
};
const throttle = function(delay, action) { // 函数节流器,定义函数执行间隔,按频率触发函数
let last = 0;
return function() {
let args = arguments;
let curr = +new Date();
if (curr - last > delay) {
action.apply(this, args);
last = curr;
}
};
};
export default {
getImgHost,
getImgUrl,
applySuccuss,
visibilitychange,
getChannel,
replaceHttp,
miniVersion,
debounce,
throttle
};