wx-auth-two.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // import network from '@/network'; // 这个是自己项目里的网络层,用于调用接口
  2. import Vue from 'vue';
  3. import {
  4. allocations
  5. } from '@/api/configget';
  6. import Router from '@/router'
  7. import {withoutCodeLoad} from '@/utils/wxconfiguration.js'
  8. import moment from 'moment' // 时间库
  9. export const wxAuth = async (pushindex, next) => {
  10. const wechatConfigUrl = location.href.split('#')[0]
  11. // const wechatConfigUrl = location.href.split("?")[1].split("=")[1]
  12. // console.log('获取url')
  13. // console.log(location.href)
  14. // console.log(wechatConfigUrl)
  15. // console.log(window.wechatConfigUrl)
  16. // console.log(window.WWOpenData)
  17. if (window.wechatConfigUrl === wechatConfigUrl && window.WWOpenData) {
  18. if (next) {
  19. next()
  20. }
  21. return Promise.resolve()
  22. } else {
  23. try {
  24. await wxConfigByCorp(wechatConfigUrl, pushindex, next)
  25. window.wechatConfigUrl = wechatConfigUrl
  26. return Promise.resolve()
  27. } catch (error) {
  28. return Promise.reject(error)
  29. }
  30. }
  31. }
  32. async function wxConfigByCorp(body, pushindex, next) {
  33. var data;
  34. await allocations(body).then((res) => {
  35. if (res.data.code == 1) {
  36. console.log("请求配置成功")
  37. data = res.data.data
  38. }else{
  39. }
  40. }).catch(()=>{
  41. // withoutCodeLoad()
  42. })
  43. const wxConfigParams = data.config
  44. const appSignature = data.agentConfig
  45. // console.log(wxConfigParams)
  46. // console.log(appSignature)
  47. // console.log('wxConfig开始')
  48. // console.log(Vue.prototype.$wx)
  49. await Vue.prototype.$wx.config({ // 鉴权(企业的身份与权限)
  50. beta: true, // 必须这么写,否则wx.invoke调用形式的jsapi会有问题
  51. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  52. appId: wxConfigParams.appId, // 必填,企业微信的corpID
  53. timestamp: wxConfigParams.timestamp, // 必填,生成签名的时间戳
  54. nonceStr: wxConfigParams.nonceStr, // 必填,生成签名的随机串
  55. signature: wxConfigParams.signature, // 必填,签名,见 附录-JS-SDK使用权限签名算法
  56. jsApiList: [
  57. 'shareAppMessage',
  58. 'previewFile',
  59. 'selectEnterpriseContact',
  60. 'onMenuShareAppMessage',
  61. 'invoke',
  62. 'hideOptionMenu',
  63. 'showOptionMenu'
  64. ] // 必填,需要使用的JS接口列表,凡是要调用的接口都需要传进来
  65. });
  66. await Vue.prototype.$wx.ready(function () {
  67. // console.log('wxConfig成功')
  68. // console.log('wx.agentConfig开始')
  69. // console.log('wx.agentConfig:BEGIN');
  70. const u = navigator.userAgent;
  71. const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; // 安卓
  72. const isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); // ios终端
  73. if (isAndroid) {
  74. console.log('安卓手机');
  75. Vue.prototype.$wx.invoke('agentConfig', {
  76. corpid: appSignature.appId, // 必填,企业微信的corpid,必须与当前登录的企业一致
  77. agentid: appSignature.agentid, // 必填,企业微信的应用id (e.g. 1000247)生产环境 写自己环境的agentid
  78. timestamp: appSignature.timestamp, // 必填,生成签名的时间戳
  79. nonceStr: appSignature.nonceStr, // 必填,生成签名的随机串
  80. signature: appSignature.signature, // 必填,签名,见附录-JS-SDK使用权限签名算法
  81. jsApiList: [
  82. 'openUserProfile',
  83. 'previewFile'
  84. ], // 必填
  85. }, function (res) {
  86. console.log('result', res);
  87. });
  88. }
  89. if (isIOS) {
  90. console.log('苹果手机');
  91. Vue.prototype.$wx.agentConfig({
  92. corpid: appSignature.appId, // 必填,企业微信的corpid,必须与当前登录的企业一致
  93. agentid: appSignature.agentid, // 必填,企业微信的应用id (e.g. 1000247)生产环境
  94. timestamp: appSignature.timestamp, // 必填,生成签名的时间戳
  95. nonceStr: appSignature.nonceStr, // 必填,生成签名的随机串
  96. signature: appSignature.signature, // 必填,签名,见附录-JS-SDK使用权限签名算法
  97. jsApiList: [
  98. 'openUserProfile',
  99. 'previewFile'
  100. ], // 必填
  101. success(res) {
  102. console.log('agentConfig', res);
  103. },
  104. fail(res) {
  105. console.log('err', res);
  106. if (res.errMsg.indexOf('function not exist') > -1) {
  107. alert('版本过低请升级');
  108. }
  109. }
  110. });
  111. }
  112. if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
  113. } else {
  114. console.log('pc端')
  115. // console.log(appSignature)
  116. Vue.prototype.$wx.agentConfig({ //鉴权(应用的身份与权限)
  117. corpid: appSignature.corpid, // 必填,企业微信的corpid,必须与当前登录的企业一致
  118. agentid: appSignature.agentid, // 必填,企业微信的应用id (e.g. 1000247)
  119. timestamp: appSignature.timestamp, // 必填,生成签名的时间戳
  120. nonceStr: appSignature.nonceStr, // 必填,生成签名的随机串
  121. signature: appSignature.signature, // 必填,签名,见附录-JS-SDK使用权限签名算法
  122. jsApiList: ['selectExternalContact'], //必填
  123. success: function (res) { // 回调 // wx.agentConfig成功回调后,WWOpenData 才会注入到 window 对象上面
  124. console.log('agentConfig成功')
  125. // console.log('回调')
  126. // console.log(res)
  127. console.log('window.WWOpenData')
  128. console.log(window.WWOpenData)
  129. if (next) {
  130. next()
  131. }
  132. if (pushindex) {
  133. console.log('跳转首页')
  134. Router.push({
  135. path: '/'
  136. }) //跳转首页
  137. }
  138. },
  139. fail: function (res) {
  140. console.log(res)
  141. if (res.errMsg.indexOf('function not exist') > -1) {
  142. alert('版本过低请升级')
  143. }
  144. }
  145. });
  146. }
  147. });
  148. }
  149. // export const wxAuth = async (to,pushindex) => {
  150. // // const tempUrl = window.location.protocol + '//' + window.location.host + '/nwd-enterprise-wechat' + to.fullPath;
  151. // // const urlNow = encodeURIComponent(tempUrl);
  152. // // console.log('当前授权URL:', urlNow);
  153. // // const noncestr = Math.floor(Math.random() * 100000000000000);
  154. // // const body = {
  155. // // url: urlNow,
  156. // // timestamp: (new Date()) - 0,
  157. // // nonceStr: noncestr
  158. // // };
  159. // console.log('111111')
  160. // const body = window.location.href.split('#')[0]
  161. // console.log(body)
  162. // var data ;
  163. // await allocations(body).then((res)=>{
  164. // console.log('1111111111')
  165. // if(res.data.code == 1){
  166. // data = res.data.data
  167. // console.log(res.data.data.agentConfig)
  168. // return res.data.data.agentConfig
  169. // }
  170. // })
  171. // const wxConfigParams = data.config
  172. // const appSignature = data.agentConfig
  173. // console.log(wxConfigParams)
  174. // console.log(appSignature)
  175. // // const wxConfigParams = network.common.getWxConfigParams(body); // 通过接口,获取wxConfig的参数
  176. // // const appSignature = network.common.getAppSignature(body); // 通过接口,获取agentConfig的参数
  177. // Vue.prototype.$wx.config({
  178. // beta: true, // 必须这么写,否则wx.invoke调用形式的jsapi会有问题
  179. // debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  180. // appId: wxConfigParams.appId, // 必填,企业微信的corpID
  181. // timestamp: wxConfigParams.timestamp, // 必填,生成签名的时间戳
  182. // nonceStr: wxConfigParams.nonceStr, // 必填,生成签名的随机串
  183. // signature: wxConfigParams.signature, // 必填,签名,见 附录-JS-SDK使用权限签名算法
  184. // jsApiList: [
  185. // 'shareAppMessage',
  186. // 'previewFile',
  187. // 'selectEnterpriseContact',
  188. // 'onMenuShareAppMessage',
  189. // 'invoke',
  190. // 'hideOptionMenu',
  191. // 'showOptionMenu'
  192. // ] // 必填,需要使用的JS接口列表,凡是要调用的接口都需要传进来
  193. // });
  194. // Vue.prototype.$wx.ready(function() {
  195. // console.log('wx.agentConfig:BEGIN');const u = navigator.userAgent;
  196. // const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; // 安卓
  197. // const isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); // ios终端
  198. // if (isAndroid) {
  199. // console.log('安卓手机');
  200. // Vue.prototype.$wx.invoke('agentConfig', {
  201. // corpid: appSignature.appId, // 必填,企业微信的corpid,必须与当前登录的企业一致
  202. // agentid: appSignature.agentid, // 必填,企业微信的应用id (e.g. 1000247)生产环境 写自己环境的agentid
  203. // timestamp: appSignature.timestamp, // 必填,生成签名的时间戳
  204. // nonceStr: appSignature.nonceStr, // 必填,生成签名的随机串
  205. // signature: appSignature.signature, // 必填,签名,见附录-JS-SDK使用权限签名算法
  206. // jsApiList: [
  207. // 'openUserProfile',
  208. // 'previewFile'
  209. // ], // 必填
  210. // }, function(res) {
  211. // console.log('result', res);
  212. // });
  213. // }
  214. // if (isIOS) {
  215. // console.log('苹果手机');
  216. // Vue.prototype.$wx.agentConfig({
  217. // corpid: appSignature.appId, // 必填,企业微信的corpid,必须与当前登录的企业一致
  218. // agentid: appSignature.agentid, // 必填,企业微信的应用id (e.g. 1000247)生产环境
  219. // timestamp: appSignature.timestamp, // 必填,生成签名的时间戳
  220. // nonceStr: appSignature.nonceStr, // 必填,生成签名的随机串
  221. // signature: appSignature.signature, // 必填,签名,见附录-JS-SDK使用权限签名算法
  222. // jsApiList: [
  223. // 'openUserProfile',
  224. // 'previewFile'
  225. // ], // 必填
  226. // success(res) {
  227. // console.log('agentConfig', res);
  228. // },
  229. // fail(res) {
  230. // console.log('err', res);
  231. // if (res.errMsg.indexOf('function not exist') > -1) {
  232. // alert('版本过低请升级');
  233. // }
  234. // }
  235. // });
  236. // }
  237. // if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
  238. // }else{
  239. // console.log('pc端')
  240. // console.log('wxConfig成功')
  241. // Vue.prototype.$wx.agentConfig({
  242. // corpid: appSignature.corpid, // 必填,企业微信的corpid,必须与当前登录的企业一致
  243. // agentid: appSignature.agentid, // 必填,企业微信的应用id (e.g. 1000247)
  244. // timestamp: appSignature.timestamp, // 必填,生成签名的时间戳
  245. // nonceStr: appSignature.nonceStr, // 必填,生成签名的随机串
  246. // signature: appSignature.signature,// 必填,签名,见附录-JS-SDK使用权限签名算法
  247. // jsApiList: ['selectExternalContact'], //必填
  248. // success: function(res) { // 回调
  249. // console.log('agentConfig成功')
  250. // console.log(res)
  251. // //wx.agentConfig成功回调后,WWOpenData 才会注入到 window 对象上面
  252. // console.log(window.WWOpenData)
  253. // // if(routpush){
  254. // // Router.push({path: '/'})//跳转首页
  255. // // }
  256. // console.log(to)
  257. // if(pushindex){
  258. // Router.push({path: '/'})//跳转首页
  259. // }else{
  260. // Router.push({path: to.path})//跳转首页
  261. // }
  262. // },
  263. // fail: function(res) {
  264. // console.log(res)
  265. // if(res.errMsg.indexOf('function not exist') > -1){
  266. // alert('版本过低请升级')
  267. // }
  268. // }
  269. // });
  270. // }
  271. // });
  272. // };