util.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import {
  2. getStorage
  3. } from '@/utils/localStorage';
  4. window.sessionTime = new Date();
  5. var util = {
  6. reformParam(methodName, para) {
  7. var parameter = {}
  8. parameter['merchantid'] = '1'
  9. parameter['version'] = '1'
  10. parameter['sign_type'] = 'RSA'
  11. parameter['sign'] = '123'
  12. parameter['charset'] = 'UTF-8'
  13. parameter['method'] = methodName
  14. var context = ''
  15. for (var key in para) {
  16. context += '&' + key + '=' + para[key]
  17. }
  18. parameter['context'] = context
  19. return parameter
  20. },
  21. dateFormat(date, fmt) {
  22. let ret
  23. const opt = {
  24. 'y+': date.getFullYear().toString(), // 年
  25. 'M+': (date.getMonth() + 1).toString(), // 月
  26. 'd+': date.getDate().toString(), // 日
  27. 'H+': date.getHours().toString(), // 时
  28. 'm+': date.getMinutes().toString(), // 分
  29. 's+': date.getSeconds().toString(), // 秒
  30. // 有其他格式化字符需求可以继续添加,必须转化成字符串
  31. }
  32. for (let k in opt) {
  33. ret = new RegExp('(' + k + ')').exec(fmt)
  34. if (ret) {
  35. fmt = fmt.replace(
  36. ret[1],
  37. ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, '0')
  38. )
  39. }
  40. }
  41. return fmt
  42. },
  43. formatDate: function(date, fmt) {
  44. if (/(y+)/.test(fmt)) {
  45. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  46. }
  47. let o = {
  48. 'M+': date.getMonth() + 1,
  49. 'd+': date.getDate(),
  50. 'h+': date.getHours(),
  51. 'm+': date.getMinutes(),
  52. 's+': date.getSeconds()
  53. }
  54. for (let k in o) {
  55. if (new RegExp(`(${k})`).test(fmt)) {
  56. let str = o[k] + ''
  57. fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : this.padLeftZero(str))
  58. }
  59. }
  60. return fmt
  61. },
  62. padLeftZero: function(str) {
  63. return ('00' + str).substr(str.length)
  64. },
  65. formatDatetime(val, format) {
  66. const year = val.getFullYear();
  67. const month = val.getMonth() + 1;
  68. const day = val.getDate();
  69. const hour = val.getHours();
  70. const minute = val.getMinutes();
  71. const second = val.getSeconds();
  72. return year + '-' + (month > 9 ? month : '0' + month) + '-' + day + ' ' + hour + ':' + (minute > 9 ?
  73. minute : '0' + minute) + ':' + (second > 9 ? second : '0' + second);
  74. },
  75. formatDayTime(val) {
  76. const year = val.getFullYear();
  77. const month = val.getMonth() + 1;
  78. const day = val.getDate();
  79. const hour = val.getHours();
  80. const minute = val.getMinutes();
  81. const second = val.getSeconds();
  82. return year + '-' + (month > 9 ? month : '0' + month) + '-' + day + ' ' + hour + ':' + (minute > 9 ?
  83. minute : '0' + minute);
  84. },
  85. formatTodayTime(val) {
  86. const year = val.getFullYear();
  87. const month = val.getMonth() + 1;
  88. const day = val.getDate();
  89. const hour = val.getHours();
  90. const minute = val.getMinutes();
  91. if (month == new Date().getMonth() + 1 && day + 1 == new Date().getDate()) {
  92. return '昨天' + hour + ':' + (minute > 9 ? minute : '0' + minute);
  93. } else if (month == new Date().getMonth() + 1 && day == new Date().getDate()) {
  94. return hour + ':' + (minute > 9 ? minute : '0' + minute);
  95. } else {
  96. return year + '-' + (month > 9 ? month : '0' + month) + '-' + day + ' ' + hour + ':' + (minute > 9 ?
  97. minute : '0' + minute);
  98. }
  99. },
  100. /**
  101. * 计算天数
  102. * @param date1
  103. * @param date2
  104. * @returns {number}
  105. * @constructor
  106. */
  107. getNumberOfDays(date1, date2) { //获得天数
  108. if (!date1 || !date2) {
  109. return 0;
  110. }
  111. //date1:开始日期,date2结束日期
  112. var a1 = Date.parse(new Date(date1));
  113. var a2 = Date.parse(new Date(date2));
  114. var day = parseInt((a2 - a1) / (1000 * 60 * 60 * 24)) + 1; //核心:时间戳相减,然后除以天数
  115. return day
  116. },
  117. getUrlParams(url) {
  118. url = url == null ? window.location.href : url;
  119. var search = url.substring(url.lastIndexOf("?") + 1);
  120. var obj = {};
  121. var reg = /([^?&=]+)=([^?&=]*)/g;
  122. // [^?&=]+表示:除了?、&、=之外的一到多个字符
  123. // [^?&=]*表示:除了?、&、=之外的0到多个字符(任意多个)
  124. search.replace(reg, function(rs, $1, $2) {
  125. var name = decodeURIComponent($1);
  126. var val = decodeURIComponent($2);
  127. val = String(val);
  128. obj[name] = val;
  129. return rs;
  130. });
  131. return obj;
  132. },
  133. formatTime(date) {
  134. var year = date.getFullYear()
  135. var month = date.getMonth() + 1
  136. var day = date.getDate()
  137. var hour = date.getHours()
  138. var minute = date.getMinutes()
  139. var second = date.getSeconds()
  140. return [year, month, day].map(this.formatNumber).join('-') + ' ' + [hour, minute, second].map(this
  141. .formatNumber).join(
  142. ':')
  143. },
  144. formatNumber(n) {
  145. n = n.toString()
  146. return n[1] ? n : '0' + n
  147. },
  148. trackRequest(para, app = null) {
  149. if ((para.type && para.type.includes('Error'))) {
  150. //所有报错埋点以及曝光埋点不再发送至服务器
  151. return
  152. }
  153. try {
  154. var session = Number(getStorage('sessionNumber')) + 1;
  155. let data = {
  156. session: '',
  157. userAgent: navigator.userAgent.substring(0, 255) || '',
  158. browserName: navigator.appName || '',
  159. browserVersion: navigator.appVersion.substring(0, 255) || '',
  160. platform: location.href.indexOf("shareSign") > -1 ? 'h5' : 'tfb', //iframeUrl代表是顾问分享的外链
  161. fromPlatform: util.getUrlParams(location.href).fromPlatform || util.getUrlParams(location.href)
  162. .fromProduce || 'tfb',
  163. deviceType: '' || "",
  164. ip: window.ip || '',
  165. cookieId: from_cookie || getStorage("cookie_id") || '',
  166. openId: util.getUrlParams(location.href).openid,
  167. userId: util.getUrlParams(location.href).leavePhoneCustomerId || '',
  168. brandUserId: util.getUrlParams(location.href).leavePhoneCustomerId || '',
  169. createTime: this.formatTime(new Date()),
  170. uploadTime: this.formatTime(new Date()),
  171. product: location.href.indexOf("shareSign") > -1 ? 'h5' : 'tfb', //iframeUrl代表是顾问分享的外链
  172. project: para.project,
  173. brandId: util.getUrlParams(location.href).special_ID || util.getUrlParams(location.href)
  174. .brandId || null,
  175. // expand:typeof para.expand==='object'?JSON.stringify(para.expand):para.expand,//扩展字段
  176. expand: JSON.stringify(util.getUrlParams(location.href)), //扩展字段
  177. imTalkId: para.imTalkId || '', //IM对话编号
  178. imTalkType: para.imTalkType || '', //IM对话类型
  179. eventModuleDes: para.eventModuleDes || '', //模块描述信息
  180. eventInnerModuleId: para.eventInnerModuleId || '', //事件内部模块信息
  181. eventName: para.eventName || '', //事件名称
  182. eventId: para.eventId || '', //埋点ID
  183. adviserId: para.adviserId || '', //顾问id
  184. clkDesPage: para.clkDesPage || '', //点击前往的页面名称
  185. clkId: para.clkId || '', //点击ID
  186. clkName: para.clkName || '',
  187. pvId: para.pvId || '', //PV埋点ID
  188. clkParams: typeof para.clkParams === 'object' ? JSON.stringify(para.clkParams) : para
  189. .clkParams, //点击参数
  190. pvPageStayTime: para.pvPageStayTime || '',
  191. pvCurPageName: para.pvCurPageName || '', //当前页面名称
  192. pvCurPageParams: typeof para.pvCurPageParams === 'object' ? JSON.stringify(para
  193. .pvCurPageParams) : para.pvCurPageParams ||
  194. '', //当前页面参数
  195. pvLastPageName: para.pvLastPageName || '', //上一页页面名称
  196. pvLastPageParams: para.pvLastPageParams || '', //上一页页面参数
  197. pvPageLoadTime: para.pvPageLoadTime || '', //加载时间
  198. type: para.type || '', //埋点类型
  199. }
  200. let timeNow = new Date().getTime();
  201. if (timeNow - sessionTime > 180000 && !from_session) {
  202. // session++;
  203. getStorage('sessionNumber', session)
  204. }
  205. session = Number(getStorage('sessionNumber')) + 1;
  206. data.session = from_session || getStorage("cookie_id") + "_" + session || '';
  207. sessionTime = timeNow
  208. console.log(data.session)
  209. // return data;
  210. // app.globalData.session_id = data.session
  211. // app.globalData.sessionTime = timeNow;
  212. // requestConfig('upload', data, true);
  213. // let param = ["SEND" +
  214. // "\nproject:" + "elab-marketing-system" +
  215. // "\nmethod:" + 'POST' +
  216. // "\npath:" + '/behavior/brandMiniWeb/upload' +
  217. // "\ndestination:" + '/ws/remote/invoke' +
  218. // "\n\n" + JSON.stringify(data) +
  219. // "\u0000"
  220. // ];
  221. // app.wsSendOrder(param,data);//socket 消息发送
  222. console.warn("***mook***", (data.pvId || data.clkId || data.eventId), data.reserve3)
  223. } catch (e) {
  224. console.warn("***util.js-onError***", e);
  225. }
  226. },
  227. getSession() { //获取session
  228. let timeNow = new Date().getTime();
  229. let session = uni.getStorageSync('sessionNumber') || timeNow; //session具体的值
  230. uni.setStorage({
  231. key: "sessionNumber",
  232. data: session
  233. })
  234. return session;
  235. },
  236. };
  237. window.from_session = util.getUrlParams(location.href).session || '';
  238. window.from_cookie = util.getUrlParams(location.href).cookie || '';
  239. export default util;
  240. // module.exports = util;