Authored by 沈志敏

1. 用户相关 1. 获取用户的信息和状态 2. 登录 3. 收藏商品 2. 促销相关 1. 优惠券领取 2. YOHO币领取

... ... @@ -4,6 +4,7 @@
"es6": true
},
"globals": {
"__$styleInject": true,
"ENV": true
},
"extends": "eslint:recommended",
... ... @@ -27,7 +28,8 @@
"error",
"always"
],
"no-undef": 1,
"no-console": 1
"no-undef": 2,
"no-console": 1,
"no-unused-vars": 1
}
}
\ No newline at end of file
... ...
import jssdk from './src/jssdk';
import jssdk from './src/js/jssdk';
(function(factory) {
/* eslint-disable */
if (typeof module === 'object' && typeof module.exports === 'object') {
module.exports = factory();
} else if (typeof define === 'function' && define.amd) {
... ... @@ -8,6 +9,7 @@ import jssdk from './src/jssdk';
} else {
window._jssdk = factory();
}
/* eslint-disable */
}(function() {
return jssdk;
}));
\ No newline at end of file
... ...
... ... @@ -8,7 +8,7 @@
],
"main": "index.js",
"scripts": {
"build": "rollup -c"
"build": "rollup -c --watch"
},
"repository": {
"type": "git",
... ... @@ -18,12 +18,18 @@
"licenses": "MIT",
"devDependencies": {
"babel-preset-es2015-rollup": "^3.0.0",
"cssnano": "^3.9.1",
"postcss-cssnext": "^2.9.0",
"postcss-nested": "^1.0.0",
"postcss-simple-vars": "^3.0.0",
"rollup": "^0.38.0",
"rollup-plugin-babel": "^2.7.1",
"rollup-plugin-commonjs": "^6.0.1",
"rollup-plugin-eslint": "^3.0.0",
"rollup-plugin-json": "^2.1.0",
"rollup-plugin-node-resolve": "^2.0.0",
"rollup-plugin-uglify": "^1.0.1"
"rollup-plugin-postcss": "^0.2.0",
"rollup-plugin-uglify": "^1.0.1",
"rollup-watch": "^2.5.0"
}
}
... ...
... ... @@ -5,6 +5,12 @@ import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import uglify from 'rollup-plugin-uglify';
import json from 'rollup-plugin-json';
import postcss from 'rollup-plugin-postcss';
import simplevars from 'postcss-simple-vars';
import nested from 'postcss-nested';
import cssnext from 'postcss-cssnext';
import cssnano from 'cssnano';
const pkg = require('./package.json');
... ... @@ -12,11 +18,24 @@ export default {
entry: 'index.js',
dest: `dist/js-sdk/${pkg.version}/jssdk.js`,
format: 'iife',
external: ['jquery'],
globals: {
jquery: 'jQuery'
},
plugins: [
json({
include: 'node_modules/**',
preferConst: true, // Default: false
}),
postcss({
plugins: [
simplevars(),
nested(),
cssnext({ warnForDuplicates: false, }),
cssnano(),
],
extensions: ['.css'],
}),
resolve({
jsnext: true,
main: true,
... ...
.feature-coin .tip-wrap {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
background: rgba(0, 0, 0, .5);
}
.feature-coin .tip {
position: fixed;
left: 50%;
top: 50%;
margin-left: -140px;
margin-top: -140px;
width: 280px;
background: #fff;
text-align: center;
line-height: 1.25;
background-repeat: no-repeat;
}
.feature-coin .tip .tip-close{
position: absolute;
right: 0;
top: 0;
width: 25px;
height: 25px;
line-height: 25px;
text-align: center;
font-size: 16px;
color: #fff;
background: #000;
cursor: pointer;
}
.feature-coin .tip .title {
margin: 21px auto;
font-size: 22px;
font-weight: 700;
text-align: center;
}
.feature-coin .tip .highlight {
font-size: 18px;
}
.feature-coin .tip .content {
padding: 4px;
font-size: 16px;
color: #444;
text-align: center;
height: 48px;
overflow: hidden;
}
.feature-coin .bottom-button {
text-align: center;
}
.feature-coin .tip .button, .feature-coin .tip .coin {
display: inline-block;
margin: 20px auto;
width: 116px;
height: 26px;
font-size: 14px;
line-height: 26px;
text-align: center;
color: #fff;
background: #000;
text-decoration: none;
border: 2px solid #000;
}
.feature-coin .tip .coin {
color: #000;
background: #fff;
}
\ No newline at end of file
... ...
.featuretip .tip-wrap {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
background: rgba(0, 0, 0, .5);
}
.featuretip .tip {
position: fixed;
left: 50%;
top: 50%;
margin-left: -140px;
margin-top: -140px;
width: 280px;
background: #fff;
border-top: 40px solid #000;
text-align: center;
line-height: 1.25;
}
.featuretip .tip .title {
margin: 20px auto;
font-size: 24px;
font-weight: 700;
text-align: center;
}
.featuretip .tip .highlight {
font-size: 18px;
}
.featuretip .tip .content {
font-size: 16px;
color: #444;
text-align: center;
}
.featuretip .tip .button {
display: block;
margin: 20px auto;
width: 150px;
height: 30px;
font-size: 14px;
line-height: 30px;
text-align: center;
color: #fff;
background: #000;
text-decoration: none;
}
\ No newline at end of file
... ...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<style>
body {
text-align: center;
}
a {
text-decoration: none;
color: #444;
}
hr {
margin-top: 30px;
margin-bottom: 30px;
}
img {
max-width: 90%;
}
</style>
</head>
<body>
<h1>测试 Token </h1>
<a class="yoho-conpon" href="javascript:;" data-token="50f033ab37c3">
<h1>50f033ab37c3</h1>
</a>
<hr>
<a class="yoho-conpon" href="javascript:;" data-token="4e35f4a8d465">
<h1>4e35f4a8d465</h1>
</a>
<hr>
<a class="yoho-conpon" href="javascript:;" data-token="4cfbd7939d67">
<h1>4cfbd7939d67</h1>
</a>
<hr>
<a class="yoho-conpon" href="javascript:;" data-token="4aad61ab1432">
<h1>4aad61ab1432</h1>
</a>
<hr>
<a class="yoho-conpon" href="javascript:;" data-token="467cbbc409ec">
<img src="http://img02.yohoboys.com/staticimg/2016/04/22/14/02a8b1c09363f84c34aa56d11bb5cd7eb4.png">
</a>
<hr>
<a class="yoho-conpon" href="javascript:;" data-token="5693db85ed90">
<h1>5693db85ed90</h1>
</a>
<hr>
<a class="yoho-conpon" href="javascript:;" data-token="582a38a4a931">
<h1>582a38a4a931</h1>
</a>
<hr>
<a class="yoho-conpon" href="javascript:;" data-token="5a8613985ec4">
<h1>5a8613985ec4</h1>
</a>
<hr>
<a class="yoho-conpon" href="javascript:;" data-token="5c92cc227532">
<h1>5c92cc227532</h1>
</a>
<hr>
<a class="yoho-conpon" href="javascript:;" data-token="5ef4b9ec30ad">
<h1>5ef4b9ec30ad</h1>
</a>
<hr>
<a class="yoho-conpon" href="javascript:;" data-token="62ed3d2c2199">
<h1>62ed3d2c2199</h1>
</a>
<hr>
<a class="yoho-conpon" href="javascript:;" data-token="6026657d5ff9">
<h1>6026657d5ff9</h1>
</a>
<hr>
<a class="yoho-conpon" href="javascript:;" data-token="7e069059b7ef">
<h1>7e069059b7ef</h1>
</a>
<hr>
<a class="yoho-conpon" href="javascript:;" data-token="8602522a2b27">
<h1>8602522a2b27</h1>
</a>
<hr>
<a class="yoho-conpon" href="javascript:;" data-token="8465ded5353c">
<h1>8465ded5353c</h1>
</a>
<hr>
<a class="yoho-conpon" href="javascript:;" data-token="4bd09bf41544">
<h1>4bd09bf41544</h1>
</a>
<hr>
<a class="yoho-conpon" href="javascript:;" data-token="4d28dd2c7955">
<h1>4d28dd2c7955</h1>
</a>
<hr>
<script src="//cdn.bootcss.com/jquery/1.8.3/jquery.min.js"></script>
<script src="../dist/js/coupon.js"></script>
</body>
</html>
... ...
function cookie(name) {
var re = new RegExp(name + '=([^;$]*)', 'i'),
matchPattern = '$1';
return re.test(decodeURIComponent(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('');
}
}
export default {
cookie,
setCookie
};
\ No newline at end of file
... ...
export default {
test() {
console.log("test");
}
};
\ No newline at end of file
... ...
// app.js
import $ from 'jquery';
import util from './utils';
import user from './user';
import promotion from './promotion';
import individuation from './individuation';
// init
$(function() {});
export default {
getUid: user.getUid,
auth: user.auth,
favout: user.favout,
getCoupon: user.getCoupon,
getCoin: user.getCoin
};
\ No newline at end of file
... ...
// 1. 优惠券领取
// 2. YOHO币领取
import $ from 'jquery';
import util from './utils';
export default {
getCoupon(data) {
if (util.isApp()) {
data.app = util.queryString();
}
$.ajax({
url: '//m.yohobuy.com/coupon/couponSend',
dataType: 'jsonp',
data: data
}).then(function(res) {
if (res.code === 200) {
util.showTip({
title: '领取成功',
content: '恭喜您,领取成功',
close: true
});
} else if (res.code === 401) {
util.showTip({
title: '已经领取',
content: '快去分享给更多<br>喜爱<span class="highlight">潮流</span>的小伙伴吧!',
close: true
});
} else if (res.message) {
util.showTip({
title: '领取失败<br>请刷新重新领取',
content: res.message + '<br>如多次领取失败,请联系客服人员<br>带来不便敬请谅解',
close: false
});
}
});
},
getCoin(data) {
if (util.isApp()) {
data.app = util.queryString();
}
return $.ajax({
url: '//m.yohobuy.com/activity/coin/sendCoin',
dataType: 'jsonp',
data: data
}).then(function(res) {
if (res.code === 200 && res.data) {
if (res.data.code === 200) {
util.showCoinTip({
title: '恭喜您,成功领取有货币!',
content: '特殊情况下到账有延时<br>请耐心等待',
close: true,
coin: true,
img: res.data.popupImg
});
} else if (res.data.code === 501) {
util.showCoinTip({
title: '领取失败',
content: '哎呀,你来的有点早,活动还没开始呢<br/>稍后再来哦',
close: true,
img: res.data.popupImg
});
} else if (res.data.code === 502) {
util.showCoinTip({
title: '领取失败',
content: '抱歉,活动已结束了,下次要快哟~~',
close: true,
img: res.data.popupImg
});
} else if (res.data.code === 503) {
util.showCoinTip({
title: '已经领取',
content: '贪心会长胖,你已经领取过了啦~~',
close: true,
img: res.data.popupImg
});
} else if (res.data.code === 504) {
util.showCoinTip({
title: '领取失败',
content: '哎呀,你来晚了,有货币已经领完了,<br/>下次早点来哦',
close: true,
img: res.data.popupImg
});
} else if (res.data.msg) {
util.showCoinTip({
title: '领取失败',
content: '抱歉,系统错误,有货君正奋力解决中...请稍后再来',
close: true,
img: res.data.popupImg
});
}
} else if (res.message) {
util.showCoinTip({
title: '领取失败',
content: '抱歉,系统错误,有货君正奋力解决中...请稍后再来',
close: true,
img: res.data && res.data.popupImg
});
}
});
}
};
\ No newline at end of file
... ...
// 1. 获取用户的信息和状态
// 2. 登录
// 3. 收藏商品
import $ from 'jquery';
import util from './utils';
export default {
uid: '',
getUid() {
if (this.uid) {
return this.uid;
}
if (!util.isApp()) {
// 获取当前登录用户信息
$.ajax({
url: '//m.yohobuy.com/passport/login/user',
dataType: 'jsonp'
}).then(function(res) {
if (res.code === 200) {
this.uid = res.data;
}
});
} else {
this.uid = util.queryString().uid;
}
return this.uid;
},
auth() {
let uid = this.getUid();
if (!uid) {
let referrer = location.href.split('?')[0];
if (!util.isApp()) {
let qs = {
refer: referrer
};
return `//m.yohobuy.com/signin.html?${util.encode(qs)}`;
} else {
return '//m.yohobuy.com/signin.html?openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"' + referrer.replace(/\//g, '\\/') + '","param":{"from":"app"}},"requesturl":{"url":"","param":{}},"priority":"N"}}';
}
}
},
favout(skn) {
let uid = this.getUid();
if (!skn || !uid) {
return;
}
$.ajax({
url: `//service.yoho.cn/activity/favorite/addFavorite?productSkn=${skn}&uid=${uid}`,
dataType: 'jsonp'
}).then(function(res) {
if (res.code === 200) {
util.showTip({
content: '恭喜您,收藏成功',
close: true
});
} else {
util.showTip({
content: res.message,
close: true
});
}
});
}
};
\ No newline at end of file
... ...
import tip from '../css/featuretip.css'; //eslint-disable-line
import cointip from '../css/featuretip-coin.css'; //eslint-disable-line
import $ from 'jquery';
const isMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i) ? true : false;
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i) ? true : false;
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i) ? true : false;
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i) ? true : false;
},
any: function() {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Windows());
}
};
const mycurrency = isMobile.any() ? 'http://m.yohobuy.com/home/mycurrency' : 'http://www.yohobuy.com/home/currency';
const $tipTmpl = $('<div class="featuretip tip-wrap"><div class="featuretip tip"><div class="featuretip title"></div><div class="featuretip content"></div><a class="featuretip button" href="">返回</a></div></div>'); // eslint-disable-line
const $cointipTmpl = $('<div class="feature-coin tip-wrap"><div class="feature-coin tip"><div class="feature-coin tip-close">&times;</div><div class="feature-coin title"></div><div class="feature-coin content"></div><div class="feature-coin bottom-button"><a class="feature-coin button" href="">去逛逛</a><a class="feature-coin coin" href=\'' + mycurrency + '?openby:yohobuy={"action":"go.mine"}\'>查看有货币</a></div></div></div>'); // eslint-disable-line
export default {
isApp() {
let qs = this.queryString();
let isApp = !!qs.app_version || (qs.openrefer === 'app' && qs.uid);
return !!isApp;
},
queryString() {
if (!window._jssdkQS) {
let vars = {},
hash,
i,
hashes = window.location.search.slice(1).split('&');
for (i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
window._jssdkQS = vars;
}
return window._jssdkQS;
},
encode(obj) {
let stringifyPrimitive = function(v) {
switch (typeof v) {
case 'string':
return v;
case 'boolean':
return v ? 'true' : 'false';
case 'number':
return isFinite(v) ? v : '';
default:
return '';
}
};
let sep = sep || '&';
let eq = eq || '=';
if (obj === null) {
obj = undefined;
}
if (typeof obj === 'object') {
return Object.keys(obj).map(function(k) {
let ks = encodeURIComponent(stringifyPrimitive(k)) + eq;
if (Array.isArray(obj[k])) {
return obj[k].map(function(v) {
return ks + encodeURIComponent(stringifyPrimitive(v));
}).join(sep);
} else {
return ks + encodeURIComponent(stringifyPrimitive(obj[k]));
}
}).join(sep);
}
return '';
},
showTip(data) {
data = data || {
title: '',
content: '',
close: true
};
$tipTmpl.find('.title').html(data.title);
$tipTmpl.find('.content').html(data.content);
if (data.close) {
$tipTmpl.find('.button').addClass('close');
} else {
$tipTmpl.find('.button').html('刷新');
}
$('body').append($tipTmpl);
$tipTmpl.show();
},
showCoinTip(data) {
data = data || {
title: '',
content: '',
close: true
};
$cointipTmpl.find('.title').html(data.title);
$cointipTmpl.find('.content').html(data.content);
if (data.close) {
$cointipTmpl.find('.button').addClass('close');
}
if (data.coin) {
$cointipTmpl.find('.coin').css('display', 'inline-block');
} else {
$cointipTmpl.find('.coin').hide();
}
if (data.img) {
$cointipTmpl.find('.tip').css('background-image', 'url(' + data.img + ')');
}
$('body').append($cointipTmpl);
$cointipTmpl.show();
}
};
\ No newline at end of file
... ...