Home  >  Article  >  WeChat Applet  >  Summary of js methods commonly used in WeChat development

Summary of js methods commonly used in WeChat development

高洛峰
高洛峰Original
2017-03-15 17:26:101713browse

This article summarizes for you some js methods that are often used in our daily development of WeChat projects. They are all used very frequently and are very Simple method, recommended to everyone here.

1. WebpagePicturesSwipe left and right to view the pictures, as shown below:

js effect

The code is as follows:

var pictures = [];
 angular.forEach(pitctures,function(k,i){
         pictures[i] = k.imgPath;
 });
 $scope.previewPics = function(currentUrl){
        if (typeof window.WeixinJSBridge != 'undefined') {
            //微信图片集查看
            WeixinJSBridge.invoke('imagePreview', {
                'current':currentUrl, //当前地址
                'urls':pictures //组
            });
        } else {
            alert( "请在微信中查看", null, function () {});
        }
    }

Page elements:

The code is as follows:

  <p class="infoPics">
           <p class="picImg" ng-repeat="picture in info.infoContent.pitctures">
                <img ng-src="{{picture.imgPath}}" ng-click="previewPics(picture.imgPath)">
           </p>
     </p>

2. WeChat window closing event , The example is as follows:

The code is as follows:

 WeixinJSBridge.invoke(&#39;closeWindow&#39;,{},function(res){
       //alert(res.err_msg);
 });

3. Share the web link to friends, Moments, and Weibo

The code is as follows:

var lineLink = &#39;http://../..&#39;,
  imgUrl = &#39;http://../..&#39;,
  shareTitle = &#39;页面标题&#39;,
  descContent=&#39;内容简介&#39;,
  appid = &#39;&#39;;
//判断是否支持微信js
 if(typeof WeixinJsBridge == &#39;undefined&#39;){
  if(document.addEventListener){
    document.addEventListener(&#39;WeixinJsBridgeReady&#39;,onBridgeReady,false);
  }else if(document.attachEvent){
    document.attachEvent(&#39;WeixinJsBridgeReady&#39;,onBridgeReady);
    document.attachEvent(&#39;onWeixinJsBridgeReady&#39;,onBridgeReady);
  }
 }else{
  onBridgeReady();
 }
function onBridgeReady (){
  WeixinJsBridgeReady.on(&#39;menu:share:appmessage&#39;,wx_shareFriend);//分享朋友
  WeixinJsBridgeReady.on(&#39;menu:share:timeline&#39;,wx_shareTimeline);//分享到朋友圈
  WeixinJsBridgeReady.on(&#39;menu:share:weibo&#39;,wx_shareWeibo);//分享朋友
}
function wx_shareFriend (){
  WeixinJsBridge.invoke(&#39;sendAppMessage&#39;,{
    "appid":appid,
    "img_url":imgurl,
    "img_width":&#39;640&#39;,
    "img_height":&#39;500&#39;,
    "link":lineLink,
    "desc":descContent,
    "title":shareTitle
    },function(res){
      console.log(res.err_msg);
    }
  });
}
function wx_shareTimeline (){
  WeixinJsBridge.invoke(&#39;sendTimeline&#39;,{
    "appid":appid,
    "img_url":imgurl,
    "img_width":&#39;640&#39;,
    "img_height":&#39;500&#39;,
    "link":lineLink,
    "desc":descContent,
    "title":shareTitle
    },function(res){
      console.log(res.err_msg);
    }
  })
}  
function wx_shareWeibo (){
  WeixinJsBridge.invoke(&#39;sendWeibo&#39;,{
    "appid":appid,
    "img_url":imgurl,
    "img_width":&#39;640&#39;,
    "img_height":&#39;500&#39;,
    "link":lineLink,
    "desc":descContent,
    "title":shareTitle
    },function(res){
      console.log(res.err_msg);
    }
  })
}

4. Hide the button in the upper right corner of the web page

The code is as follows:

 WeixinJsBridge.call(&#39;hideOptionMenu&#39;); 

5. Hide the bottom of the web page NavigationBar

The code is as follows:

 WeixinJsBridge.call(&#39;hideToolbar&#39;);

6. Get the current network connection type:

The code is as follows:

 WeixinJsBridge.invoke(&#39;getNetworkType&#39;,{},function(e){
    console.log(e.err_msg);
 })

7. Prohibit users from sharing

The code is as follows:

  WeixinJsBridge.invoke(&#39;disabledShare&#39;,{},function(e){
  })

8. Determine whether it is built-in in WeChat Open

in the browser and the code is as follows:

  // true or false
  var flag = WeixinApi.openInWeixin();

The above 8 items are the content shared with you in this article. I hope it can be helpful to everyone’s WeChat development.

The above is the detailed content of Summary of js methods commonly used in WeChat development. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn