utils.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. /**
  2. * utils.js
  3. * 各类js工具函数
  4. */
  5. /*
  6. * 时间格式化函数(将时间格式化为,2019年08月12日,2019-08-12,2019/08/12的形式)
  7. *
  8. *
  9. * pattern参数(想要什么格式的数据就传入什么格式的数据)
  10. * · 'yyyy-MM-dd' ---> 输出如2019-09-20
  11. * · 'yyyy-MM-dd hh:mm' ---> 输出如2019-09-20 08:20
  12. * · 'yyyy-MM-dd HH:mm:ss' ---> 输出如2019-09-20 18:20:23
  13. * · 'yyyy/MM/dd' ---> 输出如2019/09/20
  14. * · 'yyyy年MM月dd日' ---> 输出如2019年09月20日
  15. * · 'yyyy年MM月dd日 hh时mm分' ---> 输出如2019年09月20日 08时20分
  16. * · 'yyyy年MM月dd日 hh时mm分ss秒' ---> 输出如2019年09月20日 08时20分23秒
  17. * · 'yyyy年MM月dd日 hh时mm分ss秒 EE' ---> 输出如2019年09月20日 08时20分23秒 周二
  18. * · 'yyyy年MM月dd日 hh时mm分ss秒 EEE' ---> 输出如2019年09月20日 08时20分23秒 星期二
  19. *
  20. * 参考: https://www.cnblogs.com/mr-wuxiansheng/p/6296646.html
  21. */
  22. Date.prototype.pattern = function (fmt) {
  23. if (!fmt) return ''
  24. var o = {
  25. 'y+': this.getFullYear(),
  26. 'M+': this.getMonth() + 1, // 月份
  27. 'd+': this.getDate(), // 日
  28. // 12小时制
  29. 'h+': this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, // 小时
  30. // 24小时制
  31. 'H+': this.getHours(), // 小时
  32. 'm+': this.getMinutes(), // 分
  33. 's+': this.getSeconds(), // 秒
  34. 'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
  35. 'S': this.getMilliseconds(), // 毫秒
  36. 'E+': this.getDay(), // 周
  37. };
  38. var week = {
  39. '0': '日',
  40. '1': '一',
  41. '2': '二',
  42. '3': '三',
  43. '4': '四',
  44. '5': '五',
  45. '6': '六'
  46. };
  47. if (/(y+)/.test(fmt)) {
  48. fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
  49. }
  50. if (/(E+)/.test(fmt)) {
  51. fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '星期' : '周') : '') + week[
  52. this.getDay() + '']);
  53. }
  54. for (var k in o) {
  55. if (new RegExp('(' + k + ')').test(fmt)) {
  56. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k])
  57. .length)));
  58. }
  59. }
  60. return fmt;
  61. }
  62. /* 挂载到Number原型上,格式化小数用tofixed会造成位数不够补零的情况(处理不让自动补零)
  63. 栗子: 不是number类型的需要转一下
  64. Number('').fixed()
  65. Number('213412DDD').fixed()
  66. 1234.152354.fixed()
  67. 1234.152354.fixed(2)
  68. 1234.152354.fixed(10)
  69. */
  70. Number.prototype.fixed = function(len){
  71. len = isNaN(len)? 0 : len
  72. var num = Math.pow(10,len)
  73. return Math.round(this*num) / num
  74. }
  75. var popupTip = {
  76. setAlert: function (obj) {
  77. var html = `<p class="shiyong_text">试用功能数额已超限,请购买剑鱼大会员正式版</p><button onClick="location.href='/jyapp/big/page/landingPage'" class="shiyong_buy">立即购买</button>`
  78. obj.$dialog.alert({
  79. className: 'shiyong_common',
  80. title: '温馨提醒',
  81. message: html,
  82. theme: 'round-button',
  83. confirmButtonText:'取消,暂不使用',
  84. confirmButtonColor:'#2ABED1'
  85. }).then(() => {
  86. // on close
  87. });
  88. }
  89. }
  90. var versionChangeLog = {
  91. '0.0.1': ['init'],
  92. '0.0.2': ['utils.dateFromNow 函数逻辑修改,具体修改内容查看函数注释'],
  93. '0.0.3': ['utils.formatMoney 新增不使用,分割返回值的选项'],
  94. '0.0.4': ['utils.checkNowInAppTabbarPage 判断是否app tabbar 页面,返回对应的key'],
  95. '0.0.5': ['新增utils.$env, utils.$envs 等变量'],
  96. }
  97. if (!window.utilsEnv) {
  98. // $env, $envs的辅助函数
  99. window.utilsEnv = {
  100. // 获取当前是android或者ios
  101. androidOrIOS: function () {
  102. var u = navigator.userAgent.toLowerCase()
  103. var agent = null
  104. if (/iphone|ipod|ipad|ios/.test(u)) {
  105. agent = 'ios'
  106. } else if (/(Android)/i.test(u)) {
  107. agent = 'android'
  108. }
  109. return agent
  110. },
  111. getIsH5HostName: function () {
  112. var hostname = location.hostname.toLowerCase()
  113. return hostname.indexOf('h5') !== -1
  114. },
  115. // 是否是在app里面运行
  116. getIsInTheAppContainer: function () {
  117. // 判断是否在app环境下
  118. var inApp = false
  119. var u = navigator.userAgent.toLowerCase()
  120. if (u.indexOf('jianyuapp') !== -1) {
  121. inApp = true
  122. return inApp
  123. }
  124. try {
  125. if (this.androidOrIOS() === 'ios') {
  126. var iniOSApp = typeof window.webkit.messageHandlers.skipAppointTab.postMessage === 'function'
  127. inApp = iniOSApp
  128. } else {
  129. var inAndroidApp = typeof window.JyObj !== 'undefined'
  130. inApp = inAndroidApp
  131. }
  132. } catch (e) {
  133. console.warn(e)
  134. inApp = false
  135. }
  136. return inApp
  137. },
  138. getPlatformEnvs: function () {
  139. var inWX = navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1
  140. var inApp = this.getIsInTheAppContainer()
  141. var platformOS = this.androidOrIOS()
  142. var getPlatform = function () {
  143. var h5host = window.utilsEnv.getIsH5HostName()
  144. if (inApp) {
  145. return 'app'
  146. } else if (h5host) {
  147. return 'h5'
  148. } else if (inWX) {
  149. return 'wx'
  150. } else {
  151. return 'h5'
  152. }
  153. }
  154. var platform = getPlatform()
  155. return {
  156. platformOS: platformOS,
  157. platform: platform,
  158. inApp: inApp,
  159. inWX: inWX
  160. }
  161. }
  162. }
  163. utilsEnv.platformEnvs = utilsEnv.getPlatformEnvs()
  164. }
  165. var utils = {
  166. // utils版本号
  167. version: '0.0.5',
  168. $env: {
  169. appVersion: '', // app版本号
  170. platformOS: window.utilsEnv.platformEnvs.platformOS, // android或者ios
  171. platform: window.utilsEnv.platformEnvs.platform, // wx/app/h5
  172. },
  173. $envs: {
  174. inWX: window.utilsEnv.platformEnvs.platform === 'wx', // 是否在微信中
  175. inH5: window.utilsEnv.platformEnvs.platform === 'h5', // 是否在h5中
  176. inApp: window.utilsEnv.platformEnvs.inApp, // 是否在app中
  177. inAndroid: window.utilsEnv.platformEnvs.platformOS === 'android', // 是否在android中
  178. inIOS: window.utilsEnv.platformEnvs.platformOS === 'ios', // 是否在android中
  179. },
  180. isWeiXinBrowser: navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1,
  181. isAndroid: navigator.userAgent.toLowerCase().indexOf('android') !== -1,
  182. isIos: /iphone|ipod|ipad|ios/.test(navigator.userAgent.toLowerCase()),
  183. // 传入你要获取的参数的名字
  184. getParam: function (name) {
  185. var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  186. var r = window.location.search.substr(1).match(reg); //获取url中'?'符后的字符串并正则匹配
  187. var context = '';
  188. if (r != null) context = r[2];
  189. // 释放变量
  190. reg = null;
  191. r = null;
  192. return context == null || context == '' || context == 'undefined' ? '' : context;
  193. },
  194. // 移除url参数,但是不刷新页面
  195. delUrlParam: function (name){
  196. var href = window.location.href;
  197. const e = eval(`/&?${name}=[^&#]*/g`);
  198. href = href.replace(e, '');
  199. history.replaceState('','',href);// replaceState方法不会触发页面刷新,只是导致history对象发生变化,地址栏
  200. },
  201. // 取[m, n]随机数
  202. getRandomNumber: function (min, max) {
  203. return Math.floor(Math.random() * (max - min + 1) + min);
  204. },
  205. // 获取唯一的uuid
  206. // https://www.kancloud.cn/ifeng/js100/622666
  207. getRandomUuid: function () {
  208. var s = [];
  209. var hexDigits = "0123456789abcdef";
  210. for (var i = 0; i < 36; i++) {
  211. s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  212. }
  213. s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  214. s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  215. s[8] = s[13] = s[18] = s[23] = "-";
  216. var uuid = s.join("");
  217. return uuid;
  218. },
  219. // 获取随机字符串
  220. // 不传参数则获取长度不固定的字符串
  221. getRandomString: function (len) {
  222. var randomString = '';
  223. if (len) {
  224. /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
  225. var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
  226. var maxPos = $chars.length;
  227. for (i = 0; i < len; i++) {
  228. randomString += $chars.charAt(Math.floor(Math.random() * maxPos));
  229. }
  230. } else {
  231. // Math.random() 生成随机数字, eg: 0.123456
  232. // .toString(36) 转化成36进制 : "0.4fzyo82mvyr"
  233. // .substring(2) 去掉前面两位 : "yo82mvyr"
  234. // .slice(-8) 截取最后八位 : "yo82mvyr"
  235. randomString = Math.random().toString(36).substring(2)
  236. }
  237. return randomString;
  238. },
  239. // FROM: https://www.jianshu.com/p/90ed8b728975
  240. // 比较两个对象是否相等
  241. // 返回true为相等,返回false为不相等
  242. deepCompare: function (x, y) {
  243. var i, l, leftChain, rightChain;
  244. function compare2Objects(x, y) {
  245. var p;
  246. // remember that NaN === NaN returns false
  247. // and isNaN(undefined) returns true
  248. if (isNaN(x) && isNaN(y) && typeof x === 'number' && typeof y === 'number') {
  249. return true;
  250. }
  251. // Compare primitives and functions.
  252. // Check if both arguments link to the same object.
  253. // Especially useful on the step where we compare prototypes
  254. if (x === y) {
  255. return true;
  256. }
  257. // Works in case when functions are created in constructor.
  258. // Comparing dates is a common scenario. Another built-ins?
  259. // We can even handle functions passed across iframes
  260. if ((typeof x === 'function' && typeof y === 'function') ||
  261. (x instanceof Date && y instanceof Date) ||
  262. (x instanceof RegExp && y instanceof RegExp) ||
  263. (x instanceof String && y instanceof String) ||
  264. (x instanceof Number && y instanceof Number)) {
  265. return x.toString() === y.toString();
  266. }
  267. // At last checking prototypes as good as we can
  268. if (!(x instanceof Object && y instanceof Object)) {
  269. return false;
  270. }
  271. if (x.isPrototypeOf(y) || y.isPrototypeOf(x)) {
  272. return false;
  273. }
  274. if (x.constructor !== y.constructor) {
  275. return false;
  276. }
  277. if (x.prototype !== y.prototype) {
  278. return false;
  279. }
  280. // Check for infinitive linking loops
  281. if (leftChain.indexOf(x) > -1 || rightChain.indexOf(y) > -1) {
  282. return false;
  283. }
  284. // Quick checking of one object being a subset of another.
  285. // todo: cache the structure of arguments[0] for performance
  286. for (p in y) {
  287. if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
  288. return false;
  289. } else if (typeof y[p] !== typeof x[p]) {
  290. return false;
  291. }
  292. }
  293. for (p in x) {
  294. if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
  295. return false;
  296. } else if (typeof y[p] !== typeof x[p]) {
  297. return false;
  298. }
  299. switch (typeof (x[p])) {
  300. case 'object':
  301. case 'function':
  302. leftChain.push(x);
  303. rightChain.push(y);
  304. if (!compare2Objects(x[p], y[p])) {
  305. return false;
  306. }
  307. leftChain.pop();
  308. rightChain.pop();
  309. break;
  310. default:
  311. if (x[p] !== y[p]) {
  312. return false;
  313. }
  314. break;
  315. }
  316. }
  317. return true;
  318. }
  319. if (arguments.length < 1) {
  320. return true; //Die silently? Don't know how to handle such case, please help...
  321. // throw "Need two or more arguments to compare";
  322. }
  323. for (i = 1, l = arguments.length; i < l; i++) {
  324. leftChain = []; //Todo: this can be cached
  325. rightChain = [];
  326. if (!compare2Objects(arguments[0], arguments[i])) {
  327. return false;
  328. }
  329. }
  330. return true;
  331. },
  332. // 判断变量是否是数组
  333. isArray: function (o){
  334. return Object.prototype.toString.call(o) == '[object Array]';
  335. },
  336. // 数组对象根据某一个值进行从小到大冒泡排序
  337. // arr 数组
  338. // value 字符串
  339. bSort: function (arr, value) {
  340. var len = arr.length;
  341. for (var i = 0; i < len - 1; i++) {
  342. for (var j = 0; j < len - 1 - i; j++) {
  343. // 相邻元素两两对比,元素交换,大的元素交换到后面
  344. if (arr[j][value] > arr[j + 1][value]) {
  345. var temp = arr[j];
  346. arr[j] = arr[j + 1];
  347. arr[j + 1] = temp;
  348. }
  349. }
  350. }
  351. return arr;
  352. },
  353. // 数组去重
  354. unique: function (arr) {
  355. if (!this.isArray(arr)) {
  356. console.log('type error!')
  357. return
  358. }
  359. var array = [];
  360. for (var i = 0; i < arr.length; i++) {
  361. if (array .indexOf(arr[i]) === -1) {
  362. array .push(arr[i])
  363. }
  364. }
  365. return array
  366. },
  367. // 数组乱序
  368. shuffle: function (arr) {
  369. var targetArr = JSON.parse(JSON.stringify(arr || []))
  370. var hitArr = []
  371. var len = targetArr.length
  372. for (var i = 0; i < len; i++) {
  373. // 获取随机下标
  374. var index = Math.floor(Math.random() * targetArr.length)
  375. hitArr.push(targetArr[index])
  376. targetArr.splice(index, 1)
  377. }
  378. return hitArr.concat(targetArr)
  379. },
  380. // 解决ios返回不刷新页面的问题
  381. iosBackRefresh: function () {
  382. var isPageHide = false;
  383. let superSearch_iosNoRefresh = sessionStorage.getItem('superSearch_iosNoRefresh')
  384. window.addEventListener('pageshow', function () {
  385. if (isPageHide) {
  386. // ios从三级页返回不刷新页面
  387. if(!superSearch_iosNoRefresh) {
  388. location.reload();
  389. } else {
  390. sessionStorage.removeItem('superSearch_iosNoRefresh')
  391. }
  392. } else {
  393. if(superSearch_iosNoRefresh) {
  394. sessionStorage.removeItem('superSearch_iosNoRefresh')
  395. }
  396. }
  397. });
  398. window.addEventListener('pagehide', function () {
  399. isPageHide = true;
  400. });
  401. },
  402. // ios返回调用函数
  403. iosBackInvoke: function (callback) {
  404. var isPageHide = false;
  405. window.addEventListener('pageshow', function () {
  406. if (isPageHide) {
  407. callback && callback();
  408. }
  409. });
  410. window.addEventListener('pagehide', function () {
  411. isPageHide = true;
  412. });
  413. },
  414. /**
  415. * 格式化金钱的函数
  416. * @param {number} s 金额必传
  417. * @param {int:0-100} n 保留小数的位数(int:0-100)
  418. * @param {Boolean} withoutComma 传true则表示不使用,分割返回值
  419. */
  420. formatMoney: function (s, n, withoutComma) {
  421. // 如果不传s或者s为空,则直接返回0
  422. if (!s) return 0
  423. if (n === undefined || n === null) {
  424. n = -1
  425. } else {
  426. n = n > 0 && n <= 20 ? n : 2;
  427. }
  428. var intS = parseInt(s)
  429. var point = '.'
  430. var left;
  431. var right;
  432. s = parseFloat((s + '').replace(/[^\d\.-]/g, ''))
  433. // 没传n,默认(如果为整数,则不保留小数。如果为浮点数,则保留两位小数)
  434. if (n === -1) {
  435. if (s === intS) {
  436. n = 0
  437. right = ''
  438. point = ''
  439. } else {
  440. n = 2
  441. s = s.toFixed(n);
  442. right = s.split('.')[1];
  443. }
  444. s = s + ''
  445. left = s.split('.')[0].split('').reverse();
  446. } else {
  447. s = parseFloat((s + '').replace(/[^\d\.-]/g, '')).toFixed(n) + '';
  448. left = s.split('.')[0].split('').reverse();
  449. right = s.split('.')[1];
  450. }
  451. // 默认进行,拼接
  452. if (!withoutComma) {
  453. t = '';
  454. for (i = 0; i < left.length; i++) {
  455. t += left[i] + ((i + 1) % 3 == 0 && (i + 1) != left.length ? ',' : '');
  456. }
  457. return t.split('').reverse().join('') + point + right;
  458. }
  459. return left.reverse().join('') + point + right;;
  460. },
  461. // 金额大写,链接:https://juejin.im/post/5a2a7a5051882535cd4abfce
  462. // upDigit(1682) result:"人民币壹仟陆佰捌拾贰元整"
  463. // upDigit(-1693) result:"欠壹仟陆佰玖拾叁元整"
  464. upPrice: function (n) {
  465. var fraction = ['角', '分', '厘']
  466. var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
  467. var unit = [
  468. ['元', '万', '亿'],
  469. ['', '拾', '佰', '仟']
  470. ]
  471. // var head = n < 0 ? '欠人民币' : '人民币'
  472. var head = ''
  473. n = Math.abs(n)
  474. var s = ''
  475. for (var i = 0; i < fraction.length; i++) {
  476. s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '')
  477. }
  478. s = s || '整'
  479. n = Math.floor(n)
  480. for (var i = 0; i < unit[0].length && n > 0; i++) {
  481. var p = ''
  482. for (var j = 0; j < unit[1].length && n > 0; j++) {
  483. p = digit[n % 10] + unit[1][j] + p
  484. n = Math.floor(n / 10)
  485. }
  486. s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s
  487. // s = p + unit[0][i] + s;
  488. }
  489. return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整')
  490. },
  491. // 手机号中间4位加*
  492. addConfusionForTel: function (tel) {
  493. var reg = /^(\d{3})\d{4}(\d{4})$/
  494. return tel.replace(reg, '$1****$2')
  495. },
  496. // 手机号加空格
  497. addSpaceForTel: function (tel) {
  498. var regMap = {
  499. isConfuse: /^(\d{3})\*{4}(\d{4})$/,
  500. addSpace: /^(\d{3})(\d{4})(\d{4})$/
  501. }
  502. var confusion = regMap.isConfuse.test(tel)
  503. if (confusion) {
  504. return tel.replace(regMap.isConfuse, '$1 **** $2')
  505. } else {
  506. return tel.replace(regMap.addSpace, '$1 $2 $3')
  507. }
  508. },
  509. /**
  510. * 时间戳转换 多少秒、多少分、多少小时前 昨天以及昨天之前的显示年月日(今年的时间不显示年份)
  511. * @param {Date} originTime 要转换的时间对象(或者时间戳)
  512. */
  513. dateFromNow: function (originTime, useOld) {
  514. if (!originTime) return
  515. // 原始时间 - 传入的时间戳
  516. var originTimeStamp = +new Date(originTime)
  517. // 当前时间戳
  518. var nowTimeStamp = +new Date()
  519. // 时间戳相差多少
  520. var diffTimeStamp = nowTimeStamp - originTimeStamp
  521. var postfix = diffTimeStamp > 0 ? '前' : '后'
  522. // 求绝对值 ms(毫秒)
  523. var diffTimeStampAbsMs = Math.abs(diffTimeStamp)
  524. var diffTimeStampAbsS = Math.round(diffTimeStampAbsMs / 1000)
  525. // 10天的秒数
  526. var days11 = 11 * 24 * 60 * 60
  527. // var dataMap = {
  528. // zh: ['年', '个月', '星期', '天', '小时', '分钟', '秒'],
  529. // number: [365*24*60*60, 30*24*60*60, 7*24*60*60, 24*60*60, 60*60, 60, 1]
  530. // }
  531. var dataMap = {
  532. zh: ['天', '小时', '分钟', '秒'],
  533. number: [24 * 60 * 60, 60 * 60, 60, 1]
  534. }
  535. var timeString = ''
  536. // 10天前
  537. var tenDaysAgo = diffTimeStampAbsS > days11
  538. // 是否是当天
  539. var isCurrentDay = new Date(originTimeStamp).pattern('yyyy.MM.dd') === new Date().pattern('yyyy.MM.dd')
  540. var condition = !isCurrentDay
  541. if (useOld) {
  542. condition = tenDaysAgo
  543. }
  544. if (condition) {
  545. // 不是当天,则使用正常日期显示
  546. var originDate = new Date(originTimeStamp)
  547. var nowDate = new Date()
  548. // 是否同年
  549. var sameYear = originDate.getFullYear() === nowDate.getFullYear()
  550. // 如果是当年,则不显示年
  551. var patternString = sameYear ? 'MM-dd' : 'yyyy-MM-dd'
  552. timeString = originDate.pattern(patternString)
  553. } else {
  554. for (var i = 0; i < dataMap.number.length; i++) {
  555. var inm = Math.floor(diffTimeStampAbsS / dataMap.number[i])
  556. if (inm != 0) {
  557. timeString = inm + dataMap.zh[i] + postfix
  558. break
  559. }
  560. }
  561. }
  562. return timeString
  563. },
  564. /**
  565. * 输入框获取焦点,隐藏底部固定按钮方法
  566. * @param {Array} inputs 传入输入框的DOM对象
  567. * @param {Object} footer 按键弹起要隐藏的footer的 DOM对象
  568. */
  569. inputFocusHideFooter: function (inputs, footer) {
  570. var isShowBtn = function (f) {
  571. if (f) {
  572. footer.style.display = ''
  573. } else {
  574. footer.style.display = 'none'
  575. }
  576. }
  577. // ios监听输入框的软键盘弹起和收起事件
  578. if (this.isIos) {
  579. inputs.forEach(function (item) {
  580. item.addEventListener('focus', function () {
  581. console.log('IOS 键盘弹出')
  582. // IOS 键盘弹起后操作
  583. isShowBtn(false)
  584. }, false)
  585. // IOS 键盘收起:IOS 点击输入框以外区域或点击收起按钮,输入框都会失去焦点,键盘会收起,
  586. item.addEventListener('blur', function () {
  587. console.log('IOS 键盘收起')
  588. // IOS 键盘收起后操作
  589. isShowBtn(true)
  590. })
  591. })
  592. }
  593. // Android 键盘收起:Android 键盘弹起或收起页面高度会发生变化,以此为依据获知键盘收起
  594. if (this.isAndroid) {
  595. var clientHeight = document.documentElement.clientHeight || document.body.clientHeight
  596. window.addEventListener('resize', function () {
  597. var nowClientHeight = document.documentElement.clientHeight || document.body.clientHeight
  598. if (clientHeight > nowClientHeight) {
  599. // 键盘弹出的事件处理
  600. console.log('Android 键盘弹出')
  601. isShowBtn(false)
  602. } else {
  603. console.log('Android 键盘收起')
  604. // 键盘收起的事件处理
  605. isShowBtn(true)
  606. }
  607. }, false)
  608. }
  609. },
  610. /**
  611. * 通用关键字高亮替换
  612. * @param {String} value 要高亮的字符串
  613. * @param {String|Array} oldChar 要被替换的字符串(或数组)
  614. * @param {String|Array} newChar 要替换成的字符串(或数组)
  615. *
  616. * 比如:要将 - `剑鱼标讯工具函数` 字符串中的 `工具` 高亮
  617. * 则此时 value -> `剑鱼标讯工具函数`
  618. * oldChar -> `工具`
  619. * newChar -> `<span class="highlight-text">工具</span>`
  620. *
  621. * 批量高亮-----
  622. * 比如:要将 - `剑鱼标讯工具函数` 字符串中的 `工具` `剑鱼` 高亮
  623. * 则此时 value -> `剑鱼标讯工具函数批量高亮`
  624. * oldChar -> ['工具', '剑鱼']
  625. * newChar -> ['<span class="highlight-text">', '</span>']
  626. *
  627. * 注意:此时newChar为一个长度为2的数组,数组中为高亮标签的起始标签和结束标签
  628. *
  629. */
  630. replaceKeyword: function (value, oldChar, newChar) {
  631. var _this = this
  632. if (!oldChar || !newChar) return value
  633. // oldChar的字符串数组,用来循环替换
  634. var oldCharArr = []
  635. if (this.isArray(oldChar)) {
  636. oldCharArr = oldChar.concat()
  637. } else {
  638. oldCharArr.push(oldChar)
  639. }
  640. // 数组去重
  641. oldCharArr = this.unique(oldCharArr)
  642. try {
  643. oldCharArr.forEach(function (item) {
  644. // 去空格之后为空字符串,则直接跳过当前替换
  645. if (item.replace(/\s+/g, '')) {
  646. var oc = item
  647. oc = oc.replace(/\$/g, '\\$')
  648. .replace(/\(/g, '\\(')
  649. .replace(/\)/g, '\\)')
  650. .replace(/\*/g, '\\*')
  651. .replace(/\+/g, '\\+')
  652. .replace(/\./g, '\\.')
  653. .replace(/\[/g, '\\[')
  654. .replace(/\]/g, '\\]')
  655. .replace(/\?/g, '\\?')
  656. .replace(/\\/g, '\\')
  657. .replace(/\//g, '\\/')
  658. .replace(/\^/g, '\\^')
  659. .replace(/\{/g, '\\{')
  660. .replace(/\}/g, '\\}')
  661. .replace(/\|/g, '\\|')
  662. if (_this.isArray(newChar)) {
  663. // 批量高亮
  664. var tempV = value
  665. value = value.replace(new RegExp('(' + oc + ')', 'gmi'), newChar[0] + oc + newChar[1])
  666. if (value === tempV && oc.indexOf('+') !== -1) {
  667. var splitReg = oc.split('\\+')
  668. splitReg.map(function (v) {
  669. value = value.replace(new RegExp('('+v+')', 'gmi'), newChar[0] + v + newChar[1])
  670. })
  671. }
  672. } else {
  673. // 普通单个高亮
  674. value = value.replace(new RegExp('(' + oc + ')', 'gmi'), newChar)
  675. }
  676. }
  677. })
  678. } catch (e) {
  679. console.log(e)
  680. return value
  681. }
  682. return value
  683. },
  684. /**
  685. * 批量删除sessionStorage中的数据
  686. * @param {RegExp} reg 传入一个要匹配的正则表达式
  687. */
  688. clearBatchSessionStorage: function (reg) {
  689. if (sessionStorage) {
  690. var sessKeys = Object.keys(sessionStorage)
  691. sessKeys.forEach(function (item) {
  692. if (reg.test(item)) {
  693. sessionStorage.removeItem(item)
  694. }
  695. })
  696. }
  697. },
  698. /**
  699. * 获取图片原始宽高
  700. * @param {Image/Object} img
  701. * @param {Function} callback
  702. */
  703. getImgNaturalDimensions: function (img, callback) {
  704. var naturalSize = {}
  705. if (img.naturalWidth && img.naturalHeight) { // 现代浏览器
  706. naturalSize.with = img.naturalWidth
  707. naturalSize.height = img.naturalHeight
  708. } else { // IE6/7/8
  709. var image = new Image()
  710. image.src = img.src
  711. image.onload = function() {
  712. naturalSize.with = image.width
  713. naturalSize.height = image.height
  714. callback && callback(naturalSize)
  715. }
  716. }
  717. return naturalSize
  718. },
  719. goToCustomerPage: function () {
  720. if (this.isWeiXinBrowser) {
  721. location.href = '/big/wx/page/customer'
  722. } else {
  723. location.href = '/jyapp/free/customer'
  724. }
  725. },
  726. moneyUnit: function (num, type, lv) {
  727. if (num === '' || num == null || num == undefined || isNaN(num)) return ''
  728. var levelArr = ['元', '万元', '亿元', '万亿元']
  729. if (type === void 0) {
  730. type = 'string';
  731. }
  732. if (lv === void 0) {
  733. lv = 0;
  734. }
  735. if (num === 0) {
  736. if (type === 'string') {
  737. return '0';
  738. }
  739. if (type === 'lv') {
  740. return levelArr[lv];
  741. }
  742. if (type === 'number') {
  743. return 0;
  744. }
  745. if (type === 'index') {
  746. return lv;
  747. }
  748. }
  749. // 需要传入固定的lv(此时lv为 levelArr 中的一个)
  750. function transfer (num, lvString) {
  751. var index = levelArr.indexOf(lvString)
  752. if (index === -1 || index === 0) {
  753. return num
  754. } else {
  755. return (num / Math.pow(10000, index)).toFixed(2) + lvString
  756. }
  757. }
  758. if (type === 'transfer') {
  759. return transfer(num, lv)
  760. }
  761. var result = num / Math.pow(10000, lv);
  762. if (result > 10000 && lv < 2) {
  763. return this.moneyUnit(num, type, lv + 1)
  764. } else {
  765. if (type === 'string') {
  766. return String(Math.floor(result * 100) / 100).replace('.00', '') + levelArr[lv];
  767. }
  768. if (type === 'lv') {
  769. return levelArr[lv];
  770. }
  771. if (type === 'index') {
  772. return lv;
  773. }
  774. if (type === 'number') {
  775. return String(Math.floor(result * 100) / 100).replace('.00', '');
  776. }
  777. }
  778. },
  779. /**
  780. * 防抖
  781. * @param {Function} func 函数
  782. * @param {Number} delay 延时间隔 默认 200ms
  783. * @param {Boolean} immediate 是否首次执行
  784. */
  785. debounce: function (func, delay, immediate){
  786. var timer = null;
  787. delay = delay || 200
  788. return function() {
  789. var context = this;
  790. var args = arguments;
  791. if(timer) clearTimeout(timer);
  792. if(immediate){
  793. var doNow = !timer;
  794. timer = setTimeout(function(){
  795. timer = null;
  796. },delay);
  797. if(doNow){
  798. func.apply(context,args);
  799. }
  800. }else{
  801. timer = setTimeout(function(){
  802. func.apply(context,args);
  803. },delay);
  804. }
  805. }
  806. },
  807. /**
  808. * 检查是否 app tabbar页面,并返回对应的key || ''
  809. * @param link - 需要检查的地址 || location.href
  810. * @returns {string} - 对应的key
  811. */
  812. checkNowInAppTabbarPage: function (link) {
  813. const appTabbars = {
  814. search: ['/jyapp/jylab/mainSearch', '/jy_mobile/tabbar/home'],
  815. subscribe: ['/jyapp/swordfish/historypush', '/jy_mobile/tabbar/subscribe'],
  816. message: ['/jyapp/frontPage/messageCenter/sess/index'],
  817. box: ['/jyapp/jylab/index', '/jy_mobile/tabbar/box'],
  818. me: ['/jyapp/free/me']
  819. }
  820. let appTabbarName = ''
  821. for (const appTabbarsKey in appTabbars) {
  822. const hasTabbar = appTabbars[appTabbarsKey].some(function (v) {
  823. return (link || location.href).indexOf(v) > -1
  824. })
  825. if (hasTabbar) {
  826. appTabbarName = appTabbarsKey
  827. break
  828. }
  829. }
  830. return appTabbarName
  831. },
  832. // 部分h5浏览器(华为、荣耀)页面返回不刷新兼容
  833. fixSomeH5BackRefresh () {
  834. // 如果全局有,则说明已经被调用过,就不需要再次调用
  835. if (window.fixSomeH5BackRefresh && typeof window.fixSomeH5BackRefresh) return
  836. if (utilsEnv.platformEnvs.platform !== 'h5') return
  837. const ua = navigator.userAgent.toLowerCase()
  838. // 判断是不是华为/荣耀浏览器
  839. const huawei = ua.includes('huawei') || ua.includes('honor')
  840. if (huawei) {
  841. window.addEventListener('visibilitychange', function () {
  842. const v = document.visibilityState
  843. if (v === 'hidden') {
  844. // do something
  845. } else if (v === 'visible') {
  846. location.reload()
  847. }
  848. })
  849. }
  850. },
  851. }
  852. Object.defineProperty(utils.$env, 'appVersion', {
  853. get () {
  854. var version
  855. try {
  856. version = JyObj.getVersion()
  857. } catch (e) {
  858. console.warn('error: get app version failed')
  859. }
  860. return version
  861. }
  862. })
  863. // h5头部公共样式
  864. if (utils.$envs.inH5) {
  865. $('body').addClass('h5-body')
  866. $('.jy-app-header').addClass('h5-header')
  867. $('.childNav').addClass('h5-tabfix')
  868. $('.share1 .app-layout-content-b').css('top', '10.33333vw')
  869. } else if (utils.$envs.inWX) {
  870. $('body').addClass('wx-body')
  871. $('.jy-app-header').addClass('wx-header')
  872. $('.childNav').addClass('wx-tabfix')
  873. $('.share1 .app-layout-content-b').css('top', '10.33333vw')
  874. }
  875. utils.fixSomeH5BackRefresh()