rocessor.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //身份证验证
  2. export function isCardNo(card) {
  3. // 身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X
  4. var reg =
  5. /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/;
  6. if (reg.test(card) === false) {
  7. console.log(card);
  8. return false;
  9. }
  10. return true
  11. }
  12. // 金额显示变化
  13. export function getMoneyStyle(value = 0) {
  14. if (typeof value == 'string') {
  15. value = (+value).toFixed(2)
  16. }
  17. if (typeof value == 'number') {
  18. value = value.toFixed(2)
  19. }
  20. // 将字符串转为数组
  21. let n = value.split("");
  22. // 反转数组并复制循环处理
  23. let arr = n.reverse().map(function(e, ind, ar) {
  24. // 判断当前下标是否为3的整数倍数且不为最后一个下标
  25. if (ind % 3 == 0 && ind / 3 > 1 && ind != ar.length) {
  26. return e + ','
  27. } else {
  28. return e
  29. }
  30. })
  31. // 反转数组回复原来排序并合并回字符串
  32. arr = arr.reverse().join('')
  33. return arr;
  34. }
  35. // 倒计时计算
  36. // 计算倒计时时间
  37. export function timeComputed(time) {
  38. // 获取当前时间
  39. const actTime = (new Date()).getTime();
  40. // 获取到期时间
  41. let stopTime = time - actTime;
  42. // 判断是否小于0
  43. if (stopTime < 0) {
  44. stopTime = stopTime * -1
  45. }
  46. let day = Math.floor(stopTime / 1000 / 60 / 60 / 24) //获取剩余天数
  47. let hours = Math.floor((stopTime / 1000 / 60 / 60) % 24); //获取剩余小时数
  48. let minutes = Math.floor((stopTime / 1000 / 60) % 60); //获取分钟
  49. let seconds = Math.floor((stopTime / 1000) % 60); //获取秒数
  50. return {
  51. hours, //倒计时小时数
  52. minutes, //倒计时分钟数
  53. seconds, //倒计时秒数
  54. day //倒计时天数
  55. }
  56. }
  57. // #ifdef MP
  58. // 调用打开地图方法
  59. export function openMap(e) {
  60. const that = this
  61. return new Promise((resolve, reject) => {
  62. wx.getSetting({
  63. success(res) {
  64. //这里判断是否有地位权限
  65. if (!res.authSetting['scope.userLocation']) {
  66. wx.showModal({
  67. title: '提示',
  68. content: '请求获取位置权限',
  69. success: function(res) {
  70. if (res.confirm == false) {
  71. // 授权失败
  72. reject()
  73. return false;
  74. }
  75. wx.openSetting({
  76. success(res) {
  77. //如果再次拒绝则返回页面并提示
  78. if (!res.authSetting['scope.userLocation']) {
  79. wx.showToast({
  80. title: '此功能需获取位置信息,请重新设置',
  81. duration: 3000,
  82. icon: 'none'
  83. })
  84. } else {
  85. //允许授权,调用地图
  86. resolve()
  87. }
  88. }
  89. })
  90. }
  91. })
  92. } else {
  93. //如果有定位权限,调用地图
  94. resolve()
  95. }
  96. }
  97. })
  98. })
  99. }
  100. // #endif
  101. //时间戳转换成时间
  102. export function getTime(time) {
  103. const num = 13 - (time + '').length;
  104. let l = 1; //倍数
  105. for (let i = 0; i < num; i++) {
  106. l += '0';
  107. }
  108. // 重新解析为数字
  109. l = parseInt(l)
  110. const date = new Date(parseInt(time) * l);
  111. const year = date.getFullYear();
  112. const mon = date.getMonth() + 1;
  113. const day = date.getDate();
  114. const hours = date.getHours();
  115. const minu = date.getMinutes();
  116. const sec = date.getSeconds();
  117. return year + '-' + mon + '-' + day + ' ' + hours + ':' + minu + ':' + sec;
  118. }
  119. // #ifdef H5
  120. // 获取网络资源
  121. /**
  122. * @param {string} url = '' 网络地址
  123. * @param {string} type = ['script','link']
  124. * @param {function} callback
  125. */
  126. export function getWwwJs(url, type, callback) {
  127. if (!url || typeof(url) != "string") {
  128. return false;
  129. }
  130. const jsList = document.getElementsByTagName(type)
  131. // 判断是否已经加载过该js
  132. let jsBoole = false;
  133. const hrefStr = type == 'script' ? 'src' : 'href';
  134. for (let i = 0; i < jsList.length; i++) {
  135. if (jsList[i].getAttribute(hrefStr) == url) {
  136. jsBoole = true;
  137. }
  138. }
  139. if (!jsBoole) {
  140. let head = document.getElementsByTagName('head')[0];
  141. let js = document.createElement(type)
  142. if (type == 'script') {
  143. js.type = 'text/javascript';
  144. }
  145. if(type == 'link'){
  146. js.rel = 'stylesheet';
  147. }
  148. js.setAttribute(hrefStr, url);
  149. // 判断是否需要处理回调事件
  150. if (callback) {
  151. js.onload = js.onreadystatechange = function() {
  152. if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") {
  153. // 加载完成回调数据
  154. callback();
  155. js.onload = js.onreadystatechange = null;
  156. }
  157. };
  158. }
  159. // 添加js到页面
  160. head.appendChild(js);
  161. }
  162. }
  163. // #endif