1.使用umeng sdk进行分享

2.在Android的Application中设置

PlatformConfig.setWeixin(AppConstant.WEIXIN_APP_KEY, AppConstant.WEIXIN_APP_SECRET);此处的AppId和secret为微信开放平台上的id

3.在我们分享出去的网页中需要添的认证代码大概如下:


<script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>



<script>
      window.οnlοad=function(){//进入页面就执行ajax,目的为了传送当前页面url#前的完整url
         var ajaxurl =  'https://test.lem6.cn/xxxxx/index.php';该文件已放入我们的服务器
         var query = new Object();
         var urll = location.href.split('#')[0]; //页面url#前的完整url,可alert弹出查看
         query.urll = $.trim(urll);
            console.log("window onload");
         console.log(query.urll);
         query.post_type = "jsonp";
         $.ajax({ 
            url: ajaxurl,
            data:query,
            type: "POST",
            dataType: "jsonp",
            crossDomain: true,
            success: function(ress){
               //成功则执行JS-SDK
               console.log(ress);//查看返回结果
               //执行JS_SDK
               wx.config({
                  debug: true,
                  appId: ress.appid,
                  timestamp: ress.timestamp,
                  nonceStr: ress.nonceStr, 
                  signature: ress.signature,
                  jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage'] 
               }); 


               wx.ready(function(){
                  // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,
                  // 所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
                        console.log("wx.ready");//查看返回结果
                  wx.onMenuShareTimeline({
                     title: app.datas.title, // 分享标题
                     link: window.location.href, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                     imgUrl: app.shareIcon, // 分享图标
                     success: function () {
                        // 用户确认分享后执行的回调函数
                     },
                     cancel: function () {
                        // 用户取消分享后执行的回调函数
                     }
                  });

                  wx.onMenuShareAppMessage({
                     title: app.datas.title, // 分享标题
                     desc: app.desc, // 分享描述
                     link: window.location.href, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                     imgUrl: app.shareIcon, // 分享图标
                     type: '', // 分享类型,music、video或link,不填默认为link
                     dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
                     success: function () {
                     // 用户确认分享后执行的回调函数

                     },
                     cancel: function () {
                     // 用户取消分享后执行的回调函数

                     }
                  });
               });


               wx.error(function(res){
                        console.log("ajaxurl error res = " + res);
                  // config信息验证失败会执行error函数,
                  //如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
               });
            },
            error:function(){
                    console.log("ajaxurl 通信失败");
               //console.log(app.datas.title);
               //var t=document.title;
               //document.title=app.datas.title;
            }
         });    
      }

</script>

4.index.php的写法

<?php

require_once 'weixin.php';
 $weixin=new weixin();
 $weixin->index();
?>
5.weixin.php
<?php
class weixin{
   public function index()
   {  
      //微信
      //$url = $GLOBALS['request']['urll'];//获取当前页面的url,接收请求参数
      $query_string = $_SERVER["QUERY_STRING"];//获取当前页面的url,接收请求参数
                parse_str($query_string, $res);
                $url = $res['urll'];
      $root['url'] = $url;
      //获取access_token,并缓存
      $file = 'access_token';//缓存文件名access_token
      $expires = 3600;//缓存时间1个小时
      if(file_exists($file)) {
         $time = filemtime($file);
         if(time() - $time > $expires) {
            $token = null;
         }else {
            $token = file_get_contents($file);
         }
      }else{
         fopen("$file", "w+");
         $token = null;
      }
      if (!$token || strlen($token) < 6) {
         $res = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=xxxx&secret=xxxx");//自己的appid,通过微信公众平台查看appid和AppSecret
         $res = json_decode($res, true);
         $token = $res['access_token'];
         // write('access_token', $token, 3600);
         @file_put_contents($file, $token);
      }

      //获取jsapi_ticket,并缓存
      $file1 = 'jsapi_ticket';
      if(file_exists($file1)) {
         $time = filemtime($file1);
         if(time() - $time > $expires) {
            $jsapi_ticket = null;
         }else {
            $jsapi_ticket = file_get_contents($file1);
         }
      }else{
         fopen("$file1", "w+");
         $jsapi_ticket = null;
      }
      if (!$jsapi_ticket || strlen($jsapi_ticket) < 6) {
         $ur = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$token&type=jsapi";
         $res = file_get_contents($ur);
         $res = json_decode($res, true);
         $jsapi_ticket = $res['ticket'];
         @file_put_contents($file1, $jsapi_ticket);
      }

      $timestamp = time();//生成签名的时间戳
      $metas = range(0, 9);
      $metas = array_merge($metas, range('A', 'Z'));
      $metas = array_merge($metas, range('a', 'z'));
      $nonceStr = '';
      for ($i=0; $i < 16; $i++) {
         $nonceStr .= $metas[rand(0, count($metas)-1)];//生成签名的随机串
      }

      $string1="jsapi_ticket=".$jsapi_ticket."&noncestr=$nonceStr"."×tamp=$timestamp"."&url=$url";

      $signature=sha1($string1);
      $root['appid'] = 'xxxxx';//通过微信公众平台查看得到的appid
      $root['nonceStr'] = $nonceStr;
      $root['timestamp'] = $timestamp;
      $root['signature'] = $signature;
      $root['jsapi_ticket'] = $jsapi_ticket;

                $callback = $_GET['callback'];
                echo $callback.'('.json_encode($root).')';
      #echo json_encode($root);
   }
}
?>
6.将我们的服务器地址放入微信公众平台的ip白名单中,最后一步非常重要