640 lines
17 KiB
JavaScript
640 lines
17 KiB
JavaScript
window.yl = new Array();
|
||
window.yl.weixin = new Array();
|
||
function weixin_ready(data){
|
||
wx.config({
|
||
debug: false,
|
||
appId: data.appId,
|
||
timestamp: data.timestamp,
|
||
nonceStr: data.nonceStr,
|
||
signature: data.signature,
|
||
jsApiList: [
|
||
'checkJsApi',
|
||
'onMenuShareTimeline',
|
||
'onMenuShareAppMessage',
|
||
'onMenuShareQQ',
|
||
'onMenuShareWeibo',
|
||
'onMenuShareQZone',
|
||
'hideMenuItems',
|
||
'showMenuItems',
|
||
'hideAllNonBaseMenuItem',
|
||
'showAllNonBaseMenuItem',
|
||
'translateVoice',
|
||
'startRecord',
|
||
'stopRecord',
|
||
'onVoiceRecordEnd',
|
||
'playVoice',
|
||
'onVoicePlayEnd',
|
||
'pauseVoice',
|
||
'stopVoice',
|
||
'uploadVoice',
|
||
'downloadVoice',
|
||
'chooseImage',
|
||
'previewImage',
|
||
'uploadImage',
|
||
'downloadImage',
|
||
'getNetworkType',
|
||
'openLocation',
|
||
'getLocation',
|
||
'hideOptionMenu',
|
||
'showOptionMenu',
|
||
'closeWindow',
|
||
'scanQRCode',
|
||
'chooseWXPay',
|
||
'openProductSpecificView',
|
||
'addCard',
|
||
'chooseCard',
|
||
'openCard'
|
||
]
|
||
});
|
||
}
|
||
|
||
wx.ready(function () {
|
||
// 1 判断当前版本是否支持指定 JS 接口,支持批量判断
|
||
// p_jsApiList:需要判断的接口列表
|
||
function checkJsApi(p_jsApiList) {
|
||
wx.checkJsApi({
|
||
jsApiList: p_jsApiList,
|
||
success: function (res) {
|
||
return res;
|
||
}
|
||
});
|
||
};
|
||
window.yl.weixin.checkJsApi = checkJsApi;
|
||
|
||
// 2. 分享接口
|
||
// 2.1 监听“分享给朋友”,按钮点击、自定义分享内容及分享结果接口
|
||
function shareAppMessage(p_title,p_desc,p_link,p_imgurl,
|
||
trigger_callback,success_callback,cancel_callback,fail_callback) {
|
||
wx.onMenuShareAppMessage({
|
||
title: p_title,
|
||
desc: p_desc,
|
||
link: p_link,
|
||
imgUrl: p_imgurl,
|
||
trigger: function (res) {
|
||
// 不要尝试在trigger中使用ajax异步请求修改本次分享的内容,因为客户端分享操作是一个同步操作,这时候使用ajax的回包会还没有返回
|
||
trigger_callback(res);
|
||
},
|
||
success: function (res) {
|
||
success_callback(res);
|
||
},
|
||
cancel: function (res) {
|
||
cancel_callback(res);
|
||
},
|
||
fail: function (res) {
|
||
fail_callback(res);
|
||
}
|
||
});
|
||
};
|
||
window.yl.weixin.shareAppMessage = shareAppMessage;
|
||
|
||
// 2.2 监听“分享到朋友圈”按钮点击、自定义分享内容及分享结果接口
|
||
function shareTimeline(p_title,p_link,p_imgurl,
|
||
trigger_callback,success_callback,cancel_callback,fail_callback) {
|
||
wx.onMenuShareTimeline({
|
||
title: p_title,
|
||
link: p_link,
|
||
imgUrl: p_imgurl,
|
||
trigger: function (res) {
|
||
// 不要尝试在trigger中使用ajax异步请求修改本次分享的内容,因为客户端分享操作是一个同步操作,这时候使用ajax的回包会还没有返回
|
||
trigger_callback(res);
|
||
},
|
||
success: function (res) {
|
||
success_callback(res);
|
||
},
|
||
cancel: function (res) {
|
||
cancel_callback(res);
|
||
},
|
||
fail: function (res) {
|
||
fail_callback(res);
|
||
}
|
||
});
|
||
};
|
||
window.yl.weixin.shareTimeline = shareTimeline;
|
||
|
||
// 2.3 监听“分享到QQ”按钮点击、自定义分享内容及分享结果接口
|
||
function shareQQ(p_title,p_desc,p_link,p_imgurl,
|
||
trigger_callback,complete_callback,success_callback,cancel_callback,fail_callback) {
|
||
wx.onMenuShareQQ({
|
||
title: p_title,
|
||
desc: p_desc,
|
||
link: p_link,
|
||
imgUrl: p_imgurl,
|
||
trigger: function (res) {
|
||
trigger_callback(res);
|
||
},
|
||
complete: function (res) {
|
||
complete_callback(res);
|
||
},
|
||
success: function (res) {
|
||
success_callback(res);
|
||
},
|
||
cancel: function (res) {
|
||
cancel_callback(res);
|
||
},
|
||
fail: function (res) {
|
||
fail_callback(res);
|
||
}
|
||
});
|
||
};
|
||
window.yl.weixin.shareQQ = shareQQ;
|
||
|
||
// 2.4 监听“分享到微博”按钮点击、自定义分享内容及分享结果接口
|
||
function shareWeibo(p_title,p_desc,p_link,p_imgurl,
|
||
trigger_callback,complete_callback,success_callback,cancel_callback,fail_callback) {
|
||
wx.onMenuShareWeibo({
|
||
title: p_title,
|
||
desc: p_desc,
|
||
link: p_link,
|
||
imgUrl: p_imgurl,
|
||
trigger: function (res) {
|
||
trigger_callback(res);
|
||
},
|
||
complete: function (res) {
|
||
complete_callback(res);
|
||
},
|
||
success: function (res) {
|
||
success_callback(res);
|
||
},
|
||
cancel: function (res) {
|
||
cancel_callback(res);
|
||
},
|
||
fail: function (res) {
|
||
fail_callback(res);
|
||
}
|
||
});
|
||
};
|
||
window.yl.weixin.shareWeibo = shareWeibo;
|
||
|
||
// 2.5 监听“分享到QZone”按钮点击、自定义分享内容及分享接口
|
||
function shareQZone(p_title,p_desc,p_link,p_imgurl,
|
||
trigger_callback,complete_callback,success_callback,cancel_callback,fail_callback) {
|
||
wx.onMenuShareQZone({
|
||
title: p_title,
|
||
desc: p_desc,
|
||
link: p_link,
|
||
imgUrl: p_imgurl,
|
||
trigger: function (res) {
|
||
trigger_callback(res);
|
||
},
|
||
complete: function (res) {
|
||
complete_callback(res);
|
||
},
|
||
success: function (res) {
|
||
success_callback(res);
|
||
},
|
||
cancel: function (res) {
|
||
cancel_callback(res);
|
||
},
|
||
fail: function (res) {
|
||
fail_callback(res);
|
||
}
|
||
});
|
||
};
|
||
window.yl.weixin.shareQZone = shareQZone;
|
||
|
||
// 3 智能接口
|
||
//var voice = {
|
||
// localId: '',
|
||
// serverId: ''
|
||
//};
|
||
// 3.1 识别音频并返回识别结果
|
||
// voice_localId:录制的声音ID
|
||
function translateVoice(voice_localId) {
|
||
if (voice_localId == '') {
|
||
alert('请先使用 startRecord 接口录制一段声音');
|
||
return;
|
||
}
|
||
wx.translateVoice({
|
||
localId: voice_localId,
|
||
complete: function (res) {
|
||
if (res.hasOwnProperty('translateResult')) {
|
||
return res.translateResult;
|
||
// alert('识别结果:' + res.translateResult);
|
||
} else {
|
||
return false;
|
||
//alert('无法识别');
|
||
}
|
||
}
|
||
});
|
||
};
|
||
window.yl.weixin.translateVoice = translateVoice;
|
||
|
||
// 4 音频接口
|
||
// 4.2 开始录音
|
||
function startRecord() {
|
||
wx.startRecord({
|
||
cancel: function () {
|
||
alert('用户拒绝授权录音');
|
||
}
|
||
});
|
||
};
|
||
window.yl.weixin.startRecord = startRecord;
|
||
|
||
// 4.3 停止录音
|
||
function stopRecord() {
|
||
wx.stopRecord({
|
||
success: function (res) {
|
||
return res.localId;
|
||
},
|
||
fail: function (res) {
|
||
return res;
|
||
}
|
||
});
|
||
};
|
||
window.yl.weixin.stopRecord = stopRecord;
|
||
|
||
// 4.4 监听录音自动停止
|
||
wx.onVoiceRecordEnd({
|
||
complete: function (res) {
|
||
voice.localId = res.localId;
|
||
alert('录音时间已超过一分钟');
|
||
}
|
||
});
|
||
|
||
// 4.5 播放音频
|
||
// voice_localId:音频ID
|
||
function playVoice(voice_localId) {
|
||
if (voice_localId == '') {
|
||
alert('请先使用 startRecord 接口录制一段声音');
|
||
return;
|
||
}
|
||
wx.playVoice({
|
||
localId: voice_localId
|
||
});
|
||
};
|
||
window.yl.weixin.playVoice = playVoice;
|
||
|
||
// 4.6 暂停播放音频
|
||
// voice_localId:音频ID
|
||
function pauseVoice(voice_localId) {
|
||
wx.pauseVoice({
|
||
localId: voice_localId
|
||
});
|
||
};
|
||
window.yl.weixin.pauseVoice = pauseVoice;
|
||
|
||
// 4.7 停止播放音频
|
||
// voice_localId:音频ID
|
||
function stopVoice(voice_localId) {
|
||
wx.stopVoice({
|
||
localId: voice_localId
|
||
});
|
||
};
|
||
window.yl.weixin.stopVoice = stopVoice;
|
||
|
||
// 4.8 监听录音播放停止
|
||
wx.onVoicePlayEnd({
|
||
complete: function (res) {
|
||
alert('录音(' + res.localId + ')播放结束');
|
||
}
|
||
});
|
||
|
||
// 4.8 上传语音
|
||
// voice_localId:音频ID
|
||
function uploadVoice(voice_localId) {
|
||
if (voice_localId == '') {
|
||
alert('请先使用 startRecord 接口录制一段声音');
|
||
return;
|
||
}
|
||
wx.uploadVoice({
|
||
localId: voice_localId,
|
||
success: function (res) {
|
||
return res.serverId;
|
||
}
|
||
});
|
||
};
|
||
window.yl.weixin.uploadVoice = uploadVoice;
|
||
|
||
// 4.9 下载语音
|
||
// voice_serverId:上传的语音ID
|
||
function downloadVoice(voice_serverId) {
|
||
if (voice_serverId == '') {
|
||
alert('请先使用 uploadVoice 上传声音');
|
||
return;
|
||
}
|
||
wx.downloadVoice({
|
||
serverId: voice_serverId,
|
||
success: function (res) {
|
||
return res.localId;
|
||
}
|
||
});
|
||
};
|
||
window.yl.weixin.downloadVoice = downloadVoice;
|
||
|
||
// 5 图片接口
|
||
// 5.1 拍照、本地选图
|
||
//var images = {
|
||
// localId: [],
|
||
// serverId: []
|
||
//};
|
||
function chooseImage() {
|
||
wx.chooseImage({
|
||
success: function (res) {
|
||
return res.localIds;
|
||
//alert('已选择 ' + res.localIds.length + ' 张图片');
|
||
}
|
||
});
|
||
};
|
||
window.yl.weixin.chooseImage = chooseImage;
|
||
|
||
// 5.2 图片预览
|
||
// p_curr_img:当前显示的图片链接
|
||
// p_urls:需要预览的图片http链接列表(字符串数组格式)
|
||
function previewImage(p_curr_img,p_urls) {
|
||
wx.previewImage({
|
||
current: p_curr_img,
|
||
urls: p_urls
|
||
});
|
||
};
|
||
window.yl.weixin.previewImage = previewImage;
|
||
|
||
// 5.3 上传图片
|
||
// p_images_localId:需要上传的图片集合
|
||
function uploadImage(p_images_localId) {
|
||
if (p_images_localId.length == 0) {
|
||
alert('请先使用 chooseImage 接口选择图片');
|
||
return;
|
||
}
|
||
var i = 0, length = p_images_localId.length;
|
||
images_serverId = [];
|
||
function upload() {
|
||
wx.uploadImage({
|
||
localId: p_images_localId[i],
|
||
success: function (res) {
|
||
i++;
|
||
//alert('已上传:' + i + '/' + length);
|
||
images_serverId.push(res.serverId);
|
||
if (i < length) {
|
||
upload();
|
||
}
|
||
},
|
||
fail: function (res) {
|
||
return res;
|
||
}
|
||
});
|
||
}
|
||
upload();
|
||
};
|
||
window.yl.weixin.uploadImage = uploadImage;
|
||
|
||
// 5.4 下载图片
|
||
function downloadImage(images_serverId) {
|
||
if (images_serverId.length === 0) {
|
||
alert('请先使用 uploadImage 上传图片');
|
||
return;
|
||
}
|
||
var i = 0, length = images_serverId.length;
|
||
images_localId = [];
|
||
function download() {
|
||
wx.downloadImage({
|
||
serverId: images_serverId[i],
|
||
success: function (res) {
|
||
i++;
|
||
//alert('已下载:' + i + '/' + length);
|
||
images_localId.push(res.localId);
|
||
if (i < length) {
|
||
download();
|
||
}
|
||
}
|
||
});
|
||
}
|
||
download();
|
||
};
|
||
window.yl.weixin.downloadImage = downloadImage;
|
||
|
||
// 6 设备信息接口
|
||
// 6.1 获取当前网络状态
|
||
function getNetworkType() {
|
||
wx.getNetworkType({
|
||
success: function (res) {
|
||
return res.networkType;
|
||
},
|
||
fail: function (res) {
|
||
return res;
|
||
}
|
||
});
|
||
};
|
||
window.yl.weixin.getNetworkType = getNetworkType;
|
||
|
||
// 7 地理位置接口
|
||
// 7.1 查看地理位置
|
||
// p_lat:纬度,浮点数,范围为90 ~ -90
|
||
// p_lng:经度,浮点数,范围为180 ~ -180
|
||
// p_name:位置名
|
||
// p_address:地址详情说明
|
||
// p_scale:地图缩放级别,整形值,范围从1~28。默认为最大
|
||
// p_infoUrl:在查看位置界面底部显示的超链接,可点击跳转
|
||
function openLocation(p_lat,p_lng,p_name,p_address,p_scale,p_infoUrl) {
|
||
wx.openLocation({
|
||
latitude: p_lat,// 纬度,浮点数,范围为90 ~ -90
|
||
longitude: p_lng,// 经度,浮点数,范围为180 ~ -180
|
||
name: p_name,// 位置名
|
||
address: p_address,// 地址详情说明
|
||
scale: p_scale,// 地图缩放级别,整形值,范围从1~28。默认为最大
|
||
infoUrl: p_infoUrl // 在查看位置界面底部显示的超链接,可点击跳转
|
||
});
|
||
};
|
||
window.yl.weixin.openLocation = openLocation;
|
||
|
||
// 7.2 获取当前地理位置
|
||
function getLocation() {
|
||
wx.getLocation({
|
||
success: function (res) {
|
||
return res;
|
||
},
|
||
cancel: function (res) {
|
||
alert('用户拒绝授权获取地理位置');
|
||
}
|
||
});
|
||
};
|
||
window.yl.weixin.getLocation = getLocation;
|
||
|
||
// 8 界面操作接口
|
||
// 8.1 隐藏右上角菜单
|
||
function hideOptionMenu() {
|
||
wx.hideOptionMenu();
|
||
};
|
||
window.yl.weixin.hideOptionMenu = hideOptionMenu;
|
||
|
||
// 8.2 显示右上角菜单
|
||
function showOptionMenu() {
|
||
wx.showOptionMenu();
|
||
};
|
||
window.yl.weixin.showOptionMenu = showOptionMenu;
|
||
|
||
// 8.3 批量隐藏菜单项
|
||
function hideMenuItems() {
|
||
wx.hideMenuItems({
|
||
menuList: [
|
||
'menuItem:readMode', // 阅读模式
|
||
'menuItem:share:timeline', // 分享到朋友圈
|
||
'menuItem:copyUrl' // 复制链接
|
||
],
|
||
success: function (res) {
|
||
return true;
|
||
//alert('已隐藏“阅读模式”,“分享到朋友圈”,“复制链接”等按钮');
|
||
},
|
||
fail: function (res) {
|
||
return res;
|
||
}
|
||
});
|
||
};
|
||
window.yl.weixin.hideMenuItems = hideMenuItems;
|
||
|
||
// 8.4 批量显示菜单项
|
||
function showMenuItems() {
|
||
wx.showMenuItems({
|
||
menuList: [
|
||
'menuItem:readMode', // 阅读模式
|
||
'menuItem:share:timeline', // 分享到朋友圈
|
||
'menuItem:copyUrl' // 复制链接
|
||
],
|
||
success: function (res) {
|
||
//alert('已显示“阅读模式”,“分享到朋友圈”,“复制链接”等按钮');
|
||
return res;
|
||
},
|
||
fail: function (res) {
|
||
return res;
|
||
}
|
||
});
|
||
};
|
||
window.yl.weixin.showMenuItems = showMenuItems;
|
||
|
||
// 8.5 隐藏所有非基本菜单项
|
||
function hideAllNonBaseMenuItem() {
|
||
wx.hideAllNonBaseMenuItem({
|
||
success: function () {
|
||
alert('已隐藏所有非基本菜单项');
|
||
}
|
||
});
|
||
};
|
||
window.yl.weixin.hideAllNonBaseMenuItem = hideAllNonBaseMenuItem;
|
||
|
||
// 8.6 显示所有被隐藏的非基本菜单项
|
||
function showAllNonBaseMenuItem() {
|
||
wx.showAllNonBaseMenuItem({
|
||
success: function () {
|
||
alert('已显示所有非基本菜单项');
|
||
}
|
||
});
|
||
};
|
||
window.yl.weixin.showAllNonBaseMenuItem = showAllNonBaseMenuItem;
|
||
|
||
// 8.7 关闭当前窗口
|
||
function closeWindow() {
|
||
wx.closeWindow();
|
||
};
|
||
window.yl.weixin.closeWindow = closeWindow;
|
||
|
||
// 9 微信原生接口
|
||
// 9.1.1 扫描二维码并返回结果
|
||
function scanQRCode0() {
|
||
wx.scanQRCode();
|
||
};
|
||
window.yl.weixin.scanQRCode0 = scanQRCode0;
|
||
|
||
// 9.1.2 扫描二维码并返回结果
|
||
function scanQRCode1(p_desc) {
|
||
wx.scanQRCode({
|
||
needResult: 1,
|
||
desc: p_desc,
|
||
success: function (res) {
|
||
return res;
|
||
}
|
||
});
|
||
};
|
||
window.yl.weixin.scanQRCode1 = scanQRCode1;
|
||
|
||
// 10 微信支付接口
|
||
// 10.1 发起一个支付请求
|
||
function chooseWXPay(p_timestamp,p_nonceStr,p_package,p_signType,p_paySign) {
|
||
// 注意:此 Demo 使用 2.7 版本支付接口实现,建议使用此接口时参考微信支付相关最新文档。
|
||
wx.chooseWXPay({
|
||
timestamp: p_timestamp,
|
||
nonceStr: p_nonceStr,
|
||
package: p_package,
|
||
signType: p_signType, // 注意:新版支付接口使用 MD5 加密
|
||
paySign: p_paySign
|
||
});
|
||
};
|
||
window.yl.weixin.chooseWXPay = chooseWXPay;
|
||
|
||
// 11.3 跳转微信商品页
|
||
function openProductSpecificView(p_productId,p_extInfo) {
|
||
wx.openProductSpecificView({
|
||
productId: p_productId,
|
||
extInfo: p_extInfo
|
||
});
|
||
};
|
||
window.yl.weixin.openProductSpecificView = openProductSpecificView;
|
||
|
||
// 12 微信卡券接口
|
||
// 12.1 添加卡券
|
||
function addCard(p_cardList) {
|
||
wx.addCard({
|
||
cardList: p_cardList,
|
||
success: function (res) {
|
||
return res.cardList;
|
||
// alert('已添加卡券:' + JSON.stringify(res.cardList));
|
||
},
|
||
cancel: function (res) {
|
||
return res;
|
||
}
|
||
});
|
||
};
|
||
window.yl.weixin.addCard = addCard;
|
||
|
||
var codes = [];
|
||
// 12.2 选择卡券
|
||
document.querySelector('#chooseCard').onclick = function () {
|
||
wx.chooseCard({
|
||
cardSign: '6caa49f4a5af3d64ac247e1f563e5b5eb94619ad',
|
||
timestamp: 1437997723,
|
||
nonceStr: 'k0hGdSXKZEj3Min5',
|
||
success: function (res) {
|
||
res.cardList = JSON.parse(res.cardList);
|
||
encrypt_code = res.cardList[0]['encrypt_code'];
|
||
alert('已选择卡券:' + JSON.stringify(res.cardList));
|
||
decryptCode(encrypt_code, function (code) {
|
||
codes.push(code);
|
||
});
|
||
},
|
||
cancel: function (res) {
|
||
alert(JSON.stringify(res))
|
||
}
|
||
});
|
||
};
|
||
|
||
// 12.3 查看卡券
|
||
document.querySelector('#openCard').onclick = function () {
|
||
if (codes.length < 1) {
|
||
alert('请先使用 chooseCard 接口选择卡券。');
|
||
return false;
|
||
}
|
||
var cardList = [];
|
||
for (var i = 0; i < codes.length; i++) {
|
||
cardList.push({
|
||
cardId: 'pDF3iY9tv9zCGCj4jTXFOo1DxHdo',
|
||
code: codes[i]
|
||
});
|
||
}
|
||
wx.openCard({
|
||
cardList: cardList,
|
||
cancel: function (res) {
|
||
alert(JSON.stringify(res))
|
||
}
|
||
});
|
||
};
|
||
|
||
function decryptCode(code, callback) {
|
||
$.getJSON('/jssdk/decrypt_code.php?code=' + encodeURI(code), function (res) {
|
||
if (res.errcode == 0) {
|
||
codes.push(res.code);
|
||
}
|
||
});
|
||
}
|
||
});
|
||
|
||
wx.error(function (res) {
|
||
alert(res.errMsg);
|
||
}); |