Authored by huangyi

活动sdk init

@@ -2,17 +2,18 @@ @@ -2,17 +2,18 @@
2 <html lang="en"> 2 <html lang="en">
3 <head> 3 <head>
4 <meta charset="UTF-8"> 4 <meta charset="UTF-8">
  5 + <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, viewport-fit=cover">
  6 + <meta http-equiv="X-UA-Compatible" content="chrome=1,IE=edge">
5 <title>Title</title> 7 <title>Title</title>
6 </head> 8 </head>
7 <body> 9 <body>
8 <div class="auth">1111111</div> 10 <div class="auth">1111111</div>
9 -<a class="auth" href="http://www.baidu.com">123123</a> 11 +<a class="auth" href="http://www.baidu.com">123123</a>
10 <div id="aaa">232323</div> 12 <div id="aaa">232323</div>
11 <script src="../build/sdk.js"></script> 13 <script src="../build/sdk.js"></script>
12 <script> 14 <script>
13 - document.getElementById('aaa').onclick = function(event){  
14 - _jssdk.goLogin();  
15 - 15 + document.getElementById('aaa').onclick = function (event) {
  16 + yo_sdk.goLogin();
16 } 17 }
17 </script> 18 </script>
18 </body> 19 </body>
  1 +<!DOCTYPE html>
  2 +<html lang="en">
  3 +<head>
  4 + <meta charset="UTF-8">
  5 + <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, viewport-fit=cover">
  6 + <meta http-equiv="X-UA-Compatible" content="chrome=1,IE=edge">
  7 + <title>Title</title>
  8 +</head>
  9 +<body>
  10 +<div id="share"> 分享</div>
  11 +<script src="../build/sdk.js"></script>
  12 +<script>
  13 + let shareData = {
  14 + title: '国潮崛起年,中西碰撞博出位!',
  15 + imgUrl: 'https://img01.yohoboys.com/o_1cnilugbs15ajak1gfo1d2h1p348.png?imageView2/1/w/200/h/200',
  16 + desc: '这里有一份中西潮流榜单,请收好!',
  17 + link: location.href,
  18 + success: function () {
  19 + alert('1123')
  20 + }
  21 + };
  22 + // let sign = window.yo_sdk.wxSignature;
  23 + // sign().then(function(r){
  24 + // console.log(r)
  25 + // window.yo_sdk.wxShare(shareData)
  26 + // })
  27 +
  28 + window.yo_sdk.wxSignature(window.yo_sdk.wxShare(shareData));
  29 +
  30 +
  31 +</script>
  32 +</body>
  33 +</html>
1 import base from './public/base' 1 import base from './public/base'
2 import Login from './public/login' 2 import Login from './public/login'
3 import Goto from './public/goto' 3 import Goto from './public/goto'
4 -import share from './public/share' 4 +import wxCtrl from './public/share'
5 5
6 const env = base.env(); 6 const env = base.env();
7 const loginUrl = base.loginUrl(); 7 const loginUrl = base.loginUrl();
8 const uid = base.uid(); 8 const uid = base.uid();
9 const login = new Login({loginUrl, env, uid}); 9 const login = new Login({loginUrl, env, uid});
10 const goto = new Goto(env); 10 const goto = new Goto(env);
11 -const wxShare = new share(); 11 +
  12 +
