public.js 21 KB

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