Skip to content

Instantly share code, notes, and snippets.

@LeiZeng
Forked from morlay/WechatJSSDKWrapper.js
Last active February 29, 2016 08:52
Show Gist options
  • Save LeiZeng/a59ec5b28a273fda0973 to your computer and use it in GitHub Desktop.
Save LeiZeng/a59ec5b28a273fda0973 to your computer and use it in GitHub Desktop.
import _ from 'lodash';
const wechatScript = '//res.wx.qq.com/open/js/jweixin-1.1.0.js';
export const loadSdk = () =>
new Promise((resolve) => {
require('scriptjs')(wechatScript, () => {
resolve(global.wx);
});
});
const setConfig = _.curry(({ appId, timestamp, nonceStr, signature }, wx) =>
new Promise((resolve, reject) => {
let isError = false;
wx.config({
appId,
timestamp,
nonceStr,
signature,
jsApiList: Object.keys(wx)
});
wx.error((err) => {
isError = true;
reject(err);
});
wx.ready(() =>
// When has error the ready callback will be called too,
// Tiny timeout to ignore error callback;
setTimeout(() => {
if (!isError) {
resolve(wx);
console.log('wechat config is ready');
}
}, 0.02);
);
})
);
const shareRegister = _.curry(({ title, desc, imgUrl, link, onShareSuccess, onShareCancel }, wx) =>
Promise.resolve([
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo',
'onMenuShareQZone'
].map((eventName) => wx[eventName]({
title: eventName === 'onMenuShareTimeline' ? desc : title,
desc,
imgUrl,
link,
success: onShareSuccess,
cancel: onShareCancel
})))
);
export default _.curry(({ appId, timestamp, nonceStr, signature }, registerOptions) => {
const shareRegister = loadSdk()
.then(setConfig({ appId, timestamp, nonceStr, signature }))
.then(shareRegister(registerOptions))
return { shareRegister }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment