未完待續···
在util新增一個Js,代碼如下:
const API = require("./api.js")
//由於企業微信jssdk不是模塊化js文件,所以採用自定義index.html模板,並用<script>標籤引入
// const jWeixin = require('../static/jweixin-1.2.0.js');
const initJssdk = function() {
return new Promise((reslove, reject) => {
API.getConfig({
url: encodeURIComponent(window.location.href.split('#')[0])
}).then(res => {
if (res.success) {
let info = JSON.parse(res.module.config);
jWeixin.config({
beta: info.beta, // 必須這麼寫,否則wx.invoke調用形式的jsapi會有問題
debug: info.debug, // 開啓調試模式
appId: info.appId, // 必填,企業微信的corpID
timestamp: info.timestamp, // 必填,生成簽名的時間戳
nonceStr: info.nonceStr, // 必填,生成簽名的隨機串
signature: info.signature, // 必填,簽名,見 附錄-JS-SDK使用權限簽名算法
jsApiList: ['shareToExternalContact'] || info.jsApiList // 必填,需要使用的JS接口列表,凡是要調用的接口都需要傳進來
});
jWeixin.ready((res) => {
console.log('config初始化成功');
reslove(true)
})
jWeixin.error(function(res) {
console.log('config初始化失敗', res);
reslove(false)
});
} else {
reslove(false)
}
})
})
}
const initAgentConfig = function() {
return new Promise((resolve, reject) => {
initJssdk().then(next => {
if (next) {
API.getAgentConfig({
url: encodeURIComponent(window.location.href.split('#')[0])
}).then(res => {
if (res.success) {
let info = JSON.parse(res.module.config);
jWeixin.agentConfig({
corpid: info.corpid, // 必填,企業微信的corpid,必須與當前登錄的企業一致
agentid: info.agentid, // 必填,企業微信的應用id
timestamp: info.timestamp, // 必填,生成簽名的時間戳
nonceStr: info.nonceStr, // 必填,生成簽名的隨機串
signature: info.signature, // 必填,簽名,見附錄1
jsApiList: ['shareToExternalContact'], //必填
success: function(res) {
console.log('agentConfig初始化成功');
resolve(true)
},
fail: function(res) {
if (res.errMsg.indexOf('function not exist') > -1) {
alert('版本過低請升級')
}
console.log('agentConfig初始化失敗');
resolve(false)
}
});
} else {
resolve(false)
}
})
} else {
resolve(false)
}
})
})
}
const sendBatch = function(payload) {
jWeixin.invoke("shareToExternalContact", {
title: payload.title, // 消息的標題
desc: payload.desc, // 消息的描述
link: payload.link, // 消息鏈接
imgUrl: payload.imgUrl // 消息封面
}, function(res) {
if (res.err_msg == "shareToExternalContact:ok") {
console.log("羣發成功")
}
});
}
module.exports = {
initJssdk,
initAgentConfig,
sendBatch
}
使用方式
return new Promise(function(resolve, reject) {
wx.getImageInfo({
src: url,
success: function(res) {
console.log(res)
resolve(res)
},
fail: function(err) {
console.log(err)
}
})
})
完成