util.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. const formatTime = date => {
  2. const year = date.getFullYear()
  3. const month = date.getMonth() + 1
  4. const day = date.getDate()
  5. const hour = date.getHours()
  6. const minute = date.getMinutes()
  7. const second = date.getSeconds()
  8. return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  9. }
  10. const formatNumber = n => {
  11. n = n.toString()
  12. return n[1] ? n : '0' + n
  13. }
  14. // 验证是否为整数
  15. const e_checkNumber = strnumber => {
  16. return /^[1-9]\d*$|^0$/.test(strnumber);
  17. }
  18. // 验证手机号码
  19. const e_checkPhone = strtel => {
  20. var strIsMobile = /^(?:13\d|14\d|15\d|16\d|17\d|18\d|19\d)\d{5}(\d{3}|\*{3})$/; //手机号码验证规则
  21. var strIsPhone = /^((0\d{2,3})(-)?)?(\d{7,8})(-(\d{3,}))?$/; //座机验证规则
  22. var strPhone = strtel; //获得用户填写的号码值 赋值给变量dianhua
  23. // if (!strIsMobile.test(strPhone) && !strIsPhone.test(strPhone)) { //如果用户输入的值不同时满足手机号和座机号的正则
  24. // return false;
  25. // }
  26. if (!strIsMobile.test(strPhone)) { //如果用户输入的值不满足手机号的正则
  27. return false;
  28. }
  29. return true;
  30. }
  31. // 验证邮箱
  32. const e_checkEMail = stremail => {
  33. return /^\w+([-.]\w+)*@([\w-]){1,}(\.([\w]){1,}){1,}$/.test(stremail);
  34. }
  35. // 验证身份证
  36. const e_checkIDCard = strIDCard => {
  37. if (15 == strIDCard.length)
  38. return (/^([0-9]){15,15}$/.test(strIDCard));
  39. if (18 == strIDCard.length)
  40. return (/^([0-9]){17,17}([0-9xX]){1,1}$/.test(strIDCard));
  41. return false;
  42. }
  43. /**
  44. * 获取密码强度等级
  45. * 0-密码过短(没有达到最小长度)
  46. * 1-密码安全性差
  47. * 2-密码安全性良好
  48. * 3-密码安全性强
  49. * @param {string} strPwd
  50. * @param {number} minLength
  51. */
  52. const e_getPwdLevel = (strPwd, minLength) => {
  53. minLength = minLength || 6;
  54. if (strPwd.length < minLength) {
  55. return 0;
  56. }
  57. let level = 0;
  58. if (strPwd.match(/[a-z]/ig)) {
  59. level++;
  60. }
  61. if (strPwd.match(/[0-9]/ig)) {
  62. level++;
  63. }
  64. if (strPwd.match(/(.[^a-z0-9])/ig)) {
  65. level++;
  66. }
  67. if (strPwd.length < 6 && ls > 0) {
  68. level--;
  69. }
  70. return level;
  71. }
  72. // 是否登录验证
  73. const loginVerification = fun => {
  74. let objUserInfo = uni.getStorageSync('userInfo');
  75. if (objUserInfo.openID) {
  76. typeof fun === 'function' && fun();
  77. } else {
  78. uni.showToast({
  79. title: '请先授权登录',
  80. icon: 'none'
  81. })
  82. }
  83. }
  84. const msg = (title, fun, duration = 2000, mask = true, icon = 'none') => {
  85. //统一提示方便全局修改
  86. if (Boolean(title) === false) {
  87. typeof fun === 'function' && fun();
  88. return;
  89. }
  90. uni.showToast({
  91. title: title,
  92. duration: duration,
  93. mask: mask,
  94. icon: icon,
  95. success() {
  96. setTimeout(function() {
  97. // 执行成功-回调方法
  98. typeof fun === 'function' && fun();
  99. }, duration)
  100. }
  101. })
  102. }
  103. /**
  104. * 生成唯一标识字符
  105. */
  106. const Guid = () => {
  107. function Rfour() {
  108. return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  109. }
  110. return (Rfour() + Rfour() + "-" + Rfour() + "-" + Rfour() + "-" + Rfour() + "-" + Rfour() + Rfour() + Rfour());
  111. }
  112. module.exports = {
  113. formatTime: formatTime,
  114. e_checkNumber: e_checkNumber,
  115. e_checkPhone: e_checkPhone,
  116. e_checkEMail: e_checkEMail,
  117. e_checkIDCard: e_checkIDCard,
  118. e_getPwdLevel: e_getPwdLevel,
  119. loginVerification: loginVerification,
  120. msg: msg,
  121. Guid: Guid
  122. }