Skip to content

Commit

Permalink
v0.0.3
Browse files Browse the repository at this point in the history
add functions:
setReadyCallBack(),
setDefaultShare(),
backToDefault()
  • Loading branch information
cycjimmy committed Feb 7, 2018
1 parent 57ecf43 commit 0147f6a
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 8 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,25 @@ let WxShare = require('weixin-share');

```javascript
new WxShare()
.config([WechatJSSDKConfig])
.config([wechatJSSDKConfig])
.setReadyCallBack([wechatConfigReadyCallBack])
.setDefaultShare([defaultShare])
.share([shareConfig]);
```

* `WechatJSSDKConfig`: [object] Wechat JS-SDK Config
* `shareConfig`: [object] Share Config
* Function:
* `config()`: Set Wechat JS-SDK Config.
* `setReadyCallBack()`: Set CallBack function on Wechat Config Ready.
* `setDefaultShare()`: Set Default Share Config.
* `share()`: Run Main Task of Share.
* `backToDefault()`: Back To Default Share Config.

* Params:
* `wechatJSSDKConfig`: [Object] Wechat JS-SDK Config.
* `wechatConfigReadyCallBack`: [Function] CallBack function on Wechat Config Ready.
* `defaultShare`: [Object] Default Share Config.
* `shareConfig`: [Object] Share Config.

