global.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //网关对应地址
  2. // var api = "http://gateway.js-dctech.com/api/";
  3. // var api = "http://192.168.2.35:8080/api/";
  4. var api = "http://192.168.2.40:8080/api/";
  5. //批次号
  6. var batch = getUrlParam("batch");
  7. //列序号
  8. var columnSort = getUrlParam("columnSort");
  9. //二维码值
  10. var qrCode = getUrlParam("qrCode");
  11. //当前url
  12. var currentUrl = location.href.split('#')[0];
  13. //缓存中的accessToken
  14. var accessToken = localStorage.getItem("access_token") || "";
  15. //微信重定向后添加的code参数
  16. var oauthCode = getUrlParam("code");
  17. //调用微信鉴权方法
  18. redirectNoCodeUrl();
  19. //封装ajax请求,自动在请求头中加入access_token
  20. jQuery.extend({
  21. authAjax: function (options) {
  22. var data = options.data || {};
  23. if (batch != null) {
  24. data.batch = batch;
  25. }
  26. if (columnSort != null) {
  27. data.columnSort = columnSort;
  28. }
  29. if (qrCode != null) {
  30. data.qrCode = qrCode;
  31. }
  32. //如果accessToken失效,重新发起静默授权
  33. if (accessToken == "" && (typeof(options.data) == "undefined" || options.data.oauthCode == null)) {
  34. data.redirectUrl = currentUrl.split('?')[0];
  35. redirectAuthUrl(data);
  36. return false;
  37. }
  38. options.data = data;
  39. return $.ajax($.extend({
  40. headers: {"access_token":accessToken}
  41. },options));
  42. }
  43. });
  44. //如果oauthCode不为空,进行微信用户鉴权,并重定向回不含code参数的地址
  45. function redirectNoCodeUrl() {
  46. if (oauthCode != null) {
  47. $.ajax({
  48. url: api+"marketing/internal/redirectNoCodeUrl",
  49. data: {
  50. batch: batch,
  51. columnSort: columnSort,
  52. qrCode: qrCode,
  53. redirectUrl: currentUrl.split('?')[0],
  54. oauthCode: oauthCode
  55. },
  56. success: function (res) {
  57. localStorage.setItem("access_token", res.data.access_token);
  58. location.replace(res.data.redictUrl);
  59. }
  60. });
  61. }
  62. }
  63. //重定向到微信静默授权地址
  64. function redirectAuthUrl(data) {
  65. $.ajax({
  66. url: api+"marketing/internal/redirectAuthUrl",
  67. data: data,
  68. success: function (res) {
  69. location.replace(res);
  70. }
  71. });
  72. }
  73. //获取url中的参数
  74. function getUrlParam(name) {
  75. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
  76. var r = window.location.search.substr(1).match(reg); //匹配目标参数
  77. if(r != null){
  78. return decodeURIComponent(r[2]);//路径后面的参数形式为参数名=参数值,而第一个字符为参数名,第二个为=,第三个就为参数值
  79. }
  80. return null;//返回参数值
  81. }
  82. //获取js-sdk签名
  83. function createJsapiSignature(url) {
  84. $.authAjax({
  85. type : "GET",
  86. url : api+"marketing/weixin/createJsapiSignature",
  87. async: false,
  88. data: {url: url},
  89. dataType:"json",
  90. success : function(data) {
  91. wx.config({
  92. beta: true,// 必须这么写,否则wx.invoke调用形式的jsapi会有问题
  93. // debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  94. appId: data.appId, // 必填,企业微信的corpID
  95. timestamp: data.timestamp, // 必填,生成签名的时间戳
  96. nonceStr: data.nonceStr, // 必填,生成签名的随机串
  97. signature: data.signature,// 必填,签名,见附录1
  98. jsApiList: [
  99. 'checkJsApi',
  100. 'openLocation',// 使用微信内置地图查看地理位置接口
  101. 'getLocation' // 获取地理位置接口
  102. ]
  103. });
  104. }
  105. });
  106. }
  107. //获取用户位置
  108. function getLocation(successFun, cancelFun) {
  109. wx.getLocation({
  110. success : function(res) {
  111. var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
  112. var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
  113. var speed = res.speed; // 速度,以米/每秒计
  114. var accuracy = res.accuracy; // 位置精度
  115. successFun(latitude,longitude);
  116. },
  117. cancel : function (res) {
  118. if(typeof cancelFun != 'undefined' && cancelFun instanceof Function){
  119. cancelFun();
  120. }
  121. },
  122. fail : function(res) {
  123. $.hideLoading();
  124. $.toast("获取位置失败", "cancel");
  125. }
  126. });
  127. }