utils.js 6.3 KB

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