目的:實現(xiàn)微信圖文分享功能
文檔:
https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html
一、公眾號配置
微信公眾號管理后臺:開發(fā) -> 基本配置
1、獲取:? AppID 和 AppSecret
2后专、將后臺申請微信token和簽名的服務(wù)器的ip添加到IP白名單中食磕。
3、公眾號需要申請認(rèn)證答憔,才能獲取分享的權(quán)限
4味赃、h5發(fā)布的域名配置
將測試域名和正式域名添加到JS接口安全域名。
添加的域名根目錄必須放入微信校驗文件虐拓,才可以配置通過
二心俗、H5工程中接入SDK
1、head中引入
<script?src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
2蓉驹、判斷微信環(huán)境
??????var?obj?=?{
????????"isIos":?false,
????????"isWeChat":?false,
????????"audio":?media
??????}
??????let?ua?=?window.navigator.userAgent.toLowerCase();
??????if?(ua.match(/wxwork/i)?==?'wxwork')?{
? ? //企業(yè)微信
????????obj.isWeChat?=?false;
??????}?else?if?(ua.match(/MicroMessenger/i)?==?'micromessenger')?{
? ? //微信
????????obj.isWeChat?=?true;
??????}?else?{
? ? //其他環(huán)境
????????obj.isWeChat?=?false;
??????}
?? ? window["GAME_HTML_INFO"]?=?obj;
3城榛、微信環(huán)境請求微信簽名
????????const?DEVELOP?=?{
??????????/**?簽名請求地址?*/
??????????ip:?'https://xxxxxxxx/edu-game-math-core/core/operate/getH5Ticket',
??????????/**?分享圖地址?*/
??????????imgUrl:?'https://xxxxxxxx/test/games/EduGamesSlicingH5/h5/share/001.png'
????????}
????????const?RELEASE?=?{
??????????/**?簽名請求地址?*/
??????????ip:?'https://xxxxxxx/edu-game-math-core/core/operate/getH5Ticket',
??????????/**?分享圖地址?*/
??????????imgUrl:?'https://xxxxxxxx/games/EduGamesSlicingH5/h5/share/001.png'
????????}
????????/**?當(dāng)前環(huán)境?*/
????????const?env?=?RELEASE;
發(fā)送get請求到我們自己的服務(wù)器,獲取簽名
????????function?GET(url,?reqData,?callback)?{
??????????url?+=?"?";
??????????for?(var?item?in?reqData)?{
????????????url?+=?item?+?"="?+?reqData[item]?+?"&";
??????????}
??????????console.log(url);
??????????var?xhr?=?new?XMLHttpRequest();
??????????xhr.onreadystatechange?=?function?()?{
????????????if?(xhr.readyState?==?4)?{
??????????????if?(xhr.status?>=?200?&&?xhr.status?<?400)?{
????????????????var?response?=?xhr.responseText;
????????????????console.log(response)
????????????????if?(response)?{
??????????????????var?responseJson?=?JSON.parse(response);
??????????????????callback(responseJson);
????????????????}?else?{
??????????????????console.log("返回數(shù)據(jù)不存在")
??????????????????callback(false);
????????????????}
??????????????}?else?{
????????????????console.log("請求失敗")
????????????????callback(false);
??????????????}
????????????}
??????????};
??????????xhr.open("GET",?url,?true);
??????????xhr.send();
????????}
4态兴、設(shè)置微信分享
?GET(ip,?{?url:?localUrl?},?(ret)?=>?{
??????????var?APPID?=?'wx1111111111111111111111111111';
??????????var?title?=?'尋找對稱軸';
??????????var?desc?=?'5分鐘玩轉(zhuǎn)軸對稱圖形狠持,將抽象變?yōu)樾蜗螅瓉頂?shù)學(xué)如此有趣~';
??????????var?imgUrl?=?env.imgUrl;
??????????var?noncestr?=?ret.data.nonceStr;
??????????var?timestamp?=?ret.data.timestamp;
??????????var?link?=?ret.data.url;
??????????var?signature?=?ret.data.signature;
? wx.config({
????????????debug:?false,
????????????appId:?APPID,
????????????timestamp:?timestamp,
????????????nonceStr:?noncestr,
????????????signature:?signature,
????????????jsApiList:?[
??????????????'checkJsApi',
??????????????'updateAppMessageShareData',
??????????????'updateTimelineShareData',
??????????????'onMenuShareTimeline',
??????????????'onMenuShareAppMessage',
??????????????'onMenuShareQQ',
??????????????'onMenuShareWeibo',
??????????????'onMenuShareQZone'
????????????]
??????????});
? wx.checkJsApi({
????????????jsApiList:?[
??????????????'checkJsApi',
??????????????'updateAppMessageShareData',
??????????????'updateTimelineShareData',
??????????????'onMenuShareTimeline',
??????????????'onMenuShareAppMessage',
??????????????'onMenuShareQQ',
??????????????'onMenuShareWeibo',
??????????????'onMenuShareQZone'
????????????],
????????????success:?function?(res)?{
??????????????console.log(JSON.stringify(res));
????????????}
??????????});
? wx.ready(function?()?{
????????????window["WECHAT_READY"]?=?true;
????????????if?(window["GAME_READY"]?===?true)?{
??????????????playMusic();
????????????}
????????????wx.updateAppMessageShareData({
??????????????title:?title,?//?分享標(biāo)題
??????????????desc:?desc,?//?分享描述
??????????????link:?link,?//?分享鏈接瞻润,該鏈接域名或路徑必須與當(dāng)前頁面對應(yīng)的公眾號JS安全域名一致
??????????????imgUrl:?imgUrl,?//?分享圖標(biāo)
??????????????success:?function?()?{
????????????????//?設(shè)置成功
??????????????}
????????????})
????????????wx.updateTimelineShareData({
??????????????title:?title,?//?分享標(biāo)題
??????????????link:?link,?//?分享鏈接喘垂,該鏈接域名或路徑必須與當(dāng)前頁面對應(yīng)的公眾號JS安全域名一致
??????????????imgUrl:?imgUrl,?//?分享圖標(biāo)
??????????????success:?function?()?{
????????????????//?設(shè)置成功
??????????????}
????????????})
????????????wx.onMenuShareTimeline({
??????????????title:?title,?//?分享標(biāo)題
??????????????link:?link,?//?分享鏈接,該鏈接域名或路徑必須與當(dāng)前頁面對應(yīng)的公眾號JS安全域名一致
??????????????imgUrl:?imgUrl,?//?分享圖標(biāo)
??????????????success:?function?()?{
????????????????//?用戶點擊了分享后執(zhí)行的回調(diào)函數(shù)
??????????????}
????????????})
????????????wx.onMenuShareAppMessage({
??????????????title:?title,?//?分享標(biāo)題
??????????????desc:?desc,?//?分享描述
??????????????link:?link,?//?分享鏈接绍撞,該鏈接域名或路徑必須與當(dāng)前頁面對應(yīng)的公眾號JS安全域名一致
??????????????imgUrl:?imgUrl,?//?分享圖標(biāo)
??????????????type:?'',?//?分享類型,music正勒、video或link,不填默認(rèn)為link
??????????????dataUrl:?'',?//?如果type是music或video楚午,則要提供數(shù)據(jù)鏈接昭齐,默認(rèn)為空
??????????????success:?function?()?{
????????????????//?用戶點擊了分享后執(zhí)行的回調(diào)函數(shù)
??????????????}
????????????});
????????????wx.onMenuShareQQ({
??????????????title:?title,?//?分享標(biāo)題
??????????????desc:?desc,?//?分享描述
??????????????link:?link,?//?分享鏈接
??????????????imgUrl:?imgUrl,?//?分享圖標(biāo)
??????????????success:?function?()?{
????????????????//?用戶確認(rèn)分享后執(zhí)行的回調(diào)函數(shù)
??????????????},
??????????????cancel:?function?()?{
????????????????//?用戶取消分享后執(zhí)行的回調(diào)函數(shù)
??????????????}
????????????});
????????????wx.onMenuShareWeibo({
??????????????title:?title,?//?分享標(biāo)題
??????????????desc:?desc,?//?分享描述
??????????????link:?link,?//?分享鏈接
??????????????imgUrl:?imgUrl,?//?分享圖標(biāo)
??????????????success:?function?()?{
????????????????//?用戶確認(rèn)分享后執(zhí)行的回調(diào)函數(shù)
??????????????},
??????????????cancel:?function?()?{
????????????????//?用戶取消分享后執(zhí)行的回調(diào)函數(shù)
??????????????}
????????????});
????????????wx.onMenuShareQZone({
??????????????title:?title,?//?分享標(biāo)題
??????????????desc:?desc,?//?分享描述
??????????????link:?link,?//?分享鏈接
??????????????imgUrl:?imgUrl,?//?分享圖標(biāo)
??????????????success:?function?()?{
????????????????//?用戶確認(rèn)分享后執(zhí)行的回調(diào)函數(shù)
??????????????},
??????????????cancel:?function?()?{
????????????????//?用戶取消分享后執(zhí)行的回調(diào)函數(shù)
??????????????}
????????????});
??????????});
??????????wx.error(function?(res)?{
????????????//?config信息驗證失敗會執(zhí)行error函數(shù),如簽名過期導(dǎo)致驗證失敗矾柜,具體錯誤信息可以打開config的debug模式查看阱驾,也可以在返回的res參數(shù)中查看,對于SPA可以在這里更新簽名怪蔑。
??????????});
????????});
}
5里覆、服務(wù)器需求
(1)向微信請求獲取access_token(有效期7200秒)
請求地址及參數(shù):
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=公眾號AppID &secret=公眾號密鑰
調(diào)試連接:
注意事項:必須將請求的服務(wù)器ip添加到公眾號后臺管理的白名單才能請求成功。
(2)向微信請求獲取jsapi_ticket(有效期7200秒)
請求地址及參數(shù):
https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi
(3)簽名
簽名校驗工具:
https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign
簽名算法:
https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#62
(4)請求參數(shù)及返回值:
Req: {
url:?location.href.split('#')[0]
}
Ret:{
data:{
nonceStr:string缆瓣,
tmestamp:string,
url:string,
signature: string
}
}
6喧枷、注意事項:
(1)、服務(wù)器IP必須加入白名單
(2)、請求簽名的地址必須和當(dāng)前訪問網(wǎng)頁的地址完全一致
(3)隧甚、游戲地址的域名必須配置再js安全域名中
(4)车荔、分享圖的地址,域名必須配置再js安全域名中
(5)戚扳、appid前后端必須一致忧便,AppSecret不允許放到前端代碼中
7、電腦調(diào)試方法
(1)帽借、安裝微信開發(fā)者工具
(2)珠增、打開公眾號網(wǎng)頁
(3)、輸入需調(diào)試的地址即可
8砍艾、微信分享圖配置
在web-mobile目錄中添加share文件夾蒂教,將分享圖放入其中,每次發(fā)布版本脆荷,分享圖會上傳到服務(wù)器中項目目錄下凝垛。并在代碼中配置分享地址。
測試地址:
imgUrl:?'https://xxxxxxxxxxx/test/games/EduGamesSlicingH5/h5/share/001.png'
正式地址:imgUrl:?'xxxxxxxxxxx/games/EduGamesSlicingH5/h5/share/001.png'