Merge branch 'feature/mini' into 'master'
es5 改写 See merge request !8
Showing
19 changed files
with
504 additions
and
373 deletions
.gitlab-ci.yml
deleted
100644 → 0
1 | -language : node_js | ||
2 | -node_js: | ||
3 | - - "10" | ||
4 | - | ||
5 | -branches: | ||
6 | - only: | ||
7 | - - master | ||
8 | - | ||
9 | -install: | ||
10 | -- npm install | ||
11 | - | ||
12 | -os: | ||
13 | - - linux | ||
14 | - | ||
15 | -stages: | ||
16 | - - name: deploy | ||
17 | - | ||
18 | -jobs: | ||
19 | - include: | ||
20 | - - stage: deploy | ||
21 | - script: | ||
22 | - - echo "NPM Deploying Started ..." | ||
23 | - - npm version | ||
24 | - - npm run build | ||
25 | - - echo "NPM Building Finished." | ||
26 | - | ||
27 | - deploy: | ||
28 | - provider: npm | ||
29 | - email: chenfengjw@hotmail.com | ||
30 | - api_key: "$NPM_TOKEN" | ||
31 | - skip_cleanup: true | ||
32 | - on: | ||
33 | - all_branches: true |
@@ -18,7 +18,11 @@ | @@ -18,7 +18,11 @@ | ||
18 | 2. 微信验签方法提供 | 18 | 2. 微信验签方法提供 |
19 | ## 使用说明 | 19 | ## 使用说明 |
20 | 20 | ||
21 | -可以直接import也可以define() | 21 | +activity-sdk依赖wx-js-sdk,无论登陆还是页面跳转还是分享等等都依赖wx-js-sdk |
22 | + | ||
23 | +所以在加载activity-sdk之前先加载wx-js-sdk,之后再调用activity-sdk的方法 | ||
24 | + | ||
25 | +可以直接import也可以define(); | ||
22 | 26 | ||
23 | 如果是在html页面引入window.yo_sdk可以获取到所有的api | 27 | 如果是在html页面引入window.yo_sdk可以获取到所有的api |
24 | 28 | ||
@@ -51,17 +55,18 @@ | @@ -51,17 +55,18 @@ | ||
51 | 55 | ||
52 | 用法:`let env = window.yo_sdk.env` | 56 | 用法:`let env = window.yo_sdk.env` |
53 | 57 | ||
54 | -#### getUid: | 58 | +#### getUser: |
55 | 类型:function | 59 | 类型:function |
56 | 60 | ||
57 | -描述:获取当前用户uid,如果没有用户则该值为0 | 61 | +描述:获取当前用户uid,sessionKey,sessionType,appVersion以Object类型返回,如果没有用户则该uid值为0 |
58 | 62 | ||
59 | 用法: | 63 | 用法: |
60 | ``` | 64 | ``` |
61 | //执行返回的是一个Promise对象 | 65 | //执行返回的是一个Promise对象 |
62 | -window.yo_sdk.getUid(). | ||
63 | - then(function(uid){ | ||
64 | - console.log(uid); | 66 | +window.yo_sdk.getUser(). |
67 | + then(function(user){ | ||
68 | + console.log(user); | ||
69 | + //返回 {uid:1,sessionType:'',sessionKey:'',appVersion:''} | ||
65 | }) | 70 | }) |
66 | 71 | ||
67 | ``` | 72 | ``` |
This diff could not be displayed because it is too large.
@@ -12,15 +12,18 @@ | @@ -12,15 +12,18 @@ | ||
12 | <a class="auth" href="http://www.baidu.com">123123</a> | 12 | <a class="auth" href="http://www.baidu.com">123123</a> |
13 | <div id="uid"></div> | 13 | <div id="uid"></div> |
14 | <div id="url"></div> | 14 | <div id="url"></div> |
15 | +<script src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script> | ||
15 | <script src="../build/sdk.js"></script> | 16 | <script src="../build/sdk.js"></script> |
16 | <script> | 17 | <script> |
17 | window.yo_sdk.auth(); | 18 | window.yo_sdk.auth(); |
18 | document.getElementById('aaa').onclick = function (event) { | 19 | document.getElementById('aaa').onclick = function (event) { |
19 | - window.yo_sdk.getUid().then(function(uid){ | ||
20 | - console.log(uid); | ||
21 | - document.getElementById('uid').innerText = uid; | 20 | + window.yo_sdk.getUser().then(function (user) { |
21 | + console.log(user.uid); | ||
22 | + if (user.uid) | ||
23 | + document.getElementById('uid').innerText = user.uid; | ||
24 | + else | ||
25 | + window.yo_sdk.goLogin(); | ||
22 | }) | 26 | }) |
23 | - window.yo_sdk.goLogin(); | ||
24 | } | 27 | } |
25 | document.getElementById('url').innerText = location.href; | 28 | document.getElementById('url').innerText = location.href; |
26 | </script> | 29 | </script> |
example/testPromise.html
0 → 100644
@@ -8,9 +8,10 @@ | @@ -8,9 +8,10 @@ | ||
8 | </head> | 8 | </head> |
9 | <body> | 9 | <body> |
10 | <div id="share"> 分享</div> | 10 | <div id="share"> 分享</div> |
11 | +<script src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script> | ||
11 | <script src="../build/sdk.js"></script> | 12 | <script src="../build/sdk.js"></script> |
12 | <script> | 13 | <script> |
13 | - let shareData = { | 14 | + var shareData = { |
14 | title: '国潮崛起年,中西碰撞博出位!', | 15 | title: '国潮崛起年,中西碰撞博出位!', |
15 | imgUrl: 'https://img01.yohoboys.com/o_1cnilugbs15ajak1gfo1d2h1p348.png?imageView2/1/w/200/h/200', | 16 | imgUrl: 'https://img01.yohoboys.com/o_1cnilugbs15ajak1gfo1d2h1p348.png?imageView2/1/w/200/h/200', |
16 | desc: '这里有一份中西潮流榜单,请收好!', | 17 | desc: '这里有一份中西潮流榜单,请收好!', |
@@ -30,4 +31,4 @@ | @@ -30,4 +31,4 @@ | ||
30 | 31 | ||
31 | </script> | 32 | </script> |
32 | </body> | 33 | </body> |
33 | -</html> | ||
34 | +</html> |
1 | { | 1 | { |
2 | - "name": "yoho-activity-sdk", | ||
3 | - "version": "1.0.8", | ||
4 | - "description": "YOHO!前端js的功能封装包,主要用于在活动页面中,对主要功能的封装。", | ||
5 | - "keywords": [ | ||
6 | - "YOHO!", | ||
7 | - "JS-SDK" | ||
8 | - ], | ||
9 | - "main": "build/sdk.js", | ||
10 | - "scripts": { | ||
11 | - "build": "webpack --mode development --config ./webpack.config.babel.js ", | ||
12 | - "lint-js": "lint-js" | ||
13 | - }, | ||
14 | - "config": { | ||
15 | - "lintJs": [ | ||
16 | - { | ||
17 | - "title": "JS", | ||
18 | - "path": [ | ||
19 | - "." | 2 | + "name": "yoho-activity-sdk", |
3 | + "version": "1.0.9", | ||
4 | + "description": "YOHO!前端js的功能封装包,主要用于在活动页面中,对主要功能的封装。", | ||
5 | + "keywords": [ | ||
6 | + "YOHO!", | ||
7 | + "JS-SDK" | ||
8 | + ], | ||
9 | + "main": "build/sdk.js", | ||
10 | + "scripts": { | ||
11 | + "build": "webpack --mode production --config ./webpack.config.js ", | ||
12 | + "lint-js": "lint-js" | ||
13 | + }, | ||
14 | + "config": { | ||
15 | + "lintJs": [ | ||
16 | + { | ||
17 | + "title": "JS", | ||
18 | + "path": [ | ||
19 | + "." | ||
20 | + ] | ||
21 | + } | ||
20 | ] | 22 | ] |
21 | - } | ||
22 | - ] | ||
23 | - }, | ||
24 | - "repository": { | ||
25 | - "type": "git", | ||
26 | - "url": "http://git.yoho.cn/fe/yoho-activity-sdk.git" | ||
27 | - }, | ||
28 | - "author": "y.huang@yoho.cn", | ||
29 | - "licenses": "MIT", | ||
30 | - "devDependencies": { | ||
31 | - "babel-cli": "^6.26.0", | ||
32 | - "babel-core": "^6.26.0", | ||
33 | - "babel-loader": "^7.1.4", | ||
34 | - "babel-plugin-import": "^1.7.0", | ||
35 | - "babel-plugin-transform-runtime": "^6.23.0", | ||
36 | - "babel-plugin-syntax-dynamic-import": "^6.18.0", | ||
37 | - "babel-preset-env": "^1.6.1", | ||
38 | - "babel-preset-es2015": "^6.24.1", | ||
39 | - "babel-preset-stage-0": "^6.24.1", | ||
40 | - "clean-webpack-plugin": "^0.1.19", | ||
41 | - "compression-webpack-plugin": "^1.1.11", | ||
42 | - "copy-webpack-plugin": "^4.5.1", | ||
43 | - "extract-text-webpack-plugin": "^4.0.0-beta.0", | ||
44 | - "uglifyjs-webpack-plugin": "^2.0.1", | ||
45 | - "file-loader": "^1.1.11", | ||
46 | - "url-loader": "^1.0.1", | ||
47 | - "webpack": "^4.17.2", | ||
48 | - "webpack-cli": "^3.1.2", | ||
49 | - "weixin-js-sdk": "^1.3.3", | ||
50 | - "fetch-jsonp": "^1.1.3", | ||
51 | - "eslint": "^3.19.0", | ||
52 | - "eslint-config-yoho": "^1.0.1", | ||
53 | - "eslint-loader": "^1.7.1", | ||
54 | - "eslint-plugin-html": "^2.0.3", | ||
55 | - "promise-polyfill": "^8.1.0" | ||
56 | - }, | ||
57 | - "dependencies": {}, | ||
58 | - "directories": { | ||
59 | - "example": "example" | ||
60 | - }, | ||
61 | - "license": "ISC" | 23 | + }, |
24 | + "repository": { | ||
25 | + "type": "git", | ||
26 | + "url": "http://git.yoho.cn/fe/yoho-activity-sdk.git" | ||
27 | + }, | ||
28 | + "author": "y.huang@yoho.cn", | ||
29 | + "licenses": "MIT", | ||
30 | + "devDependencies": { | ||
31 | + "babel-cli": "^6.26.0", | ||
32 | + "babel-core": "^6.26.0", | ||
33 | + "babel-loader": "^7.1.4", | ||
34 | + "babel-plugin-import": "^1.7.0", | ||
35 | + "babel-plugin-syntax-dynamic-import": "^6.18.0", | ||
36 | + "babel-plugin-transform-runtime": "^6.23.0", | ||
37 | + "babel-preset-env": "^1.6.1", | ||
38 | + "babel-preset-es2015": "^6.24.1", | ||
39 | + "babel-preset-stage-0": "^6.24.1", | ||
40 | + "clean-webpack-plugin": "^0.1.19", | ||
41 | + "compression-webpack-plugin": "^1.1.11", | ||
42 | + "copy-webpack-plugin": "^4.5.1", | ||
43 | + "eslint": "^3.19.0", | ||
44 | + "eslint-config-yoho": "^1.0.1", | ||
45 | + "eslint-loader": "^1.7.1", | ||
46 | + "eslint-plugin-html": "^2.0.3", | ||
47 | + "extract-text-webpack-plugin": "^4.0.0-beta.0", | ||
48 | + "fetch-jsonp": "^1.1.3", | ||
49 | + "file-loader": "^1.1.11", | ||
50 | + "uglifyjs-webpack-plugin": "^2.0.1", | ||
51 | + "url-loader": "^1.0.1", | ||
52 | + "webpack": "^4.17.2", | ||
53 | + "webpack-bundle-analyzer": "^3.0.3", | ||
54 | + "webpack-cli": "^3.1.2" | ||
55 | + }, | ||
56 | + "dependencies": {}, | ||
57 | + "directories": { | ||
58 | + "example": "example" | ||
59 | + }, | ||
60 | + "license": "ISC" | ||
62 | } | 61 | } |
1 | -import base from './public/base' | ||
2 | -import Login from './public/login' | ||
3 | -import Goto from './public/goto' | ||
4 | -import wxCtrl from './public/share' | ||
5 | -import {getQueryObj,invokeMethod} from './utils/common' | 1 | +var base = require('./public/base'); |
2 | +var Login = require('./public/login'); | ||
3 | +var Goto = require('./public/goto'); | ||
4 | +var wxCtrl = require('./public/share'); | ||
5 | +var common = require('./utils/common'); | ||
6 | +var env = base.env(); | ||
7 | +var loginUrl = base.loginUrl(); | ||
8 | +var login = new Login({loginUrl:loginUrl, env:env}); | ||
9 | +var goto = new Goto(env); | ||
10 | +var wx = new wxCtrl(); | ||
6 | 11 | ||
7 | -const env = base.env(); | ||
8 | -const loginUrl = base.loginUrl(); | ||
9 | -const login = new Login({loginUrl, env}); | ||
10 | -const goto = new Goto(env); | ||
11 | -const wx = new wxCtrl(); | ||
12 | - | ||
13 | -export default { | 12 | +module.exports = { |
14 | auth:login.auth.bind(login), | 13 | auth:login.auth.bind(login), |
15 | goLogin: login.goLogin.bind(login), | 14 | goLogin: login.goLogin.bind(login), |
16 | getUser:base.getUser, | 15 | getUser:base.getUser, |
@@ -19,6 +18,6 @@ export default { | @@ -19,6 +18,6 @@ export default { | ||
19 | goBack: goto.goBack.bind(goto), | 18 | goBack: goto.goBack.bind(goto), |
20 | wxShare: wx.wxShare.bind(wx), | 19 | wxShare: wx.wxShare.bind(wx), |
21 | wxSignature: wx.wxSignature, | 20 | wxSignature: wx.wxSignature, |
22 | - getQueryObj, | ||
23 | - invokeMethod | 21 | + getQueryObj:common.getQueryObj, |
22 | + invokeMethod:common.invokeMethod | ||
24 | } | 23 | } |
1 | -import jsonp from "../utils/jsonp"; | ||
2 | -import cookies from '../utils/cookie'; | ||
3 | -import {getQueryObj} from '../utils/common' | ||
4 | -import Promise from 'promise-polyfill' | ||
5 | -import {invokeMethod} from '../utils/common' | ||
6 | - | ||
7 | -const parseUrl = (url) => { | ||
8 | - let query = {}, | 1 | +var jsonp = require('../utils/jsonp'); |
2 | +var cookies = require('../utils/cookie'); | ||
3 | +var common = require('../utils/common'); | ||
4 | +var Promise = require('../utils/promise'); | ||
5 | +var getQueryObj = common.getQueryObj; | ||
6 | +var parseUrl = function (url) { | ||
7 | + var query = {}, | ||
9 | hashs, | 8 | hashs, |
10 | hash, | 9 | hash, |
11 | i; | 10 | i; |
@@ -25,42 +24,44 @@ const parseUrl = (url) => { | @@ -25,42 +24,44 @@ const parseUrl = (url) => { | ||
25 | query: query | 24 | query: query |
26 | } | 25 | } |
27 | }; | 26 | }; |
28 | -let env = ''; | ||
29 | 27 | ||
30 | -const _appGetUid = function () { | 28 | +var env = ''; |
29 | + | ||
30 | +var _appGetUid = function () { | ||
31 | return new Promise(function (resolve) { | 31 | return new Promise(function (resolve) { |
32 | - invokeMethod({ | 32 | + common.invokeMethod({ |
33 | method: 'get.uid', | 33 | method: 'get.uid', |
34 | arguments: {}, | 34 | arguments: {}, |
35 | success: function (uid) { | 35 | success: function (uid) { |
36 | resolve(parseInt(uid)); | 36 | resolve(parseInt(uid)); |
37 | }, | 37 | }, |
38 | fail: function () { | 38 | fail: function () { |
39 | - let uid = Number(cookies.cookie('app_uid')) || Number(getQueryObj().uid) || 0; | 39 | + var uid = Number(cookies.cookie('app_uid')) || Number(getQueryObj().uid) || 0; |
40 | resolve(uid); | 40 | resolve(uid); |
41 | } | 41 | } |
42 | }); | 42 | }); |
43 | }) | 43 | }) |
44 | -} | ||
45 | -const _appGetSessionKey = function () { | 44 | +}; |
45 | +var _appGetSessionKey = function () { | ||
46 | return new Promise(function (resolve) { | 46 | return new Promise(function (resolve) { |
47 | - invokeMethod({ | 47 | + common.invokeMethod({ |
48 | method: 'get.sessionId', | 48 | method: 'get.sessionId', |
49 | arguments: {}, | 49 | arguments: {}, |
50 | success: function (sessionKey) { | 50 | success: function (sessionKey) { |
51 | resolve(sessionKey); | 51 | resolve(sessionKey); |
52 | }, | 52 | }, |
53 | fail: function () { | 53 | fail: function () { |
54 | - let sessionKey = cookies.cookie('app_session_key') || getQueryObj().session_key || ''; | 54 | + var sessionKey = cookies.cookie('app_session_key') || getQueryObj().session_key || ''; |
55 | resolve(sessionKey); | 55 | resolve(sessionKey); |
56 | } | 56 | } |
57 | }); | 57 | }); |
58 | }) | 58 | }) |
59 | -} | ||
60 | -export default { | 59 | +}; |
60 | + | ||
61 | +module.exports = { | ||
61 | //evn | 62 | //evn |
62 | env: function () { | 63 | env: function () { |
63 | - let envFlag = window.__wxjs_environment; | 64 | + var envFlag = window.__wxjs_environment; |
64 | if (!envFlag && navigator.userAgent.match(/yohobuy/i)) { | 65 | if (!envFlag && navigator.userAgent.match(/yohobuy/i)) { |
65 | env = 'app'; | 66 | env = 'app'; |
66 | document.addEventListener('deviceready', function () { | 67 | document.addEventListener('deviceready', function () { |
@@ -76,7 +77,7 @@ export default { | @@ -76,7 +77,7 @@ export default { | ||
76 | }, | 77 | }, |
77 | //url | 78 | //url |
78 | loginUrl: function () { | 79 | loginUrl: function () { |
79 | - let url, refer = location.href; | 80 | + var url, refer = location.href; |
80 | if (env === 'app') { | 81 | if (env === 'app') { |
81 | url = 'http://m.yohobuy.com/signin.html?refer=' + encodeURIComponent(refer); | 82 | url = 'http://m.yohobuy.com/signin.html?refer=' + encodeURIComponent(refer); |
82 | refer = parseUrl(refer); | 83 | refer = parseUrl(refer); |
@@ -122,12 +123,12 @@ export default { | @@ -122,12 +123,12 @@ export default { | ||
122 | //uid | 123 | //uid |
123 | getUser: function () { | 124 | getUser: function () { |
124 | return new Promise(function (resolve) { | 125 | return new Promise(function (resolve) { |
125 | - let obj = { | 126 | + var obj = { |
126 | sessionType: cookies.cookie('app_client_type') || getQueryObj().app_client_type || '', | 127 | sessionType: cookies.cookie('app_client_type') || getQueryObj().app_client_type || '', |
127 | appVersion: cookies.cookie('app_version') || getQueryObj().app_version || '' | 128 | appVersion: cookies.cookie('app_version') || getQueryObj().app_version || '' |
128 | - } | 129 | + }; |
129 | if (env === 'h5' || env === 'pc') { | 130 | if (env === 'h5' || env === 'pc') { |
130 | - jsonp('//m.yohobuy.com/passport/login/user').then(r => { | 131 | + jsonp({url: '//m.yohobuy.com/passport/login/user', jsonp: 'callback'}).then(function (r) { |
131 | obj.uid = 0; | 132 | obj.uid = 0; |
132 | if (r.code === 200) { | 133 | if (r.code === 200) { |
133 | obj.uid = Number(r.data) | 134 | obj.uid = Number(r.data) |
@@ -136,13 +137,13 @@ export default { | @@ -136,13 +137,13 @@ export default { | ||
136 | resolve(obj); | 137 | resolve(obj); |
137 | }); | 138 | }); |
138 | } else if (env === 'app') { | 139 | } else if (env === 'app') { |
139 | - document.addEventListener('deviceready', () => { | 140 | + document.addEventListener('deviceready',function() { |
140 | return Promise.all([_appGetUid(), _appGetSessionKey()]).then(function (r) { | 141 | return Promise.all([_appGetUid(), _appGetSessionKey()]).then(function (r) { |
141 | - obj.uid = r[0] | ||
142 | - obj.sessionKey = r[1] | 142 | + obj.uid = r[0]; |
143 | + obj.sessionKey = r[1]; | ||
143 | resolve(obj); | 144 | resolve(obj); |
144 | }) | 145 | }) |
145 | - }); | 146 | + }) |
146 | } else { | 147 | } else { |
147 | obj.uid = Number(cookies.cookie('app_uid')) || Number(getQueryObj().uid) || 0; | 148 | obj.uid = Number(cookies.cookie('app_uid')) || Number(getQueryObj().uid) || 0; |
148 | obj.sessionKey = cookies.cookie('app_session_key') || getQueryObj().session_key || ''; | 149 | obj.sessionKey = cookies.cookie('app_session_key') || getQueryObj().session_key || ''; |
@@ -150,4 +151,4 @@ export default { | @@ -150,4 +151,4 @@ export default { | ||
150 | } | 151 | } |
151 | }) | 152 | }) |
152 | } | 153 | } |
153 | -} | 154 | +}; |
1 | -import wx from 'weixin-js-sdk'; | ||
2 | -import {linkToMiniApp, invokeMethod, createLinkButton} from "../utils/common"; | 1 | +var common = require('../utils/common'); |
3 | 2 | ||
4 | -export default class { | ||
5 | - constructor(_env) { | ||
6 | - this.env = _env; | ||
7 | - } | ||
8 | - | ||
9 | - link(event) { | ||
10 | - let element = event.currentTarget; | ||
11 | - let type = element.getAttribute('data-type') || 'other'; | ||
12 | - let id = element.getAttribute('data-id') || ''; | ||
13 | - let url = element.getAttribute('data-url') || ''; | ||
14 | - let name = element.getAttribute('data-name') || ''; | ||
15 | - let linkUrl = ''; | ||
16 | - if (this.env === 'miniprogram') { | ||
17 | - if (type === 'product') { | ||
18 | - linkUrl = '/pages/goodsDetail/goodsDetail?productSkn=' + id + '&page_name=goodsList&page_param=5' | ||
19 | - } else if (type === 'brand') { | ||
20 | - linkUrl = '/pages/goodsList/brandStore?shop_id=' + id + '&shop_name=' + name | ||
21 | - } else { | ||
22 | - linkUrl = url | ||
23 | - } | ||
24 | - return linkToMiniApp(linkUrl, type); | ||
25 | - } else if (this.env === 'pc') { | ||
26 | - if (type === 'product') { | ||
27 | - linkUrl = 'https://www.yohobuy.com/product/' + id + '.html'; | ||
28 | - } else if (type === 'brand') { | ||
29 | - linkUrl = 'https://www.yohobuy.com/shop/' + name + '-' + id + '.html'; | ||
30 | - } else { | ||
31 | - linkUrl = url | ||
32 | - } | 3 | +function goto(_env){ |
4 | + this.env = _env; | ||
5 | +} | ||
6 | +goto.prototype.link = function(event){ | ||
7 | + var element = event.currentTarget; | ||
8 | + var type = element.getAttribute('data-type') || 'other'; | ||
9 | + var id = element.getAttribute('data-id') || ''; | ||
10 | + var url = element.getAttribute('data-url') || ''; | ||
11 | + var name = element.getAttribute('data-name') || ''; | ||
12 | + var linkUrl = ''; | ||
13 | + if (this.env === 'miniprogram') { | ||
14 | + if (type === 'product') { | ||
15 | + linkUrl = '/pages/goodsDetail/goodsDetail?productSkn=' + id + '&page_name=goodsList&page_param=5' | ||
16 | + } else if (type === 'brand') { | ||
17 | + linkUrl = '/pages/goodsList/brandStore?shop_id=' + id + '&shop_name=' + name | ||
33 | } else { | 18 | } else { |
34 | - if (type === 'product') { | ||
35 | - linkUrl = '//m.yohobuy.com/product/' + id + '.html?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":' + id + '}}' | ||
36 | - } else if (type === 'brand') { | ||
37 | - linkUrl = '//m.yohobuy.com/product/index/brand?shop_id=' + id + '&openby:yohobuy={"action":"go.shop","params":{"shop_id":"' + id + '","shop_template_type":"1","shop_name":"' + name + '","is_red_shop":"1"}}' | ||
38 | - } else { | ||
39 | - linkUrl = url | ||
40 | - } | 19 | + linkUrl = url |
41 | } | 20 | } |
42 | - if (this.env === 'app') { | ||
43 | - let dom = document.getElementById('yosdk-goto'); | ||
44 | - if (!dom) { | ||
45 | - dom = createLinkButton(linkUrl, 'yosdk-goto'); | ||
46 | - } else { | ||
47 | - dom.setAttribute('href', linkUrl); | ||
48 | - } | ||
49 | - return dom.click(); | ||
50 | - | 21 | + return common.linkToMiniApp(linkUrl, type); |
22 | + } else if (this.env === 'pc') { | ||
23 | + if (type === 'product') { | ||
24 | + linkUrl = 'https://www.yohobuy.com/product/' + id + '.html'; | ||
25 | + } else if (type === 'brand') { | ||
26 | + linkUrl = 'https://www.yohobuy.com/shop/' + name + '-' + id + '.html'; | ||
27 | + } else { | ||
28 | + linkUrl = url | ||
29 | + } | ||
30 | + } else { | ||
31 | + if (type === 'product') { | ||
32 | + linkUrl = '//m.yohobuy.com/product/' + id + '.html?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":' + id + '}}' | ||
33 | + } else if (type === 'brand') { | ||
34 | + linkUrl = '//m.yohobuy.com/product/index/brand?shop_id=' + id + '&openby:yohobuy={"action":"go.shop","params":{"shop_id":"' + id + '","shop_template_type":"1","shop_name":"' + name + '","is_red_shop":"1"}}' | ||
51 | } else { | 35 | } else { |
52 | - window.location.href = linkUrl | 36 | + linkUrl = url |
53 | } | 37 | } |
54 | } | 38 | } |
55 | - | ||
56 | - goBack() { | ||
57 | - if (this.env === 'app') { | ||
58 | - invokeMethod({method: 'go.back'}) | ||
59 | - } else if (this.env === 'miniprogram') { | ||
60 | - wx.miniProgram.navigateBack(); | 39 | + if (this.env === 'app') { |
40 | + var dom = document.getElementById('yosdk-goto'); | ||
41 | + if (!dom) { | ||
42 | + dom = common.createLinkButton(linkUrl, 'yosdk-goto'); | ||
61 | } else { | 43 | } else { |
62 | - history.go(-1); | 44 | + dom.setAttribute('href', linkUrl); |
63 | } | 45 | } |
46 | + return dom.click(); | ||
47 | + | ||
48 | + } else { | ||
49 | + window.location.href = linkUrl | ||
64 | } | 50 | } |
51 | +}; | ||
65 | 52 | ||
66 | -} | 53 | +goto.prototype.goBack=function () { |
54 | + if (this.env === 'app') { | ||
55 | + common.invokeMethod({method: 'go.back'}) | ||
56 | + } else if (this.env === 'miniprogram') { | ||
57 | + wx.miniProgram.navigateBack(); | ||
58 | + } else { | ||
59 | + history.go(-1); | ||
60 | + } | ||
61 | +}; | ||
62 | +module.exports = goto; |
1 | -import wx from 'weixin-js-sdk'; | ||
2 | -import {createLinkButton} from "../utils/common"; | ||
3 | -import base from './base'; | 1 | +var common = require('../utils/common'); |
2 | +var base = require('./base'); | ||
4 | 3 | ||
5 | -export default class { | ||
6 | - constructor(obj) { | ||
7 | - this.env = obj.env; | ||
8 | - this.url = obj.loginUrl; | ||
9 | - } | 4 | +function login(obj) { |
5 | + var _this = this; | ||
6 | + _this.env = obj.env; | ||
7 | + _this.url = obj.loginUrl; | ||
10 | 8 | ||
11 | - auth() { | ||
12 | - base.getUser().then(({uid}) => { | ||
13 | - if (!uid) { | ||
14 | - let list = document.querySelectorAll('.auth'); | 9 | + _this.goLogin = function () { |
10 | + if (_this.env === 'miniprogram') { | ||
11 | + wx.miniProgram.switchTab({url: _this.url}); | ||
12 | + } else if (_this.env === 'app') { | ||
13 | + var loginButton = document.getElementById('yosdk-login'); | ||
14 | + if (!loginButton) { | ||
15 | + loginButton = common.createLinkButton(_this.url, 'yosdk-login'); | ||
16 | + } | ||
17 | + loginButton.click(); | ||
18 | + } else { | ||
19 | + window.location.href = _this.url; | ||
20 | + } | ||
21 | + return false; | ||
22 | + }; | ||
23 | + | ||
24 | + _this.auth = function () { | ||
25 | + base.getUser().then(function (user) { | ||
26 | + if (!user.uid) { | ||
27 | + var list = document.querySelectorAll('.auth'); | ||
15 | if (list.length) { | 28 | if (list.length) { |
16 | - list.forEach((value) => { | ||
17 | - value.onclick = (event) => { | 29 | + list.forEach(function (value) { |
30 | + value.onclick = function (event) { | ||
18 | event.preventDefault(); | 31 | event.preventDefault(); |
19 | - this.goLogin(); | 32 | + _this.goLogin(); |
20 | } | 33 | } |
21 | }); | 34 | }); |
22 | return true; | 35 | return true; |
@@ -24,19 +37,6 @@ export default class { | @@ -24,19 +37,6 @@ export default class { | ||
24 | } | 37 | } |
25 | }); | 38 | }); |
26 | } | 39 | } |
27 | - | ||
28 | - goLogin() { | ||
29 | - if (this.env === 'miniprogram') { | ||
30 | - wx.miniProgram.switchTab({url: this.url}); | ||
31 | - } else if(this.env === 'app'){ | ||
32 | - let loginButton = document.getElementById('yosdk-login'); | ||
33 | - if (!loginButton) { | ||
34 | - loginButton = createLinkButton(this.url, 'yosdk-login'); | ||
35 | - } | ||
36 | - loginButton.click(); | ||
37 | - } else { | ||
38 | - window.location.href = this.url; | ||
39 | - } | ||
40 | - return false; | ||
41 | - } | ||
42 | } | 40 | } |
41 | + | ||
42 | +module.exports = login; |
1 | //初始化config信息 | 1 | //初始化config信息 |
2 | -import httpServer from '../utils/jsonp'; | ||
3 | -import wx from "weixin-js-sdk"; | 2 | +var httpServer = require('../utils/jsonp'); |
4 | 3 | ||
5 | -const _weChatInterface = '//action.yoho.cn/api/share/getSignPackage';//签名等相关配置,yoho公众号 | ||
6 | -export default class { | ||
7 | - | ||
8 | - wxSignature = fun => { | ||
9 | - httpServer(_weChatInterface + "?pageurl=" + encodeURIComponent(location.href.split('#')[0])).then((json) => { | ||
10 | - if (!json.appId) { | ||
11 | - return false; | ||
12 | - } else { | ||
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 | - }); | ||
61 | - fun && setTimeout(fun,500); | ||
62 | - return true; | ||
63 | - } | ||
64 | - }) | ||
65 | - }; | 4 | +var _weChatInterface = '//action.yoho.cn/api/share/getSignPackage';//签名等相关配置,yoho公众号 |
5 | +function wx() { | ||
6 | +} | ||
66 | 7 | ||
67 | - wxShare = shareDate => { | ||
68 | - this.wxSignature(function(){ | ||
69 | - let share_data = shareDate || { | ||
70 | - title: '', | ||
71 | - imgUrl: '', | ||
72 | - desc: '', | ||
73 | - link: document.location.href, | ||
74 | - success: function () { | ||
75 | - } | ||
76 | - }; | ||
77 | - wx.ready(function () { | ||
78 | - // 2.1 “分享给朋友” | ||
79 | - wx.onMenuShareAppMessage(share_data); | ||
80 | - // 2.2 “分享到朋友圈” | ||
81 | - wx.onMenuShareTimeline(share_data); | ||
82 | - // 2.3 “分享到QQ” | ||
83 | - wx.onMenuShareQQ(share_data); | ||
84 | - // 2.4 “分享到微博” | ||
85 | - wx.onMenuShareWeibo(share_data); | 8 | +wx.prototype.wxSignature = function (fun) { |
9 | + httpServer(_weChatInterface + "?pageurl=" + encodeURIComponent(location.href.split('#')[0])).then(function (json) { | ||
10 | + if (!json.appId) { | ||
11 | + return false; | ||
12 | + } else { | ||
13 | + var _appId = json.appId.toString(); | ||
14 | + var _timestamp = json.timestamp; | ||
15 | + var _nonceStr = json.nonceStr.toString(); | ||
16 | + var _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 | + ] | ||
86 | }); | 60 | }); |
87 | - }) | 61 | + fun && setTimeout(fun, 500); |
62 | + return true; | ||
63 | + } | ||
64 | + }) | ||
65 | +}; | ||
66 | + | ||
67 | +wx.prototype.wxShare = function (shareDate) { | ||
68 | + this.wxSignature(function () { | ||
69 | + var share_data = shareDate || { | ||
70 | + title: '', | ||
71 | + imgUrl: '', | ||
72 | + desc: '', | ||
73 | + link: document.location.href, | ||
74 | + success: function () { | ||
75 | + } | ||
76 | + }; | ||
77 | + wx.ready(function () { | ||
78 | + // 2.1 “分享给朋友” | ||
79 | + wx.onMenuShareAppMessage(share_data); | ||
80 | + // 2.2 “分享到朋友圈” | ||
81 | + wx.onMenuShareTimeline(share_data); | ||
82 | + // 2.3 “分享到QQ” | ||
83 | + wx.onMenuShareQQ(share_data); | ||
84 | + // 2.4 “分享到微博” | ||
85 | + wx.onMenuShareWeibo(share_data); | ||
86 | + }); | ||
87 | + }) | ||
88 | 88 | ||
89 | - } | ||
90 | } | 89 | } |
91 | 90 | ||
91 | +module.exports = wx; |
1 | -import wx from "weixin-js-sdk"; | ||
2 | - | ||
3 | -const invokeMethod = (obj) => { | 1 | +var invokeMethod = function(obj) { |
4 | if (window.yohoInterface) { | 2 | if (window.yohoInterface) { |
5 | window.yohoInterface.triggerEvent(obj.success || function () { | 3 | window.yohoInterface.triggerEvent(obj.success || function () { |
6 | }, obj.fail || function () { | 4 | }, obj.fail || function () { |
@@ -13,20 +11,20 @@ const invokeMethod = (obj) => { | @@ -13,20 +11,20 @@ const invokeMethod = (obj) => { | ||
13 | } | 11 | } |
14 | } | 12 | } |
15 | 13 | ||
16 | -const linkToMiniApp = (goUrl, type) => { | ||
17 | - let url = goUrl || ''; | 14 | +var linkToMiniApp = function(goUrl, type) { |
15 | + var url = goUrl || ''; | ||
18 | if (url && url.indexOf('http') < 0 && type === 'other') { | 16 | if (url && url.indexOf('http') < 0 && type === 'other') { |
19 | url = document.location.protocol + '//' + document.location.host + url; | 17 | url = document.location.protocol + '//' + document.location.host + url; |
20 | } | 18 | } |
21 | if (url) { | 19 | if (url) { |
22 | - let scene; | 20 | + var scene; |
23 | if (type === 'product' || type === 'brand') { | 21 | if (type === 'product' || type === 'brand') { |
24 | scene = url; | 22 | scene = url; |
25 | } else { | 23 | } else { |
26 | - let base_url = decodeURIComponent(url).split('?')[0]; | ||
27 | - let params = getQueryObj(url); | ||
28 | - let paramStr = ''; | ||
29 | - Object.keys(params).forEach(key => { | 24 | + var base_url = decodeURIComponent(url).split('?')[0]; |
25 | + var params = getQueryObj(url); | ||
26 | + var paramStr = ''; | ||
27 | + Object.keys(params).forEach(function(key) { | ||
30 | paramStr += paramStr === '' ? '?' + key + '=' + params[key] : '&' + key + '=' + params[key]; | 28 | paramStr += paramStr === '' ? '?' + key + '=' + params[key] : '&' + key + '=' + params[key]; |
31 | }) | 29 | }) |
32 | scene = '/pages/webview/webview?url=' + base_url + encodeURIComponent(paramStr); | 30 | scene = '/pages/webview/webview?url=' + base_url + encodeURIComponent(paramStr); |
@@ -39,14 +37,14 @@ const linkToMiniApp = (goUrl, type) => { | @@ -39,14 +37,14 @@ const linkToMiniApp = (goUrl, type) => { | ||
39 | }; | 37 | }; |
40 | 38 | ||
41 | 39 | ||
42 | -const getQueryObj = (link) => { | ||
43 | - let loc = decodeURIComponent(document.location.href); | 40 | +var getQueryObj = function(link) { |
41 | + var loc = decodeURIComponent(document.location.href); | ||
44 | if (link) { | 42 | if (link) { |
45 | loc = decodeURIComponent(link); | 43 | loc = decodeURIComponent(link); |
46 | } | 44 | } |
47 | - let variables = ''; | ||
48 | - let variableArr = []; | ||
49 | - let finalArr = []; | 45 | + var variables = ''; |
46 | + var variableArr = []; | ||
47 | + var finalArr = []; | ||
50 | 48 | ||
51 | if (loc.indexOf('?') > 0) { | 49 | if (loc.indexOf('?') > 0) { |
52 | variables = loc.split('?')[1]; | 50 | variables = loc.split('?')[1]; |
@@ -56,30 +54,30 @@ const getQueryObj = (link) => { | @@ -56,30 +54,30 @@ const getQueryObj = (link) => { | ||
56 | variableArr = variables.split('#')[0].split('&'); | 54 | variableArr = variables.split('#')[0].split('&'); |
57 | } | 55 | } |
58 | 56 | ||
59 | - for (let i = 0; i < variableArr.length; i++) { | ||
60 | - let obj = {}; | 57 | + for (var i = 0; i < variableArr.length; i++) { |
58 | + var obj = {}; | ||
61 | 59 | ||
62 | obj.name = variableArr[i].split('=')[0]; | 60 | obj.name = variableArr[i].split('=')[0]; |
63 | obj.value = variableArr[i].split('=')[1]; | 61 | obj.value = variableArr[i].split('=')[1]; |
64 | if (variableArr[i].split('=').length > 2) { | 62 | if (variableArr[i].split('=').length > 2) { |
65 | - for (let j = 2; j < variableArr[i].split('=').length; j++) { | 63 | + for (var j = 2; j < variableArr[i].split('=').length; j++) { |
66 | obj.value += '=' + variableArr[i].split('=')[j]; | 64 | obj.value += '=' + variableArr[i].split('=')[j]; |
67 | } | 65 | } |
68 | } | 66 | } |
69 | finalArr.push(obj); | 67 | finalArr.push(obj); |
70 | } | 68 | } |
71 | 69 | ||
72 | - let query_obj = {}; | 70 | + var query_obj = {}; |
73 | 71 | ||
74 | - for (let i = 0; i < finalArr.length; i++) { | 72 | + for (var i = 0; i < finalArr.length; i++) { |
75 | query_obj[finalArr[i].name] = finalArr[i].value; | 73 | query_obj[finalArr[i].name] = finalArr[i].value; |
76 | } | 74 | } |
77 | 75 | ||
78 | return query_obj; | 76 | return query_obj; |
79 | }; | 77 | }; |
80 | 78 | ||
81 | -const createLinkButton = (url, id) => { | ||
82 | - let a = document.createElement('a'); | 79 | +var createLinkButton = function(url, id) { |
80 | + var a = document.createElement('a'); | ||
83 | a.style.position = 'fixed'; | 81 | a.style.position = 'fixed'; |
84 | a.style.top = 0; | 82 | a.style.top = 0; |
85 | a.style.left = 0; | 83 | a.style.left = 0; |
@@ -92,10 +90,11 @@ const createLinkButton = (url, id) => { | @@ -92,10 +90,11 @@ const createLinkButton = (url, id) => { | ||
92 | a.setAttribute('href', url); | 90 | a.setAttribute('href', url); |
93 | document.body.appendChild(a); | 91 | document.body.appendChild(a); |
94 | return a; | 92 | return a; |
95 | -} | ||
96 | -export { | ||
97 | - invokeMethod, | ||
98 | - getQueryObj, | ||
99 | - linkToMiniApp, | ||
100 | - createLinkButton | 93 | +}; |
94 | + | ||
95 | +module.exports = { | ||
96 | + invokeMethod: invokeMethod, | ||
97 | + getQueryObj: getQueryObj, | ||
98 | + linkToMiniApp: linkToMiniApp, | ||
99 | + createLinkButton: createLinkButton | ||
101 | }; | 100 | }; |
1 | -import fetchJsonp from 'fetch-jsonp' | ||
2 | - | ||
3 | -module.exports = function (uri, option) { | ||
4 | - return new Promise(resolve => { | ||
5 | - fetchJsonp(uri) | ||
6 | - .then(function (response) { | ||
7 | - return response.json() | ||
8 | - }).then(function (json) { | ||
9 | - return resolve(json) | ||
10 | - }).catch(function (ex) { | ||
11 | - return resolve({code: 202, data: ex}) | ||
12 | - }) | 1 | +var Promise = require('./promise'); |
2 | + | ||
3 | +// jsonp请求 | ||
4 | +function jsonp(params) { | ||
5 | + return new Promise(function (resovle, reject) { | ||
6 | + params = params || {}; | ||
7 | + params.data = params.data || {}; | ||
8 | + //创建script标签并加入到页面中 | ||
9 | + var callbackName = params.jsonp; | ||
10 | + | ||
11 | + // 设置传递给后台的回调参数名 | ||
12 | + params.data['callback'] = callbackName; | ||
13 | + var data = formatParams(params.data); | ||
14 | + var script = document.createElement('script'); | ||
15 | + document.body.appendChild(script); | ||
16 | + | ||
17 | + // 创建jsonp回调函数 | ||
18 | + window[callbackName] = function (json) { | ||
19 | + document.body.removeChild(script); | ||
20 | + clearTimeout(script.timer); | ||
21 | + window[callbackName] = null; | ||
22 | + return resovle(json); | ||
23 | + }; | ||
24 | + | ||
25 | + // 发送请求 | ||
26 | + script.src = params.url + '?' + data; | ||
27 | + | ||
28 | + //为了得知此次请求是否成功,设置超时处理 | ||
29 | + if (params.time) { | ||
30 | + script.timer = setTimeout(function () { | ||
31 | + window[callbackName] = null; | ||
32 | + document.body.removeChild(script); | ||
33 | + reject({message: '超时'}); | ||
34 | + }, time); | ||
35 | + } | ||
13 | }) | 36 | }) |
14 | -} | ||
37 | +} | ||
38 | + | ||
39 | +//格式化参数 | ||
40 | +function formatParams(data) { | ||
41 | + var arr = []; | ||
42 | + for (var name in data) { | ||
43 | + arr.push(encodeURIComponent(name) + '=' + encodeURIComponent(data[name])); | ||
44 | + } | ||
45 | + | ||
46 | + // 添加一个随机数,防止缓存 | ||
47 | + var date = new Date(); | ||
48 | + arr.push('v=' + date.getTime()); | ||
49 | + return arr.join('&'); | ||
50 | +} | ||
51 | + | ||
52 | +module.exports = jsonp; | ||
53 | + |
src/utils/promise.js
0 → 100644
1 | +function Promise(fn) { | ||
2 | + var promise = this; | ||
3 | + promise._value = null; | ||
4 | + promise._reason = function(){}; | ||
5 | + promise._resolves = []; | ||
6 | + promise._rejects = []; | ||
7 | + promise._status = 'PENDING'; | ||
8 | + | ||
9 | + this.then = function (onFulfilled, onRejected) { | ||
10 | + return new Promise(function(resolve, reject) { | ||
11 | + function handle(value) { | ||
12 | + var ret = typeof onFulfilled === 'function' && onFulfilled(value) || value; | ||
13 | + if(ret && typeof ret['then'] === 'function'){ | ||
14 | + ret.then(function(value){ | ||
15 | + resolve(value); | ||
16 | + },function(reason){ | ||
17 | + reject(reason); | ||
18 | + }); | ||
19 | + } else { | ||
20 | + resolve(ret); | ||
21 | + } | ||
22 | + } | ||
23 | + function errback(reason){ | ||
24 | + reason = typeof onRejected === 'function' && onRejected(reason) || reason; | ||
25 | + reject(reason); | ||
26 | + } | ||
27 | + if (promise._status === 'PENDING') { | ||
28 | + promise._resolves.push(handle); | ||
29 | + promise._rejects.push(errback); | ||
30 | + } else if(promise._status === 'FULFILLED'){ | ||
31 | + handle(promise._value); | ||
32 | + } else if(promise._status === 'REJECTED') { | ||
33 | + errback(promise._reason); | ||
34 | + } | ||
35 | + }) | ||
36 | + | ||
37 | + }; | ||
38 | + | ||
39 | + | ||
40 | + function resolve(value) { | ||
41 | + setTimeout(function(){ | ||
42 | + promise._status = "FULFILLED"; | ||
43 | + promise._resolves.forEach(function (callback) { | ||
44 | + promise._value = callback(value); | ||
45 | + }) | ||
46 | + },0); | ||
47 | + } | ||
48 | + | ||
49 | + function reject(value) { | ||
50 | + setTimeout(function(){ | ||
51 | + promise._status = "REJECTED"; | ||
52 | + promise._rejects.forEach(function (callback) { | ||
53 | + promise._reason = callback(value); | ||
54 | + }) | ||
55 | + },0); | ||
56 | + } | ||
57 | + | ||
58 | + fn(resolve, reject); | ||
59 | +} | ||
60 | + | ||
61 | +Promise.all = function(promises){ | ||
62 | + if (!Array.isArray(promises)) { | ||
63 | + throw new TypeError('You must pass an array to all.'); | ||
64 | + } | ||
65 | + return new Promise(function(resolve,reject){ | ||
66 | + var i = 0, | ||
67 | + result = [], | ||
68 | + len = promises.length, | ||
69 | + count = len; | ||
70 | + | ||
71 | + function resolver(index) { | ||
72 | + return function(value) { | ||
73 | + resolveAll(index, value); | ||
74 | + }; | ||
75 | + } | ||
76 | + | ||
77 | + function rejecter(reason){ | ||
78 | + reject(reason); | ||
79 | + } | ||
80 | + | ||
81 | + function resolveAll(index,value){ | ||
82 | + result[index] = value; | ||
83 | + if( --count === 0){ | ||
84 | + resolve(result) | ||
85 | + } | ||
86 | + } | ||
87 | + | ||
88 | + for (; i < len; i++) { | ||
89 | + promises[i].then(resolver(i),rejecter); | ||
90 | + } | ||
91 | + }); | ||
92 | +}; | ||
93 | + | ||
94 | +Promise.race = function(promises){ | ||
95 | + if (!Array.isArray(promises)) { | ||
96 | + throw new TypeError('You must pass an array to race.'); | ||
97 | + } | ||
98 | + return new Promise(function(resolve,reject){ | ||
99 | + var i = 0, | ||
100 | + len = promises.length; | ||
101 | + | ||
102 | + function resolver(value) { | ||
103 | + resolve(value); | ||
104 | + } | ||
105 | + | ||
106 | + function rejecter(reason){ | ||
107 | + reject(reason); | ||
108 | + } | ||
109 | + | ||
110 | + for (; i < len; i++) { | ||
111 | + promises[i].then(resolver,rejecter); | ||
112 | + } | ||
113 | + }); | ||
114 | +}; | ||
115 | + | ||
116 | +module.exports = Promise; |
1 | -import webpack,{DefinePlugin} from 'webpack'; | ||
2 | -import path from 'path'; | ||
3 | -import CleanWebpackPlugin from 'clean-webpack-plugin'; | ||
4 | -const uglify = require('uglifyjs-webpack-plugin'); | 1 | +var path = require('path'); |
2 | +var CleanWebpackPlugin = require('clean-webpack-plugin'); | ||
5 | 3 | ||
6 | -let pathsToClean = [ | 4 | +var pathsToClean = [ |
7 | 'build' | 5 | 'build' |
8 | ]; | 6 | ]; |
9 | -let cleanOptions = { | 7 | +var cleanOptions = { |
10 | root: __dirname, | 8 | root: __dirname, |
11 | verbose: true, | 9 | verbose: true, |
12 | dry: false | 10 | dry: false |
13 | }; | 11 | }; |
14 | module.exports = { | 12 | module.exports = { |
15 | //项目入口js文件 | 13 | //项目入口js文件 |
14 | + mode: 'production', | ||
16 | entry: ['./src/index.js'], | 15 | entry: ['./src/index.js'], |
17 | //项目输出目录 | 16 | //项目输出目录 |
18 | output: { | 17 | output: { |
19 | path: path.resolve(__dirname, 'build'), | 18 | path: path.resolve(__dirname, 'build'), |
20 | filename: 'sdk.js', | 19 | filename: 'sdk.js', |
21 | libraryTarget: 'umd', | 20 | libraryTarget: 'umd', |
22 | - library: "yo_sdk", | ||
23 | - libraryExport: "default" | 21 | + library: "yo_sdk" |
24 | }, | 22 | }, |
25 | //插件 | 23 | //插件 |
26 | plugins: [ | 24 | plugins: [ |
27 | - new CleanWebpackPlugin(pathsToClean, cleanOptions),//清除历史版本 | ||
28 | - new uglify({test: /\.js($|\?)/i,parallel: false}) | 25 | + new CleanWebpackPlugin(pathsToClean, cleanOptions)//清除历史版本 |
29 | ], | 26 | ], |
30 | //加载器 | 27 | //加载器 |
31 | module: { | 28 | module: { |
@@ -41,7 +38,6 @@ module.exports = { | @@ -41,7 +38,6 @@ module.exports = { | ||
41 | } | 38 | } |
42 | ] | 39 | ] |
43 | }, | 40 | }, |
44 | - devtool: "inline-source-map", | ||
45 | //模块路径 | 41 | //模块路径 |
46 | resolve:{ | 42 | resolve:{ |
47 | alias: { | 43 | alias: { |
yarn.lock
0 → 100644
This diff could not be displayed because it is too large.
-
Please register or login to post a comment