123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- import {
- getStorage,
- setStorage
- } from '@/utils/localStorage';
- window.sessionTime = new Date();
- var util = {
- reformParam(methodName, para) {
- var parameter = {}
- parameter['merchantid'] = '1'
- parameter['version'] = '1'
- parameter['sign_type'] = 'RSA'
- parameter['sign'] = '123'
- parameter['charset'] = 'UTF-8'
- parameter['method'] = methodName
- var context = ''
- for (var key in para) {
- context += '&' + key + '=' + para[key]
- }
- parameter['context'] = context
- return parameter
- },
- dateFormat(date, fmt) {
- let ret
- const opt = {
- 'y+': date.getFullYear().toString(),
- 'M+': (date.getMonth() + 1).toString(),
- 'd+': date.getDate().toString(),
- 'H+': date.getHours().toString(),
- 'm+': date.getMinutes().toString(),
- 's+': date.getSeconds().toString(),
-
- }
- for (let k in opt) {
- ret = new RegExp('(' + k + ')').exec(fmt)
- if (ret) {
- fmt = fmt.replace(
- ret[1],
- ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, '0')
- )
- }
- }
- return fmt
- },
- formatDate: function(date, fmt) {
- if (/(y+)/.test(fmt)) {
- fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
- }
- let o = {
- 'M+': date.getMonth() + 1,
- 'd+': date.getDate(),
- 'h+': date.getHours(),
- 'm+': date.getMinutes(),
- 's+': date.getSeconds()
- }
- for (let k in o) {
- if (new RegExp(`(${k})`).test(fmt)) {
- let str = o[k] + ''
- fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : this.padLeftZero(str))
- }
- }
- return fmt
- },
- padLeftZero: function(str) {
- return ('00' + str).substr(str.length)
- },
- formatDatetime(val, format) {
- const year = val.getFullYear();
- const month = val.getMonth() + 1;
- const day = val.getDate();
- const hour = val.getHours();
- const minute = val.getMinutes();
- const second = val.getSeconds();
- return year + '-' + (month > 9 ? month : '0' + month) + '-' + day + ' ' + hour + ':' + (minute > 9 ?
- minute : '0' + minute) + ':' + (second > 9 ? second : '0' + second);
- },
- formatDayTime(val) {
- const year = val.getFullYear();
- const month = val.getMonth() + 1;
- const day = val.getDate();
- const hour = val.getHours();
- const minute = val.getMinutes();
- const second = val.getSeconds();
- return year + '-' + (month > 9 ? month : '0' + month) + '-' + day + ' ' + hour + ':' + (minute > 9 ?
- minute : '0' + minute);
- },
- formatTodayTime(val) {
- const year = val.getFullYear();
- const month = val.getMonth() + 1;
- const day = val.getDate();
- const hour = val.getHours();
- const minute = val.getMinutes();
- if (month == new Date().getMonth() + 1 && day + 1 == new Date().getDate()) {
- return '昨天' + hour + ':' + (minute > 9 ? minute : '0' + minute);
- } else if (month == new Date().getMonth() + 1 && day == new Date().getDate()) {
- return hour + ':' + (minute > 9 ? minute : '0' + minute);
- } else {
- return year + '-' + (month > 9 ? month : '0' + month) + '-' + day + ' ' + hour + ':' + (minute > 9 ?
- minute : '0' + minute);
- }
- },
-
- getNumberOfDays(date1, date2) {
- if (!date1 || !date2) {
- return 0;
- }
-
- var a1 = Date.parse(new Date(date1));
- var a2 = Date.parse(new Date(date2));
- var day = parseInt((a2 - a1) / (1000 * 60 * 60 * 24)) + 1;
- return day
- },
- getUrlParams(url) {
- url = url == null ? window.location.href : url;
- var search = url.substring(url.lastIndexOf("?") + 1);
- var obj = {};
- var reg = /([^?&=]+)=([^?&=]*)/g;
-
-
- search.replace(reg, function(rs, $1, $2) {
- var name = decodeURIComponent($1);
- var val = decodeURIComponent($2);
- val = String(val);
- obj[name] = val;
- return rs;
- });
- return obj;
- },
- formatTime(date) {
- var year = date.getFullYear()
- var month = date.getMonth() + 1
- var day = date.getDate()
- var hour = date.getHours()
- var minute = date.getMinutes()
- var second = date.getSeconds()
- return [year, month, day].map(this.formatNumber).join('-') + ' ' + [hour, minute, second].map(this
- .formatNumber).join(
- ':')
- },
- formatNumber(n) {
- n = n.toString()
- return n[1] ? n : '0' + n
- },
- trackRequest(para, app = null) {
- if ((para.type && para.type.includes('Error'))) {
-
- return
- }
- try {
-
- if(typeof (para.clkParams) == 'object'){
- let obj = {
- locusBehaviorName: para.locusBehaviorName ? para.locusBehaviorName : ''
- }
- para.clkParams = Object.assign(obj,para.clkParams)
- }
-
- let pvCurPageParams = "";
- if(para.pvCurPageParams){
- pvCurPageParams = typeof para.pvCurPageParams === 'object' ? para.pvCurPageParams : JSON.parse(para.pvCurPageParams)
- }else{
- pvCurPageParams = {};
- }
- let queryObj = getStorage('queryObj') ? JSON.parse(getStorage('queryObj')) : null;
- let urlObj = queryObj || util.getUrlParams(location.href) || {};
- let brandId = urlObj.special_ID || $config.brandId || ''
-
- pvCurPageParams.brandId = brandId;
- pvCurPageParams.locusBehaviorName = para.locusBehaviorName || currentPage.locusBehaviorName || '';
- pvCurPageParams.locusValue = para.locusValue || currentPage.locusValue || '';
- pvCurPageParams.locusName = para.locusName || currentPage.locusName || '';
- let data = {
- session: '',
- userAgent: navigator.userAgent.substring(0, 255) || '',
- browserName: navigator.appName || '',
- browserVersion: navigator.appVersion.substring(0, 255) || '',
- platform: 'h5',
- fromPlatform: urlObj.fromPlatform || urlObj.fromProduce || 'h5',
- ip: window.ip || '',
- cookieId: from_cookie || getStorage("cookie_id") || '',
- openId: urlObj.openid || '',
- userId: urlObj.leavePhoneCustomerId || '',
- brandUserId: urlObj.leavePhoneCustomerId || '',
- createTime: this.formatTime(new Date()),
- uploadTime: this.formatTime(new Date()),
- product: 'h5',
- project: para.project || urlObj.xcxHouseId || $config.xcxHouseId || '',
- brandId: brandId,
- expand: JSON.stringify(urlObj),
- imTalkId: para.imTalkId || '',
- imTalkType: para.imTalkType || '',
- eventName: para.eventName || '',
- clkDesPage: para.clkDesPage || '',
- clkId: para.clkId || '',
- clkName: para.clkName || '',
- pvId: para.pvId || '',
- clkParams: typeof para.clkParams === 'object' ? JSON.stringify(para.clkParams) : (para.clkParams || ''),
- pvCurPageName: para.pvCurPageName || currentPage.pvCurPageName || '',
- pvCurPageParams: typeof pvCurPageParams === 'object' ? JSON.stringify(pvCurPageParams) : (pvCurPageParams ||''),
- pvLastPageName: para.pvLastPageName || '',
- pvLastPageParams: para.pvLastPageParams || '',
- pvPageLoadTime: para.pvPageLoadTime || '',
- type: para.type || '',
- }
- let timeNow = new Date().getTime();
- let session = getStorage('sessionNumber') ? Number(getStorage('sessionNumber')) : timeNow;
- if (timeNow - sessionTime > 180000 && !urlObj.session) {
-
- session = timeNow;
- setStorage('sessionNumber', session)
- }
- data.session = urlObj.session || data.userId + "_" + session || '';
- sessionTime = timeNow;
-
-
-
- requestConfig('upload', data, true);
-
-
-
-
-
-
-
-
-
- console.warn("***mook***", (data.pvId || data.clkId || data.eventId), data)
- } catch (e) {
- console.warn("***util.js-onError***", e);
- }
- },
- getSession() {
- let timeNow = new Date().getTime();
- let session = uni.getStorageSync('sessionNumber') || timeNow;
- uni.setStorage({
- key: "sessionNumber",
- data: session
- })
- return session;
- },
- };
- window.from_session = util.getUrlParams(location.href).session || '';
- window.from_cookie = util.getUrlParams(location.href).cookie || '';
- export default util;
|