12 export default { 13 export default {
13 goLogin: login.goLogin, 14 goLogin: login.goLogin,
14 - isLogin: base.uid, 15 + uid: base.geAppUid,
15 env: env, 16 env: env,
16 link: goto.link, 17 link: goto.link,
17 goBack: goto.goBack, 18 goBack: goto.goBack,
18 - wxShare: wxShare.wxShare 19 + wxShare: wxCtrl.wxShare,
  20 + wxSignature: wxCtrl.wxSignature
19 } 21 }
@@ -23,7 +23,7 @@ const parseUrl = (url) => { @@ -23,7 +23,7 @@ const parseUrl = (url) => {
23 query: query 23 query: query
24 }; 24 };
25 } 25 }
26 - 26 +let env = ''
27 export default { 27 export default {
28 //evn 28 //evn
29 env: function () { 29 env: function () {
@@ -31,21 +31,21 @@ export default { @@ -31,21 +31,21 @@ export default {
31 if (!envFlag && navigator.userAgent.match(/yohobuy/i)) { 31 if (!envFlag && navigator.userAgent.match(/yohobuy/i)) {
32 document.addEventListener('deviceready', function () { 32 document.addEventListener('deviceready', function () {
33 }); 33 });
34 - return 'app'; 34 + env = 'app';
35 } else if ((!envFlag && navigator.userAgent.match(/miniProgram/i)) || (envFlag == 'miniprogram')) { 35 } else if ((!envFlag && navigator.userAgent.match(/miniProgram/i)) || (envFlag == 'miniprogram')) {
36 - return 'miniprogram'; 36 + env = 'miniprogram';
37 } else if (location.origin === 'https://www.yohobuy.com') { 37 } else if (location.origin === 'https://www.yohobuy.com') {
38 - return 'pc' 38 + env = 'pc'
39 }else{ 39 }else{
40 - return 'h5' 40 + env = 'h5'
41 } 41 }
  42 + return env
42 }, 43 },
43 //url 44 //url
44 loginUrl: function () { 45 loginUrl: function () {
45 - let _env = this.env();  
46 let url, refer = location.href.split('?')[0]; 46 let url, refer = location.href.split('?')[0];
47 url = 'http://m.yohobuy.com/signin.html?refer=' + encodeURIComponent(refer); 47 url = 'http://m.yohobuy.com/signin.html?refer=' + encodeURIComponent(refer);
48 - if (_env === 'app') { 48 + if (env === 'app') {
49 refer = parseUrl(refer); 49 refer = parseUrl(refer);
50 if (/Android/i.test(navigator.userAgent || '')) { 50 if (/Android/i.test(navigator.userAgent || '')) {
51 if (url.indexOf('?') < 0) { 51 if (url.indexOf('?') < 0) {
@@ -72,20 +72,20 @@ export default { @@ -72,20 +72,20 @@ export default {
72 needlogout: 'Y' 72 needlogout: 'Y'
73 } 73 }
74 }); 74 });
75 - }else if(_env === 'pc'){ 75 + }else if(env === 'pc'){
76 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"}}'; 76 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"}}';
77 } 77 }
78 return url; 78 return url;
79 }, 79 },
80 //uid 80 //uid
81 uid: async function () { 81 uid: async function () {
82 - let _env = this.env();  
83 let result = await jsonp('https://m.yohobuy.com/passport/login/user'); 82 let result = await jsonp('https://m.yohobuy.com/passport/login/user');
84 if (result.code === 200) 83 if (result.code === 200)
85 return result.data; 84 return result.data;
86 - else if (_env === 'app' || _env === 'miniprogram')  
87 - return cookies.cookie('app_uid') || getQueryObj().uid || 0; 85 + else if (env === 'app' || env === 'miniprogram')
  86 + return Number(cookies.cookie('app_uid')) || Number(getQueryObj().uid) || 0;
88 else 87 else
89 return 0; 88 return 0;
90 - } 89 + },
  90 + geAppUid: Number(cookies.cookie('app_uid')) || Number(getQueryObj().uid) || 0
91 } 91 }
1 import wx from 'weixin-js-sdk'; 1 import wx from 'weixin-js-sdk';
2 import {linkToMiniApp,invokeMethod} from "../utils/common"; 2 import {linkToMiniApp,invokeMethod} from "../utils/common";
3 3
  4 +let env ;
