123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- import axios from './axios';
- import { getQueryString } from './common'
- function getAuthInfo(cb?: any, redirect_url?: any) {//jsapi通用授权数据
- axios.post('/api/h5/auth/commonAuthData', {
- corpid: getQueryString('corpid'),
- url: location.href.split('#')[0],
- }).then((res: any) => {
- if (redirect_url) {//构造网页授权链接回调
- redirect_url(res.rst)
- }
- if (cb) {
- initQYConfig(res.rst, cb);
- }
- }).catch((err) => {
- console.log(err, 'err')
- })
- }
- function initQYConfig(authInfo, cb) { // 企业
- wx.config({
- beta: true, // 必须这么写,否则wx.invoke调用形式的jsapi会有问题
- debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
- appId: authInfo.corpid, // 必填,企业微信的corpID
- timestamp: authInfo.timestamp, // 必填,生成签名的时间戳
- nonceStr: authInfo.nonce_str, // 必填,生成签名的随机串
- signature: authInfo.signature, // 必填,签名,见 附录-JS-SDK使用权限签名算法 企业签名
- jsApiList: ['getCurExternalContact'] // 必填,需要使用的JS接口列表,凡是要调用的接口都需要传进来
- })
- wx.ready(function () {
- initYYConfig(authInfo, cb)
- })
- wx.error(function (res) {
- // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
- console.log(res, 'QYerror')
- })
- }
- function initYYConfig(authInfo, cb) { // 应用
- wx.agentConfig({
- debug: false,
- corpid: authInfo.corpid, // 必填,企业微信的corpid,必须与当前登录的企业一致
- agentid: authInfo.agent_id, // 必填,企业微信的应用id (e.g. 1000247)
- timestamp: authInfo.timestamp, // 必填,生成签名的时间戳
- nonceStr: authInfo.nonce_str, // 必填,生成签名的随机串
- signature: authInfo.app_signature, // 必填,签名,见附录-JS-SDK使用权限签名算法 应用签名
- jsApiList: ['getCurExternalContact'], // 必填,传入需要使用的接口名称
- success: function (res) {
- cb ? cb(authInfo) : ''
- },
- fail: function (res) {
- if (res.errMsg.indexOf('function not exist') > -1) {
- alert('版本过低请升级')
- }
- }
- })
- }
- export default getAuthInfo
|