Created
March 30, 2016 12:43
-
-
Save ryerh/a5eb946b706d9b492c13499b182dda90 to your computer and use it in GitHub Desktop.
wrap wechat js-sdk
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
// 微信模块 | |
// 开发文档见 http://mp.weixin.qq.com/wiki/ | |
// 执行流程: | |
// 1. 加载微信脚本(jweixin-1.0.0.js) | |
// 2. 获取配置信息(调 config jsonp 接口) | |
// 4. 执行 jWeixin.config | |
//import asyncLoad from './_async-load'; | |
//import config from './_config'; | |
import el from './_el'; | |
import loader from './_loader'; | |
import Logger from './_logger'; | |
import request from './_request'; | |
const logger = new Logger('微信'); | |
const wechat = { | |
// 获取 JSSDK config 信息的 JSONP 请求地址 | |
// 这个服务是 XP 写的,具体实现看微信开发者文档 JS Ticket 的内容 | |
configUrl: 'https://hongbao.jinbishangcheng.com/weixin/config', | |
// 所有的微信 JSSDK Api | |
jsApiList: [ | |
'onMenuShareTimeline', | |
'onMenuShareAppMessage', | |
'onMenuShareQQ', | |
'onMenuShareWeibo', | |
'onMenuShareQZone', | |
'startRecord', | |
'stopRecord', | |
'onVoiceRecordEnd', | |
'playVoice', | |
'pauseVoice', | |
'stopVoice', | |
'onVoicePlayEnd', | |
'uploadVoice', | |
'downloadVoice', | |
'chooseImage', | |
'previewImage', | |
'uploadImage', | |
'downloadImage', | |
'translateVoice', | |
'getNetworkType', | |
'openLocation', | |
'getLocation', | |
'hideOptionMenu', | |
'showOptionMenu', | |
'hideMenuItems', | |
'showMenuItems', | |
'hideAllNonBaseMenuItem', | |
'showAllNonBaseMenuItem', | |
'closeWindow', | |
'scanQRCode', | |
'chooseWXPay', | |
'openProductSpecificView', | |
'addCard', | |
'chooseCard', | |
'openCard' | |
], | |
// 初始化微信模块 | |
// @returns {Promise} - 这个返回值可以忽略 | |
init () { | |
logger.log('1. 获取配置信息'); | |
return this.getConfig() | |
.then(res => { | |
logger.log('2. 执行微信设置'); | |
return this.setConfig(JSON.parse(res)); | |
}) | |
.then(res => { | |
logger.log('3. 屏蔽微信按钮'); | |
// return checkApiList(); | |
return Promise.resolve(this.hideMenu()); | |
}) | |
.then(res => logger.log('4. 微信初始化成功')) | |
.catch(err => logger.error('4. 微信初始化失败')); | |
}, | |
// 获取 appid 信息 | |
// @returns {Promise} | |
getConfig () { | |
return request({ | |
url: this.configUrl, | |
dataType: 'jsonp', | |
jsonpCallback: 'config', | |
data: { | |
// url 不能包含 # 及以后的内容 | |
url: location.href.split('#')[0] | |
}, | |
expect: res => !!res | |
}); | |
}, | |
// 执行微信设置 | |
// @param {JSON} config - 包含 appid 等信息的配置 | |
// @returns {Promise} | |
setConfig (config) { | |
return new Promise((resolve, reject) => { | |
jWeixin.ready(resolve); | |
jWeixin.error(reject); | |
jWeixin.config({ | |
debug: false, | |
appId: config.appid, | |
timestamp: config.timestamp, | |
nonceStr: config.noncestr, | |
signature: config.signature, | |
jsApiList: this.jsApiList | |
}); | |
}); | |
}, | |
// 检查 Api 列表权限 | |
// 调用此函数会导致微信 Web 开发者工具报错,已向微信提交 bug,暂无反馈 | |
// @returns {Promise} | |
checkApiList () { | |
return new Promise((resolve, reject) => { | |
jWeixin.checkJsApi({ | |
jsApiList: this.jsApiList, | |
success: resolve, | |
fail: reject | |
}); | |
}); | |
}, | |
// 分享到朋友圈 | |
// @param {JSON} config - 分享内容: { title, link, imgUrl } | |
shareTimeLine (config) { | |
return new Promise((resolve, reject) => { | |
const merge = Object.assign({}, config, { | |
success: resolve, | |
cancel: reject | |
}); | |
jWeixin.onMenuShareTimeline(merge); | |
}); | |
}, | |
// 分享给微信好友 | |
// @param {JSON} config - 分享内容: { title, link, imgUrl } | |
shareFriend (config) { | |
return new Promise((resolve, reject) => { | |
const merge = Object.assign({}, config, { | |
success: resolve, | |
cancel: reject | |
}); | |
jWeixin.onMenuShareAppMessage(merge); | |
}); | |
}, | |
hideMenu: jWeixin.hideOptionMenu.bind(jWeixin), | |
showMenu: jWeixin.showOptionMenu.bind(jWeixin), | |
hideMenuItems: jWeixin.hideMenuItems.bind(jWeixin), | |
showMenuItems: jWeixin.showMenuItems.bind(jWeixin) | |
}; | |
export default wechat; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment