WeChat-Entwicklungshinweise: WeChat-Browser-Freigabeeinstellungen und Rückrufe
Aug 04, 2016 am 08:53 AMDie Funktion ?Mit Freunden teilen“/?Mit Momenten teilen“ sollte in WeChat relativ üblich sein. Der geteilte Inhalt wird in Moments mit einem kleinen Bild und einer einfachen Einführung angezeigt Details werden nach dem Klicken angezeigt. Auf diese Weise werden dieses kleine Bild und diese kleine Einführung direkt zur obersten Priorit?t der Klickrate des geteilten Inhalts. Standardm??ig wird dieses Bild als erstes gro?es Bild im Themenbereich des Inhalts geladen, w?hrend die Einleitung nur eine URL l?dt. Diese Art der Anzeige ist immer noch recht unbefriedigend. Schauen wir uns also an, wie diese Inhalte eingerichtet sind. Nehmen Sie PHP als Beispiel:
Zuerst müssen wir ein ?ffentliches Konto haben und die App-ID und das App-Geheimnis erhalten.
Dann k?nnen wir das access_token von der WeChat-Plattform über appid und appsecret austauschen.
<span style="color: #008080;">define</span>("APPID", <span style="color: #800080;">$appid</span><span style="color: #000000;">); </span><span style="color: #008080;">define</span>("APPSECRET", <span style="color: #800080;">$appsecret</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 獲取access_token</span> <span style="color: #800080;">$token_access_url</span> = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . APPID . "&secret=" .<span style="color: #000000;"> APPSECRET; </span><span style="color: #800080;">$res</span> = <span style="color: #008080;">file_get_contents</span>(<span style="color: #800080;">$token_access_url</span>); <span style="color: #008000;">//</span><span style="color: #008000;">獲取文件內(nèi)容或獲取網(wǎng)絡請求的內(nèi)容</span> <span style="color: #800080;">$result</span> = json_decode(<span style="color: #800080;">$res</span>, <span style="color: #0000ff;">true</span>); <span style="color: #008000;">//</span><span style="color: #008000;">接受一個 JSON 格式的字符串并且把它轉(zhuǎn)換為 PHP 變量</span> <span style="color: #800080;">$access_token</span> = <span style="color: #800080;">$result</span>['access_token'];
über access_token k?nnen wir ein jsapi_ticket von der WeChat-Plattform anfordern:
<span style="color: #008000;">//</span><span style="color: #008000;"> 獲取jsapi_ticket</span> <span style="color: #800080;">$ticket_url</span> = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=TOKEN"<span style="color: #000000;">; </span><span style="color: #800080;">$res</span> = <span style="color: #008080;">file_get_contents</span>(<span style="color: #800080;">$ticket_url</span>); <span style="color: #008000;">//</span><span style="color: #008000;">獲取文件內(nèi)容或獲取網(wǎng)絡請求的內(nèi)容</span> <span style="color: #800080;">$result</span> = json_decode(<span style="color: #800080;">$res</span>, <span style="color: #0000ff;">true</span>); <span style="color: #008000;">//</span><span style="color: #008000;">接受一個 JSON 格式的字符串并且把它轉(zhuǎn)換為 PHP 變量</span> <span style="color: #800080;">$ticket</span> = <span style="color: #800080;">$result</span>['ticket'];
Okay, alles ist fertig, wir k?nnen mit der Einrichtung beginnen.
WeChat-Freigabeeinstellungen werden über wx.config festgelegt.
<script><span style="color: #000000;"> wx.config({ debug: </span><span style="color: #0000ff;">false</span>, <span style="color: #008000;">//</span><span style="color: #008000;"> 開啟調(diào)試模式,調(diào)用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數(shù),可以在pc端打開,參數(shù)信息會通過log打出,僅在pc端時才會打印。</span> appId: '<?php echo APPID;?>', <span style="color: #008000;">//</span><span style="color: #008000;"> 必填,公眾號的唯一標識</span> timestamp: <?php echo $timestamp;?>, <span style="color: #008000;">//</span><span style="color: #008000;"> 必填,生成簽名的時間戳</span> nonceStr: '<?php echo $noncestr;?>', <span style="color: #008000;">//</span><span style="color: #008000;"> 必填,生成簽名的隨機串</span> signature: '<?php echo $signature;?>',<span style="color: #008000;">//</span><span style="color: #008000;"> 必填,簽名</span> jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage'] <span style="color: #008000;">//</span><span style="color: #008000;"> 必填,需要使用的JS接口列表</span> <span style="color: #000000;">}); </span></script>
Die mittlere App-ID ist die App-ID unseres offiziellen WeChat-Kontos, Timestamp ist der aktuelle Zeitstempel, Noncestr ist eine zuf?llige Zeichenfolge, die zum Generieren einer Signatur verwendet wird, Signatur ist die generierte Signatur, Jsapilist ist die WeChat-Schnittstelle, die wir verwenden müssen, hier Wenn Wir k?nnen also einfach die beiden Schnittstellen ?Teilen an Freunde“ und ?Teilen an Moments“ verwenden.
Listen Sie kurz den Generierungsprozess von Zeitstempel, Noncestr und Signatur auf:
<span style="color: #008000;">//</span><span style="color: #008000;"> 生成簽名 // 生成隨機字符串</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> RandChar{ </span><span style="color: #0000ff;">function</span> getRandChar(<span style="color: #800080;">$length</span><span style="color: #000000;">){ </span><span style="color: #800080;">$str</span> = <span style="color: #0000ff;">null</span><span style="color: #000000;">; </span><span style="color: #800080;">$strPol</span> = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"<span style="color: #000000;">; </span><span style="color: #800080;">$max</span> = <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$strPol</span>)-1<span style="color: #000000;">; </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=0;<span style="color: #800080;">$i</span><<span style="color: #800080;">$length</span>;<span style="color: #800080;">$i</span>++<span style="color: #000000;">){ </span><span style="color: #800080;">$str</span>.=<span style="color: #800080;">$strPol</span>[<span style="color: #008080;">rand</span>(0,<span style="color: #800080;">$max</span>)];<span style="color: #008000;">//</span><span style="color: #008000;">rand($min,$max)生成介于min和max兩個數(shù)之間的一個隨機整數(shù)</span> <span style="color: #000000;"> } </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$str</span><span style="color: #000000;">; } } </span><span style="color: #800080;">$randCharObj</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> RandChar(); </span><span style="color: #800080;">$noncestr</span> = <span style="color: #800080;">$randCharObj</span>->getRandChar(16<span style="color: #000000;">); </span><span style="color: #800080;">$timestamp</span> = <span style="color: #008080;">time</span><span style="color: #000000;">(); </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$_SERVER</span>['QUERY_STRING'<span style="color: #000000;">]){ </span><span style="color: #800080;">$url</span> = 'http://'.<span style="color: #800080;">$_SERVER</span>['HTTP_HOST'].<span style="color: #800080;">$_SERVER</span>['PHP_SELF'].'?'.<span style="color: #800080;">$_SERVER</span>['QUERY_STRING'<span style="color: #000000;">]; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #800080;">$url</span> = 'http://'.<span style="color: #800080;">$_SERVER</span>['HTTP_HOST'].<span style="color: #800080;">$_SERVER</span>['PHP_SELF'<span style="color: #000000;">]; } </span><span style="color: #800080;">$parameters</span> = <span style="color: #0000ff;">array</span>("noncestr" => <span style="color: #800080;">$noncestr</span>, "jsapi_ticket" => <span style="color: #800080;">$ticket</span>, "timestamp" => <span style="color: #800080;">$timestamp</span>, "url" => <span style="color: #800080;">$url</span><span style="color: #000000;">); </span><span style="color: #008080;">ksort</span>(<span style="color: #800080;">$parameters</span><span style="color: #000000;">); </span><span style="color: #800080;">$string1</span> = ""<span style="color: #000000;">; </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$parameters</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key</span> => <span style="color: #800080;">$val</span><span style="color: #000000;">){ </span><span style="color: #800080;">$string1</span> .= <span style="color: #800080;">$key</span>."=".<span style="color: #800080;">$val</span>."&"<span style="color: #000000;">; } </span><span style="color: #800080;">$string1</span> = <span style="color: #008080;">substr</span>(<span style="color: #800080;">$string1</span>,0,-1<span style="color: #000000;">); </span><span style="color: #800080;">$signature</span> = <span style="color: #008080;">sha1</span>(<span style="color: #800080;">$string1</span>);
An diesem Punkt haben wir die Konfiguration von wx.config abgeschlossen. Als n?chstes k?nnen wir die kleinen Bilder und Einführungsinhalte, die wir gerade erw?hnt haben, frei festlegen:
wx.ready(<span style="color: #0000ff;">function</span><span style="color: #000000;">(){ </span><span style="color: #008000;">//</span><span style="color: #008000;"> 分享到朋友圈設置</span> <span style="color: #000000;"> wx.onMenuShareTimeline({ title: </span>'測試標題', <span style="color: #008000;">//</span><span style="color: #008000;"> 分享標題</span> link: 'http://www.baidu.com', <span style="color: #008000;">//</span><span style="color: #008000;"> 分享鏈接</span> imgUrl: 'http://mp.weixin.qq.com/wiki/static/assets/dc5de672083b2ec495408b00b96c9aab.png', <span style="color: #008000;">//</span><span style="color: #008000;"> 分享圖標</span> success: <span style="color: #0000ff;">function</span><span style="color: #000000;"> () { alert(</span>"分享成功"<span style="color: #000000;">); }, cancel: </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> () { alert(</span>"分享失敗"<span style="color: #000000;">); } }); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 分享給好友</span> <span style="color: #000000;"> wx.onMenuShareAppMessage({ title: </span>'測試標題', <span style="color: #008000;">//</span><span style="color: #008000;"> 分享標題</span> desc: '測試分享描述', <span style="color: #008000;">//</span><span style="color: #008000;"> 分享描述</span> link: 'http://www.baidu.com', <span style="color: #008000;">//</span><span style="color: #008000;"> 分享鏈接</span> imgUrl: 'http://mp.weixin.qq.com/wiki/static/assets/dc5de672083b2ec495408b00b96c9aab.png', <span style="color: #008000;">//</span><span style="color: #008000;"> 分享圖標</span> type: '', <span style="color: #008000;">//</span><span style="color: #008000;"> 分享類型,music、video或link,不填默認為link</span> dataUrl: '', <span style="color: #008000;">//</span><span style="color: #008000;"> 如果type是music或video,則要提供數(shù)據(jù)鏈接,默認為空</span> success: <span style="color: #0000ff;">function</span><span style="color: #000000;"> () { alert(</span>"分享成功"<span style="color: #000000;">); }, cancel: </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> () { alert(</span>"分享失敗"<span style="color: #000000;">); } }); })</span>
In der Mitte werden auch die Werte ?success“ und ?cancel“ sehr h?ufig verwendet. Sie stellen den js-Rückruf nach erfolgreichem Teilen bzw. den Rückruf nach Abbruch des Teilens dar. Sie werden verwendet, um einige Urteile zu f?llen und psychologische Testantworten anzuzeigen Nachdem der Benutzer im Freundeskreis geteilt hat, sind die kleinen Funktionen der Klasse immer noch sehr nützlich.

Hei?e KI -Werkzeuge

Undress AI Tool
Ausziehbilder kostenlos

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Clothoff.io
KI-Kleiderentferner

Video Face Swap
Tauschen Sie Gesichter in jedem Video mühelos mit unserem v?llig kostenlosen KI-Gesichtstausch-Tool aus!

Hei?er Artikel

Hei?e Werkzeuge

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)