* [Wechat Official Wiki](https://mp.weixin.qq.com/wiki)

### Use in browser
Expand Down
102 changes: 101 additions & 1 deletion build/WxShare.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,16 @@ return /******/ (function(modules) { // webpackBootstrap
"use strict";
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_awesome_js_funcs_designPattern_CreateInstance__ = __webpack_require__(1);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1_assign_polyfill__ = __webpack_require__(2);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1_assign_polyfill___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_1_assign_polyfill__);
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

// constructor


// polyfill


var _instance = new __WEBPACK_IMPORTED_MODULE_0_awesome_js_funcs_designPattern_CreateInstance__["a" /* default */]();

var WX_JSSDK_URL = 'https://res.wx.qq.com/open/js/jweixin-1.2.0.js';
Expand All @@ -99,6 +104,15 @@ var WxShare = function () {
this.isConfigReady = false;
this.wx = null;
this.wxConfig = null;
this.readyCallBack = null;

this.defaultShare = {
title: '',
desc: '',
link: window.location.href.replace(/(\?|#).*/g, ''),
imgUrl: ''
};
this._isInitDefaultShare = false;

_instance(this);
}
Expand Down Expand Up @@ -136,6 +150,31 @@ var WxShare = function () {
return this;
};

/**
* setReadyCallBack
* @param readyCallBack
* @return {WxShare}
*/
WxShare.prototype.setReadyCallBack = function setReadyCallBack() {
var readyCallBack = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};

this.readyCallBack = readyCallBack;
return this;
};

/**
* setDefaultShare
* @param defaultShare
* @return {WxShare}
*/
WxShare.prototype.setDefaultShare = function setDefaultShare() {
var defaultShare = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

this.defaultShare = Object.assign({}, this.defaultShare, defaultShare);
this._isInitDefaultShare = true;
return this;
};

/**
* share
* @param shareData
Expand All @@ -152,9 +191,19 @@ var WxShare = function () {
* fail:() => {},
* }
*/
WxShare.prototype.share = function share(shareData) {
WxShare.prototype.share = function share() {
var _this = this;

var shareData = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

if (!this._isInitDefaultShare) {
this.setDefaultShare(shareData);
} else {
shareData = Object.assign({}, this.defaultShare, shareData);
}

console.log(shareData);

return Promise.resolve().then(function () {
return _this._initWxSDK();
}).then(function () {
Expand All @@ -168,6 +217,14 @@ var WxShare = function () {
});
};

/**
* backToDefault
* @return {*}
*/
WxShare.prototype.backToDefault = function backToDefault() {
return this.share();
};

/**
* init Wechat JSSDK
* @return {Promise<any>}
Expand Down Expand Up @@ -220,6 +277,11 @@ var WxShare = function () {

_this3.wx.ready(function () {
_this3.isConfigReady = true;

if (_this3.readyCallBack) {
_this3.readyCallBack();
}

resolve();
});
}
Expand Down Expand Up @@ -252,6 +314,44 @@ var WxShare = function () {
};
});

/***/ }),
/* 2 */
/***/ (function(module, exports) {

if (typeof Object.assign != 'function') {
// Must be writable: true, enumerable: false, configurable: true
Object.defineProperty(Object, "assign", {
value: function assign(target, varArgs) {
// .length of function is 2
'use strict';

if (target == null) {
// TypeError if undefined or null
throw new TypeError('Cannot convert undefined or null to object');
}

var to = Object(target);

for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];

if (nextSource != null) {
// Skip over if undefined or null
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
},
writable: true,
configurable: true
});
}

/***/ })
/******/ ])["default"];
});
2 changes: 1 addition & 1 deletion build/WxShare.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "weixin-share",
"version": "0.0.2",
"version": "0.0.3",
"description": "Easier way to call Wechat share on web page.",
"main": "build/WxShare.js",
"scripts": {
Expand Down
29 changes: 29 additions & 0 deletions src/assign.polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
if (typeof Object.assign != 'function') {
// Must be writable: true, enumerable: false, configurable: true
Object.defineProperty(Object, "assign", {
value: function assign(target, varArgs) { // .length of function is 2
'use strict';
if (target == null) { // TypeError if undefined or null
throw new TypeError('Cannot convert undefined or null to object');
}

var to = Object(target);

for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];

if (nextSource != null) { // Skip over if undefined or null
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
},
writable: true,
configurable: true
});
}
58 changes: 56 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// constructor
import CreateInstance from 'awesome-js-funcs/designPattern/CreateInstance';

// polyfill
import 'assign.polyfill';

let
_instance = new CreateInstance()
;
Expand All @@ -15,6 +18,15 @@ export default class WxShare {
this.isConfigReady = false;
this.wx = null;
this.wxConfig = null;
this.readyCallBack = null;

this.defaultShare = {
title: '',
desc: '',
link: window.location.href.replace(/(\?|#).*/g, ''),
imgUrl: '',
};
this._isInitDefaultShare = false;

_instance(this);
};
Expand Down Expand Up @@ -56,6 +68,28 @@ export default class WxShare {
return this;
};

/**
* setReadyCallBack
* @param readyCallBack
* @return {WxShare}
*/
setReadyCallBack(readyCallBack = () => {
}) {
this.readyCallBack = readyCallBack;
return this;
};

/**
* setDefaultShare
* @param defaultShare
* @return {WxShare}
*/
setDefaultShare(defaultShare = {}) {
this.defaultShare = Object.assign({}, this.defaultShare, defaultShare);
this._isInitDefaultShare = true;
return this;
};

/**
* share
* @param shareData
Expand All @@ -72,7 +106,15 @@ export default class WxShare {
* fail:() => {},
* }
*/
share(shareData) {
share(shareData = {}) {
if(!this._isInitDefaultShare) {
this.setDefaultShare(shareData);
} else {
shareData = Object.assign({}, this.defaultShare, shareData);
}

console.log(shareData);

return Promise.resolve()
.then(() => this._initWxSDK())
.then(() => this._ready())
Expand All @@ -85,6 +127,14 @@ export default class WxShare {
});
};

/**
* backToDefault
* @return {*}
*/
backToDefault() {
return this.share();
};

/**
* init Wechat JSSDK
* @return {Promise<any>}
Expand Down Expand Up @@ -133,6 +183,11 @@ export default class WxShare {

this.wx.ready(() => {
this.isConfigReady = true;

if (this.readyCallBack) {
this.readyCallBack();
}

resolve();
});
}
Expand All @@ -141,4 +196,3 @@ export default class WxShare {
};



0 comments on commit 0147f6a

Please sign in to comment.