123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- //网关对应地址
- // var api = "http://gateway.js-dctech.com/api/";
- // var api = "http://192.168.2.35:8080/api/";
- var api = "http://192.168.2.40:8080/api/";
- //批次号
- var batch = getUrlParam("batch");
- //列序号
- var columnSort = getUrlParam("columnSort");
- //二维码值
- var qrCode = getUrlParam("qrCode");
- //当前url
- var currentUrl = location.href.split('#')[0];
- //缓存中的accessToken
- var accessToken = localStorage.getItem("access_token") || "";
- //微信重定向后添加的code参数
- var oauthCode = getUrlParam("code");
- //调用微信鉴权方法
- redirectNoCodeUrl();
- //封装ajax请求,自动在请求头中加入access_token
- jQuery.extend({
- authAjax: function (options) {
- var data = options.data || {};
- data.batch = batch;
- data.columnSort = columnSort;
- data.qrCode = qrCode;
- //如果accessToken失效,重新发起静默授权
- if (accessToken == "" && (typeof(options.data) == "undefined" || options.data.oauthCode == null)) {
- data.redirectUrl = currentUrl.split('?')[0];
- redirectAuthUrl(data);
- return false;
- }
- options.data = data;
- return $.ajax($.extend({
- headers: {"access_token":accessToken}
- },options));
- }
- });
- //如果oauthCode不为空,进行微信用户鉴权,并重定向回不含code参数的地址
- function redirectNoCodeUrl() {
- if (oauthCode != null) {
- $.ajax({
- url: api+"marketing/internal/redirectNoCodeUrl",
- data: {
- batch: batch,
- columnSort: columnSort,
- qrCode: qrCode,
- redirectUrl: currentUrl.split('?')[0],
- oauthCode: oauthCode
- },
- success: function (res) {
- localStorage.setItem("access_token", res.data.access_token);
- location.href = res.data.redictUrl;
- }
- });
- }
- }
- //重定向到微信静默授权地址
- function redirectAuthUrl(data) {
- $.ajax({
- url: api+"marketing/internal/redirectAuthUrl",
- data: data,
- success: function (res) {
- location.href = res;
- }
- });
- }
- //获取url中的参数
- function getUrlParam(name) {
- var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
- var r = window.location.search.substr(1).match(reg); //匹配目标参数
- if(r != null){
- return decodeURIComponent(r[2]);//路径后面的参数形式为参数名=参数值,而第一个字符为参数名,第二个为=,第三个就为参数值
- }
- return null;//返回参数值
- }
- //获取js-sdk签名
- function createJsapiSignature(url) {
- $.authAjax({
- type : "GET",
- url : api+"marketing/weixin/createJsapiSignature",
- async: false,
- data: {url: url},
- dataType:"json",
- success : function(data) {
- wx.config({
- beta: true,// 必须这么写,否则wx.invoke调用形式的jsapi会有问题
- // debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
- appId: data.appId, // 必填,企业微信的corpID
- timestamp: data.timestamp, // 必填,生成签名的时间戳
- nonceStr: data.nonceStr, // 必填,生成签名的随机串
- signature: data.signature,// 必填,签名,见附录1
- jsApiList: [
- 'checkJsApi',
- 'openLocation',// 使用微信内置地图查看地理位置接口
- 'getLocation' // 获取地理位置接口
- ]
- });
- }
- });
- }
- //获取用户位置
- function getLocation(successFun, cancelFun) {
- wx.getLocation({
- success : function(res) {
- var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
- var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
- var speed = res.speed; // 速度,以米/每秒计
- var accuracy = res.accuracy; // 位置精度
- successFun(latitude,longitude);
- },
- cancel : function (res) {
- if(typeof cancelFun != 'undefined' && cancelFun instanceof Function){
- cancelFun();
- }
- },
- fail : function(res) {
- $.hideLoading();
- alert("获取位置失败");
- }
- });
- }
- //回活动首页
- function redirectActivityIndex() {
- $.authAjax({
- url: api+"marketing/internal/redirectActivityIndex",
- async: false,
- success: function (res) {
- location.href = res;
- }
- })
- }
- //拼接含必需参数的url
- function spliceUrl(url) {
- return url + "batch="+batch+"&columnSort="+columnSort+"&qrCode="+qrCode;
- }
|