barbabravo / blog

3 stars 1 forks source link

cordova分享到微信的三种方法 #67

Open barbabravo opened 6 years ago

barbabravo commented 6 years ago
plugin: 
    <plugin name="cordova-plugin-x-socialsharing" spec="^5.3.2" />
code:
navigator.share({
          title: "Optional title",
          text: "Optional message",
          url: "http://www.myurl.com"
        })
        .then(function() {
          alert("Successful share");
        })
        .catch(function(error) {
          alert("Error sharing:", error);
        });
plugin: 
    <plugin name="cordova-plugin-x-socialsharing" spec="^5.3.2" />
code:
let options = {
        message: artical.title,
        subject: artical.title,
        files: [""],
        url: artical.nid,
        chooserTitle: "分享" // Android only, you can override the default share sheet title
      };

      var onSuccess = function(result) {
        alert("Share completed? " + result.completed);
        alert("Shared to app: " + result.app);
      };

      var onError = function(msg) {
        alert("分享失败,请联系客服")
      };

      try {
        window.plugins.socialsharing.available(isAvailable => {
        if (isAvailable) {
          window.plugins.socialsharing.shareWithOptions(
            options,
            onSuccess,
            onError
          );
        }
        })
      } catch (err) {
        alert("分享失败,请联系客服")
      }
plugin: 
    <plugin name="cordova-plugin-wechat" spec="^2.1.0">
        <variable name="WECHATAPPID" value="wx12313123123" />
    </plugin>
code:
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);
        }
      );

      Wechat.share(
        {
          message: {
            media: {
              type: Wechat.Type.WEBPAGE,
              webpageUrl: "http://tech.qq.com/zt2012/tmtdecode/252.htm"
            }
          },
          scene: Wechat.Scene.TIMELINE // share to Timeline
        },
        function() {
          alert("Success");
        },
        function(reason) {
          alert("Failed: " + reason);
        }
      );
barbabravo commented 6 years ago

记住: 一定要在index.html里加上cordova.js的引用 例如

<body>
    <div id="app"></div>
    <script type="text/javascript" src="cordova.js"></script>
    <!-- built files will be auto injected -->
  </body>