註冊企業微信,在應用與小程序欄目中,設置可信域名,配置公衆號菜單。可信域名不得不說下,在最初開發時,認爲設置並驗證後,微信認證接口會實現跨域請求,其實並無。因此全在H5端還得配合服務端完成票據獲取等操做。javascript
//author herbert QQ:464884492
getAccessToken() {
// 判斷是否緩存有
return new Promise((resolve, reject) => {
var access_token = localStorage.getItem("accessToken");
var expires = localStorage.getItem("expires_accessToken");
if (expires > new Date().getTime() - 2000) {
resolve(access_token);
return;
}
let accessTokenUrl = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + this.groupId + "&corpsecret=" + this.secretId;
// fetch(accessTokenUrl, { method: "GET" })
fetch(this.porxyUrl, {
method: "POST",
body: JSON.stringify({
method: "GET",
url: accessTokenUrl
})
}).then(resp => {
return resp.json()
}).then(data => {
if (data.errcode == 0) {
//保存本次獲取的accessToken
localStorage.setItem("accessToken", data.access_token);
localStorage.setItem("expires_accessToken", new Date().getTime() + data.expires_in * 1000);
resolve(data.access_token);
}
}).catch(data => {
reject();
})
});
},
複製代碼
使用上一步驟獲取到的access_token獲取ticket,接口地址https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=ACCESS_TOKEN H5不能直接訪問,須要服務端經過代理訪問html
//author herbert QQ:464884492
getTicket() {
return new Promise((resolve, reject) => {
var ticket = localStorage.getItem("ticket");
var expires = localStorage.getItem("expires_ticket");
if (expires > new Date().getTime() - 2000) {
resolve(ticket);
return;
}
let ticketUrl = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=" + localStorage.getItem("accessToken");
//fetch(ticketUrl, { method: "GET" })
fetch(this.porxyUrl, {
method: "POST",
body: JSON.stringify({
method: "GET",
url: ticketUrl
})
}).then(resp => {
return resp.json()
}).then(data => {
if (data.errcode == 0) {
//保存本次獲取的accessToken
localStorage.setItem("ticket", data.ticket);
localStorage.setItem("expires_ticket", new Date().getTime() + data.expires_in * 1000);
resolve(data.ticket);
}
}).catch(data => {
reject();
})
});
},
複製代碼
//author herbert QQ:464884492
getSignature(timestamp, ticket) {
let url = window.location.href.split("#")[0];
let jsapi_ticket = "jsapi_ticket=" + ticket + "&noncestr=" + timestamp + "×tamp=" + timestamp.substr(0, 10) + "&url=" + url;
this.printStatuInfo("簽名原始信息:" + jsapi_ticket);
let sha1Str = new jsSHA(decodeURIComponent(jsapi_ticket), "TEXT");
return sha1Str.getHash("SHA-1", "HEX");
}
複製代碼
//author herbert QQ:464884492
wx.config({
beta: true,// 必須這麼寫,不然wx.invoke調用形式的jsapi會有問題
debug: false, // 開啓調試模式,調用的全部api的返回值會在客戶端alert出來,若要查看傳入的參數,能夠在pc端打開,參數信息會經過log打出,僅在pc端時纔會打印。
appId: wxUtils.groupId, // 必填,企業微信的corpID
timestamp: timestamp.substr(0, 10), // 必填,生成簽名的時間戳
nonceStr: timestamp, // 必填,生成簽名的隨機串
signature: sig,// 必填,簽名,見附錄1
jsApiList: ["scanQRCode"] // 必填,須要使用的JS接口列表,全部JS接口列表見附錄2
});
複製代碼
//author herbert QQ:464884492
wx.scanQRCode({
desc: 'scanQRCode desc',
needResult: 1, // 默認爲0,掃描結果由企業微信處理,1則直接返回掃描結果,
scanType: ["qrCode", "barCode"], // 能夠指定掃二維碼仍是一維碼,默認兩者都有
success: function (res) {
// 回調
wxUtils.printStatuInfo("掃描信息:" + JSON.stringify(res));
lblCostTime.innerText = "單次掃碼總共花費:【" + (new Date().getTime() - timeStar) + "】ms";
},
error: function (res) {
if (res.errMsg.indexOf('function_not_exist') > 0) {
alert('版本太低請升級')
}
}
});
複製代碼
H5集成微信JSSDK功能雖然簡單,可是該有的步驟一個都不能少。在最初開發中遇到了如下幾個問題:前端
有喜歡聊技術朋友也歡迎入羣,若二維碼失效可加我微信回覆前端java