index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. const QRCode = require('qrcode');
  2. import {
  3. visitorRecords
  4. } from '../api/api'
  5. //身份证号校验
  6. const isCardID = (sId) => {
  7. if (!/(^\d{17}(\d|X|x)$)/.test(sId)) {
  8. return {
  9. data: "你输入的身份证长度或格式有误",
  10. state: false
  11. };
  12. }
  13. //身份证城市
  14. var aCity = {
  15. 11: "北京",
  16. 12: "天津",
  17. 13: "河北",
  18. 14: "山西",
  19. 15: "内蒙古",
  20. 21: "辽宁",
  21. 22: "吉林",
  22. 23: "黑龙江",
  23. 31: "上海",
  24. 32: "江苏",
  25. 33: "浙江",
  26. 34: "安徽",
  27. 35: "福建",
  28. 36: "江西",
  29. 37: "山东",
  30. 41: "河南",
  31. 42: "湖北",
  32. 43: "湖南",
  33. 44: "广东",
  34. 45: "广西",
  35. 46: "海南",
  36. 50: "重庆",
  37. 51: "四川",
  38. 52: "贵州",
  39. 53: "云南",
  40. 54: "西藏",
  41. 61: "陕西",
  42. 62: "甘肃",
  43. 63: "青海",
  44. 64: "宁夏",
  45. 65: "新疆",
  46. 71: "台湾",
  47. 81: "香港",
  48. 82: "澳门",
  49. 91: "国外"
  50. };
  51. if (!aCity[parseInt(sId.substr(0, 2))]) {
  52. return {
  53. data: "你的身份证地区有误",
  54. state: false
  55. };
  56. }
  57. // 出生日期验证
  58. var sBirthday = (
  59. sId.substr(6, 4) +
  60. "-" +
  61. Number(sId.substr(10, 2)) +
  62. "-" +
  63. Number(sId.substr(12, 2))
  64. ).replace(/-/g, "/"),
  65. d = new Date(sBirthday);
  66. if (
  67. sBirthday !=
  68. d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate()
  69. ) {
  70. return {
  71. data: "身份证上的出生日期有误",
  72. state: false
  73. };
  74. }
  75. // 身份证号码校验
  76. var sum = 0,
  77. weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],
  78. codes = "10X98765432";
  79. for (var i = 0; i < sId.length - 1; i++) {
  80. sum += sId[i] * weights[i];
  81. }
  82. var last = codes[sum % 11]; //计算出来的最后一位身份证号码
  83. if (sId[sId.length - 1] != last) {
  84. return {
  85. data: "你输入的身份证号长度或格式有误",
  86. state: false
  87. };
  88. }
  89. return {
  90. data: "",
  91. state: true
  92. };
  93. };
  94. //手机号校验
  95. const isPhone = phone => {
  96. if (!/^1[3-9]\d{9}$/.test(phone)) {
  97. return {
  98. data: "手机号只能由11位数字组成",
  99. state: false
  100. };
  101. } else {
  102. return {
  103. data: "",
  104. state: true
  105. };
  106. }
  107. };
  108. // 获取日期时间
  109. const getDate = data => {
  110. var date = new Date();
  111. var y = date.getFullYear();
  112. var m = date.getMonth() + 1;
  113. m = m < 10 ? "0" + m : m;
  114. var d = date.getDate();
  115. d = d < 10 ? "0" + d : d;
  116. var h = date.getHours();
  117. h = h < 10 ? "0" + h : h;
  118. var minute = date.getMinutes();
  119. minute = minute < 10 ? "0" + minute : minute;
  120. var second = date.getSeconds();
  121. second = second < 10 ? "0" + second : second;
  122. return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + second;
  123. }
  124. const getQRCodeUrl = (content, isShow = true) => {
  125. return new Promise((resolve, reject) => {
  126. QRCode.toString(content, {
  127. type: 'png',
  128. }, function (err, url) {
  129. let str = (isShow ? 'data:image/svg+xml;base64,' : '') + Buffer(url).toString('base64');
  130. if (err) {
  131. reject(err)
  132. }
  133. resolve(str)
  134. });
  135. })
  136. }
  137. //获得对应时间格式
  138. const formatTime = (time, type) => {
  139. var date = new Date(time)
  140. var format = type || 'YYYY-MM-DD HH:NN:SS';
  141. const year = date.getFullYear();
  142. const month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
  143. const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  144. const hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  145. const minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  146. const second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  147. format.indexOf('YYYY') > -1 ? format = format.replace('YYYY', year) : '';
  148. format.indexOf('MM') > -1 ? format = format.replace('MM', month) : '';
  149. format.indexOf('DD') > -1 ? format = format.replace('DD', day) : '';
  150. format.indexOf('HH') > -1 ? format = format.replace('HH', hour) : '';
  151. format.indexOf('NN') > -1 ? format = format.replace('NN', minute) : '';
  152. format.indexOf('SS') > -1 ? format = format.replace('SS', second) : '';
  153. return format;
  154. }
  155. // 重复请求
  156. const repeatedRequest = (url, params, times) => {
  157. return new Promise(async (resolve, reject) => {
  158. while (times--) {
  159. try {
  160. const res = await visitorRecords(url, params);
  161. resolve(res);
  162. break;
  163. } catch (error) {
  164. if (times <= 0) {
  165. reject(error);
  166. }
  167. }
  168. }
  169. });
  170. }
  171. // 获取地址?后的参数
  172. const getForm = (url) => {
  173. var theForm = new Object();
  174. if (url.indexOf("?") != -1) {
  175. url = url.split('?')[1]
  176. var str = url.substr(1);
  177. let strs = str.split("&");
  178. for (var i = 0; i < strs.length; i++) {
  179. theForm[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
  180. }
  181. }
  182. return theForm;
  183. }
  184. const getWaterDrop = () => {
  185. const innerAudioContext = my.createInnerAudioContext();
  186. innerAudioContext.autoplay = true;
  187. innerAudioContext.src = 'XNjM3NjQ1MDAwOA==';
  188. innerAudioContext.play();
  189. }
  190. function desensitization(val = "", start, end) {
  191. var star = '';
  192. var string = String(val);
  193. function makeStar(number) {
  194. if (number > 0) {
  195. star += '*';
  196. return makeStar(--number);
  197. } else {
  198. return star;
  199. }
  200. };
  201. return string.substring(0, start) + makeStar(end - start) + string.substring(end);
  202. }
  203. function throttle(func, delay) {
  204. let lastCall = 0;
  205. return function (...args) {
  206. const now = new Date().getTime();
  207. if (now - lastCall >= delay) {
  208. lastCall = now;
  209. func.apply(this, args);
  210. }
  211. };
  212. }
  213. function debounce(func, delay) {
  214. let timeoutId;
  215. return function (...args) {
  216. const context = this;
  217. clearTimeout(timeoutId);
  218. timeoutId = setTimeout(() => {
  219. func.apply(context, args);
  220. }, delay);
  221. };
  222. }
  223. module.exports = {
  224. isCardID: isCardID,
  225. isPhone: isPhone,
  226. getDate: getDate,
  227. getQRCodeUrl: getQRCodeUrl,
  228. formatTime: formatTime,
  229. repeatedRequest: repeatedRequest,
  230. getForm: getForm,
  231. getWaterDrop: getWaterDrop,
  232. desensitization: desensitization,
  233. throttle: throttle,
  234. debounce: debounce
  235. };