


The 'JS interface domain name' in the WeChat official account implements the sharing function
Aug 08, 2018 am 09:28 AM1. Preparation work
APPID public account id, application friend sharing interface, ip whitelist, js interface secure domain name setting (must be filed) . You must first log in to the WeChat public platform and enter the function settings of "Official Account Settings" to fill in the "JS interface domain name"
2. Import js
Note: Support the use of AMD/CMD standard loading method
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
3. Inject permission verification configuration through the config interface
All pages that need to use JS-SDK must first inject configuration information, otherwise it will not be called (the same URL only needs to be called once, for changing URLs The SPA's web app can be called every time the URL changes. Currently, the Android WeChat client does not support the new H5 features of pushShate, so using pushState to implement the web app's page will cause the signature to fail. This problem will be fixed in Android 6.2)
wx.config({ debug: true, //開啟調(diào)試模式,調(diào)用所有的api的返回值會(huì)在客戶端alert出來,若要查看傳入的參數(shù),可以在PC端打印出來 appId: '', //必填,公賬號(hào)的唯一標(biāo)識(shí) timestamp: '', //必填,生成簽名的時(shí)間戳 nonceStr: '', //必填,生成簽名的隨機(jī)串 signature: '', //必填,簽名 jsApiList: [ //必填,需要使用js列表,否則無法分享成功 'onMenuShareTimeline', //朋友圈 'onMenuShareAppMessage', //朋友 'onMenuShareQQ', //QQ 'onMenuShareWeibo', //QQ空間 ] })
4. Handle successful verification through ready interface
wx.ready(function(){ //config信息驗(yàn)證后會(huì)執(zhí)行ready方法,所有接口調(diào)用必須在config接口獲得結(jié)果之后,config是一個(gè)客戶端的異步操作,所以如果需要在頁面加載時(shí)就調(diào)用相關(guān)接口,則須把相關(guān)接口放在ready函數(shù)中調(diào)用來確保正確執(zhí)行。對(duì)于用戶觸發(fā)時(shí)才調(diào)用的接口,則可以直接調(diào)用,不需要放在ready函數(shù)中 });
5. Handle failed verification through error interface
wx.error({ //config信息驗(yàn)證失敗會(huì)執(zhí)行error函數(shù),如簽名過期導(dǎo)致驗(yàn)證失敗,具體錯(cuò)誤信息可以打開config的debug模式查看,也可以在返回的res參數(shù)中查看,對(duì)于SPA可以在這里更新簽名 });
js code
var data = { title: '', summary: '', pic: '', url: '', success: function(){ getWeixin() ;//用戶確認(rèn)分享后執(zhí)行的回調(diào)函數(shù) }, cancel: function(){ //用戶取消分享后執(zhí)行的回調(diào)函數(shù) } } wx.config({ swapTitleInWX: true, appId: "<?php echo $weixin_package['appid'];?>", timestamp: "<?php echo $weixin_package['timestamp'];?>", nonceStr: "<?php echo $weixin_package['noncestr'];?>", signature: "<?php echo $weixin_package['signature'];?>", jsApiList: [ 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', ] }) wx.ready(function () { wx.onMenuShareTimeline(data); wx.onMenuShareAppMessage(data); wx.onMenuShareQQ(data) wx.onMenuShareWeibo(data) })
Notes:
title, it is recommended to be within 14 characters
Picture size: 300*300 pixels; picture format: size does not exceed 10kB, GIF is not supported Format; will take the first qualified picture in the body of the current page
Briefly interpret the title, it is recommended to be within 20 words
- ## link:'', //Share link, change the link domain name or path must be consistent with the public account JS security domain name corresponding to the current page
<meta itemprop="name" content="標(biāo)題"/> <meta itemprop="description" name="description" content="描述"/> <meta itemprop="image" content="縮列圖地址"/>Related recommendations:
WeChat public account custom sharing content Realize
WeChat public account authorization settings, WeChat public authorization
The above is the detailed content of The 'JS interface domain name' in the WeChat official account implements the sharing function. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
