生活随笔
收集整理的這篇文章主要介紹了
H5与企业微信jssdk集成
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
H5與企業(yè)微信jssdk集成
一、公眾號設(shè)置
注冊企業(yè)微信,在應(yīng)用與小程序欄目中,設(shè)置可信域名,配置公眾號菜單。可信域名不得不說下,在最初開發(fā)時,認(rèn)為設(shè)置并驗(yàn)證后,微信認(rèn)證接口會實(shí)現(xiàn)跨域請求,其實(shí)并沒有。所以全在H5端還得配合服務(wù)端完成票據(jù)獲取等操作。
二、開發(fā)步驟
-
資源引入
-
開發(fā)文檔地址 https://work.weixin.qq.com/api/doc#90001/90144/90545
-
在html引入 http://res.wx.qq.com/open/js/jweixin-1.4.0.js
-
在html引入SHA1 庫為初始SDK提供簽名算法 https://www.npmjs.com/package/sha1
-
初始流程基本流程(https://work.weixin.qq.com/api/doc#90001/90144/90547)
獲取accesstoken
接口地址 https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRET , 獲取到的token的有效時間為2小時
H5不能直接訪問,需要服務(wù)端通過代理訪問
//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();
})
});
}, View Code 獲取ticket
使用上一步驟獲取到的access_token獲取ticket,接口地址https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=ACCESS_TOKEN
H5不能直接訪問,需要服務(wù)端通過代理訪問
//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();
})
});
}, View Code 生成簽名
文檔地址 https://work.weixin.qq.com/api/doc#90000/90136/90506
需要將參數(shù)構(gòu)造如下格式JSAPITICKET&noncestr=NONCESTR×tamp=TIMESTAMP&url=URL,然后做SHA1算法獲取字符串哈希值。其中NONCESTR是一個隨機(jī)字符串,URL不能包含#以及后邊的部分
//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");
} View Code 初始微信配置信息
根據(jù)前邊幾個步驟獲取的參數(shù),初始微信配置信息
//author herbert QQ:464884492
wx.config({
beta: true,// 必須這么寫,否則wx.invoke調(diào)用形式的jsapi會有問題
debug: false, // 開啟調(diào)試模式,調(diào)用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數(shù),可以在pc端打開,參數(shù)信息會通過log打出,僅在pc端時才會打印。
appId: wxUtils.groupId, // 必填,企業(yè)微信的corpID
timestamp: timestamp.substr(0, 10), // 必填,生成簽名的時間戳
nonceStr: timestamp, // 必填,生成簽名的隨機(jī)串
signature: sig,// 必填,簽名,見附錄1
jsApiList: ["scanQRCode"] // 必填,需要使用的JS接口列表,所有JS接口列表見附錄2
}); View Code 調(diào)用api
初始完成后,需要立即調(diào)用api需要在 wx.ready函數(shù)中注冊回調(diào)函數(shù),如果是不立即調(diào)用可以忽略。以下為調(diào)用微信掃一掃功能
//author herbert QQ:464884492
wx.scanQRCode({
desc: 'scanQRCode desc',
needResult: 1, // 默認(rèn)為0,掃描結(jié)果由企業(yè)微信處理,1則直接返回掃描結(jié)果,
scanType: ["qrCode", "barCode"], // 可以指定掃二維碼還是一維碼,默認(rèn)二者都有
success: function (res) {
// 回調(diào)
wxUtils.printStatuInfo("掃描信息:" + JSON.stringify(res));
lblCostTime.innerText = "單次掃碼總共花費(fèi):【" + (new Date().getTime() - timeStar) + "】ms";
},
error: function (res) {
if (res.errMsg.indexOf('function_not_exist') > 0) {
alert('版本過低請升級')
}
}
}); View Code 三、總結(jié)
H5集成微信JSSDK功能雖然簡單,但是該有的步驟一個都不能少。在最初開發(fā)中遇到了以下幾個問題:
獲取token與ticket存在跨域問題,需要配置一個代理完成
有時生成的簽名與官方有差別,官方提供了一個測試地址https://work.weixin.qq.com/api/jsapisign
demo地址:https://github.com/464884492/weixin/
轉(zhuǎn)載于:https://www.cnblogs.com/yfrs/p/weixinh5.html
總結(jié)
以上是生活随笔為你收集整理的H5与企业微信jssdk集成的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。