|
@@ -0,0 +1,269 @@
|
|
|
|
+/**
|
|
|
|
+ * @author 何秀刚
|
|
|
|
+ * @description wechat 工具类,依赖JQuery,如果引入此文件,自动绑定分享事件。
|
|
|
|
+ * @version V.1.0
|
|
|
|
+ */
|
|
|
|
+// 获取绑定微信JSSDK的配置参数
|
|
|
|
+var getConfigUrl = path +"/watero/config/get";
|
|
|
|
+// JSSDK是否绑定成功
|
|
|
|
+var jssdkSuccess = true;
|
|
|
|
+//是否隐藏全局菜单
|
|
|
|
+if (window.hiddenAllWechatMenu == null) {
|
|
|
|
+ window.hiddenAllWechatMenu = false;
|
|
|
|
+}
|
|
|
|
+// 分享信息全局变量
|
|
|
|
+window.wxFriend = null;
|
|
|
|
+if (window.repleFlag) {
|
|
|
|
+ // 自定义
|
|
|
|
+ window.wxFriend = window.repleWxData;
|
|
|
|
+} else {
|
|
|
|
+ // 使用全局默认
|
|
|
|
+ window.wxFriend = {
|
|
|
|
+ "appId": "",
|
|
|
|
+ "imgUrl":'https://s.iamberry.com/watero/images/shop/index.jpg',
|
|
|
|
+ "link":"https://s.iamberry.com/watero/wechat/index.html",
|
|
|
|
+ "desc":"Watero水时代商城,开启优雅生活",
|
|
|
|
+ "title":"Watero水时代商城,好水,好茶,生活不将就!",
|
|
|
|
+ "netError":"您的网络异常,请刷新重试!如多次刷新无效,请向我们反应 ~",
|
|
|
|
+ "shareTimeSuccess":"这里是分享朋友圈后调用的文字",
|
|
|
|
+ "shareTimeCancel":"这里是取消分享给朋友圈后调用的文字",
|
|
|
|
+ "shareAppSuccess":"这里是分享好友后调用的文字",
|
|
|
|
+ "shareAppCancel":"这里是取消分享给好友后调用的文字"
|
|
|
|
+ };
|
|
|
|
+}
|
|
|
|
+// 微信共享地址数据
|
|
|
|
+window.wechatAddr = {
|
|
|
|
+ "status":false, // 是否调用成功,true成功,false失败
|
|
|
|
+ "statusMsg":"失败时信息",
|
|
|
|
+ "detailInfo":"详细地址",
|
|
|
|
+ "postalCode":"邮编",
|
|
|
|
+ "userName":"收货人",
|
|
|
|
+ "userTel":"收货人电话",
|
|
|
|
+ "nationalCode":"收货地址国家码"
|
|
|
|
+};
|
|
|
|
+/** 引入JSSDK1.0基本组件,微信共享地址智能使用1.0版本,并且需要服务器返回参数支持 */
|
|
|
|
+//incoldeJSFile("//res.wx.qq.com/open/js/jweixin-1.0.0.js");
|
|
|
|
+/** 引入JSSDK1.1基本组件 ,支持摇一摇、微信共享地址 */
|
|
|
|
+incoldeJSFile("//res.wx.qq.com/open/js/jweixin-1.1.0.js");
|
|
|
|
+getConfig(); // 配置JSSDK组件
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/** 获取服务器JSSDK参数配置 */
|
|
|
|
+function getConfig(){
|
|
|
|
+ $.ajax({
|
|
|
|
+ type: 'get', url: getConfigUrl, data: {appid: "", url: location.href,"date":new Date().getTime()}, dataType: 'json', timeout: 10000, context: $('body'),
|
|
|
|
+ success: function(data){setConfig(data);},
|
|
|
|
+ error: function(xhr, type){
|
|
|
|
+ getConfig();
|
|
|
|
+ mui.alert('网络异常,请刷新重试,否则某些功能不能使用!')
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**配置JS-SDK*/
|
|
|
|
+function setConfig(config){
|
|
|
|
+ wx.config({
|
|
|
|
+ debug: false,
|
|
|
|
+ appId: config.appId,
|
|
|
|
+ timestamp: config.timestamp,
|
|
|
|
+ nonceStr: config.nonceStr,
|
|
|
|
+ signature: config.signature,
|
|
|
|
+ jsApiList: [
|
|
|
|
+ 'checkJsApi',
|
|
|
|
+ 'onMenuShareTimeline',
|
|
|
|
+ 'onMenuShareAppMessage',
|
|
|
|
+ 'hideMenuItems',
|
|
|
|
+ 'showMenuItems'
|
|
|
|
+ ]
|
|
|
|
+ });
|
|
|
|
+ wx.ready(function(){ //JSSDK绑定成功
|
|
|
|
+ wxReadySuccess();
|
|
|
|
+ });
|
|
|
|
+ wx.error(function(res){ // JSSDK绑定失败的方法
|
|
|
|
+ wxReadyError();
|
|
|
|
+ });
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/** JSSDK绑定成功调用事件 */
|
|
|
|
+function wxReadySuccess() {
|
|
|
|
+ window.jssdkSuccess = true;
|
|
|
|
+ shareMessage();
|
|
|
|
+ wx.getNetworkType({success: function (res) {}});
|
|
|
|
+ if (window.hiddenAllWechatMenu) {
|
|
|
|
+ wx.showMenuItems({
|
|
|
|
+ menuList: [
|
|
|
|
+ ] // 要显示的菜单项,所有menu项见附录3
|
|
|
|
+ });
|
|
|
|
+ wx.hideMenuItems({
|
|
|
|
+ menuList: [
|
|
|
|
+ "menuItem:copyUrl",
|
|
|
|
+ "menuItem:openWithSafari",
|
|
|
|
+ "menuItem:originPage",
|
|
|
|
+ "menuItem:share:email",
|
|
|
|
+ "menuItem:dayMode",
|
|
|
|
+ "menuItem:setFont",
|
|
|
|
+ "menuItem:nightMode",
|
|
|
|
+ "menuItem:share:qq",
|
|
|
|
+ "menuItem:dayMode",
|
|
|
|
+ "menuItem:exposeArticle",
|
|
|
|
+ "menuItem:nightMode",
|
|
|
|
+ "menuItem:openWithQQBrowser",
|
|
|
|
+ "menuItem:share:QZone",
|
|
|
|
+ "menuItem:favorite",
|
|
|
|
+ "menuItem:share:facebook",
|
|
|
|
+ "menuItem:share:appMessage",
|
|
|
|
+ "menuItem:share:timeline"
|
|
|
|
+ ] // 要隐藏的菜单项,所有menu项见附录3
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ wx.showMenuItems({
|
|
|
|
+ menuList: [
|
|
|
|
+ "menuItem:share:appMessage",
|
|
|
|
+ "menuItem:share:timeline"
|
|
|
|
+ ] // 要显示的菜单项,所有menu项见附录3
|
|
|
|
+ });
|
|
|
|
+ wx.hideMenuItems({
|
|
|
|
+ menuList: [
|
|
|
|
+ "menuItem:copyUrl",
|
|
|
|
+ "menuItem:openWithSafari",
|
|
|
|
+ "menuItem:originPage",
|
|
|
|
+ "menuItem:share:email",
|
|
|
|
+ "menuItem:dayMode",
|
|
|
|
+ "menuItem:setFont",
|
|
|
|
+ "menuItem:nightMode",
|
|
|
|
+ "menuItem:share:qq",
|
|
|
|
+ "menuItem:dayMode",
|
|
|
|
+ "menuItem:exposeArticle",
|
|
|
|
+ "menuItem:nightMode",
|
|
|
|
+ "menuItem:openWithQQBrowser",
|
|
|
|
+ "menuItem:share:QZone",
|
|
|
|
+ "menuItem:favorite",
|
|
|
|
+ "menuItem:share:facebook"
|
|
|
|
+ ] // 要隐藏的菜单项,所有menu项见附录3
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/** JSSDK 绑定失败调用方法 */
|
|
|
|
+function wxReadyError() {
|
|
|
|
+ // alert(wxFriend.netError);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/** 分享时、分享后调用方法 */
|
|
|
|
+function shareMessage(title, message) {
|
|
|
|
+ title = title || wxFriend.title;
|
|
|
|
+ message = message || wxFriend.desc;
|
|
|
|
+ wx.onMenuShareTimeline({ //分享到朋友圈,只有标题、图标,没有内容简介
|
|
|
|
+ title: title, link: wxFriend.link, imgUrl: wxFriend.imgUrl,
|
|
|
|
+ success: function () {
|
|
|
|
+ if (wxFriend.shareAppSuccess != null && wxFriend.shareAppSuccess != "") {
|
|
|
|
+ // alert(wxFriend.shareAppSuccess);
|
|
|
|
+ }
|
|
|
|
+ },
|
|
|
|
+ cancel: function () {
|
|
|
|
+ if (wxFriend.shareTimeCancel != null && wxFriend.shareTimeCancel != "") {
|
|
|
|
+ // alert(wxFriend.shareTimeCancel);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ wx.onMenuShareAppMessage({ // 分享给好友,有标题、内容简介、图标
|
|
|
|
+ title: title, desc: message, link: wxFriend.link, imgUrl: wxFriend.imgUrl,
|
|
|
|
+ success: function () {
|
|
|
|
+ if (wxFriend.shareAppSuccess != null && wxFriend.shareAppSuccess != "") {
|
|
|
|
+ // alert(wxFriend.shareAppSuccess);
|
|
|
|
+ }
|
|
|
|
+ },
|
|
|
|
+ cancel: function () {
|
|
|
|
+ if (wxFriend.shareTimeCancel != null && wxFriend.shareTimeCancel != "") {
|
|
|
|
+ // alert(wxFriend.shareTimeCancel);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 微信支付
|
|
|
|
+ * str:微信支付参数,字符串
|
|
|
|
+ * paySuccessCallBack:微信支付成功后回调方法,此方法必须为全局时,方可以回调成功
|
|
|
|
+ * payCancelCallBack:微信支付失败后回调方法,此方法必须为全局时,方可以回调成功
|
|
|
|
+ */
|
|
|
|
+function wechatPay(str, paySuccessCallBack, payCancelCallBack) {
|
|
|
|
+ if (typeof WeixinJSBridge == "undefined"){
|
|
|
|
+ alert("非微信浏览器!无法发起微信支付请求!");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ var obj = JSON.parse("{" + str + "}");
|
|
|
|
+ WeixinJSBridge.invoke('getBrandWCPayRequest', obj, function(res) {
|
|
|
|
+ if(res.err_msg=='get_brand_wcpay_request:ok'){
|
|
|
|
+ paySuccessCallBack
|
|
|
|
+ }else{
|
|
|
|
+ payCancelCallBack
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+}
|
|
|
|
+if (typeof WeixinJSBridge == "undefined"){
|
|
|
|
+ if( document.addEventListener ){
|
|
|
|
+ document.addEventListener('WeixinJSBridgeReady', wechatPay, false);
|
|
|
|
+ }else if (document.attachEvent){
|
|
|
|
+ document.attachEvent('WeixinJSBridgeReady', wechatPay);
|
|
|
|
+ document.attachEvent('onWeixinJSBridgeReady', wechatPay);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+/** 发起微信共享地址Version 1.0 */
|
|
|
|
+function editAddrV0(config) {
|
|
|
|
+ WeixinJSBridge.invoke('editAddress', {"appId" : config.appId, "scope" : "jsapi_address", "signType" : "SHA1", "addrSign" : config.addrSign, "timeStamp" : config.timestamp, "nonceStr" : config.nonceStr},
|
|
|
|
+ function(res) {
|
|
|
|
+ if ("edit_address:ok" == res.err_msg) {
|
|
|
|
+ var address = res.proviceFirstStageName + "-" + res.addressCitySecondStageName + "-" + res.addressCountiesThirdStageName + "-" + res.addressDetailInfo;
|
|
|
|
+ wechatAddr.status = true;
|
|
|
|
+ wechatAddr.detailInfo = address;
|
|
|
|
+ wechatAddr.postalCode = res.addressPostalCode;
|
|
|
|
+ wechatAddr.userName = res.userName;
|
|
|
|
+ wechatAddr.userTel = res.telNumber;
|
|
|
|
+ return wechatAddr;
|
|
|
|
+ } else if ("edit_address:fail" == res.err_msg) {// 没有选择
|
|
|
|
+ wechatAddr.statusMsg = "您没有选择收货地址!";
|
|
|
|
+ return wechatAddr;
|
|
|
|
+ } else { // 参数过时,请刷新
|
|
|
|
+ wechatAddr.statusMsg = "操作过时,请刷新重试!";
|
|
|
|
+ return wechatAddr;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ );
|
|
|
|
+}
|
|
|
|
+/**
|
|
|
|
+ * 发起微信共享地址Version 1.1<br>
|
|
|
|
+ * {<br>
|
|
|
|
+ * status : 状态,true:请求成功,false:请求失败<br>
|
|
|
|
+ * errorInfo : 请求失败时的提示信息<br>
|
|
|
|
+ * res: { 请求成功时的地址信息,失败为空<br>
|
|
|
|
+ * userName : 收货人姓名。<br>
|
|
|
|
+ * postalCode : 邮编。<br>
|
|
|
|
+ * provinceName 国标收货地址第一级地址(省)。<br>
|
|
|
|
+ * cityName : 国标收货地址第二级地址(市)。<br>
|
|
|
|
+ * countryName : 国标收货地址第三级地址(区)。<br>
|
|
|
|
+ * detailInfo : 详细收货地址信息。<br>
|
|
|
|
+ * nationalCode : 收货地址国家码。<br>
|
|
|
|
+ * telNumber : 收货人电话<br>
|
|
|
|
+ * }
|
|
|
|
+ * }
|
|
|
|
+ */
|
|
|
|
+function editAddrV1(successCallback, errorCallback) {
|
|
|
|
+ if (!window.jssdkSuccess) {
|
|
|
|
+ mui.alert("错误啦 ~~ 请刷新重试!");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ wx.openAddress({
|
|
|
|
+ success: successCallback,
|
|
|
|
+ cancel: errorCallback
|
|
|
|
+ });
|
|
|
|
+}
|
|
|
|
+/** 动态引入wechat utils JavaScript */
|
|
|
|
+function incoldeJSFile(jsUrl) {
|
|
|
|
+ var typeHand = document.getElementsByTagName('head').item(0);
|
|
|
|
+ var typeScript= document.createElement("script");
|
|
|
|
+ typeScript.type = "text/javascript";
|
|
|
|
+ typeScript.src=jsUrl;
|
|
|
|
+ typeHand.appendChild(typeScript);
|
|
|
|
+}
|