ionic 微信分享

来源:互联网 发布:数据挖掘的特点 编辑:程序博客网 时间:2024/05/21 08:46

先添加cordova插件:

$ cordova plugin add cordova-plugin-wechat --variable wechatappid=YOUR_WECHAT_APPID

controllers.js

app.controller('DashCtrl',  function($scope, $ionicActionSheet, $ionicPopup) {  document.addEventListener("deviceready", onDeviceReady, false);  function onDeviceReady() {    console.log(Wechat);    Wechat.isInstalled(function (installed) {        alert("Wechat installed: " + (installed ? "Yes" : "No"));    }, function (reason) {        alert("Failed: " + reason);    });    var scope = "snsapi_userinfo",        state = "_" + (+new Date());    Wechat.auth(scope, state, function (response) {        // you may use response.code to get the access token.        alert(JSON.stringify(response));    }, function (reason) {        alert("Failed: " + reason);    });    // Add similar listeners for other events  }////只是个小DEMO所以就直接登录微信了,如果需要单独登录稍微改一下就可以了  $scope.share = function(title, desc, url, thumb) {    $ionicActionSheet.show({      buttons: [        { text:'分享给微信好友' },        { text:'分享至微信朋友圈' }      ],      titleText: '分享',      cancelText:'取消',      cancel: function() {      // 取消时执行                      },      buttonClicked: function(index) {        console.log(index);        if(index == 0) {          $scope.shareViaWechat(0, title, desc, url, thumb);        }        if(index == 1) {          $scope.shareViaWechat(1, title, desc, url, thumb);        }      }    });   };  $scope.shareViaWechat = function(scene, title, desc, url, thumb) {    //scene:1 表示分享到朋友圈    //scene:0 表示分享给好友    console.log(scene);    var msg = {      title: "Hi, there",      description: "This is description.",      thumb: "www/img/pyq.png",      mediaTagName: "TEST-TAG-001",      messageExt: "这是第三方带的测试字段",      messageAction: "<action>dotalist</action>",      //media: "YOUR_MEDIA_OBJECT_HERE",      //网页链接      media: {          type: Wechat.Type.WEBPAGE,          webpageUrl: "http://tech.qq.com/zt2012/tmtdecode/252.htm"      }    }    //分享文字      // Wechat.share({      //     text: "This is just a plain string",      //     scene: Wechat.Scene.TIMELINE   // share to Timeline      // }, function () {      //     alert("Success");      // }, function (reason) {      //     alert("Failed: " + reason);      // });  //分享链接,设置好标题,描述,图片,链接 参数就OK。    Wechat.share({        message: msg,        scene: scene   // share to Timeline    }, function () {        alert("Success");    }, function (reason) {        alert("Failed: " + reason);    });  };})

html

<ion-icon class="button button-assertive button-outlinebutton-block" ng-click="share()">分享</ion-icon>