func.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. * showInCheck是否默认在查体中展开
  81. * */
  82. export const fullfillText = (arr,noPre=false,noEnd=false,ifEmpty=true)=>{
  83. let newArr =[],
  84. pre={},
  85. textLabel={},
  86. _textLabel={},
  87. notText = true,
  88. saveText=[],
  89. tempText = '',
  90. value = '',
  91. item={},
  92. checkHiddenDefault=false;
  93. arr.map((it,i)=>{
  94. notText = notTextLabel(it);
  95. value = it.value||'';
  96. textLabel = !ifEmpty&&i==0?Object.assign({},JSON.parse(config.textLabel),{showInCheck:true}):JSON.parse(config.textLabel);
  97. _textLabel = !ifEmpty&&i<config.showCheckNum+1?Object.assign({},JSON.parse(config._textLabel),{showInCheck:true}):JSON.parse(config._textLabel);
  98. if(i===0){
  99. //第一个标签不是文本标签时在前面添加文本标签
  100. if(!noPre&&notText){
  101. newArr.push(textLabel);
  102. saveText.push('');
  103. }
  104. item = ifEmpty?it:Object.assign({},it,{showInCheck:true});
  105. newArr.push(item);
  106. if(it.tagType != 3){
  107. tempText = value?it.labelPrefix+value+it.labelSuffix:'';
  108. tempText = notText?tempText:it.value||it.name;
  109. }else{
  110. tempText = getSaveText(it);
  111. }
  112. saveText.push(tempText);
  113. }else{
  114. pre = arr[i-1];
  115. item = !ifEmpty&&i<config.showCheckNum?Object.assign({},it,{showInCheck:true}):it;
  116. //判断单选项是否有默认选中,位置在隐藏区域时,查体所有标签展示
  117. if(!ifEmpty&&!checkHiddenDefault&&i>config.showCheckNum&&+it.tagType===1&&(+it.controlType===0||+it.controlType===1)){
  118. if(it.questionDetailList.find((it)=>it.defaultSelect=='1')){
  119. checkHiddenDefault=true;
  120. }
  121. }
  122. //如果本身不是文本标签且前面一个也不是文本标签,该标签前面添加文本标签
  123. if(notTextLabel(pre)&&notText){
  124. // newArr.push(textLabel,it);
  125. ifEmpty?newArr.push(textLabel,it):newArr.push(_textLabel,item);
  126. if(it.tagType != 3) {
  127. tempText = value ? it.labelPrefix + value + it.labelSuffix : '';
  128. }else{
  129. tempText = getSaveText(it);
  130. }
  131. saveText.push("",tempText);
  132. }else{ //本身是或者前面是文本标签时,前面不添加文本标签
  133. newArr.push(item);
  134. if(it.tagType != 3) {
  135. tempText = value ? it.labelPrefix + value + it.labelSuffix : '';
  136. // tempText = notText?tempText:it.value||it.name;
  137. tempText = notText ? tempText : (it.value || it.value == "" ? it.value : it.name);
  138. }else{
  139. tempText = getSaveText(it);
  140. }
  141. saveText.push(tempText);
  142. }
  143. if(notText&&!noEnd&&i===arr.length-1){//最后一个非文本标签,后面添加一个文本标签
  144. //不能用上面的变量textLabel,因为上一个if可能也进了,这样就是同一个对象,值改变时会同步
  145. newArr.push(JSON.parse(config.textLabel));
  146. saveText.push("");
  147. }
  148. }
  149. });
  150. return {newArr,saveText,checkHiddenDefault};
  151. };
  152. //获取标签index,入参:病例项index+标签index+标签内index
  153. export const getLabelIndex = (index)=>{
  154. let ikey = '';
  155. if(index.length == 3){
  156. ikey = index.substr(1,1);
  157. }else if(index.length == 4){
  158. ikey = index.substr(1,2);
  159. }else if(index.length == 5){
  160. ikey = index.substr(1,3);
  161. }
  162. return ikey;
  163. };
  164. export const getWindowInnerHeight = ()=>{
  165. if(window.innerHeight!=undefined){
  166. return window.innerHeight;
  167. }else{
  168. let by = document.body, ele = document.documentElement;
  169. return Math.min(by.clientHeight,ele.clientHeight);
  170. }
  171. };
  172. export const getWindowInnerWidth = ()=>{
  173. let width = window.innerWidth || document.body.clientWidth || document.documentElement.clientWidth
  174. return width
  175. };
  176. export const getIds = (data)=>{
  177. let ids = [];
  178. data&&data.forEach((it,i)=>{
  179. ids.push(it.id);
  180. })
  181. return ids;
  182. }