utils.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // #ifdef APP-PLUS
  2. import {
  3. judgePermission
  4. } from './permission'
  5. // #endif
  6. import Vue from 'vue';
  7. export const addressCode = function(address) {
  8. let rule = /.+?(省|市|自治区|自治州|县|区)/g;
  9. const targetValues = address.match(rule)
  10. return targetValues;
  11. }
  12. //根据身份证号计算年龄
  13. export const getAgeByIdCard = function(idCard) {
  14. const sexAndAge = {}
  15. //获取用户身份证号码
  16. const userCard = idCard
  17. //如果用户身份证号码为undefined则返回空
  18. if (!userCard) {
  19. return sexAndAge
  20. }
  21. // 获取出生日期
  22. const yearBirth = userCard.substring(6, 10)
  23. const monthBirth = userCard.substring(10, 12)
  24. const dayBirth = userCard.substring(12, 14)
  25. // 获取当前年月日并计算年龄
  26. const myDate = new Date()
  27. const monthNow = myDate.getMonth() + 1
  28. const dayNow = myDate.getDate()
  29. let age = myDate.getFullYear() - yearBirth
  30. if (monthNow < monthBirth || (monthNow == monthBirth && dayNow < dayBirth)) {
  31. age--
  32. }
  33. // 得到年龄
  34. sexAndAge.age = age
  35. return sexAndAge.age
  36. }
  37. export const findDefaultProject = function(data) {
  38. let result = {};
  39. let list = [];
  40. function traverse(items) {
  41. for (const item of items) {
  42. if (item.isDefault) {
  43. if (!item.children || item.children.length === 0) {
  44. // 如果没有children或者children为空,则直接赋值
  45. result = {
  46. ...item
  47. };
  48. } else {
  49. // 如果有children,那么递归检查children
  50. traverse(item.children);
  51. list = item.children;
  52. }
  53. result.parentCode = item.projectCode;
  54. result.parentName = item.projectName;
  55. }
  56. }
  57. }
  58. traverse(data); // 从给定的数据开始遍历
  59. return {
  60. result,
  61. list
  62. };
  63. }
  64. //字典匹配
  65. export const dictionaryMatch = function(list, code) {
  66. if (list && code) {
  67. let codeList = list
  68. const codeName = codeList.find(item => item.dictValue == code);
  69. return codeName.dictTag;
  70. }
  71. }
  72. //根据身份证号计算性别
  73. export const getGenderFromIdCard = function(idCard) {
  74. // 判断身份证号长度是否正确
  75. if (idCard.length !== 18) {
  76. return "无效身份证号";
  77. }
  78. // 获取身份证号倒数第二位数字
  79. var digit = parseInt(idCard.charAt(idCard.length - 2));
  80. // 判断性别
  81. if (digit % 2 === 0) {
  82. return "女";
  83. } else {
  84. return "男";
  85. }
  86. }
  87. // 身份证格式校验
  88. export const checkIdCard = function(sIdCard) {
  89. //Wi 加权因子 Xi 余数0~10对应的校验码 Pi省份代码
  90. let Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],
  91. Xi = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2],
  92. Pi = [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,
  93. 54,
  94. 61, 62, 63, 64, 65, 71, 81, 82, 91
  95. ],
  96. checkStatus = 0;
  97. // 检查身份证长度
  98. if (sIdCard.length == 18) {
  99. checkStatus += 1;
  100. }
  101. //检验输入的省份编码是否有效
  102. if (checkStatus >= 1) {
  103. let p2 = sIdCard.substr(0, 2);
  104. for (let i = 0; i < Pi.length; i++) {
  105. if (Pi[i] == p2) {
  106. checkStatus += 1;
  107. }
  108. }
  109. }
  110. //检验18位身份证号码出生日期是否有效
  111. //parseFloat过滤前导零,年份必需大于等于1900且小于等于当前年份,用Date()对象判断日期是否有效。
  112. if (checkStatus >= 2) {
  113. let year = parseFloat(sIdCard.substr(6, 4));
  114. let month = parseFloat(sIdCard.substr(10, 2));
  115. let day = parseFloat(sIdCard.substr(12, 2));
  116. let checkDay = new Date(year, month - 1, day);
  117. let nowDay = new Date();
  118. if (1900 <= year && year <= nowDay.getFullYear() && month == (checkDay.getMonth() + 1) && day == checkDay
  119. .getDate()) {
  120. checkStatus += 1;
  121. }
  122. }
  123. //检验校验码是否有效
  124. if (checkStatus >= 3) {
  125. let aIdCard = sIdCard.split("");
  126. let sum = 0;
  127. for (let j = 0; j < Wi.length; j++) {
  128. sum += Wi[j] * aIdCard[j]; //线性加权求和
  129. }
  130. let index = sum % 11; //求模,可能为0~10,可求对应的校验码是否于身份证的校验码匹配
  131. if (Xi[index] == aIdCard[17].toUpperCase()) {
  132. checkStatus += 1;
  133. }
  134. }
  135. if (checkStatus == 4) {
  136. return true;
  137. } else {
  138. return false;
  139. }
  140. };
  141. /**
  142. * 时间转换为XX前
  143. */
  144. export const clickDateDiff = function(value) {
  145. var result;
  146. var minute = 1000 * 60;
  147. var hour = minute * 60;
  148. var day = hour * 24;
  149. var month = day * 30;
  150. var now = new Date().getTime();
  151. var diffValue = parseInt(now) - parseInt(value);
  152. if (diffValue < 0) {
  153. return;
  154. }
  155. var monthC = diffValue / month;
  156. var weekC = diffValue / (7 * day);
  157. var dayC = diffValue / day;
  158. var hourC = diffValue / hour;
  159. var minC = diffValue / minute;
  160. if (monthC >= 1) {
  161. result = "" + parseInt(monthC) + '月前';
  162. } else if (weekC >= 1) {
  163. result = "" + parseInt(weekC) + '周前';
  164. } else if (dayC >= 1) {
  165. result = "" + parseInt(dayC) + '天前';
  166. } else if (hourC >= 1) {
  167. result = "" + parseInt(hourC) + '小时前';
  168. } else if (minC >= 1) {
  169. result = "" + parseInt(minC) + '分钟前';
  170. } else {
  171. result = '刚刚';
  172. }
  173. return result;
  174. };
  175. /**
  176. * 时间戳转换为想要的时间格式
  177. */
  178. //时间戳转换为时间 format('yyyy-MM-dd hh:mm:ss')
  179. //时间格式转换
  180. Date.prototype.format = function(fmt = 'yyyy-MM-dd hh:mm:ss') { //author: meizz
  181. var o = {
  182. "M+": this.getMonth() + 1, //月份
  183. "d+": this.getDate(), //日
  184. "h+": this.getHours(), //小时
  185. "m+": this.getMinutes(), //分
  186. "s+": this.getSeconds(), //秒
  187. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  188. "S": this.getMilliseconds() //毫秒
  189. };
  190. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  191. for (var k in o)
  192. if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : ((
  193. "00" + o[
  194. k]).substr(("" + o[k]).length)));
  195. return fmt;
  196. };
  197. export const delEmptyQueryNodes = (obj) => {
  198. Object.keys(obj).forEach((key) => {
  199. let value = obj[key];
  200. (value === '' || value === null || value === undefined || value.length === 0 || Object.keys(
  201. value)
  202. .length === 0) && delete obj[key];
  203. });
  204. return obj;
  205. };