所有需要使用JS-SDK的頁面必須先注入配置信息摔敛,否則將無法調(diào)用(同一個url僅需調(diào)用一次适袜,對于變化url的SPA(single-page application)的web app可在每次url變化時進行調(diào)用)。
這個很重要舷夺,每個頁面需要引入權限
jsApiList: [
'shareAppMessage',
'previewFile',
'selectEnterpriseContact',
'onMenuShareAppMessage',
'invoke',
'hideOptionMenu',
'showOptionMenu'
] // 必填苦酱,需要使用的JS接口列表,凡是要調(diào)用的接口都需要傳進來
官方文檔:
https://open.work.weixin.qq.com/api/doc/90000/90135/90226
2.下載好文件给猾,在main.js中引入文件疫萤。
image.png
3.路由
import { wxAuth } from '@/utils/wx-auth'; // 授權文件,見步驟4
routers.afterEach((to: any, from: any) => {
wxAuth(to);
});
4.授權的js文件敢伸,例如wx-auth.ts扯饶。內(nèi)容如下:
import network from '@/network'; // 這個是自己項目里的網(wǎng)絡層,用于調(diào)用接口
import Vue from 'vue';
const wxAuth = async (to?: any) => {
const tempUrl = window.location.protocol + '//' + window.location.host + '/nwd-enterprise-wechat' + to.fullPath;
const urlNow = encodeURIComponent(tempUrl);
console.log('當前授權URL:', urlNow);
const noncestr = Math.floor(Math.random() * 100000000000000);
const body = {
url: urlNow,
timestamp: (new Date() as any) - 0,
nonceStr: noncestr
};
const wxConfigParams = await network.common.getWxConfigParams(body); // 通過接口池颈,獲取wxConfig的參數(shù)
const appSignature = await network.common.getAppSignature(body); // 通過接口尾序,獲取agentConfig的參數(shù)
Vue.prototype.$wx.config({
beta: true, // 必須這么寫,否則wx.invoke調(diào)用形式的jsapi會有問題
debug: false, // 開啟調(diào)試模式,調(diào)用的所有api的返回值會在客戶端alert出來躯砰,若要查看傳入的參數(shù)每币,可以在pc端打開,參數(shù)信息會通過log打出琢歇,僅在pc端時才會打印兰怠。
appId: wxConfigParams.appId, // 必填,企業(yè)微信的corpID
timestamp: wxConfigParams.timestamp, // 必填李茫,生成簽名的時間戳
nonceStr: wxConfigParams.nonceStr, // 必填揭保,生成簽名的隨機串
signature: wxConfigParams.signature, // 必填,簽名魄宏,見 附錄-JS-SDK使用權限簽名算法
jsApiList: [
'shareAppMessage',
'previewFile',
'selectEnterpriseContact',
'onMenuShareAppMessage',
'invoke',
'hideOptionMenu',
'showOptionMenu'
] // 必填秸侣,需要使用的JS接口列表,凡是要調(diào)用的接口都需要傳進來
});
Vue.prototype.$wx.ready(function() {
console.log('wx.agentConfig:BEGIN');const u = navigator.userAgent;
const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; // 安卓
const isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); // ios終端
if (isAndroid) {
console.log('安卓手機');
Vue.prototype.$wx.invoke('agentConfig', {
corpid: appSignature.appId, // 必填,企業(yè)微信的corpid味榛,必須與當前登錄的企業(yè)一致
agentid: '10086', // 必填方篮,企業(yè)微信的應用id (e.g. 1000247)生產(chǎn)環(huán)境 寫自己環(huán)境的agentid
timestamp: appSignature.timestamp, // 必填,生成簽名的時間戳
nonceStr: appSignature.nonceStr, // 必填励负,生成簽名的隨機串
signature: appSignature.signature, // 必填藕溅,簽名,見附錄-JS-SDK使用權限簽名算法
jsApiList: [
'openUserProfile',
'previewFile'
], // 必填
}, function(res) {
console.log('result', res);
});
}
if (isIOS) {
console.log('蘋果手機');
Vue.prototype.$wx.agentConfig({
corpid: appSignature.appId, // 必填继榆,企業(yè)微信的corpid巾表,必須與當前登錄的企業(yè)一致
agentid: '10086', // 必填,企業(yè)微信的應用id (e.g. 1000247)生產(chǎn)環(huán)境
timestamp: appSignature.timestamp, // 必填略吨,生成簽名的時間戳
nonceStr: appSignature.nonceStr, // 必填集币,生成簽名的隨機串
signature: appSignature.signature, // 必填,簽名翠忠,見附錄-JS-SDK使用權限簽名算法
jsApiList: [
'openUserProfile',
'previewFile'
], // 必填
success(res: any) {
console.log('agentConfig', res);
},
fail(res: any) {
console.log('err', res);
if (res.errMsg.indexOf('function not exist') > -1) {
alert('版本過低請升級');
}
}
});
}
});
};
export {
wxAuth,
};
5.頁面中使用api
// 選人
selectUser() {
// 企業(yè)微信環(huán)境:
const that = this;
Vue.prototype.$wx.invoke(
'selectEnterpriseContact',
{
fromDepartmentId: 0, // 必填鞠苟,表示打開的通訊錄從指定的部門開始展示,-1表示自己所在部門開始, 0表示從最上層開始
mode: 'single', // 必填秽之,選擇模式当娱,single表示單選,multi表示多選
type: ['user'], // 必填考榨,選擇限制類型跨细,指定department、user中的一個或者多個
selectedDepartmentIds: [], // 非必填河质,已選部門ID列表冀惭。用于多次選人時可重入,single模式下請勿填入多個id
selectedUserIds: [] // 已選成員的ID
},
function(res) {
if (res.err_msg == 'selectEnterpriseContact:ok') {
const userInfo = res.result.userList[0];
}
}
);
}