func.js 6.3 KB

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