global.js 4.9 KB

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