文章目錄
簡介
TNWX: TypeScript + Node.js + WeiXin 微信系開發腳手架,支持微信公衆號、微信支付、微信小遊戲、微信小程序、企業號/企業微信、企業微信開發平臺。最最最重要的是能快速的集成至任何 Node.js 框架(Express、Nest、Egg、Koa 等)
接口權限以及文檔
JSSDK使用步驟
步驟一:綁定域名
正式號:登錄微信公衆平臺進入 公衆號設置
的 功能設置
裏填寫 JS接口安全域名
測試號:進入微信公衆帳號測試號申請系統 找到 JS接口安全域名
點擊 修改
步驟二:引入JS文件
在需要調用JS接口的頁面引入如下JS文件:http://res2.wx.qq.com/open/js/jweixin-1.4.0.js (支持https)
步驟三:通過config接口注入權限驗證配置
wx.config({
debug: true,
appId: '{{ appId }}',
timestamp: '{{ timestamp }}',
nonceStr: '{{ nonceStr }}',
signature: '{{ signature }}',
jsApiList: [
'checkJsApi',
'...省略其他'
]
});
TNWX 中籤名實現
簽名算法見 官方文檔附錄1
特別注意: url 必須爲頁面完整的URL包括參數
@Get('jssdk')
@Render('jssdk.hbs')
async jssdk(@Req() request: Request, @Res() response: Response) {
let appId = ApiConfigKit.getApiConfig.getAppId;
let timestamp = new Date().getTime() / 1000;
let nonceStr = uuid.v1();
let url = "http://xxxx/jssdk?....";//填寫完整頁面的URL包括參數
// 生成簽名
let signature = await WeChat.jssdkSignature(nonceStr, timestamp, url);
return {
appId: appId,
timestamp: timestamp,
nonceStr: nonceStr,
signature: signature
};
}
步驟四:通過ready接口處理成功驗證
wx.ready(function(){
// config 信息驗證後會執行 ready 方法,所有接口調用都必須在 config 接口獲得結果之後,config 是一個客戶端的異步操作,所以如果需要在頁面加載時就調用相關接口,則須把相關接口放在 ready 函數中調用來確保正確執行。對於用戶觸發時才調用的接口,則可以直接調用,不需要放在 ready 函數中。
});
步驟五:通過error接口處理失敗驗證
wx.error(function(res){
// config 信息驗證失敗會執行 error 函數,如簽名過期導致驗證失敗,具體錯誤信息可以打開 config 的 debug 模式查看,也可以在返回的 res 參數中查看,對於 SPA 可以在這裏更新簽名。
});
JSSDK 案例
所有JS接口列表見 官方文檔附錄2
特別注意:
- 原有的 wx.onMenuShareTimeline、wx.onMenuShareAppMessage、wx.onMenuShareQQ、wx.onMenuShareQZone 接口,即將廢棄。請儘快遷移使用客戶端 6.7.2 及 JSSDK 1.4.0 以上版本支持的 wx.updateAppMessageShareData、updateTimelineShareData 接口。
- 分享接口中的
link
鏈接必須 JS 接口安全域名下的鏈接 - 公衆號微信支付後面再詳細介紹
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>TNWX 微信公衆號開發腳手架</title>
<meta charset="utf-8">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="tnwx,nodejs,微信公衆號開發">
<meta http-equiv="description" content="tnwx 微信公衆號開發腳手架,可集成到任何 Node.js MVC後端框架中">
<meta name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="https://www.weixinsxy.com/jssdk/css/style.css">
</head>
<body>
<div class="wxapi_container">
<div class="wxapi_index_container">
<ul class="label_box lbox_close wxapi_index_list">
<li class="label_item wxapi_index_item"><a class="label_inner" href="#menu-basic">基礎接口</a></li>
<li class="label_item wxapi_index_item"><a class="label_inner" href="#menu-share">分享接口</a></li>
<li class="label_item wxapi_index_item"><a class="label_inner" href="#menu-image">圖像接口</a></li>
<li class="label_item wxapi_index_item"><a class="label_inner" href="#menu-voice">音頻接口</a></li>
<li class="label_item wxapi_index_item"><a class="label_inner" href="#menu-smart">智能接口</a></li>
<li class="label_item wxapi_index_item"><a class="label_inner" href="#menu-device">設備信息接口</a></li>
<li class="label_item wxapi_index_item"><a class="label_inner" href="#menu-location">地理位置接口</a></li>
<li class="label_item wxapi_index_item"><a class="label_inner" href="#menu-webview">界面操作接口</a></li>
<li class="label_item wxapi_index_item"><a class="label_inner" href="#menu-scan">微信掃一掃接口</a></li>
<li class="label_item wxapi_index_item"><a class="label_inner" href="#menu-shopping">微信小店接口</a></li>
<li class="label_item wxapi_index_item"><a class="label_inner" href="#menu-card">微信卡券接口</a></li>
<li class="label_item wxapi_index_item"><a class="label_inner" href="#menu-pay">微信支付接口</a></li>
</ul>
</div>
<div class="lbox_close wxapi_form">
<h3 id="menu-basic">基礎接口</h3>
<span class="desc">判斷當前客戶端是否支持指定JS接口</span>
<button class="btn btn_primary" id="checkJsApi">checkJsApi</button>
<h3 id="menu-share">分享接口</h3>
<span class="desc">“分享給朋友”及“分享到QQ”</span>
<button class="btn btn_primary" id="updateAppMessageShareData">updateAppMessageShareData</button>
<span class="desc">“分享到朋友圈”及“分享到QQ空間”</span>
<button class="btn btn_primary" id="updateTimelineShareData">updateTimelineShareData</button>
<span class="desc">分享到騰訊微博</span>
<button class="btn btn_primary" id="onMenuShareWeibo">onMenuShareWeibo</button>
<h3 id="menu-image">圖像接口</h3>
<span class="desc">拍照或從手機相冊中選圖接口</span>
<button class="btn btn_primary" id="chooseImage">chooseImage</button>
<span class="desc">預覽圖片接口</span>
<button class="btn btn_primary" id="previewImage">previewImage</button>
<span class="desc">上傳圖片接口</span>
<button class="btn btn_primary" id="uploadImage">uploadImage</button>
<span class="desc">下載圖片接口</span>
<button class="btn btn_primary" id="downloadImage">downloadImage</button>
<h3 id="menu-voice">音頻接口</h3>
<span class="desc">開始錄音接口</span>
<button class="btn btn_primary" id="startRecord">startRecord</button>
<span class="desc">停止錄音接口</span>
<button class="btn btn_primary" id="stopRecord">stopRecord</button>
<span class="desc">播放語音接口</span>
<button class="btn btn_primary" id="playVoice">playVoice</button>
<span class="desc">暫停播放接口</span>
<button class="btn btn_primary" id="pauseVoice">pauseVoice</button>
<span class="desc">停止播放接口</span>
<button class="btn btn_primary" id="stopVoice">stopVoice</button>
<span class="desc">上傳語音接口</span>
<button class="btn btn_primary" id="uploadVoice">uploadVoice</button>
<span class="desc">下載語音接口</span>
<button class="btn btn_primary" id="downloadVoice">downloadVoice</button>
<h3 id="menu-smart">智能接口</h3>
<span class="desc">識別音頻並返回識別結果接口</span>
<button class="btn btn_primary" id="translateVoice">translateVoice</button>
<h3 id="menu-device">設備信息接口</h3>
<span class="desc">獲取網絡狀態接口</span>
<button class="btn btn_primary" id="getNetworkType">getNetworkType</button>
<h3 id="menu-location">地理位置接口</h3>
<span class="desc">使用微信內置地圖查看位置接口</span>
<button class="btn btn_primary" id="openLocation">openLocation</button>
<span class="desc">獲取地理位置接口</span>
<button class="btn btn_primary" id="getLocation">getLocation</button>
<h3 id="menu-webview">界面操作接口</h3>
<span class="desc">隱藏右上角菜單接口</span>
<button class="btn btn_primary" id="hideOptionMenu">hideOptionMenu</button>
<span class="desc">顯示右上角菜單接口</span>
<button class="btn btn_primary" id="showOptionMenu">showOptionMenu</button>
<span class="desc">關閉當前網頁窗口接口</span>
<button class="btn btn_primary" id="closeWindow">closeWindow</button>
<span class="desc">批量隱藏功能按鈕接口</span>
<button class="btn btn_primary" id="hideMenuItems">hideMenuItems</button>
<span class="desc">批量顯示功能按鈕接口</span>
<button class="btn btn_primary" id="showMenuItems">showMenuItems</button>
<span class="desc">隱藏所有非基礎按鈕接口</span>
<button class="btn btn_primary" id="hideAllNonBaseMenuItem">hideAllNonBaseMenuItem</button>
<span class="desc">顯示所有功能按鈕接口</span>
<button class="btn btn_primary" id="showAllNonBaseMenuItem">showAllNonBaseMenuItem</button>
<h3 id="menu-scan">微信掃一掃</h3>
<span class="desc">調起微信掃一掃接口</span>
<button class="btn btn_primary" id="scanQRCode0">scanQRCode(微信處理結果)</button>
<button class="btn btn_primary" id="scanQRCode1">scanQRCode(直接返回結果)</button>
</div>
</div>
</body>
<script type="text/javascript" src="http://res2.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
<script type="text/javascript">
wx.config({
debug: true,//開啓debug模式 正式環境設置爲 false
appId: '{{ appId }}',
timestamp: '{{ timestamp }}',
nonceStr: '{{ nonceStr }}',
signature: '{{ signature }}',
jsApiList: [
'checkJsApi',
'updateAppMessageShareData',
'updateTimelineShareData',
'onMenuShareWeibo',
'hideMenuItems',
'showMenuItems',
'hideAllNonBaseMenuItem',
'showAllNonBaseMenuItem',
'translateVoice',
'startRecord',
'stopRecord',
'onRecordEnd',
'playVoice',
'pauseVoice',
'stopVoice',
'uploadVoice',
'downloadVoice',
'chooseImage',
'previewImage',
'uploadImage',
'downloadImage',
'getNetworkType',
'openLocation',
'getLocation',
'hideOptionMenu',
'showOptionMenu',
'closeWindow',
'scanQRCode',
'chooseWXPay',
'openProductSpecificView',
'addCard',
'chooseCard',
'openCard'
]
});
wx.ready(function () {
// 1 判斷當前版本是否支持指定 JS 接口,支持批量判斷
document.querySelector('#checkJsApi').onclick = function () {
wx.checkJsApi({
jsApiList: [
'getNetworkType',
'previewImage',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo',
],
success: function (res) {
alert(JSON.stringify(res));
}
});
};
// 2. 分享接口
// 2.1 監聽“分享給朋友”,按鈕點擊、自定義分享內容及分享結果接口
// 自定義“分享給朋友”及“分享到QQ”按鈕的分享內容(1.4.0)
document.querySelector('#updateAppMessageShareData').onclick = function () {
wx.updateAppMessageShareData({
title: 'TNWX 微信公衆號開發腳手架',
desc: '支持任何 Node.js 的 MVC 服務端框架',
link: 'https://gitee.com/javen205/IJPay(此鏈接必須JS接口安全域名下的鏈接)',
imgUrl: 'https://gitee.com/javen205/TNWX/raw/master/docs/img/logo.png',
trigger: function (res) {
alert('用戶點擊發送給朋友');
},
success: function (res) {
alert('已分享');
},
cancel: function (res) {
alert('已取消');
},
fail: function (res) {
alert(JSON.stringify(res));
}
});
alert('已註冊獲取“發送給朋友”狀態事件');
};
// 2.2 監聽“分享到朋友圈”按鈕點擊、自定義分享內容及分享結果接口
// 自定義“分享到朋友圈”及“分享到QQ空間”按鈕的分享內容(1.4.0)
document.querySelector('#updateTimelineShareData').onclick = function () {
wx.updateTimelineShareData({
title: 'TNWX 微信公衆號開發腳手架',
desc: '支持任何 Node.js 的 MVC 服務端框架',
link: 'https://gitee.com/javen205/TNWX(此鏈接必須JS接口安全域名下的鏈接)',
imgUrl: 'https://gitee.com/javen205/TNWX/raw/master/docs/img/logo.png',
trigger: function (res) {
alert('用戶點擊分享到朋友圈');
},
success: function (res) {
alert('已分享');
},
cancel: function (res) {
alert('已取消');
},
fail: function (res) {
alert(JSON.stringify(res));
}
});
alert('已註冊獲取“分享到朋友圈”狀態事件');
};
// 2.3 監聽“分享到微博”按鈕點擊、自定義分享內容及分享結果接口
document.querySelector('#onMenuShareWeibo').onclick = function () {
wx.onMenuShareWeibo({
title: 'TNWX 微信公衆號開發腳手架',
desc: '支持任何 Node.js 的 MVC 服務端框架',
link: '此鏈接必須JS接口安全域名下的鏈接,
imgUrl: 'https://gitee.com/javen205/TNWX/raw/master/docs/img/logo.png',
trigger: function (res) {
alert('用戶點擊分享到微博');
},
complete: function (res) {
alert(JSON.stringify(res));
},
success: function (res) {
alert('已分享');
},
cancel: function (res) {
alert('已取消');
},
fail: function (res) {
alert(JSON.stringify(res));
}
});
alert('已註冊獲取“分享到微博”狀態事件');
};
// 3 智能接口
var voice = {
localId: '',
serverId: ''
};
// 3.1 識別音頻並返回識別結果
document.querySelector('#translateVoice').onclick = function () {
if (voice.localId == '') {
alert('請先使用 startRecord 接口錄製一段聲音');
return;
}
wx.translateVoice({
localId: voice.localId,
complete: function (res) {
if (res.hasOwnProperty('translateResult')) {
alert('識別結果:' + res.translateResult);
} else {
alert('無法識別');
}
}
});
};
// 4 音頻接口
// 4.2 開始錄音
document.querySelector('#startRecord').onclick = function () {
wx.startRecord({
cancel: function () {
alert('用戶拒絕授權錄音');
}
});
};
// 4.3 停止錄音
document.querySelector('#stopRecord').onclick = function () {
wx.stopRecord({
success: function (res) {
voice.localId = res.localId;
},
fail: function (res) {
alert(JSON.stringify(res));
}
});
};
// 4.4 監聽錄音自動停止
wx.onVoiceRecordEnd({
complete: function (res) {
voice.localId = res.localId;
alert('錄音時間已超過一分鐘');
}
});
// 4.5 播放音頻
document.querySelector('#playVoice').onclick = function () {
if (voice.localId == '') {
alert('請先使用 startRecord 接口錄製一段聲音');
return;
}
wx.playVoice({
localId: voice.localId
});
};
// 4.6 暫停播放音頻
document.querySelector('#pauseVoice').onclick = function () {
wx.pauseVoice({
localId: voice.localId
});
};
// 4.7 停止播放音頻
document.querySelector('#stopVoice').onclick = function () {
wx.stopVoice({
localId: voice.localId
});
};
// 4.8 監聽錄音播放停止
wx.onVoicePlayEnd({
complete: function (res) {
alert('錄音(' + res.localId + ')播放結束');
}
});
// 4.8 上傳語音
document.querySelector('#uploadVoice').onclick = function () {
if (voice.localId == '') {
alert('請先使用 startRecord 接口錄製一段聲音');
return;
}
wx.uploadVoice({
localId: voice.localId,
success: function (res) {
alert('上傳語音成功,serverId 爲' + res.serverId);
voice.serverId = res.serverId;
}
});
};
// 4.9 下載語音
document.querySelector('#downloadVoice').onclick = function () {
if (voice.serverId == '') {
alert('請先使用 uploadVoice 上傳聲音');
return;
}
wx.downloadVoice({
serverId: voice.serverId,
success: function (res) {
alert('下載語音成功,localId 爲' + res.localId);
voice.localId = res.localId;
}
});
};
// 5 圖片接口
// 5.1 拍照、本地選圖
var images = {
localId: [],
serverId: []
};
document.querySelector('#chooseImage').onclick = function () {
wx.chooseImage({
success: function (res) {
images.localId = res.localIds;
alert('已選擇 ' + res.localIds.length + ' 張圖片');
}
});
};
// 5.2 圖片預覽
document.querySelector('#previewImage').onclick = function () {
wx.previewImage({
current: 'https://gitee.com/javen205/TNWX/raw/master/docs/img/logo.png',
urls: [
'https://gitee.com/javen205/IJPay/raw/master/assets/img/IJPay-t.png',
'https://gitee.com/javen205/TNWX/raw/master/docs/img/logo.png'
]
});
};
// 5.3 上傳圖片
document.querySelector('#uploadImage').onclick = function () {
if (images.localId.length == 0) {
alert('請先使用 chooseImage 接口選擇圖片');
return;
}
var i = 0, length = images.localId.length;
images.serverId = [];
function upload() {
wx.uploadImage({
localId: images.localId[i],
success: function (res) {
alert(JSON.stringify(res));
i++;
alert('已上傳:' + i + '/' + length);
images.serverId.push(res.serverId);
if (i < length) {
upload();
}
},
fail: function (res) {
alert(JSON.stringify(res));
}
});
}
upload();
};
// 5.4 下載圖片
document.querySelector('#downloadImage').onclick = function () {
if (images.serverId.length === 0) {
alert('請先使用 uploadImage 上傳圖片');
return;
}
var i = 0, length = images.serverId.length;
images.localId = [];
function download() {
wx.downloadImage({
serverId: images.serverId[i],
success: function (res) {
alert(JSON.stringify(res));
i++;
alert('已下載:' + i + '/' + length);
images.localId.push(res.localId);
if (i < length) {
download();
}
}
});
}
download();
};
// 6 設備信息接口
// 6.1 獲取當前網絡狀態
document.querySelector('#getNetworkType').onclick = function () {
wx.getNetworkType({
success: function (res) {
alert(res.networkType);
},
fail: function (res) {
alert(JSON.stringify(res));
}
});
};
// 7 地理位置接口
// 7.1 查看地理位置
document.querySelector('#openLocation').onclick = function () {
wx.openLocation({
latitude: 23.099994,
longitude: 113.324520,
name: 'TIT 創意園',
address: '廣州市海珠區新港中路 397 號',
scale: 14,
infoUrl: 'http://weixin.qq.com'
});
};
// 7.2 獲取當前地理位置
document.querySelector('#getLocation').onclick = function () {
wx.getLocation({
success: function (res) {
alert(JSON.stringify(res));
},
cancel: function (res) {
alert('用戶拒絕授權獲取地理位置');
}
});
};
// 8 界面操作接口
// 8.1 隱藏右上角菜單
document.querySelector('#hideOptionMenu').onclick = function () {
wx.hideOptionMenu();
};
// 8.2 顯示右上角菜單
document.querySelector('#showOptionMenu').onclick = function () {
wx.showOptionMenu();
};
// 8.3 批量隱藏菜單項
document.querySelector('#hideMenuItems').onclick = function () {
wx.hideMenuItems({
menuList: [
'menuItem:readMode', // 閱讀模式
'menuItem:share:timeline', // 分享到朋友圈
'menuItem:copyUrl' // 複製鏈接
],
success: function (res) {
alert('已隱藏“閱讀模式”,“分享到朋友圈”,“複製鏈接”等按鈕');
},
fail: function (res) {
alert(JSON.stringify(res));
}
});
};
// 8.4 批量顯示菜單項
document.querySelector('#showMenuItems').onclick = function () {
wx.showMenuItems({
menuList: [
'menuItem:readMode', // 閱讀模式
'menuItem:share:timeline', // 分享到朋友圈
'menuItem:copyUrl' // 複製鏈接
],
success: function (res) {
alert('已顯示“閱讀模式”,“分享到朋友圈”,“複製鏈接”等按鈕');
},
fail: function (res) {
alert(JSON.stringify(res));
}
});
};
// 8.5 隱藏所有非基本菜單項
document.querySelector('#hideAllNonBaseMenuItem').onclick = function () {
wx.hideAllNonBaseMenuItem({
success: function () {
alert('已隱藏所有非基本菜單項');
}
});
};
// 8.6 顯示所有被隱藏的非基本菜單項
document.querySelector('#showAllNonBaseMenuItem').onclick = function () {
wx.showAllNonBaseMenuItem({
success: function () {
alert('已顯示所有非基本菜單項');
}
});
};
// 8.7 關閉當前窗口
document.querySelector('#closeWindow').onclick = function () {
wx.closeWindow();
};
// 9 微信原生接口
// 9.1.1 掃描二維碼並返回結果
document.querySelector('#scanQRCode0').onclick = function () {
wx.scanQRCode({
desc: 'scanQRCode desc'
});
};
// 9.1.2 掃描二維碼並返回結果
document.querySelector('#scanQRCode1').onclick = function () {
wx.scanQRCode({
needResult: 1,
desc: 'scanQRCode desc',
success: function (res) {
alert(JSON.stringify(res));
}
});
};
var shareData = typeof (shareData) === 'undefined' ? {
title: 'TNWX 微信公衆號開發腳手架',
desc: '支持任何 Node.js 的 MVC 服務端框架',
link: 'https://gitee.com/javen205/TNWX',
imgUrl: 'https://gitee.com/javen205/TNWX/raw/master/docs/img/logo.png',
} : shareData;
wx.onMenuShareAppMessage(shareData);
wx.onMenuShareTimeline(shareData);
});
wx.error(function (res) {
alert(res.errMsg);
});
</script>
</html>
Java 版本
開源推薦
TNWX
微信公衆號開發腳手架:https://gitee.com/javen205/TNWIJPay
讓支付觸手可及:https://gitee.com/javen205/IJPay- SpringBoot 微服務高效開發
mica
工具集:https://gitee.com/596392912/mica Avue
一款基於 vue 可配置化的神奇框架:https://gitee.com/smallweigit/avuepig
宇宙最強微服務(架構師必備):https://gitee.com/log4j/pigSpringBlade
完整的線上解決方案(企業開發必備):https://gitee.com/smallc/SpringBlade