4 export default class { 5 export default class {
5 - constructor(env) {  
6 - this.env = env; 6 + constructor(_env) {
  7 + env = _env;
7 this.createButton(); 8 this.createButton();
8 } 9 }
9 10
10 createButton() { 11 createButton() {
11 - if (!document.getElementById('yosdk-goto') && this.env !== 'miniprogram') { 12 + if (!document.getElementById('yosdk-goto') && env !== 'miniprogram') {
12 let a = document.createElement('a') 13 let a = document.createElement('a')
13 a.style.position = 'fixed'; 14 a.style.position = 'fixed';
14 a.style.top = 0; 15 a.style.top = 0;
@@ -31,7 +32,7 @@ export default class { @@ -31,7 +32,7 @@ export default class {
31 let url = element.getAttribute('data-url') || ''; 32 let url = element.getAttribute('data-url') || '';
32 let name = element.getAttribute('data-name') || ''; 33 let name = element.getAttribute('data-name') || '';
33 let linkUrl = ''; 34 let linkUrl = '';
34 - if (this.env === 'miniprogram') { 35 + if (env === 'miniprogram') {
35 if (type === 'product') { 36 if (type === 'product') {
36 linkUrl = '/pages/goodsDetail/goodsDetail?productSkn=' + id + '&page_name=goodsList&page_param=5' 37 linkUrl = '/pages/goodsDetail/goodsDetail?productSkn=' + id + '&page_name=goodsList&page_param=5'
37 } else if (type === 'brand') { 38 } else if (type === 'brand') {
@@ -40,7 +41,7 @@ export default class { @@ -40,7 +41,7 @@ export default class {
40 linkUrl = url.split('?')[0]; 41 linkUrl = url.split('?')[0];
41 } 42 }
42 return linkToMiniApp(linkUrl, type); 43 return linkToMiniApp(linkUrl, type);
43 - } else if (this.env === 'pc'){ 44 + } else if (env === 'pc'){
44 if (type === 'product') { 45 if (type === 'product') {
45 linkUrl = 'https://www.yohobuy.com/product/'+id+'.html' 46 linkUrl = 'https://www.yohobuy.com/product/'+id+'.html'
46 } else if (type === 'brand') { 47 } else if (type === 'brand') {
@@ -64,9 +65,9 @@ export default class { @@ -64,9 +65,9 @@ export default class {
64 65
65 66
66 goBack () { 67 goBack () {
67 - if(this.env === 'app'){ 68 + if(env === 'app'){
68 invokeMethod({method:'go.back'}) 69 invokeMethod({method:'go.back'})
69 - }else if(this.env === 'miniprogram'){ 70 + }else if(env === 'miniprogram'){
70 wx.miniProgram.navigateBack(); 71 wx.miniProgram.navigateBack();
71 }else{ 72 }else{
72 history.go(-1); 73 history.go(-1);
1 import wx from 'weixin-js-sdk'; 1 import wx from 'weixin-js-sdk';
2 2
  3 +let env, url, uri;
3 export default class { 4 export default class {
4 constructor(obj) { 5 constructor(obj) {
5 - this.env = obj.env;  
6 - this.url = obj.loginUrl;  
7 - obj.uid.then(uid=>{  
8 - this.uid = uid; 6 + env = obj.env
  7 + url = obj.loginUrl;
  8 + uri = ''
  9 + obj.uid.then(uid => {
  10 + this.uri = uid
9 this.createloginButton(uid); 11 this.createloginButton(uid);
10 this.auth(uid); 12 this.auth(uid);
11 }); 13 });
12 } 14 }
13 15
14 createloginButton(uid) { 16 createloginButton(uid) {
15 - if (!document.getElementById('yosdk-login') && ! uid && this.env !== 'miniprogram') { 17 + if (!document.getElementById('yosdk-login') && !uid && env !== 'miniprogram') {
16 let a = document.createElement('a') 18 let a = document.createElement('a')
17 a.style.position = 'fixed'; 19 a.style.position = 'fixed';
18 a.style.top = 0; 20 a.style.top = 0;
@@ -23,7 +25,7 @@ export default class { @@ -23,7 +25,7 @@ export default class {
23 a.style.background = 'transparent'; 25 a.style.background = 'transparent';
24 a.style.color = 'transparent'; 26 a.style.color = 'transparent';
25 a.setAttribute('id', 'yosdk-login'); 27 a.setAttribute('id', 'yosdk-login');
26 - a.setAttribute('href', this.url); 28 + a.setAttribute('href', url);
27 document.body.appendChild(a); 29 document.body.appendChild(a);
28 } 30 }
29 } 31 }
@@ -35,16 +37,21 @@ export default class { @@ -35,16 +37,21 @@ export default class {
35 list.forEach((value) => { 37 list.forEach((value) => {
36 value.onclick = function (event) { 38 value.onclick = function (event) {
37 event.preventDefault(); 39 event.preventDefault();
38 - let loginButton = document.getElementById('yosdk-login');  
39 - loginButton.click(); 40 + if (env === 'miniprogram') {
  41 + wx.miniProgram.switchTab({url: '/pages/userCenter/userCenter'});
  42 + } else {
  43 + let loginButton = document.getElementById('yosdk-login');
  44 + loginButton.click();
  45 + }
40 } 46 }
41 }) 47 })
  48 + return true;
42 } 49 }
43 } 50 }
44 } 51 }
45 52
46 goLogin() { 53 goLogin() {
47 - if (this.env === 'miniprogram') { 54 + if (env === 'miniprogram') {
48 return wx.miniProgram.switchTab({url: '/pages/userCenter/userCenter'}); 55 return wx.miniProgram.switchTab({url: '/pages/userCenter/userCenter'});
49 } else { 56 } else {
50 let loginButton = document.getElementById('yosdk-login'); 57 let loginButton = document.getElementById('yosdk-login');
@@ -3,65 +3,72 @@ import httpServer from '../utils/jsonp'; @@ -3,65 +3,72 @@ import httpServer from '../utils/jsonp';
3 import wx from "weixin-js-sdk"; 3 import wx from "weixin-js-sdk";
4 4
5 const _weChatInterface = '//action.yoho.cn/api/share/getSignPackage';//签名等相关配置,yoho公众号 5 const _weChatInterface = '//action.yoho.cn/api/share/getSignPackage';//签名等相关配置,yoho公众号
6 -export default class {  
7 - constructor() {  
8 - this.wxSignature();  
9 - } 6 +export default {
10 7
11 - wxSignature() { 8 + wxSignature: fun => {
12 httpServer(_weChatInterface + "?pageurl=" + encodeURIComponent(location.href.split('#')[0])).then((json) => { 9 httpServer(_weChatInterface + "?pageurl=" + encodeURIComponent(location.href.split('#')[0])).then((json) => {
13 - let _appId = json.appId.toString();  
14 - let _timestamp = json.timestamp;  
15 - let _nonceStr = json.nonceStr.toString();  
16 - let _signature = json.signature.toString();  
17 - wx.config({  
18 - debug: false,  
19 - appId: _appId,  
20 - timestamp: _timestamp,  
21 - nonceStr: _nonceStr,  
22 - signature: _signature,  
23 - jsApiList: [  
24 - 'checkJsApi',  
25 - 'onMenuShareTimeline',  
26 - 'onMenuShareAppMessage',  
27 - 'onMenuShareQQ',  
28 - 'onMenuShareWeibo',  
29 - 'hideMenuItems',  
30 - 'showMenuItems',  
31 - 'hideAllNonBaseMenuItem',  
32 - 'showAllNonBaseMenuItem',  
33 - 'translateVoice',  
34 - 'startRecord',  
35 - 'stopRecord',  
36 - 'onVoiceRecordEnd',  
37 - 'playVoice',  
38 - 'pauseVoice',  
39 - 'stopVoice',  
40 - 'uploadVoice',  
41 - 'onVoicePlayEnd',  
42 - 'downloadVoice',  
43 - 'chooseImage',  
44 - 'previewImage',  
45 - 'uploadImage',  
46 - 'downloadImage',  
47 - 'getNetworkType',  
48 - 'openLocation',  
49 - 'getLocation',  
50 - 'hideOptionMenu',  
51 - 'showOptionMenu',  
52 - 'closeWindow',  
53 - 'scanQRCode',  
54 - 'chooseWXPay',  
55 - 'openProductSpecificView',  
56 - 'addCard',  
57 - 'chooseCard',  
58 - 'openCard'  
59 - ]  
60 - }); 10 + if (!json.appId) {
  11 + document.getElementById('share').innerText = 'false';
  12 + return false;
  13 + } else {
  14 + document.getElementById('share').innerText = 'true';
  15 + let _appId = json.appId.toString();
  16 + let _timestamp = json.timestamp;
  17 + let _nonceStr = json.nonceStr.toString();
  18 + let _signature = json.signature.toString();
  19 + wx.config({
  20 + debug: false,
  21 + appId: _appId,
  22 + timestamp: _timestamp,
  23 + nonceStr: _nonceStr,
  24 + signature: _signature,
  25 + jsApiList: [
  26 + 'checkJsApi',
  27 + 'onMenuShareTimeline',
  28 + 'onMenuShareAppMessage',
  29 + 'onMenuShareQQ',
  30 + 'onMenuShareWeibo',
  31 + 'hideMenuItems',
  32 + 'showMenuItems',
  33 + 'hideAllNonBaseMenuItem',
  34 + 'showAllNonBaseMenuItem',
  35 + 'translateVoice',
  36 + 'startRecord',
  37 + 'stopRecord',
  38 + 'onVoiceRecordEnd',
  39 + 'playVoice',
  40 + 'pauseVoice',
  41 + 'stopVoice',
  42 + 'uploadVoice',
  43 + 'onVoicePlayEnd',
  44 + 'downloadVoice',
  45 + 'chooseImage',
  46 + 'previewImage',
  47 + 'uploadImage',
  48 + 'downloadImage',
  49 + 'getNetworkType',
  50 + 'openLocation',
  51 + 'getLocation',
  52 + 'hideOptionMenu',
  53 + 'showOptionMenu',
  54 + 'closeWindow',
  55 + 'scanQRCode',
  56 + 'chooseWXPay',
  57 + 'openProductSpecificView',
  58 + 'addCard',
  59 + 'chooseCard',
  60 + 'openCard'
  61 + ]
  62 + });
  63 + fun && setTimeout(fun,500);
  64 + return true;
  65 + }
61 }) 66 })
62 - } 67 + },
  68 +
  69 + wxShare: shareDate => {
  70 + document.getElementById('share').innerText = 'wxShare';
63 71
64 - wxShare(shareDate) {  
65 let share_data = shareDate || { 72 let share_data = shareDate || {
66 title: '', 73 title: '',
67 imgUrl: '', 74 imgUrl: '',
@@ -71,20 +78,15 @@ export default class { @@ -71,20 +78,15 @@ export default class {
71 } 78 }
72 }; 79 };
73 wx.ready(function () { 80 wx.ready(function () {
74 - //构造分享信息  
75 - let shareData = share_data;  
76 - let voice_localId = '';  
77 - let is_start = false; 81 + document.getElementById('share').innerText = JSON.stringify(share_data);
78 // 2.1 “分享给朋友” 82 // 2.1 “分享给朋友”
79 - wx.onMenuShareAppMessage(shareData); 83 + wx.onMenuShareAppMessage(share_data);
80 // 2.2 “分享到朋友圈” 84 // 2.2 “分享到朋友圈”
81 - wx.onMenuShareTimeline(shareData); 85 + wx.onMenuShareTimeline(share_data);
82 // 2.3 “分享到QQ” 86 // 2.3 “分享到QQ”
83 - wx.onMenuShareQQ(shareData); 87 + wx.onMenuShareQQ(share_data);
84 // 2.4 “分享到微博” 88 // 2.4 “分享到微博”
85 - wx.onMenuShareWeibo(shareData);  
86 - //document.getElementById('media').play();  
87 - //bindUploadEvent(); 89 + wx.onMenuShareWeibo(share_data);
88 }); 90 });
89 } 91 }
90 } 92 }
@@ -3,7 +3,9 @@ import wx from "weixin-js-sdk"; @@ -3,7 +3,9 @@ import wx from "weixin-js-sdk";
3 const invokeMethod = (obj) => { 3 const invokeMethod = (obj) => {
4 let appInterface = window.yohoInterface; 4 let appInterface = window.yohoInterface;
5 if (appInterface) { 5 if (appInterface) {
6 - appInterface.triggerEvent(obj.success || function(){}, obj.fail || function(){}, { 6 + appInterface.triggerEvent(obj.success || function () {
  7 + }, obj.fail || function () {
  8 + }, {
7 method: obj.method, 9 method: obj.method,
8 arguments: obj.args 10 arguments: obj.args
9 }); 11 });
@@ -12,28 +14,24 @@ const invokeMethod = (obj) => { @@ -12,28 +14,24 @@ const invokeMethod = (obj) => {
12 } 14 }
13 } 15 }
14 16
15 -const linkToMiniApp = (goUrl,type) => { 17 +const linkToMiniApp = (goUrl, type) => {
16 let url = goUrl || ''; 18 let url = goUrl || '';
17 if (url && url.indexOf('http') < 0 && type === 'other') { 19 if (url && url.indexOf('http') < 0 && type === 'other') {
18 url = document.location.protocol + '//' + document.location.host + url; 20 url = document.location.protocol + '//' + document.location.host + url;
19 } 21 }
20 -  
21 if (url) { 22 if (url) {
22 - let base_url = decodeURIComponent(url).split('?')[0];  
23 - let params = getQueryObj(url);  
24 - let paramStr = '';  
25 - Object.keys(params).forEach(key => {  
26 - paramStr += paramStr === '' ? '?' + key + '=' + params[key] : '&' + key + '=' + params[key];  
27 - })  
28 -  
29 - let mini_url = '/pages/webview/webview?url=' + base_url;  
30 - let scene = mini_url + encodeURIComponent(paramStr);  
31 - 23 + let scene;
32 if (type === 'product' || type === 'brand') { 24 if (type === 'product' || type === 'brand') {
33 - mini_url = url;  
34 - scene = mini_url; 25 + scene = url;
  26 + } else {
  27 + let base_url = decodeURIComponent(url).split('?')[0];
  28 + let params = getQueryObj(url);
  29 + let paramStr = '';
  30 + Object.keys(params).forEach(key => {
  31 + paramStr += paramStr === '' ? '?' + key + '=' + params[key] : '&' + key + '=' + params[key];
  32 + })
  33 + scene = '/pages/webview/webview?url=' + base_url + encodeURIComponent(paramStr);
35 } 34 }
36 -  
37 wx.miniProgram.navigateTo({url: scene}); 35 wx.miniProgram.navigateTo({url: scene});
38 return false; 36 return false;
39 } else { 37 } else {
@@ -44,11 +42,9 @@ const linkToMiniApp = (goUrl,type) => { @@ -44,11 +42,9 @@ const linkToMiniApp = (goUrl,type) => {
44 42
45 const getQueryObj = (link) => { 43 const getQueryObj = (link) => {
46 let loc = decodeURIComponent(document.location.href); 44 let loc = decodeURIComponent(document.location.href);
47 -  
48 if (link) { 45 if (link) {
49 loc = decodeURIComponent(link); 46 loc = decodeURIComponent(link);
50 } 47 }
51 - console.log('loc:', loc);  
52 let variables = ''; 48 let variables = '';
53 let variableArr = []; 49 let variableArr = [];
54 let finalArr = []; 50 let finalArr = [];
@@ -19,7 +19,7 @@ module.exports = { @@ -19,7 +19,7 @@ module.exports = {
19 path: path.resolve(__dirname, 'build'), 19 path: path.resolve(__dirname, 'build'),
20 filename: 'sdk.js', 20 filename: 'sdk.js',
21 libraryTarget: 'umd', 21 libraryTarget: 'umd',
22 - library: "_jssdk", 22 + library: "yo_sdk",
23 libraryExport: "default" 23 libraryExport: "default"
24 }, 24 },
25 //插件 25 //插件
@@ -41,7 +41,7 @@ module.exports = { @@ -41,7 +41,7 @@ module.exports = {
41 } 41 }
42 ] 42 ]
43 }, 43 },
44 - devtool: "source-map", 44 + devtool: "inline-source-map",
45 //模块路径 45 //模块路径
46 resolve:{ 46 resolve:{
47 alias: { 47 alias: {