util.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. export function timeFix () {
  2. const time = new Date()
  3. const hour = time.getHours()
  4. return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
  5. }
  6. export function welcome () {
  7. const arr = ['休息一会儿吧', '准备吃什么呢?', '要不要喝杯咖啡', '我猜你可能累了']
  8. const index = Math.floor(Math.random() * arr.length)
  9. return arr[index]
  10. }
  11. /**
  12. * 触发 window.resize
  13. */
  14. export function triggerWindowResizeEvent () {
  15. const event = document.createEvent('HTMLEvents')
  16. event.initEvent('resize', true, true)
  17. event.eventType = 'message'
  18. window.dispatchEvent(event)
  19. }
  20. export function handleScrollHeader (callback) {
  21. let timer = 0
  22. let beforeScrollTop = window.pageYOffset
  23. callback = callback || function () {}
  24. window.addEventListener(
  25. 'scroll',
  26. event => {
  27. clearTimeout(timer)
  28. timer = setTimeout(() => {
  29. let direction = 'up'
  30. const afterScrollTop = window.pageYOffset
  31. const delta = afterScrollTop - beforeScrollTop
  32. if (delta === 0) {
  33. return false
  34. }
  35. direction = delta > 0 ? 'down' : 'up'
  36. callback(direction)
  37. beforeScrollTop = afterScrollTop
  38. }, 50)
  39. },
  40. false
  41. )
  42. }
  43. /**
  44. * Remove loading animate
  45. * @param id parent element id or class
  46. * @param timeout
  47. */
  48. export function removeLoadingAnimate (id = '', timeout = 1500) {
  49. if (id === '') {
  50. return
  51. }
  52. setTimeout(() => {
  53. document.body.removeChild(document.getElementById(id))
  54. }, timeout)
  55. }
  56. /**
  57. * 身份证15位编码规则:dddddd yymmdd xx p
  58. * dddddd:6位地区编码
  59. * yymmdd: 出生年(两位年)月日,如:910215
  60. * xx: 顺序编码,系统产生,无法确定
  61. * p: 性别,奇数为男,偶数为女
  62. *
  63. * 身份证18位编码规则:dddddd yyyymmdd xxx y
  64. * dddddd:6位地区编码
  65. * yyyymmdd: 出生年(四位年)月日,如:19910215
  66. * xxx:顺序编码,系统产生,无法确定,奇数为男,偶数为女
  67. * y: 校验码,该位数值可通过前17位计算获得
  68. *
  69. * 前17位号码加权因子为 Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ]
  70. * 验证位 Y = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ]
  71. * 如果验证码恰好是10,为了保证身份证是十八位,那么第十八位将用X来代替
  72. * 校验位计算公式:Y_P = mod( ∑(Ai×Wi),11 )
  73. * i为身份证号码1...17 位; Y_P为校验码Y所在校验码数组位置
  74. */
  75. export function isIdCardNo (num) {
  76. num = num.toUpperCase() // 身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X。
  77. if (!(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(num))) {
  78. // alert('输入的身份证号长度不对,或者号码不符合规定!\n15位号码应全为数字,18位号码末位可以为数字或X。');
  79. // alert('身份证号长度不正确或不符合规定!');
  80. return false
  81. }
  82. // 验证前2位,城市符合
  83. const aCity = { 11: '北京', 12: '天津', 13: '河北', 14: '山西', 15: '内蒙古', 21: '辽宁', 22: '吉林', 23: '黑龙江 ', 31: '上海', 32: '江苏', 33: '浙江', 34: '安徽', 35: '福建', 36: '江西', 37: '山东', 41: '河南', 42: '湖北', 43: '湖南', 44: '广东', 45: '广西', 46: '海南', 50: '重庆', 51: '四川', 52: '贵州', 53: '云南', 54: '西藏', 61: '陕西', 62: '甘肃', 63: '青海', 64: '宁夏', 65: '新疆', 71: '台湾', 81: '香港', 82: '澳门', 91: '国外' }
  84. if (aCity[parseInt(num.substr(0, 2))] == null) {
  85. // alert('身份证号不正确或不符合规定!');
  86. return false
  87. }
  88. // alert('城市:'+aCity[parseInt(num.substr(0,2))]);
  89. // 下面分别分析出生日期和校验位
  90. let re
  91. const len = num.length
  92. if (len === 15) {
  93. re = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/)
  94. const arrSplit = num.match(re) // 检查生日日期是否正确
  95. const dtmBirth = new Date('19' + arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4])
  96. const bGoodDay = (dtmBirth.getYear() === Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) === Number(arrSplit[3])) && (dtmBirth.getDate() === Number(arrSplit[4]))
  97. if (!bGoodDay) {
  98. // alert('身份证号的出生日期不对!');
  99. return false
  100. } else { // 将15位身份证转成18位 //校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。
  101. const arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
  102. const arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
  103. let nTemp = 0; let i
  104. num = num.substr(0, 6) + '19' + num.substr(6, num.length - 6)
  105. for (i = 0; i < 17; i++) {
  106. nTemp += num.substr(i, 1) * arrInt[i]
  107. }
  108. num += arrCh[nTemp % 11]
  109. return true
  110. }
  111. }
  112. if (len === 18) {
  113. re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/)
  114. const arrSplit = num.match(re) // 检查生日日期是否正确
  115. const dtmBirth = new Date(arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4])
  116. const bGoodDay = (dtmBirth.getFullYear() === Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) === Number(arrSplit[3])) && (dtmBirth.getDate() === Number(arrSplit[4]))
  117. if (!bGoodDay) {
  118. // alert(dtmBirth.getYear());
  119. // alert(arrSplit[2]);
  120. // alert('身份证号的出生日期不对!');
  121. return false
  122. } else { // 检验18位身份证的校验码是否正确。 //校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。
  123. const arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
  124. const arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
  125. let nTemp = 0; let i
  126. for (i = 0; i < 17; i++) {
  127. nTemp += num.substr(i, 1) * arrInt[i]
  128. }
  129. const valnum = arrCh[nTemp % 11]
  130. if (valnum !== num.substr(17, 1)) {
  131. // alert('18位身份证的校验码不正确!应该为:' + valnum);
  132. // alert('18位身份证号的校验码不正确!');
  133. return false
  134. }
  135. return true
  136. }
  137. }
  138. return false
  139. }
  140. export function formatDate (date, fmt) {
  141. console.log(date)
  142. if (/(y+)/.test(fmt)) {
  143. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  144. }
  145. const o = {
  146. 'M+': date.getMonth() + 1,
  147. 'd+': date.getDate(),
  148. 'h+': date.getHours(),
  149. 'm+': date.getMinutes(),
  150. 's+': date.getSeconds()
  151. }
  152. for (const k in o) {
  153. if (new RegExp(`(${k})`).test(fmt)) {
  154. const str = o[k] + ''
  155. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str))
  156. }
  157. }
  158. return fmt
  159. }
  160. export function timestampToTime (cjsj) {
  161. // 时间戳为10位需*1000,时间戳为13位的话不需乘1000
  162. const date = new Date(cjsj)
  163. const Y = date.getFullYear() + '-'
  164. const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
  165. const D = date.getDate() + ' '
  166. const h = date.getHours() + ':'
  167. const m = date.getMinutes() + ':'
  168. const s = date.getSeconds()
  169. return Y + M + D + h + m + s
  170. }
  171. function padLeftZero (str) {
  172. return ('00' + str).substr(str.length)
  173. }
  174. export function calculateRepayInterest (rate, capital, days) {
  175. return Math.round(rate / 360 * capital * days) / 100
  176. }
  177. export function transformNumberIntoCHN (number) {
  178. if (!/^(0|[1-9]\d*)(\.\d{0,2})?$/.test(number)) {
  179. this.$message.error('不合法的金额:' + number)
  180. return ''
  181. }
  182. let unit = '仟佰拾亿仟佰拾万仟佰拾元角分'
  183. let str = ''
  184. number += '00'
  185. const point = number.indexOf('.')
  186. if (point >= 0) {
  187. number = number.substring(0, point) + number.substr(point + 1, 2)
  188. }
  189. unit = unit.substr(unit.length - number.length)
  190. for (let i = 0; i < number.length; i++) {
  191. str += '零壹贰叁肆伍陆柒捌玖'.charAt(number.charAt(i)) + unit.charAt(i)
  192. }
  193. if (str.replace(/零(仟|佰|拾|角)/g, '零').replace(/(零)+/g, '零').replace(/零(万|亿|元)/g, '$1').replace(/(亿)万|(拾)/g, '$1$2').replace(/^元零?|零分/g, '').replace(/元$/g, '元整') === '分') {
  194. return '零元整'
  195. } else {
  196. return str.replace(/零(仟|佰|拾|角)/g, '零').replace(/(零)+/g, '零').replace(/零(万|亿|元)/g, '$1').replace(/(亿)万|(拾)/g, '$1$2').replace(/^元零?|零分/g, '').replace(/元$/g, '元整')
  197. }
  198. }