2017-10-29  229 views 发表评论

微信分享时没有缩略图时的解决方案!

 标签:      
商品详情页代码:

<script type="text/javascript" src=" 
<script type="text/javascript">

    wx.config({
        debug: false, 
        appId: "{$signPackage.appId}", // 必填,公众号的唯一标识
        timestamp: "{$signPackage.timestamp}", // 必填,生成签名的时间戳
        nonceStr: "{$signPackage.nonceStr}", // 必填,生成签名的随机串
        signature: "{$signPackage.signature}",// 必填,签名,见附录1
        jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'] //必填
    });

    wx.ready(function(){

         //IOS系统分享时读取图片路径会出现问题 用 encodeURI 来处理下
        var img_url = encodeURI("http://{$smarty.server.SERVER_NAME}{$goods_share_img}");
        //分享到朋友圈
        wx.onMenuShareTimeline({
                // 分享标题
            title: "{$goods.goods_style_name}", 
            // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            link: "http://{$smarty.server.SERVER_NAME}{$smarty.server.REQUEST_URI}",
            imgUrl: img_url, // 分享图标

            success: function () { 
                alert('已分享');
            },
            cancel: function () { 
                alert('已取消');
            }
        });
        //分享给好友
        wx.onMenuShareAppMessage({
            title: "{$goods.goods_style_name}", // 分享标题
            desc: "{$goods.goods_style_name}", // 分享描述
            // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            link: "http://{$smarty.server.SERVER_NAME}{$smarty.server.REQUEST_URI}",
            imgUrl: img_url, // 分享图标
            type: '', // 分享类型,music、video或link,不填默认为link
            dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空

            success: function () { 
                 alert('已分享');
            },
            cancel: function () { 
                alert('已取消');
            }
        });
    });
</script>



<?php

//把要用全部封装一个方法里,方便模版中使用
function getSignPackage() {
    $jsapiTicket = getJsApiTicket();
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $timestamp = time();
    $nonceStr = createNonceStr();
    $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
    $signature = sha1($string);//生成签名
    $signPackage = array(
      "appId"     => 'wxf888b6qb96c0cf91', //自己公众号的AppId,我这里随便写一个
      "nonceStr"  => $nonceStr,
      "timestamp" => $timestamp,
      "signature" => $signature
    );
    return $signPackage; 
}
//生成随机字符串
function createNonceStr($length = 16) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $str = "";
    for ($i = 0; $i < $length; $i++) {
      $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    }
    return $str;
}
//获取jsapi_ticket
function getJsApiTicket() {
    $data = $_SESSION['jsapiticket_all'];
    if ($data['expire_time'] < time()) {
      $accessToken = getAccessToken();
      $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
      $res = json_decode(httpGet($url));
      foreach ($res as $k=>$v){
           $all[$k] = $v;
      }
      $ticket = $all['ticket'];
      if ($ticket) {
       $_SESSION['jsapiticket_all']['expire_time'] = time() + 7000;
       $_SESSION['jsapiticket_all']['jsapi_ticket'] = $ticket;
      }
    } else {
      $ticket = $_SESSION['jsapiticket_all']['jsapi_ticket'];
    }
 
    return $ticket;
}
//获取AccessToken
function getAccessToken() {
    $data = $_SESSION['access_token_all'];
    if ($data['expire_time'] < time()) {
      $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxb338b6a276c0cf25&secret=28cafa17194e9241e6685260b7ebef89";
      $res = json_decode(httpGet($url));
      foreach ($res as $k=>$v){
           $all[$k] = $v;
      }
      $access_token = $all['access_token'];
      if ($access_token) {
            $_SESSION['access_token_all']['expire_time'] = time() + 7000;
            $access_token = $all['access_token'];
            $_SESSION['access_token_all'] =$all;
      }
    } else {
      $access_token = $_SESSION['access_token_all']['access_token'];
    }
    return $access_token;
}
//安全处理
function httpGet($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
    curl_setopt($curl, CURLOPT_URL, $url);
 
    $res = curl_exec($curl);
    curl_close($curl);
 
    return $res;
}
 
$smarty->assign('signPackage',getSignPackage()); //赋值到模板
?>

给我留言

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: