1\导入核心文件
http://res.wx.qq.com/open/js/jweixin-1.0.0.js
调用方式
<script type="text/javascript">
$.getScript('http://res.wx.qq.com/open/js/jweixin-1.0.0.js', function(){
$.getScript('http://m.to8to.com/js/wx.js?20150401', function(){
/*********** 微信分享 ************/
try {
var shareConfig = {
title: '【号外】七夕赶上了七周年,土巴兔怒砸百万,还不快约!',
desc: '老板疯了,土巴兔APP七夕派大奖,敢来就敢送,就怕你不来~',
link: 'http://mobileapi.to8to.com/index.php?module=lottery&action=index&version=2.5',// 分享链接
imgUrl: 'http://img.to8to.com/front_end/pic/app_share_weixin_love.jpg' // 分享图标
};
to8toWx && to8toWx.init({
onReady: function() {
// 分享到朋友圈
to8toWx.exec('onMenuShareTimeline', shareConfig);
// 发送给朋友
to8toWx.exec('onMenuShareAppMessage', shareConfig);
// 分享到QQ
to8toWx.exec('onMenuShareQQ', shareConfig);
}
});
} catch (e) {}
/*********** 微信分享 ************/
});
});
</script>
////////////////////wx.js/////////////////////
/**
* Created by carl.wu on 15-2-7.
*/
var to8toWx = to8toWx || {};
// 微信API 列表
to8toWx.apiList = [
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo',
'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'
];
// 异步获取签名参数url
to8toWx.ajaxUrl = 'http://m.to8to.com/hfive/getsignstr/';
/**
* 初始化微信JS
* @param opts
* @returns {to8toWx}
*/
to8toWx.init = function(opts) {
var _this = this;
opts = $.extend({}, opts);
// 获取签名串
getSignature(function(configs) {
// 初始化微信配置
var wxConfig = {
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: configs.appId, // 必填,公众号的唯一标识
timestamp: configs.timestamp, // 必填,生成签名的时间戳
nonceStr: configs.nonceStr, // 必填,生成签名的随机串
signature: configs.signature,// 必填,签名,见附录1
jsApiList: to8toWx.apiList // 必填,需要使用的JS接口列表
};
// 设置微信配置
wx.config(wxConfig);
// 微信初始化callback
wx.ready(function(){
if (typeof opts.onReady === 'function') {
opts.onReady.call(_this);
}
});
});
return this;
}
/**
* 执行WX api列表中的方法
* 有些方法必须在wx.ready中执行,有些不受限制
*
* @param apiName
* @param opt
* @returns {to8toWx}
*/
to8toWx.exec = function(apiName, opt) {
try {
wx[apiName](opt);
} catch (e) {
throw new Error('方法'+apiName+'不存在!');
}
return this;
}
/**
* 获取签名串
* 支持同步和异步
* @param callback
*/
function getSignature(callback) {
if (typeof to8toWxsignPackage !== 'undefined') {
if (typeof callback === 'function') {
callback(to8toWxsignPackage);
} else {
return to8toWxsignPackage;
}
} else {
// 修改为jsonp方式获取数据
$.ajax({
url: to8toWx.ajaxUrl,
'type': 'GET',
'dataType': 'jsonp',
'data': {"url": encodeURIComponent(location.href.split('#')[0])},
'success': function(configs) {
if (typeof callback === 'function') {
callback(configs);
}
}
});
//$.post(to8toWx.ajaxUrl, {
// url: encodeURIComponent(location.href.split('#')[0])
//},function(configs) {
// if (typeof callback === 'function') {
// callback(configs);
// }
//}, 'json');
}
}