corpWXparam.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import requests from "@common/requests";
  2. const wxTokenUrl = 'http://mobworktime.ttkuaiban.com/api/wechat/loginByWXCode'
  3. const coprWxTokenUrl = 'https://mobcrm.ttkuaiban.com/api/corpWXAuth'
  4. export const WX_APPID = 'wx749c84daac654e1e' // 微信appId
  5. // export const CORP_WX_APPID = 'ww4e237fd6abb635af' // 企业微信appId
  6. export const CORP_WX_APPID = 'wwdd1137a65ce0fc87' // 企业微信appId
  7. /**
  8. * 企业微信重定向地址
  9. * @param {Boolean} isCorpWx 是否为企业微信环境
  10. * @returns 企业微信重定向地址
  11. */
  12. export function addressRedirection(isCorpWx = false) {
  13. console.log(wx, '<==== wx')
  14. const appId = isCorpWx ? CORP_WX_APPID : WX_APPID
  15. const url = isCorpWx ? coprWxTokenUrl : wxTokenUrl
  16. return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${encodeURI(url)}&response_type=code&scope=snsapi_base&state=0#wechat_redirect`
  17. }
  18. /**
  19. * 企业微信转译授权
  20. * @param {Object} data 登录接口返回的data的数据
  21. */
  22. export function obtainEnterpriseWeChatParameters(data = {}) {
  23. const token = data.id
  24. const curUrl = window.location.href.split('#')[0]
  25. console.log('开始调用接口')
  26. requests.post(`/wxcorp/getCorpWXConfig`, { url: curUrl, token }).then((res) => {
  27. console.log(res, '<====== 返回的参数 /wxcorp/getCorpWXConfig')
  28. wx.config({
  29. beta: true,
  30. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  31. appId: res.data.appid, // 必填,公众号的唯一标识
  32. timestamp: res.data.timestamp, // 必填,生成签名的时间戳
  33. nonceStr: res.data.noncestr, // 必填,生成签名的随机串
  34. signature: res.data.sign, // 必填,签名,见附录1
  35. jsApiList: ['chooseImage', 'previewImage', 'uploadImage', 'downloadImage', 'previewFile', 'getLocation', 'agentConfig']
  36. })
  37. wx.ready(function () {
  38. // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
  39. requests.post(`/wxcorp/getCorpWXAgentConfig`, { url: curUrl, token }).then((res) => {
  40. console.log(res, '<====== 返回的参数 /wxcorp/getCorpWXAgentConfig')
  41. wx.agentConfig({
  42. corpid: res.data.corpid, // 必填,企业微信的corpid,必须与当前登录的企业一致
  43. agentid: res.data.agentid, // 必填,企业微信的应用id (e.g. 1000247)
  44. timestamp: res.data.timestamp, // 必填,生成签名的时间戳
  45. nonceStr: res.data.nonceStr, // 必填,生成签名的随机串
  46. signature: res.data.signature, // 必填,签名,见附录-JS-SDK使用权限签名算法
  47. jsApiList: ['selectExternalContact', 'openThirdAppServiceChat', 'openAppManage'], //必填,传入需要使用的接口名称
  48. success: function (result) {
  49. console.log(result, '《+========== 成功1')
  50. // wx.agentConfig成功回调后,WWOpenData 才会注入到 window 对象上面
  51. window.WWOpenData.bind(document.querySelector('ww-open-data'))
  52. },
  53. fail: function (res) {
  54. console.log(res, '<===== 失败1')
  55. if (res.errMsg.indexOf('function not exist') > -1) {
  56. alert('版本过低请升级')
  57. }
  58. },
  59. })
  60. }).catch(err => {
  61. console.log(err, '<===== 失败2')
  62. if (err.errMsg.indexOf('function not exist') > -1) {
  63. alert('版本过低请升级')
  64. }
  65. })
  66. })
  67. wx.error(function (res) {
  68. // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
  69. // alert('wxConfig发生异常:'+JSON.stringify(res));
  70. // 企业第一次授权安装进入后会报not in reliable domain的错误,刷新后正常
  71. location.reload();
  72. });
  73. }).catch(err => {
  74. alert(err);
  75. })
  76. }