Authored by huangyi

活动sdk init

... ... @@ -58,10 +58,11 @@
用法:
```
//这是一个callback方法
window.yo_sdk.getUid(function(uid){
//获取到uid执行内容
})
//执行返回的是一个Promise对象
window.yo_sdk.getUid().
then(function(uid){
console.log(uid);
})
```
... ...
... ... @@ -7,14 +7,16 @@
<title>Title</title>
</head>
<body>
<div id="aaa">232323</div>
<div class="auth">1111111</div>
<a class="auth" href="http://www.baidu.com">123123</a>
<div id="aaa">232323</div>
<script src="../build/sdk.js"></script>
<script>
window.yo_sdk.auth();
document.getElementById('aaa').onclick = function (event) {
yo_sdk.goLogin();
window.yo_sdk.getUid().then(function(uid){
console.log(uid);
})
}
</script>
</body>
... ...
... ... @@ -51,7 +51,8 @@
"eslint": "^3.19.0",
"eslint-config-yoho": "^1.0.1",
"eslint-loader": "^1.7.1",
"eslint-plugin-html": "^2.0.3"
"eslint-plugin-html": "^2.0.3",
"promise-polyfill": "^8.1.0"
},
"dependencies": {},
"directories": {
... ...
... ... @@ -7,15 +7,15 @@ const env = base.env();
const loginUrl = base.loginUrl();
const login = new Login({loginUrl, env});
const goto = new Goto(env);
const wx = new wxCtrl();
export default {
auth:login.auth,
goLogin: login.goLogin,
auth:login.auth.bind(login),
goLogin: login.goLogin.bind(login),
getUid:base.getUid,
env: env,
link: goto.link,
goBack: goto.goBack,
wxShare: wxCtrl.wxShare,
wxSignature: wxCtrl.wxSignature
link: goto.link.bind(goto),
goBack: goto.goBack.bind(goto),
wxShare: wx.wxShare.bind(wx),
wxSignature: wx.wxSignature
}
... ...
import jsonp from "../utils/jsonp";
import cookies from '../utils/cookie';
import {getQueryObj} from '../utils/common'
import Promise from 'promise-polyfill'
const parseUrl = (url) => {
let query = {},
... ... @@ -74,24 +75,26 @@ export default {
});
} else if (env === 'pc') {
url = 'https://www.yoho/signin.html?refer=' + encodeURIComponent(location.href) + '&openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"' + refer.replace(/\//g, '\\/') + '","param":{"from":"app"}},"requesturl":{"url":"","param":{}},"priority":"N"}}';
} else if (env === 'miniprogram'){
} else if (env === 'miniprogram') {
url = '/pages/userCenter/userCenter'
}
return url;
},
//uid
getUid: function (callback) {
getUid: function () {
return new Promise(function (resolve) {
if (env === 'h5' || env === 'pc') {
jsonp('https://m.yohobuy.com/passport/login/user').then(r => {
let uid = 0;
if(r.code === 200 ){
if (r.code === 200) {
uid = Number(r.data)
}
callback && callback(uid);
resolve(uid)
});
} else {
let uid = Number(cookies.cookie('app_uid')) || Number(getQueryObj().uid) || 0;
callback && callback(uid);
resolve(uid)
}
})
}
}
... ...
import wx from 'weixin-js-sdk';
import {linkToMiniApp, invokeMethod, createLinkButton} from "../utils/common";
let env;
export default class {
constructor(_env) {
env = _env;
this.env = _env;
}
link(event) {
... ... @@ -14,7 +13,7 @@ export default class {
let url = element.getAttribute('data-url') || '';
let name = element.getAttribute('data-name') || '';
let linkUrl = '';
if (env === 'miniprogram') {
if (this.env === 'miniprogram') {
if (type === 'product') {
linkUrl = '/pages/goodsDetail/goodsDetail?productSkn=' + id + '&page_name=goodsList&page_param=5'
} else if (type === 'brand') {
... ... @@ -23,7 +22,7 @@ export default class {
linkUrl = url.split('?')[0];
}
return linkToMiniApp(linkUrl, type);
} else if (env === 'pc') {
} else if (this.env === 'pc') {
if (type === 'product') {
linkUrl = 'https://www.yohobuy.com/product/' + id + '.html';
} else if (type === 'brand') {
... ... @@ -40,23 +39,24 @@ export default class {
linkUrl = url
}
}
if (env === 'app') {
if (document.getElementById('yosdk-goto')) {
createLinkButton(linkUrl,'yosdk-goto');
}
if (this.env === 'app') {
let dom = document.getElementById('yosdk-goto');
if (!dom) {
dom = createLinkButton(linkUrl, 'yosdk-goto');
} else {
dom.setAttribute('href', linkUrl);
}
return dom.click();
} else {
window.location.href = linkUrl
}
}
goBack() {
if (env === 'app') {
if (this.env === 'app') {
invokeMethod({method: 'go.back'})
} else if (env === 'miniprogram') {
} else if (this.env === 'miniprogram') {
wx.miniProgram.navigateBack();
} else {
history.go(-1);
... ...
... ... @@ -2,24 +2,22 @@ import wx from 'weixin-js-sdk';
import {createLinkButton} from "../utils/common";
import base from './base';
let env, url;
export default class {
constructor(obj) {
env = obj.env;
url = obj.loginUrl;
this.env = obj.env;
this.url = obj.loginUrl;
}
auth() {
let _gologin = this.goLogin;
base.getUid(function (uid) {
console.log(uid);
base.getUid().then((uid) => {
if (!uid) {
let list = document.querySelectorAll('.auth');
if (list.length) {
list.forEach((value) => {
value.onclick = function (event) {
value.onclick = (event) => {
event.preventDefault();
_gologin();
console.log(this);
this.goLogin();
}
});
return true;
... ... @@ -29,16 +27,16 @@ export default class {
}
goLogin() {
if (env === 'miniprogram') {
wx.miniProgram.switchTab({url: url});
} else if(env === 'app'){
if (!document.getElementById('yosdk-login')) {
createLinkButton(url, 'yosdk-login');
}
if (this.env === 'miniprogram') {
wx.miniProgram.switchTab({url: this.url});
} else if(this.env === 'app'){
let loginButton = document.getElementById('yosdk-login');
if (!loginButton) {
loginButton = createLinkButton(this.url, 'yosdk-login');
}
loginButton.click();
} else {
window.location.href = url;
window.location.href = this.url;
}
return false;
}
... ...
... ... @@ -3,15 +3,13 @@ import httpServer from '../utils/jsonp';
import wx from "weixin-js-sdk";
const _weChatInterface = '//action.yoho.cn/api/share/getSignPackage';//签名等相关配置,yoho公众号
export default {
export default class {
wxSignature: fun => {
wxSignature = fun => {
httpServer(_weChatInterface + "?pageurl=" + encodeURIComponent(location.href.split('#')[0])).then((json) => {
if (!json.appId) {
document.getElementById('share').innerText = 'false';
return false;
} else {
document.getElementById('share').innerText = 'true';
let _appId = json.appId.toString();
let _timestamp = json.timestamp;
let _nonceStr = json.nonceStr.toString();
... ... @@ -64,11 +62,10 @@ export default {
return true;
}
})
},
wxShare: shareDate => {
document.getElementById('share').innerText = 'wxShare';
}
wxShare = shareDate => {
this.wxSignature(function(){
let share_data = shareDate || {
title: '',
imgUrl: '',
... ... @@ -78,7 +75,6 @@ export default {
}
};
wx.ready(function () {
document.getElementById('share').innerText = JSON.stringify(share_data);
// 2.1 “分享给朋友”
wx.onMenuShareAppMessage(share_data);
// 2.2 “分享到朋友圈”
... ... @@ -88,6 +84,8 @@ export default {
// 2.4 “分享到微博”
wx.onMenuShareWeibo(share_data);
});
})
}
}
... ...
... ... @@ -87,6 +87,7 @@ const createLinkButton = (url, id) => {
a.setAttribute('id', id);
a.setAttribute('href', url);
document.body.appendChild(a);
return a;
}
export {
invokeMethod,
... ...