public.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. const md = {
  2. empty:(val)=>{
  3. if(val.length==0||val.replace(/\s/g, "").length==0||!val){
  4. return true
  5. }
  6. // if(isNull(val)||typeof val=='object'&&Object.keys(val).length === 0|| typeof val != "undefined" ){
  7. // }
  8. },
  9. /**
  10. * @removeSpace 检查图片是否存在
  11. * imgUrl 图片链接
  12. */
  13. checkImg: (imgUrl) => {
  14. if(imgUrl.length<0){
  15. return false
  16. }
  17. var ImgObj = new Image();
  18. ImgObj.src = imgUrl;
  19. console.log(ImgObj,(ImgObj.width > 0 && ImgObj.height > 0))
  20. if (ImgObj.fileSize > 0 || (ImgObj.width > 0 && ImgObj.height > 0)) {
  21. return true;
  22. } else {
  23. return false;
  24. }
  25. },
  26. /**
  27. * @removeSpace 去空格
  28. * value 需要清除空格的字符串
  29. */
  30. removeSpace: (value) => {
  31. return value.replace(/\s+/g, "");
  32. },
  33. /**
  34. * @dedupe 数组去重
  35. * array 数组
  36. */
  37. dedupe: (array) => {
  38. return Array.from(new Set(array))
  39. },
  40. /**
  41. * @dedupe 对象去重
  42. * obj 对象
  43. */
  44. dedupeObject: (obj) => {
  45. let uniques = [];
  46. let stringify = {};
  47. for (let i = 0; i < obj.length; i++) {
  48. let keys = Object.keys(obj[i]);
  49. keys.sort(function(a, b) {
  50. return (Number(a) - Number(b));
  51. });
  52. let str = '';
  53. for (let j = 0; j < keys.length; j++) {
  54. str += JSON.stringify(keys[j]);
  55. str += JSON.stringify(obj[i][keys[j]]);
  56. }
  57. if (!stringify.hasOwnProperty(str)) {
  58. uniques.push(obj[i]);
  59. stringify[str] = true;
  60. }
  61. }
  62. uniques = uniques;
  63. return uniques;
  64. },
  65. /**@unique
  66. * 去重
  67. * arr 数组
  68. */
  69. unique: (arr) => {
  70. if (Array.hasOwnProperty('from')) {
  71. return Array.from(new Set(arr));
  72. } else {
  73. var n = {},
  74. r = [];
  75. for (var i = 0; i < arr.length; i++) {
  76. if (!n[arr[i]]) {
  77. n[arr[i]] = true;
  78. r.push(arr[i]);
  79. }
  80. }
  81. return r;
  82. }
  83. },
  84. /**
  85. * @getProperty 获取&设置元素属性
  86. * el html元素
  87. * name html元素属性名称
  88. * val html元素属性名称的值
  89. */
  90. getProperty: (el, name, val) => {
  91. const prefix = 'data-'
  92. name = prefix + name
  93. if (val) {
  94. return el.setAttribute(name, val)
  95. } else {
  96. return el.getAttribute(name)
  97. }
  98. },
  99. /**
  100. * @hasClass 判断是否已存在class
  101. * el html元素
  102. * className html class名称
  103. */
  104. hasClass: (el, className) => {
  105. let reg = new RegExp('(^|\\s)' + className + '(\\s|$)')
  106. return reg.test(el.className)
  107. },
  108. /**
  109. * @addClass 添加class
  110. * el html元素
  111. * className html class名称
  112. */
  113. addClass: (el, className) => {
  114. if (this.hasClass(el, className)) {
  115. return
  116. }
  117. let newClass = el.className.split(' ')
  118. newClass.push(className)
  119. el.className = newClass.join(' ')
  120. },
  121. /**
  122. * @getHost 获取域名主机
  123. * url 当前页面地址
  124. * className html class名称
  125. */
  126. getHost: (url) => {
  127. let host = "null";
  128. if (typeof url == "undefined" || null == url) {
  129. url = window.location.href;
  130. }
  131. let regex = /^\w+\:\/\/([^\/]*).*/;
  132. let match = url.match(regex);
  133. if (typeof match != "undefined" && null != match) {
  134. host = match[1];
  135. }
  136. return host;
  137. },
  138. /**
  139. * @cutstr 截取字符串长度多余用....代替
  140. * str 字符串
  141. * len 需要长度
  142. */
  143. cutstr: (str, len) => {
  144. let temp;
  145. let icount = 0;
  146. let patrn = /[^\x00-\xff]/;
  147. let strre = "";
  148. for (let i = 0; i < str.length; i++) {
  149. if (icount < len - 1) {
  150. temp = str.substr(i, 1);
  151. if (patrn.exec(temp) == null) {
  152. icount = icount + 1;
  153. } else {
  154. icount = icount + 2;
  155. }
  156. strre += temp;
  157. } else {
  158. break
  159. }
  160. }
  161. return strre + "...";
  162. },
  163. /**@fun_date
  164. * 获取时间区间
  165. * num 为负数就是日期往前,正数日期往后
  166. */
  167. fun_date: (num) => {
  168. let date1 = new Date();
  169. //今天时间
  170. let time1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate()
  171. let date2 = new Date(date1);
  172. date2.setDate(date1.getDate() + num);
  173. //num是正数表示之后的时间,num负数表示之前的时间,0表示今天
  174. let time2 = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate();
  175. return {
  176. start: time1,
  177. end: time2
  178. };
  179. },
  180. /**@isMobile
  181. * 获取手机;类型
  182. *callback 判断手机&电脑后的操作 type: function
  183. */
  184. isMobile: (callback) => {
  185. let isMobile1 = {
  186. Android: function() {
  187. return navigator.userAgent.match(/Android/i) ? true : false;
  188. },
  189. BlackBerry: function() {
  190. return navigator.userAgent.match(/BlackBerry/i) ? true : false;
  191. },
  192. iOS: function() {
  193. return navigator.userAgent.match(/iPhone|iPad|iPod/i) ? true : false;
  194. },
  195. Windows: function() {
  196. return navigator.userAgent.match(/IEMobile/i) ? true : false;
  197. },
  198. any: function() {
  199. return (isMobile1.Android() || isMobile1.BlackBerry() || isMobile1.iOS() || isMobile1
  200. .Windows());
  201. }
  202. };
  203. callback(isMobile1)
  204. },
  205. /**@decimal
  206. * 增加小数点后
  207. * num 修改数值 type: number
  208. * digit 增加小数后几位 type: number
  209. */
  210. decimal: (num, digit) => {
  211. if (parseInt(num) !== parseFloat(num)) {
  212. return num
  213. }
  214. if (typeof num == 'number') {
  215. num = num || 0;
  216. let f = num.toFixed(digit);
  217. return f
  218. } else {
  219. // console.error('请输入数值')
  220. let f = 0;
  221. return f.toFixed(digit)
  222. }
  223. },
  224. /**@timeTrans
  225. * 时间转换
  226. * time 时间 type: time
  227. * format 时间类型 type: String ('Y/M/D h:m:s')
  228. */
  229. timeTrans: ({
  230. time,
  231. format
  232. }) => {
  233. let timer = time || new Date();
  234. let date = new Date(timer);
  235. let obj = {
  236. Y: '',
  237. M: '',
  238. D: '',
  239. h: '',
  240. m: '',
  241. s: ''
  242. }
  243. obj.Y = date.getFullYear();
  244. obj.M = date.getMonth() + 1;
  245. obj.M = obj.M < 10 ? "0" + obj.M : obj.M;
  246. obj.D = date.getDate();
  247. obj.D = obj.D < 10 ? "0" + obj.D : obj.D;
  248. obj.h = date.getHours();
  249. obj.h = obj.h < 10 ? "0" + obj.h : obj.h;
  250. obj.m = date.getMinutes();
  251. obj.m = obj.m < 10 ? "0" + obj.m : obj.m;
  252. obj.s = date.getSeconds();
  253. obj.s = obj.s < 10 ? "0" + obj.s : obj.s;
  254. if (format) {
  255. let str = '';
  256. for (let index in format) {
  257. str += `${obj[format[index]] || format[index]}`;
  258. }
  259. return str;
  260. } else {
  261. return `${obj.Y}-${obj.M}-${obj.D}`
  262. }
  263. },
  264. /**@injectScript
  265. * 动态引入js
  266. * src 引入地址 url
  267. */
  268. injectScript: (src) => {
  269. const s = document.createElement('script');
  270. s.type = 'text/javascript';
  271. s.async = true;
  272. s.src = src;
  273. const t = document.getElementsByTagName('script')[0];
  274. t.parentNode.insertBefore(s, t);
  275. },
  276. /**@download
  277. * 根据url地址下载
  278. * src 地址 url
  279. */
  280. download: (url) => {
  281. var isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
  282. var isSafari = navigator.userAgent.toLowerCase().indexOf('safari') > -1;
  283. if (isChrome || isSafari) {
  284. var link = document.createElement('a');
  285. link.href = url;
  286. if (link.download !== undefined) {
  287. var fileName = url.substring(url.lastIndexOf('/') + 1, url.length);
  288. link.download = fileName;
  289. }
  290. if (document.createEvent) {
  291. var e = document.createEvent('MouseEvents');
  292. e.initEvent('click', true, true);
  293. link.dispatchEvent(e);
  294. return true;
  295. }
  296. }
  297. if (url.indexOf('?') === -1) {
  298. url += '?download';
  299. }
  300. window.open(url, '_self');
  301. return true;
  302. },
  303. /**@getScrollPosition
  304. * 获取滚动的坐标
  305. */
  306. getScrollPosition: (el = window) => ({
  307. x: el.pageXOffset !== undefined ? el.pageXOffset : el.scrollLeft,
  308. y: el.pageYOffset !== undefined ? el.pageYOffset : el.scrollTop
  309. }),
  310. /**@scrollToTop
  311. * 滚动到顶部
  312. */
  313. scrollToTop: () => {
  314. const c = document.documentElement.scrollTop || document.body.scrollTop;
  315. if (c > 0) {
  316. window.requestAnimationFrame(this.scrollToTop);
  317. window.scrollTo(0, c - c / 8);
  318. }
  319. },
  320. /**@elementIsVisibleInViewport
  321. * el是否在视口范围内
  322. * el 为div视图
  323. */
  324. elementIsVisibleInViewport: (el, partiallyVisible = false) => {
  325. const {
  326. top,
  327. left,
  328. bottom,
  329. right
  330. } = el.getBoundingClientRect();
  331. const {
  332. innerHeight,
  333. innerWidth
  334. } = window;
  335. return partiallyVisible ?
  336. ((top > 0 && top < innerHeight) || (bottom > 0 && bottom < innerHeight)) &&
  337. ((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth)) :
  338. top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth;
  339. },
  340. /**@shuffle
  341. * 洗牌算法随机
  342. * arr 需要随机排序的数组
  343. */
  344. shuffle: (arr) => {
  345. var result = [],
  346. random;
  347. while (arr.length > 0) {
  348. random = Math.floor(Math.random() * arr.length);
  349. result.push(arr[random])
  350. arr.splice(random, 1)
  351. }
  352. return result;
  353. },
  354. /**@copyTextToClipboard
  355. * 劫持粘贴板
  356. * value
  357. */
  358. copyTextToClipboard: (value) => {
  359. var textArea = document.createElement("textarea");
  360. textArea.style.background = 'transparent';
  361. textArea.value = value;
  362. document.body.appendChild(textArea);
  363. textArea.select();
  364. try {
  365. var successful = document.execCommand('copy');
  366. } catch (err) {
  367. console.log('Oops, unable to copy');
  368. }
  369. document.body.removeChild(textArea);
  370. },
  371. /**@random
  372. * 随机数范围
  373. * min 最小的数
  374. * max 最大的数
  375. */
  376. random: function(min, max) {
  377. if (arguments.length === 2) {
  378. return Math.floor(min + Math.random() * ((max + 1) - min))
  379. } else {
  380. return null;
  381. }
  382. },
  383. /**@numberToChinese
  384. * 将阿拉伯数字翻译成中文的大写数字
  385. * num 数字
  386. */
  387. numberToChinese: (num) => {
  388. var AA = new Array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十");
  389. var BB = new Array("", "十", "百", "仟", "萬", "億", "点", "");
  390. var a = ("" + num).replace(/(^0*)/g, "").split("."),
  391. k = 0,
  392. re = "";
  393. for (var i = a[0].length - 1; i >= 0; i--) {
  394. switch (k) {
  395. case 0:
  396. re = BB[7] + re;
  397. break;
  398. case 4:
  399. if (!new RegExp("0{4}//d{" + (a[0].length - i - 1) + "}$")
  400. .test(a[0]))
  401. re = BB[4] + re;
  402. break;
  403. case 8:
  404. re = BB[5] + re;
  405. BB[7] = BB[5];
  406. k = 0;
  407. break;
  408. }
  409. if (k % 4 == 2 && a[0].charAt(i + 2) != 0 && a[0].charAt(i + 1) == 0)
  410. re = AA[0] + re;
  411. if (a[0].charAt(i) != 0)
  412. re = AA[a[0].charAt(i)] + BB[k % 4] + re;
  413. k++;
  414. }
  415. if (a.length > 1) // 加上小数部分(如果有小数部分)
  416. {
  417. re += BB[6];
  418. for (var i = 0; i < a[1].length; i++)
  419. re += AA[a[1].charAt(i)];
  420. }
  421. if (re == '一十')
  422. re = "十";
  423. if (re.match(/^一/) && re.length == 3)
  424. re = re.replace("一", "");
  425. return re;
  426. },
  427. /**@contains
  428. * 判断一个元素是否在数组中
  429. * arr 数组
  430. * val 元素
  431. */
  432. contains: (arr, val) => {
  433. return arr.indexOf(val) != -1 ? true : false;
  434. },
  435. /**@sort
  436. * 数组排序
  437. * {type} 1:从小到大 2:从大到小 3:随机
  438. * arr 数组
  439. */
  440. sort: (arr, type) => {
  441. return arr.sort((a, b) => {
  442. switch (type) {
  443. case 1:
  444. return a - b;
  445. case 2:
  446. return b - a;
  447. case 3:
  448. return Math.random() - 0.5;
  449. default:
  450. return arr;
  451. }
  452. })
  453. },
  454. /**@union
  455. * 两个集合的并集
  456. * a, b 数组
  457. */
  458. union: function(a, b) {
  459. var newArr = a.concat(b);
  460. return this.unique(newArr);
  461. },
  462. /**@intersect
  463. * 求两个集合的交集
  464. * a, b 数组
  465. */
  466. intersect: (a, b) => {
  467. var _this = this;
  468. a = this.unique(a);
  469. return this.map(a, function(o) {
  470. return _this.contains(b, o) ? o : null;
  471. });
  472. },
  473. /**@removearr
  474. * 删除其中一个元素
  475. * ele 元素
  476. * arr 数组
  477. */
  478. removearr: (arr, ele) => {
  479. var index = arr.indexOf(ele);
  480. if (index > -1) {
  481. arr.splice(index, 1);
  482. }
  483. return arr;
  484. },
  485. /**@formArray
  486. * 将类数组转换为数组
  487. * ary 类数组
  488. */
  489. formArray: (ary) => {
  490. var arr = [];
  491. if (Array.isArray(ary)) {
  492. arr = ary;
  493. } else {
  494. arr = Array.prototype.slice.call(ary);
  495. };
  496. return arr;
  497. },
  498. /**@max
  499. * 数组中最大值
  500. * arr 数组
  501. */
  502. max: (arr) => {
  503. return Math.max.apply(null, arr);
  504. },
  505. /**@min
  506. * 数组中最小值
  507. * arr 数组
  508. */
  509. min: (arr) => {
  510. return Math.min.apply(null, arr);
  511. },
  512. /**@sum
  513. * 数组中 和
  514. * arr 数组
  515. */
  516. sum: (arr) => {
  517. return arr.reduce((pre, cur) => {
  518. return pre + cur
  519. })
  520. },
  521. /**@average
  522. * 数组中 平均值
  523. * arr 数组
  524. */
  525. average: (arr) => {
  526. return this.sum(arr) / arr.length
  527. },
  528. /**@isObjectEqual
  529. * 数判断两个对象是否键值相同
  530. * a, b 对象
  531. */
  532. isObjectEqual: (a, b) => {
  533. var aProps = Object.getOwnPropertyNames(a);
  534. var bProps = Object.getOwnPropertyNames(b);
  535. if (aProps.length !== bProps.length) {
  536. return false;
  537. }
  538. for (var i = 0; i < aProps.length; i++) {
  539. var propName = aProps[i];
  540. if (a[propName] !== b[propName]) {
  541. return false;
  542. }
  543. }
  544. return true;
  545. },
  546. /**@throttle
  547. * 节流函数--规定在一个单位时间内,只能触发一次函数。如果这个单位时间内触发多次函数,只有一次生效。
  548. * fun 监控的函数
  549. * delay 触发时间
  550. */
  551. throttle: (fun, delay) => {
  552. let last, deferTimer
  553. return function(args) {
  554. let that = this
  555. let _args = args
  556. let now = +new Date()
  557. if (last && now < last + delay) {
  558. clearTimeout(deferTimer)
  559. deferTimer = setTimeout(function() {
  560. last = now
  561. fun.apply(that, _args)
  562. }, delay)
  563. } else {
  564. last = now
  565. fun.apply(that, _args)
  566. }
  567. }
  568. },
  569. /**@debouncer
  570. * 防抖
  571. * fn 方法
  572. * w 间隔时间
  573. * im true 直接触发,false定时触发
  574. */
  575. debouncer: (fn, w, im) => {
  576. var T;
  577. return function() {
  578. var t = this;
  579. let a = arguments;
  580. if (T) clearTimeout(T);
  581. if (im) {
  582. // 如果已经执行过,不再执行
  583. var cN = !T;
  584. T = setTimeout(function() {
  585. T = null;
  586. }, w)
  587. if (cN) fn.apply(t, a)
  588. } else {
  589. T = setTimeout(function() {
  590. fn.apply(t, a)
  591. }, w);
  592. }
  593. }
  594. },
  595. /**@ArrayObjSort
  596. * 数组对象排序
  597. * ArrayObj 数组对象
  598. * key 对象中的key
  599. * sequence 排序方法 'a-b','b-a'
  600. */
  601. ArrayObjSort: (ArrayObj, key, sequence) => {
  602. function sortkey(a, b) {
  603. switch (sequence) {
  604. case 'a-b':
  605. return a[key] - b[key];
  606. case 'b-a':
  607. return b[key] - a[key];
  608. }
  609. }
  610. ArrayObj.sort(sortkey);
  611. return ArrayObj;
  612. },
  613. /**@randomString
  614. * 获取唯一值
  615. *
  616. */
  617. uniqueStr: () => {
  618. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  619. var r = Math.random() * 16 | 0,
  620. v = c == 'x' ? r : (r & 0x3 | 0x8);
  621. return v.toString(16);
  622. });
  623. },
  624. /**@randomString
  625. * 获取随机值
  626. * e 随机值的长度
  627. */
  628. randomStr: (e) => {
  629. // let dateStr = Date.now();//13
  630. e = e || 32;
  631. var t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789",
  632. a = t.length,
  633. n = "";
  634. for (let i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a));
  635. return n //+dateStr
  636. }
  637. }
  638. //验证规则
  639. const regExp = {
  640. //手机
  641. phone: (s) => {
  642. return /^(1[3-9])\d{9}$/.test(s)
  643. },
  644. //邮箱
  645. isEmail: (s) => {
  646. return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(s)
  647. },
  648. //手机号码
  649. isMobile: (s) => {
  650. return /^1[0-9]{10}$/.test(s)
  651. },
  652. //电话号码
  653. isPhone: (s) => {
  654. return /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(s)
  655. },
  656. //是否url地址
  657. isURL: (s) => {
  658. return /^http[s]?:\/\/.*/.test(s)
  659. },
  660. // 首字母大写
  661. isCapital: (s) => {
  662. return /\b[^\Wa-z][^\WA-Z]*\b/g.test(s);
  663. },
  664. //是否字符串
  665. isString: (o) => {
  666. return Object.prototype.toString.call(o).slice(8, -1) === 'String'
  667. },
  668. //是否数字
  669. isNumber: (o) => {
  670. return Object.prototype.toString.call(o).slice(8, -1) === 'Number'
  671. },
  672. //是否boolean
  673. isBoolean: (o) => {
  674. return Object.prototype.toString.call(o).slice(8, -1) === 'Boolean'
  675. },
  676. //是否函数
  677. isFunction: (o) => {
  678. return Object.prototype.toString.call(o).slice(8, -1) === 'Function'
  679. },
  680. //是否为null
  681. isNull: (o) => {
  682. return Object.prototype.toString.call(o).slice(8, -1) === 'Null'
  683. },
  684. //是否undefined
  685. isUndefined: (o) => {
  686. return Object.prototype.toString.call(o).slice(8, -1) === 'Undefined'
  687. },
  688. //是否对象
  689. isObj: (o) => {
  690. return Object.prototype.toString.call(o).slice(8, -1) === 'Object'
  691. },
  692. //是否数组
  693. isArray: (o) => {
  694. return Object.prototype.toString.call(o).slice(8, -1) === 'Array'
  695. },
  696. //是否时间
  697. isDate: (o) => {
  698. return Object.prototype.toString.call(o).slice(8, -1) === 'Date'
  699. },
  700. //是否正则
  701. isRegExp: (o) => {
  702. return Object.prototype.toString.call(o).slice(8, -1) === 'RegExp'
  703. },
  704. //是否错误对象
  705. isError: (o) => {
  706. return Object.prototype.toString.call(o).slice(8, -1) === 'Error'
  707. },
  708. //是否Symbol函数
  709. isSymbol: (o) => {
  710. return Object.prototype.toString.call(o).slice(8, -1) === 'Symbol'
  711. },
  712. //是否Promise对象
  713. isPromise: (o) => {
  714. return Object.prototype.toString.call(o).slice(8, -1) === 'Promise'
  715. },
  716. //是否Set对象
  717. isSet: (o) => {
  718. return Object.prototype.toString.call(o).slice(8, -1) === 'Set'
  719. },
  720. //是否是微信浏览器
  721. isWeiXin: () => {
  722. var ua = window.navigator.userAgent.toLowerCase();
  723. return ua.match(/microMessenger/i) == 'micromessenger'
  724. },
  725. //是否是移动端
  726. isDeviceMobile: () => {
  727. var ua = window.navigator.userAgent.toLowerCase();
  728. return /android|webos|iphone|ipod|balckberry/i.test(ua)
  729. },
  730. //是否是QQ浏览器
  731. isQQBrowser: () => {
  732. var ua = window.navigator.userAgent.toLowerCase();
  733. return !!ua.match(/mqqbrowser|qzone|qqbrowser|qbwebviewtype/i)
  734. },
  735. //是否ios
  736. isIos: () => {
  737. var u = navigator.userAgent;
  738. if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) { //安卓手机
  739. return false
  740. } else if (u.indexOf('iPhone') > -1) { //苹果手机
  741. return true
  742. } else if (u.indexOf('iPad') > -1) { //iPad
  743. return false
  744. } else if (u.indexOf('Windows Phone') > -1) { //winphone手机
  745. return false
  746. } else {
  747. return false
  748. }
  749. },
  750. //是否为PC端
  751. isPC: () => {
  752. var userAgentInfo = window.navigator.userAgent;
  753. var Agents = ["Android", "iPhone",
  754. "SymbianOS", "Windows Phone",
  755. "iPad", "iPod"
  756. ];
  757. var flag = true;
  758. for (var v = 0; v < Agents.length; v++) {
  759. if (userAgentInfo.indexOf(Agents[v]) > 0) {
  760. flag = false;
  761. break;
  762. }
  763. }
  764. return flag;
  765. },
  766. // 严格的身份证校验
  767. isCardID: (sId) => {
  768. if (!/(^\d{15}$)|(^\d{17}(\d|X|x)$)/.test(sId)) {
  769. console.log('你输入的身份证长度或格式错误')
  770. return false
  771. }
  772. //身份证城市
  773. var aCity = {
  774. 11: "北京",
  775. 12: "天津",
  776. 13: "河北",
  777. 14: "山西",
  778. 15: "内蒙古",
  779. 21: "辽宁",
  780. 22: "吉林",
  781. 23: "黑龙江",
  782. 31: "上海",
  783. 32: "江苏",
  784. 33: "浙江",
  785. 34: "安徽",
  786. 35: "福建",
  787. 36: "江西",
  788. 37: "山东",
  789. 41: "河南",
  790. 42: "湖北",
  791. 43: "湖南",
  792. 44: "广东",
  793. 45: "广西",
  794. 46: "海南",
  795. 50: "重庆",
  796. 51: "四川",
  797. 52: "贵州",
  798. 53: "云南",
  799. 54: "西藏",
  800. 61: "陕西",
  801. 62: "甘肃",
  802. 63: "青海",
  803. 64: "宁夏",
  804. 65: "新疆",
  805. 71: "台湾",
  806. 81: "香港",
  807. 82: "澳门",
  808. 91: "国外"
  809. };
  810. if (!aCity[parseInt(sId.substr(0, 2))]) {
  811. console.log('你的身份证地区非法')
  812. return false
  813. }
  814. // 出生日期验证
  815. var sBirthday = (sId.substr(6, 4) + "-" + Number(sId.substr(10, 2)) + "-" + Number(sId.substr(12, 2)))
  816. .replace(/-/g,
  817. "/"),
  818. d = new Date(sBirthday)
  819. if (sBirthday != (d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate())) {
  820. console.log('身份证上的出生日期非法')
  821. return false
  822. }
  823. // 身份证号码校验
  824. var sum = 0,
  825. weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],
  826. codes = "10X98765432"
  827. for (var i = 0; i < sId.length - 1; i++) {
  828. sum += sId[i] * weights[i];
  829. }
  830. var last = codes[sum % 11]; //计算出来的最后一位身份证号码
  831. // console.log(last)
  832. if (sId[sId.length - 1] != last) {
  833. console.log('你输入的身份证号非法')
  834. return false
  835. }
  836. return true
  837. }
  838. }
  839. // 获取IP
  840. const ipurl = 'https://pv.sohu.com/cityjson?ie=utf-8';
  841. const _try_ = (callback) => {
  842. try {
  843. callback()
  844. } catch (error) {
  845. // console.log(1);
  846. // uni ? uni.hideLoading() : alert('未知错误');
  847. }
  848. };
  849. export{
  850. md, //
  851. regExp, //验证规则
  852. ipurl, //ip
  853. _try_
  854. }
  855. // 移动端适配方案
  856. (function(doc, win) {
  857. var docEl = doc.documentElement,
  858. resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
  859. recalc = function() {
  860. var clientWidth = docEl.clientWidth;
  861. var fontSize = 20;
  862. docEl.style.fontSize = fontSize + 'px';
  863. var docStyles = getComputedStyle(docEl);
  864. var realFontSize = parseFloat(docStyles.fontSize);
  865. var scale = realFontSize / fontSize;
  866. console.log("realFontSize: " + realFontSize + ", scale: " + scale);
  867. fontSize = clientWidth / 667 * 20;
  868. if (isIphoneX()) fontSize = 19;
  869. fontSize = fontSize / scale;
  870. docEl.style.fontSize = fontSize + 'px';
  871. };
  872. // Abort if browser does not support addEventListener
  873. if (!doc.addEventListener) return;
  874. win.addEventListener(resizeEvt, recalc, false);
  875. doc.addEventListener('DOMContentLoaded', recalc, false);
  876. // iphoneX判断
  877. function isIphoneX() {
  878. return /iphone/gi.test(navigator.userAgent) && (win.screen.height == 812 && win.screen.width == 375)
  879. }
  880. })(document, window);
  881. // (!function (d) {
  882. // var c = d.document;
  883. // var a = c.documentElement;
  884. // var b = d.devicePixelRatio;
  885. // var f;
  886. // function e() {
  887. // var h = a.getBoundingClientRect().width, g;
  888. // if (b === 1) {
  889. // h = 720
  890. // }
  891. // if (h > 720) h = 720;//设置基准值的极限值
  892. // g = h / 7.2;
  893. // a.style.fontSize = g + "px"
  894. // }
  895. // if (b > 2) {
  896. // b = 3
  897. // } else {
  898. // if (b > 1) {
  899. // b = 2
  900. // } else {
  901. // b = 1
  902. // }
  903. // }
  904. // a.setAttribute("data-dpr", b);
  905. // d.addEventListener("resize", function () {
  906. // clearTimeout(f);
  907. // f = setTimeout(e, 200)
  908. // }, false);
  909. // e()
  910. // })(window);