func.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import config from '@config/index.js';
  2. //函数类工具,对函数进行操作 返回函数
  3. //延时操作
  4. export function debounce(func, delay) {
  5. let timer = null;
  6. return function (...args) {
  7. if (timer) {
  8. clearTimeout(timer);
  9. }
  10. timer = setTimeout(() => {
  11. func.apply(this, args);
  12. }, delay);
  13. }
  14. }
  15. // 时间戳转换日期
  16. export function dateParser(timestamp,link = '-'){
  17. let time = new Date(timestamp);
  18. let year = time.getFullYear();
  19. let month = time.getMonth()+1;
  20. let date = time.getDate();
  21. let hour = time.getHours().toString().padStart(2,'0');
  22. let minute = time.getMinutes().toString().padStart(2,'0');
  23. // let result = year+link+month+link+date;
  24. let result = year+link+month+link+date+' '+hour+':'+minute;
  25. return result;
  26. }
  27. //时间搓转换年龄
  28. export const getAge = (time) => {
  29. const birthday = new Date(time),
  30. year = birthday.getFullYear(),
  31. month = birthday.getMonth() + 1,
  32. day = birthday.getDate(),
  33. now = new Date(),
  34. now_year = now.getFullYear(),
  35. now_month = now.getMonth() + 1,
  36. now_day = now.getDate();
  37. let age= now_year - year;
  38. if (now_month > month) {
  39. age += 1;
  40. } else if (now_month === month) {
  41. if (now_day >= day) {
  42. age += 1;
  43. }
  44. }
  45. return age;
  46. };
  47. //获取URL参数-返回json对象
  48. export const parseUrl = (url) => {
  49. const r = url.substr(1).split("&"),
  50. obj = {};
  51. r.forEach((v) => {
  52. const index = v.indexOf('=');
  53. if (index > -1) {
  54. obj[v.substring(0, index)] = v.substring(index + 1);
  55. }
  56. });
  57. return obj;
  58. }
  59. //字符串去空格
  60. export const strTrim = (str) =>{
  61. return str.replace(/&nbsp;|<div>|<\/div>|<br>|\s/g,'');
  62. };
  63. //获取组合组件已填文字填入saveText
  64. function getSaveText(data){//console.log(data)
  65. const arr = data.questionMapping.map((it)=>{
  66. return it.value?(it.labelPrefix||'')+(it.value||'')+(it.labelSuffix||''):'';
  67. });
  68. return arr.join('');
  69. }
  70. //添加自由文本标签
  71. function notTextLabel(label){
  72. return +label.tagType!==8;
  73. }
  74. /*
  75. * 给标签组添加自由文本标签
  76. * 入参:arr源数组,
  77. * noPre是否不添加前置文本标签,默认false即添加
  78. * noEnd是否不添加后置文本标签,默认false即添加
  79. * ifEmpty是否添加空标签,默认true即添加,传false添加逗号,如查体
  80. * */
  81. export const fullfillText = (arr,noPre=false,noEnd=false,ifEmpty=true)=>{
  82. let newArr =[],
  83. pre={},
  84. textLabel={},
  85. _textLabel={},
  86. notText = true,
  87. saveText=[],
  88. tempText = '',
  89. value = '';
  90. arr.map((it,i)=>{
  91. notText = notTextLabel(it);
  92. value = it.value||'';
  93. textLabel = JSON.parse(config.textLabel);
  94. _textLabel = JSON.parse(config._textLabel);
  95. if(i===0){
  96. //第一个标签不是文本标签时在前面添加文本标签
  97. if(!noPre&&notText){
  98. newArr.push(textLabel);
  99. saveText.push('');
  100. }
  101. newArr.push(it);
  102. if(it.tagType != 3){
  103. tempText = value?it.labelPrefix+value+it.labelSuffix:'';
  104. tempText = notText?tempText:it.value||it.name;
  105. }else{
  106. tempText = getSaveText(it);
  107. }
  108. saveText.push(tempText);
  109. }else{
  110. pre = arr[i-1];
  111. //如果本身不是文本标签且前面一个也不是文本标签,该标签前面添加文本标签
  112. if(notTextLabel(pre)&&notText){
  113. // newArr.push(textLabel,it);
  114. ifEmpty?newArr.push(textLabel,it):newArr.push(_textLabel,it);
  115. if(it.tagType != 3) {
  116. tempText = value ? it.labelPrefix + value + it.labelSuffix : '';
  117. }else{
  118. tempText = getSaveText(it);
  119. }
  120. saveText.push("",tempText);
  121. //最后一个非文本标签,后面添加一个文本标签
  122. /*if(!noEnd&&i===arr.length-1){
  123. newArr.push(textLabel);
  124. saveText.push("");
  125. }*/
  126. }else{ //本身是或者前面是文本标签时,前面不添加文本标签
  127. newArr.push(it);
  128. if(it.tagType != 3) {
  129. tempText = value ? it.labelPrefix + value + it.labelSuffix : '';
  130. // tempText = notText?tempText:it.value||it.name;
  131. tempText = notText ? tempText : (it.value || it.value == "" ? it.value : it.name);
  132. }else{
  133. tempText = getSaveText(it);
  134. }
  135. saveText.push(tempText);
  136. }
  137. if(notText&&!noEnd&&i===arr.length-1){//最后一个非文本标签,后面添加一个文本标签
  138. //不能用上面的变量textLabel,因为上一个if可能也进了,这样就是同一个对象,值改变时会同步
  139. newArr.push(JSON.parse(config.textLabel));
  140. saveText.push("");
  141. }
  142. }
  143. });
  144. return {newArr,saveText};
  145. };
  146. //获取标签index,入参:病例项index+标签index+标签内index
  147. export const getLabelIndex = (index)=>{
  148. let ikey = '';
  149. if(index.length == 3){
  150. ikey = index.substr(1,1);
  151. }else if(index.length == 4){
  152. ikey = index.substr(1,2);
  153. }else if(index.length == 5){
  154. ikey = index.substr(1,3);
  155. }
  156. return ikey;
  157. };
  158. export const getWindowInnerHeight = ()=>{
  159. if(window.innerHeight!=undefined){
  160. return window.innerHeight;
  161. }else{
  162. let by = document.body, ele = document.documentElement;
  163. return Math.min(by.clientHeight,ele.clientHeight);
  164. }
  165. };
  166. export const getWindowInnerWidth = ()=>{
  167. let width = window.innerWidth || document.body.clientWidth || document.documentElement.clientWidth
  168. return width
  169. };
  170. export const getIds = (data)=>{
  171. let ids = [];
  172. data&&data.forEach((it,i)=>{
  173. ids.push(it.id);
  174. })
  175. return ids;
  176. }