微信內的分享預設會抓頁面標題與縮圖,但電商與品牌頁面往往需要自己決定分享出去的商品名稱、描述與圖片。透過 JS-SDK 的分享介面就能做到,本文以完整流程示範。
先由後端計算出簽名套件,前端再據此初始化。
Wx_config.html:
<?php
import("@.ORG.jssdk");
$jssdk = new JSSDK(C('oauth_config.appid'), C('oauth_config.appsecret'));
$signPackage = $jssdk->GetSignPackage();
?>
<script src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>
wx.config({
debug: false,
appId: '<?php echo $signPackage["appId"];?>',
timestamp: '<?php echo $signPackage["timestamp"];?>',
nonceStr: '<?php echo $signPackage["nonceStr"];?>',
signature: '<?php echo $signPackage["signature"];?>',
jsApiList: [
'onMenuShareTimeline',
'onMenuShareAppMessage',
'chooseImage',
'uploadImage'
]
});
</script>
這裡的簽章必須與目前頁面網址完全一致,否則 config 會失敗,後續所有介面都無法呼叫。
<script>
wx.ready(function () {
// 分享給朋友
wx.onMenuShareAppMessage({
title: '{sh:$wxShare.title}', // 商品名
desc: '{sh:$wxShare.desc}', // 店鋪名
link: '{sh:$wxShare.link}', // 商品購買網址
imgUrl: '{sh:$wxShare.imgUrl}', // 分享縮圖
fail: function (res) { alert(JSON.stringify(res)); }
});
// 分享到朋友圈
wx.onMenuShareTimeline({
title: '{sh:$wxShare.title}',
link: '{sh:$wxShare.link}',
imgUrl: '{sh:$wxShare.imgUrl}',
fail: function (res) { alert(JSON.stringify(res)); }
});
});
</script>
兩個介面的欄位略有不同:分享給朋友支援 desc,朋友圈則不支援,因此朋友圈只會顯示標題。
若不設定這些欄位,分享出去的就會是預設標題、預設 logo 與預設描述;設定之後,才會依你指定的內容呈現。
以下是一個實際的分銷推廣頁骨架,把商品資訊、二維碼與分享設定整合在一起:
<!DOCTYPE html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<meta charset="utf-8" />
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0,user-scalable=no" name="viewport" />
<meta content="yes" name="apple-mobile-web-app-capable" />
<meta content="black" name="apple-mobile-web-app-status-bar-style" />
<meta name="format-detection" content="telephone=no" />
<title>【{sh:$store.name}】{sh:$goodsData.name}</title>
<link type="text/css" rel="stylesheet" href="{sh::PUB}css/bootstrap.min.css">
<link type="text/css" rel="stylesheet" href="{sh::PUB}css/font-awesome.min.css">
<script src="{sh::PUB}js/jquery-1.10.2.min.js" type="text/javascript"></script>
<style>
body { background-color: #EFEFEF; min-width: 320px; max-width: 640px; margin: 0 auto; }
.item-bottom { position: absolute; left: 0; bottom: 0; background: rgba(0,0,0,0.4); width: 100%; color: #fff; line-height: 25px; padding-right: 5px; text-align: left; font-size: 13px; padding-left: 10px; }
.qrcode img { width: 95%; }
.qrcode strong { color: #cc0033; text-align: center; padding: 20px; display: block; }
.row_1 { width: 100%; display: -webkit-box; background-color: white; margin-bottom: 15px; }
.row_2 { width: 100%; position: relative; background-color: white; }
.money { display: inline-grid; font-size: 16px; padding: 10px 5px 5px 5px; }
.sale { font-size: 14px; padding-left: 10px; color: gray; }
.money_img { width: 70px; padding: 5px; }
.item-img { position: relative; }
.price { color: #CC3300; font-size: 16px; }
</style>
</head>
<body>
<div class="row_1">
<div class="left"><img class="money_img" src="{sh::RES}public/img/t2.png"></div>
<div class="right">
<div class="money">分銷傭金 <font color='#CC0000'>{sh:$commission}</font>元</div>
<div class="sale">已銷售 <font color='#CC0000'><php>echo ($goodsData['salecount'] + $goodsData['fakemembercount']);</php></font>件</div>
</div>
</div>
<div class="row_2">
<if condition="$wxuserData.nickname neq ''">
<div class="row_2_1">
<div class="left"><img src="{sh:$wxuserData.headimgurl}" width="70px" class="headimg"></div>
<div class="popover right">
<div class="popover-content">
<p>我是 <font color='#FF9900'>{sh:$wxuserData.nickname}</font>,<br/><span>我為<font color='#FF9900'>{sh:$store.name}</font>代言。</span></p>
</div>
</div>
</div>
</if>
<div class="item-img">
<img class="item-img-logo" src="{sh:$goodsData.logoimg}" width="100%">
<div class="item-bottom">
<span>{sh:$goodsData.name}</span>
<div>
<span class="price">¥<strong>{sh:$goodsData.price}</strong></span>
<small><s>¥{sh:$goodsData.oprice}</s></small>
</div>
</div>
</div>
<div class="qrcode text-center">
<img src="{sh:$goodsData.qrcode}" width="100%">
<strong>長按二維碼 識別圖中二維碼</strong>
</div>
</div>
<div class="row_3 tip">
<div class="title"><i class="fa fa-sitemap"></i><span>分銷如何賺錢</span></div>
<div class="content">
<strong>第一步:</strong>轉發商品鏈接或二維碼圖片給微信好友;<br/><br/>
<strong>第二步:</strong>從您轉發的鏈接進入商城的好友,系統自動鎖定為您的客戶,他們購買任何商品您都能獲得傭金;<br/><br/>
<strong>第三步:</strong>在分銷中心查看【我的團隊】與【分銷傭金】,好友確認收貨後即可提現。
</div>
</div>
<include file="./Tpl/Store/Public/Public_foot.html" />
<include file="./Tpl/Store/Wx_config.html"/>
<script>
wx.ready(function () {
wx.onMenuShareAppMessage({
title: '{sh:$wxShare.title}',
desc: '{sh:$wxShare.desc}',
link: '{sh:$wxShare.link}',
imgUrl: '{sh:$wxShare.imgUrl}',
fail: function (res) { alert(JSON.stringify(res)); }
});
wx.onMenuShareTimeline({
title: '{sh:$wxShare.title}',
link: '{sh:$wxShare.link}',
imgUrl: '{sh:$wxShare.imgUrl}',
fail: function (res) { alert(JSON.stringify(res)); }
});
});
</script>
</html>
wx.ready 之中,config 未完成前呼叫會直接失敗。debug: true 可讓微信跳出每一步的執行結果,上線前務必關閉。