parameter.js 9.5 KB
Newer Older
M
mehaotian 已提交
1 2
import {
  PAGE_PVER_TIME,
3 4
  APP_PVER_TIME,
  STAT_URL,
5
  STAT_VERSION,
6
  DIFF_TIME
M
mehaotian 已提交
7
} from './config';
8
const statConfig = require('uni-stat-config').default || require('uni-stat-config');
M
mehaotian 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
const UUID_KEY = '__DC_STAT_UUID';
const UUID_VALUE = '__DC_UUID_VALUE';

export function getUuid() {
  let uuid = '';
  if (getPlatformName() === 'n') {
    try {
      uuid = plus.runtime.getDCloudId()
    } catch (e) {
      uuid = '';
    }
    return uuid
  }

  try {
    uuid = uni.getStorageSync(UUID_KEY);
  } catch (e) {
    uuid = UUID_VALUE;
  }

  if (!uuid) {
    uuid = Date.now() + '' + Math.floor(Math.random() * 1e7);
    try {
      uni.setStorageSync(UUID_KEY, uuid);
    } catch (e) {
      uni.setStorageSync(UUID_KEY, UUID_VALUE);
    }
  }
  return uuid;
}

export const getSgin = (statData) => {
  let arr = Object.keys(statData)
  let sortArr = arr.sort();
  let sgin = {};
  let sginStr = ''
  for (var i in sortArr) {
    sgin[sortArr[i]] = statData[sortArr[i]];
    sginStr += sortArr[i] + '=' + statData[sortArr[i]] + '&'
  }
  // const options = sginStr.substr(0, sginStr.length - 1)
  // sginStr = sginStr.substr(0, sginStr.length - 1) + '&key=' + STAT_KEY;
  // const si = crypto.createHash('md5').update(sginStr).digest('hex');
  return {
    sign: '',
    options: sginStr.substr(0, sginStr.length - 1)
  };
}

export const getSplicing = (data) => {
  let str = ''
  for (var i in data) {
    str += i + '=' + data[i] + '&'
  }
  return str.substr(0, str.length - 1)
}

export const getTime = () => {
  return parseInt(new Date().getTime() / 1000);
}

export const getPlatformName = () => {
  const platformList = {
    'app-plus': 'n',
    'h5': 'h5',
    'mp-weixin': 'wx',
    'mp-alipay': 'ali',
    'mp-baidu': 'bd',
    'mp-toutiao': 'tt',
    'mp-qq': 'qq'
  }
  return platformList[process.env.VUE_APP_PLATFORM];
}

export const getPackName = () => {
84 85 86 87 88 89
  let packName = ''
  if (getPlatformName() === 'wx' || getPlatformName() === 'qq') {
    // 兼容微信小程序低版本基础库
    if (uni.canIUse('getAccountInfoSync')) {
      packName = uni.getAccountInfoSync().miniProgram.appId || '';
    }
M
mehaotian 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
  }
  return packName
}

export const getVersion = () => {
  return getPlatformName() === 'n' ? plus.runtime.version : '';
}

export const getChannel = () => {
  const platformName = getPlatformName();
  let channel = '';
  if (platformName === 'n') {
    channel = plus.runtime.channel;
  }
  if (platformName === 'wx') {
    // TODO;
  }
  return channel;
}

export const getScene = (options) => {
  const platformName = getPlatformName();
  let scene = '';
  if (options) {
    return options;
  }
  if (platformName === 'wx') {
    scene = uni.getLaunchOptionsSync().scene;
  }
  return scene;
}
const First__Visit__Time__KEY = 'First__Visit__Time'
const Last__Visit__Time__KEY = 'Last__Visit__Time'

export const getFirstVisitTime = () => {
  const timeStorge = uni.getStorageSync(First__Visit__Time__KEY);
  let time = 0;
  if (timeStorge) {
    time = timeStorge;
  } else {
    time = getTime();
    uni.setStorageSync(First__Visit__Time__KEY, time);
    uni.removeStorageSync(Last__Visit__Time__KEY);
  }
  return time;
}

export const getLastVisitTime = () => {
  const timeStorge = uni.getStorageSync(Last__Visit__Time__KEY);
  let time = 0;
  if (timeStorge) {
    time = timeStorge;
  } else {
    time = '';
  }
  uni.setStorageSync(Last__Visit__Time__KEY, getTime());
  return time;
}


const PAGE_RESIDENCE_TIME = '__page__residence__time'
let First_Page_residence_time = 0;
let Last_Page_residence_time = 0;


export const setPageResidenceTime = () => {
  First_Page_residence_time = getTime()
  if (getPlatformName() === 'n') {
    uni.setStorageSync(PAGE_RESIDENCE_TIME, getTime());
  }
  return First_Page_residence_time
}

export const getPageResidenceTime = () => {
  Last_Page_residence_time = getTime()
  if (getPlatformName() === 'n') {
    First_Page_residence_time = uni.getStorageSync(PAGE_RESIDENCE_TIME);
  }
  return Last_Page_residence_time - First_Page_residence_time
}
const TOTAL__VISIT__COUNT = 'Total__Visit__Count'
export const getTotalVisitCount = () => {
  const timeStorge = uni.getStorageSync(TOTAL__VISIT__COUNT);
  let count = 1;
  if (timeStorge) {
    count = timeStorge;
    count++
  }
  uni.setStorageSync(TOTAL__VISIT__COUNT, count);
  return count;
}

export const GetEncodeURIComponentOptions = (statData) => {
  let data = {};
  for (let prop in statData) {
    data[prop] = encodeURIComponent(statData[prop]);
  }
  return data;
}

