util.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. const app = getApp()
  2. //身份证号校验
  3. const isCardID = (sId) => {
  4. if (!/(^\d{15}$)|(^\d{17}(\d|X|x)$)/.test(sId)) {
  5. return {
  6. data: '你输入的身份证长度或格式有误',
  7. state: false
  8. }
  9. }
  10. //身份证城市
  11. var aCity = {
  12. 11: "北京",
  13. 12: "天津",
  14. 13: "河北",
  15. 14: "山西",
  16. 15: "内蒙古",
  17. 21: "辽宁",
  18. 22: "吉林",
  19. 23: "黑龙江",
  20. 31: "上海",
  21. 32: "江苏",
  22. 33: "浙江",
  23. 34: "安徽",
  24. 35: "福建",
  25. 36: "江西",
  26. 37: "山东",
  27. 41: "河南",
  28. 42: "湖北",
  29. 43: "湖南",
  30. 44: "广东",
  31. 45: "广西",
  32. 46: "海南",
  33. 50: "重庆",
  34. 51: "四川",
  35. 52: "贵州",
  36. 53: "云南",
  37. 54: "西藏",
  38. 61: "陕西",
  39. 62: "甘肃",
  40. 63: "青海",
  41. 64: "宁夏",
  42. 65: "新疆",
  43. 71: "台湾",
  44. 81: "香港",
  45. 82: "澳门",
  46. 91: "国外"
  47. };
  48. if (!aCity[parseInt(sId.substr(0, 2))]) {
  49. return {
  50. data: '你的身份证地区有误',
  51. state: false
  52. }
  53. }
  54. // 出生日期验证
  55. var sBirthday = (sId.substr(6, 4) + "-" + Number(sId.substr(10, 2)) + "-" + Number(sId.substr(12, 2))).replace(/-/g, "/"),
  56. d = new Date(sBirthday)
  57. if (sBirthday != (d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate())) {
  58. return {
  59. data: '身份证上的出生日期有误',
  60. state: false
  61. }
  62. }
  63. // 身份证号码校验
  64. var sum = 0,
  65. weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],
  66. codes = "10X98765432"
  67. for (var i = 0; i < sId.length - 1; i++) {
  68. sum += sId[i] * weights[i];
  69. }
  70. var last = codes[sum % 11]; //计算出来的最后一位身份证号码
  71. if (sId[sId.length - 1] != last) {
  72. return {
  73. data: '你输入的身份证号长度或格式有误',
  74. state: false
  75. }
  76. }
  77. return {
  78. data: '',
  79. state: true
  80. }
  81. }
  82. //手机号校验
  83. const isPhone = (phone) => {
  84. if (!(/^1[3-9]\d{9}$/).test(phone)) {
  85. return {
  86. data: '手机号只能由11位数字组成',
  87. state: false
  88. }
  89. } else {
  90. return {
  91. data: '',
  92. state: true
  93. }
  94. }
  95. }
  96. //短号校验
  97. const isCornet = (sId) => {
  98. if (!/^(?!.*-.*-)[-\d]{3,11}$/.test(sId)) {
  99. return {
  100. data: '长度或格式未达标',
  101. state: false
  102. }
  103. } else {
  104. return {
  105. data: '',
  106. state: true
  107. }
  108. }
  109. }
  110. //中文字符校验
  111. const isChineseWord = (str) => {
  112. if (!(/^[\u4e00-\u9fa5]*$/).test(str)) {
  113. return {
  114. data: '姓名只能由中文组成',
  115. state: false
  116. }
  117. } else {
  118. return {
  119. data: '',
  120. state: true
  121. }
  122. }
  123. }
  124. //获得对应时间格式
  125. const formatTime = (time, type) => {
  126. var date = new Date(time)
  127. var format = type || 'YYYY-MM-DD HH:NN:SS';
  128. const year = date.getFullYear();
  129. const month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
  130. const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  131. const hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  132. const minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  133. const second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  134. format.indexOf('YYYY') > -1 ? format = format.replace('YYYY', year) : '';
  135. format.indexOf('MM') > -1 ? format = format.replace('MM', month) : '';
  136. format.indexOf('DD') > -1 ? format = format.replace('DD', day) : '';
  137. format.indexOf('HH') > -1 ? format = format.replace('HH', hour) : '';
  138. format.indexOf('NN') > -1 ? format = format.replace('NN', minute) : '';
  139. format.indexOf('SS') > -1 ? format = format.replace('SS', second) : '';
  140. return format;
  141. }
  142. //获取url参数组成json
  143. const GetQueryJson = e => {
  144. if (e == 'undefined') {
  145. return {}
  146. } else {
  147. let url = e; //获取url
  148. let arr = []; //存储参数的数
  149. let res = {}; //存储最终JSON结果对象
  150. arr = url.split('?')[1].split('&'); //获取浏览器地址栏中的参数
  151. for (let i = 0; i < arr.length; i++) { //遍历参数
  152. if (arr[i].indexOf('=') != -1) { //如果参数中有值
  153. let str = arr[i].split('=');
  154. res[str[0]] = str[1];
  155. } else { //如果参数中无值
  156. res[arr[i]] = '';
  157. }
  158. };
  159. return res;
  160. }
  161. }
  162. const getWxLogin = () => {
  163. wx.login({
  164. success: res => {
  165. app.data.wxCode = res.code
  166. }
  167. })
  168. }
  169. //组装 adminInfo 信息成统一格式(二维码取得的临时信息)
  170. const formatAdminInfo = (data) => {
  171. return {
  172. buildingAdminId: data.visiteeBuildingId || data.adminId || "",
  173. buildingName: data.visiteeBuilding || data.name || "",
  174. companyAdminId: data.visiteeCompanyId || "",
  175. companyName: data.visiteeCompany || "",
  176. official: false
  177. }
  178. }
  179. module.exports = {
  180. isCardID: isCardID,
  181. isPhone: isPhone,
  182. isChineseWord: isChineseWord,
  183. formatTime: formatTime,
  184. GetQueryJson: GetQueryJson,
  185. isCornet,
  186. getWxLogin,
  187. formatAdminInfo
  188. }