-
-
Save LeiZeng/a59ec5b28a273fda0973 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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