let Set__First__Time = 0;
let Set__Last__Time = 0;

export const getFirstTime = () => {
  let time = new Date().getTime();
  Set__First__Time = time;
  Set__Last__Time = 0;
  return time;
}


export const getLastTime = () => {
  let time = new Date().getTime();
  Set__Last__Time = time;
  return time;
}


export const getResidenceTime = (type) => {
  let residenceTime = 0;
  if (Set__First__Time !== 0) {
    residenceTime = Set__Last__Time - Set__First__Time
  }

  residenceTime = parseInt(residenceTime / 1000);
  residenceTime = residenceTime < 1 ? 1 : residenceTime;
  if (type === 'app') {
    let overtime = residenceTime > APP_PVER_TIME ? true : false
    return {
      residenceTime,
      overtime
    };
  }
  if (type === 'page') {
    let overtime = residenceTime > PAGE_PVER_TIME ? true : false
    return {
      residenceTime,
      overtime
    };
  }

  return {
    residenceTime
  };

}

export const getRoute = () => {
  var pages = getCurrentPages();
  var page = pages[pages.length - 1];
  let _self = page.$vm

  if (getPlatformName() === 'bd') {
    return _self.$mp && _self.$mp.page.is;
  } else {
245
    return (_self.$scope && _self.$scope.route) || (_self.$mp && _self.$mp.page.route);
M
mehaotian 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259
  }
};

export const getPageRoute = (self) => {
  var pages = getCurrentPages();
  var page = pages[pages.length - 1];
  let _self = page.$vm
  let query = self._query;
  let str = query && JSON.stringify(query) !== '{}' ? '?' + JSON.stringify(query) : '';
  // clear
  self._query = '';
  if (getPlatformName() === 'bd') {
    return _self.$mp && _self.$mp.page.is + str;
  } else {
260
    return (_self.$scope && _self.$scope.route + str) || (_self.$mp && _self.$mp.page.route + str);
M
mehaotian 已提交
261 262 263 264
  }
};

export const getPageTypes = (self) => {
265 266 267
  if (self.mpType === 'page' || (self.$mp && self.$mp.mpType === 'page') || self.$options.mpType === 'page') {
    return true;
  }
M
mehaotian 已提交
268 269 270 271
  return false;
}

export const calibration = (eventName, options) => {
272 273 274 275
  //  login 、 share 、pay_success 、pay_fail 、register 、title
  if (!eventName) {
    console.error(`uni.report 缺少 [eventName] 参数`);
    return true
M
mehaotian 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
  }
  if (typeof eventName !== 'string') {
    console.error(`uni.report [eventName] 参数类型错误,只能为 String 类型`);
    return true
  }
  if (eventName.length > 255) {
    console.error(`uni.report [eventName] 参数长度不能大于 255`);
    return true
  }

  if (typeof options !== 'string' && typeof options !== 'object') {
    console.error(`uni.report [options] 参数类型错误,只能为 String 或 Object 类型`);
    return true
  }

  if (typeof options === 'string' && options.length > 255) {
    console.error(`uni.report [options] 参数长度不能大于 255`);
    return true
  }

  if (eventName === 'title' && typeof options !== 'string') {
    console.error('uni.report [eventName] 参数为 title 时,[options] 参数只能为 String 类型');
    return true
299 300 301 302
  }
}

const Report_Data_Time = 'Report_Data_Time'
303
const Report_Status = 'Report_Status'
304 305 306 307
export const isReportData = () => {
  return new Promise((resolve, reject) => {
    let start_time = ''
    let end_time = new Date().getTime()
308
    let diff_time = DIFF_TIME
309
    let report_status = 1
310 311
    try {
      start_time = uni.getStorageSync(Report_Data_Time)
312
      report_status = uni.getStorageSync(Report_Status)
313 314
    } catch (e) {
      start_time = ''
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
      report_status = 1
    }

    if (report_status === '') {
      requestData(({
        enable
      }) => {
        uni.setStorageSync(Report_Data_Time, end_time);
        uni.setStorageSync(Report_Status, enable);
        if (enable === 1) {
          resolve();
        }
      });
      return
    }

    if (report_status === 1) {
      resolve();
333 334 335 336 337 338
    }

    if (!start_time) {
      uni.setStorageSync(Report_Data_Time, end_time)
      start_time = end_time
    }
339

340 341 342 343 344
    if ((end_time - start_time) > diff_time) {
      requestData(({
        enable
      }) => {
        uni.setStorageSync(Report_Data_Time, end_time)
345
        uni.setStorageSync(Report_Status, enable)
346 347
      });
    }
348

349 350 351 352 353 354
  })
}

const requestData = (done) => {
  let formData = {
    usv: STAT_VERSION,
355
    conf: JSON.stringify({
356 357 358 359 360 361 362
      ak: statConfig.appid
    })
  }
  uni.request({
    url: STAT_URL,
    method: 'GET',
    data: formData,
363 364 365 366
    success: (res) => {
      const {
        data
      } = res
367 368 369 370 371 372 373 374 375 376 377 378 379
      if (data.ret === 0) {
        typeof done === 'function' && done({
          enable: data.enable
        })
      }
    },
    fail: (e) => {
      let report_status_code = 1
      try {
        report_status_code = uni.getStorageSync(Report_Status)
      } catch (e) {
        report_status_code = 1
      }
380 381 382
      if (report_status_code === '') {
        report_status_code = 1
      }
383 384 385
      typeof done === 'function' && done({
        enable: report_status_code
      })
386 387
    }
  });
M
mehaotian 已提交
388
}