zoukankan      html  css  js  c++  java
  • h5+ 开发分享功能

    h5+ 开发微信、QQ分享功能

    此处只做微信网页分享示例代码

    方式一、JS+HTML+h5Plus

    1.html代码

    <div class="button" onclick="shareWeb()">分享网页</div>

    2.JS代码

    var shares=null;
    var sweixin=null;
    var buttons=[
    {title:'我的好友',extra:{scene:'WXSceneSession'}},
    {title:'朋友圈',extra:{scene:'WXSceneTimeline'}},
    {title:'我的收藏',extra:{scene:'WXSceneFavorite'}}
    ];


    // H5 plus事件处理
    function plusReady(){
      updateSerivces();
    }
    if(window.plus){
      plusReady();
    }else{
      document.addEventListener('plusready', plusReady, false);
    }

    /**
    *1. 更新分享服务

    * 根据授权,通过getServices方法获取分享服务

    */

    function updateSerivces(){
        plus.share.getServices(function(s){
            shares={};
            for(var i in s){
                var t=s[i];
                shares[t.id]=t;
            }
        sweixin=shares['weixin'];
        }, function(e){
            outSet('获取分享服务列表失败:'+e.message);
        });
    }
    /*
    2.
    */
    // 分享网页
    function shareWeb(){
      var msg={
        type:'web',
        thumbs:['_www/logo.png'],
        href:'分享网页的链接',
        title:'分享网页的标题',
        content:'分享网页的描述'
      };

      sweixin?plus.nativeUI.actionSheet({title:'分享网页到微信',cancel:'取消',buttons:buttons}, function(e){
        (e.index>0)&&share(sweixin, msg, buttons[e.index-1]);
      }):plus.nativeUI.alert('当前环境不支持微信分享操作!'); 
    }
    //

    //3.分享

    function share(srv, msg, button){
      outSet('分享操作:');
      if(!srv){
        outLine('无效的分享服务!');
        return;
      }
      button&&(msg.extra=button.extra);
      // 发送分享
      if(srv.authenticated){
        
        doShare(srv, msg);
      }else{
        srv.authorize(function(){
          doShare(srv, msg);
        }, function(e){
      
    });

    //4. 发送分享

    function doShare(srv, msg){
      outLine(JSON.stringify(msg));
      srv.send(msg, function(){
        outLine('分享到"'+srv.description+'"成功!');
      }, function(e){
        outLine('分享到"'+srv.description+'"失败: '+JSON.stringify(e));
      });
    }

    Tip:时间仓促,先写这么多吧。详细注解后期会补上

  • 相关阅读:
    Zepto swipe 无效(坑)
    Zepto.js-Ajax 请求
    Zepto.js-事件处理
    excel 大文件解析原理实现
    springboot 集成J2Cache
    springboot 单元测试 指定启动类
    springboot 解决 数字长度过长导致JS精度丢失问题
    JS 基本操作
    VUE 动态菜单管理
    VUE router-view key 属性解释
  • 原文地址:https://www.cnblogs.com/ts119/p/12943787.html
Copyright © 2011-2022 走看看