utils.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. import $ from 'jquery';
  2. function getWindowInnerHeight(){
  3. if(window.innerHeight!=undefined){
  4. return window.innerHeight;
  5. }else{
  6. let by = document.body, ele = document.documentElement;
  7. return Math.min(by.clientHeight,ele.clientHeight);
  8. }
  9. };
  10. function getWindowInnerWidth(){
  11. let width = window.innerWidth || document.body.clientWidth || document.documentElement.clientWidth
  12. return width
  13. };
  14. function getPageCoordinate(event,stic){//获取鼠标点击的位置
  15. let e = event || window.event;
  16. var scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
  17. var scrollY = document.documentElement.scrollTop || document.body.scrollTop;
  18. var x = e.pageX || (e.clientX + scrollX);
  19. var y = (e.pageY+(stic?25:0)) || (e.clientY + scrollY + (stic?25:0));//弹窗位置偏下25
  20. let obj = {
  21. boxLeft : x ,
  22. boxTop : y+18
  23. }
  24. return obj;
  25. }
  26. /**
  27. * @param {拖动元素/清除拖动事件监听} type add:添加事件监听 del:移除事件监听
  28. * @param {展开元素id} addDom
  29. */
  30. let width = '',//屏幕宽度
  31. height = '', //屏幕高度
  32. dragWrapper = '',//拖拽元素
  33. mouseX = 0,//鼠标距离页面左侧
  34. mouseY = 0,//鼠标距离页面上部
  35. dragX = 0,//鼠标可拖拽到左边的最大距离(元素定位左侧距离)
  36. dragY = 0,//鼠标可拖拽到上边的最大距离(元素定位上边距离)
  37. isDrag = false,//元素是否处于拖拽中
  38. addPart = null;//展开区域,这个展开的宽度应当是固定宽度了
  39. /**
  40. *
  41. * @param {事件对象} event
  42. * @param {被拖动的元素} dragModalWrap
  43. * @param {可拖动的区域} dragModalTitle
  44. */
  45. function handleMove(event) {
  46. let wrap = $("#"+dragWrapper); //被拖拽的元素
  47. let dragDes = $("#"+addPart); //展開区域
  48. let page = getPageCoordinate(event, '0');//获取鼠标位置
  49. let maxDragX = 0,dragDesWidth = 0;
  50. let wrapHeight = wrap[0].offsetHeight;//直接用.height()获取的高度不准确
  51. let wrapWidth = wrap[0].offsetWidth;
  52. let maxDragY = height - wrapHeight; //最大可拖拽y方向距离
  53. if (dragDes[0]) { //存在就有类似治疗方案的弹窗,最大可推拽x方向距离
  54. dragDesWidth = dragDes[0].offsetWidth//展开元素的宽度
  55. maxDragX = width - wrapWidth - dragDesWidth
  56. } else {
  57. maxDragX = width - wrapWidth
  58. }
  59. if (isDrag) {
  60. dragX = page.boxLeft - mouseX //鼠标可拖拽到左边的最大距离
  61. dragY = page.boxTop - mouseY //鼠标可拖拽到上边的最大距离
  62. if (dragX < 300) { //不能超出左边界
  63. dragX = 300
  64. }
  65. if (dragY < 100) { //不能超出上边界
  66. dragY = 100
  67. }
  68. if (dragDes[0]) {
  69. if (width - dragX - dragDesWidth < wrapWidth) { //不能超出右边界
  70. dragX = maxDragX
  71. }
  72. } else {
  73. if (width - dragX < wrapWidth) { //不能超出右边界
  74. dragX = maxDragX
  75. }
  76. }
  77. if (height - dragY < wrapHeight) { //不能超出下边界
  78. dragY = maxDragY
  79. }
  80. // isMove = true
  81. wrap.css({
  82. left: dragX + 'px',
  83. marginLeft: 0 + 'px',
  84. marginTop: 0 + 'px',
  85. top: dragY + 'px',
  86. bottom:'auto',
  87. right:'inherit',
  88. })
  89. }
  90. }
  91. /**
  92. * 鼠标弹起弹窗不在跟随鼠标移动
  93. */
  94. function handleUp(){
  95. isDrag = false;
  96. return
  97. }
  98. export default {
  99. //判断字符串是否能转成json
  100. isJsonStr(str) {
  101. if (typeof str === 'string') {
  102. try {
  103. const myStr = str.replace(/^\s*/, '');
  104. const obj = JSON.parse(str);
  105. if (myStr[0] === '{') {
  106. return true;
  107. } else {
  108. return false;
  109. }
  110. } catch (e) {
  111. return false;
  112. }
  113. }
  114. },
  115. tranformDate(time,str,flag='-'){
  116. if(!time){
  117. return time;
  118. }
  119. const year=time.getFullYear();
  120. const month = time.getMonth()+1;
  121. const day = time.getDate();
  122. return year+flag+(month>9?month:'0'+month)+flag+(day>9?day:'0'+day)+str;
  123. },
  124. //获取两个时间间隔天数
  125. getTimeRangeDay(timeArr) {
  126. if(!timeArr||timeArr.length==0){
  127. return null;
  128. }
  129. return (new Date(timeArr[1]) - new Date(timeArr[0])) / (1000 * 60 * 60 * 24);
  130. },
  131. //导出接口返回流下载
  132. downloadExportedData(data, fileName) {
  133. var blob = new Blob([data], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'}); //application/vnd.openxmlformats-officedocument.spreadsheetml.sheet这里表示xlsx类型
  134. var downloadElement = document.createElement('a');
  135. var href = window.URL.createObjectURL(blob); //创建下载的链接
  136. downloadElement.href = href;
  137. downloadElement.download = fileName; //下载后文件名
  138. document.body.appendChild(downloadElement);
  139. downloadElement.click(); //点击下载
  140. document.body.removeChild(downloadElement); //下载完成移除元素
  141. window.URL.revokeObjectURL(href);
  142. },
  143. emailReg: /^([a-zA-Z0-9_\.-]+)@([\da-zA-Z\.-]+)\.([a-z\.]{2,6})$/,
  144. // pswReg: /^(?![0-9]+$)(?![a-zA-Z]+$)/,
  145. pswReg: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z\W_]{6,16}$/,//与后台保持一致
  146. phoneReg: new RegExp(/^((13|14|15|16|17|18|19)\d{9})$/),
  147. url:/[a-zA-z]+:\/\/[^\s]*/,
  148. getDateStr(string,str){
  149. var str_before = string.split(str)[0];
  150. var str_after = string.split(str)[1];
  151. },
  152. exportCondition(time){
  153. const rangeDay = this.getTimeRangeDay(time);
  154. if (!time||time.length==0) {
  155. return '请先选择导出时间';
  156. }
  157. if (rangeDay > 30) {
  158. return '目前只能导出30天内数据,请重新筛选时间';
  159. }
  160. return true;
  161. },
  162. /**
  163. *
  164. * @param {arr} 被操作的数组
  165. * @param {val} 需要剔除或者判断的值
  166. * @param {type} 是剔除(1)还是判断(2)是否存在,不存在返回[]
  167. * @param {des} 1数組元素是对象
  168. *
  169. */
  170. filterArr(arr,val,type,des){ //数组剔除指定元素
  171. if(type == 1){
  172. if(des){
  173. return arr.filter(item => item.id != val) //判断id
  174. }else{
  175. return arr.filter(item => item != val) //判断值
  176. }
  177. }else{
  178. let tmp = arr.filter(item => JSON.stringify(item) == JSON.stringify(val)) //判读是否存在
  179. if(JSON.stringify(tmp) == '[]'){
  180. return false;
  181. }else{
  182. return true;
  183. }
  184. }
  185. },
  186. /**
  187. *
  188. * @param {dataArr} 右侧选中的数据二维数组
  189. * @param {tmpArrAdd} 既往史自动生成的组合标签的id存储
  190. * @param {type} 填写单类型
  191. */
  192. dataRecombination(dataArr,type,tmpArrAdd){
  193. // console.log(11111,tmpArrAdd,dataArr)
  194. let tmpArr = [];
  195. for(let i = 0;i < dataArr.length;i++){
  196. let tmpData = dataArr[i];
  197. let mapping = {
  198. "sonQuestion": "",
  199. "position": 0, //默认值,传0
  200. "formPosition": 1, // 填写单显示位置(0:左, 1:上)
  201. "exclusionType": 1, //互斥:1
  202. "text": "", //如果有文本内容,上面的值都置空
  203. "symptomType": 0, //0:默认值 1:主症状特有 2:伴随症状特有
  204. }
  205. let sonQuestionMap = { //目前只在既往史中用到,同层数据置空
  206. "id": "",
  207. "questionMappings": []
  208. }
  209. if(type == 6 && tmpData.length > 0){//既往史
  210. let sonMappingArr=[];
  211. for(let j = 0; j < tmpData.length;j++){ //多个标签
  212. let sonMappingObj = {};
  213. if(i == 0){
  214. mapping.formPosition = 1;
  215. mapping.exclusionType = 1;
  216. mapping.sonQuestion = tmpData[j];
  217. }else if(i == 1){
  218. mapping.formPosition = 0;
  219. mapping.exclusionType = 0;
  220. mapping.sonQuestion = tmpData[j];
  221. }else{
  222. mapping.formPosition = 0;
  223. mapping.exclusionType = 0;
  224. mapping.sonQuestion = tmpArrAdd[i][0] || ''; //既往史新增标签的id
  225. sonMappingObj.sonQuestion = tmpData[j]; //{sonQuestion:18}
  226. sonQuestionMap.id = tmpArrAdd[i][0] || ''; //{sonQuestion:18}
  227. sonQuestionMap.questionMappings = sonMappingArr; //{id:"",[{sonQuestion:18},{sonQuestion:18}]}
  228. mapping.questionMappingSon = sonQuestionMap;
  229. }
  230. sonMappingArr.push(sonMappingObj); //[{sonQuestion:18},{sonQuestion:19}]
  231. }
  232. tmpArr.push(mapping)
  233. }else if(type == 2 && tmpData.length > 0){//杂音
  234. if(i == 0){
  235. mapping.formPosition = 1;
  236. mapping.exclusionType = 1;
  237. }else{
  238. mapping.formPosition = 0;
  239. mapping.exclusionType = 0;
  240. }
  241. mapping.sonQuestion = tmpData[0]; //{sonQuestion:18}
  242. tmpArr.push(mapping)
  243. }else if (type == 4 || type == 3 || type == 7) { //横铺展开(如咳嗽)
  244. mapping.formPosition = 0;
  245. mapping.exclusionType = 0;
  246. mapping.symptomType = dataArr[i].symptomType
  247. if(dataArr[i].type){
  248. if(dataArr[i].type === 'input') {
  249. if(dataArr[i].text) { //过滤掉输入框未输入内容项
  250. mapping.text = dataArr[i].text
  251. tmpArr.push(mapping)
  252. }
  253. } else {
  254. mapping.sonQuestion = dataArr[i].id
  255. tmpArr.push(mapping)
  256. }
  257. }
  258. } else if(type == 11) { //推送症状类型(添加症状)
  259. mapping.formPosition = 0;
  260. mapping.exclusionType = 0;
  261. mapping.sonQuestion = dataArr[i].id
  262. tmpArr.push(mapping)
  263. }
  264. }
  265. return tmpArr;
  266. },
  267. /**
  268. * 单选数据转换提交
  269. * **/
  270. simpleOptionData(data){
  271. let arr = [];
  272. data.map((it,i)=>{
  273. let code = '';
  274. let abnormal = 0
  275. if(it.isError){
  276. abnormal = 1;
  277. }
  278. if(it.isNone){
  279. code = '2';
  280. }
  281. if(it.isBan){
  282. code = '1';
  283. }
  284. if(it.name){
  285. arr.push({name:it.name,defaultSelect:it.defaultSelect?'1':'0',code,abnormal,remark:'单选项-'+it.name});
  286. }
  287. });
  288. return arr;
  289. },
  290. /**
  291. *将提交的参数转换成对应的组件form中的key
  292. *
  293. */
  294. transformKeys(data, tagFlag){ //tagFlag:独立标签还是标签组 (2是标签组)
  295. const maps = {
  296. "controlType": 'region2', //控件类型(0:默认值 1:下拉单选 2:下拉多选 6:文本框 7:数字键盘文本框 99:联合推送)
  297. // "tagType": 'region2',(这个注释掉,不要解开了!!!!)
  298. "id": "id", //新增id置空
  299. "type": 'region1', //标签归属
  300. "tagName": 'region3', //系统名称
  301. "name": 'region4', //界面名称
  302. "joint": 'region5', //标签间的连接符
  303. "subType": 'region6', //0:可以,1:不可以(当项目检索)
  304. "sexType": 'region7', //1:男,2:女,3:通用
  305. "ageBegin": 'region8', //最小年龄
  306. "ageEnd": 'region9', //最大年龄
  307. "copyType":'region10', //是否复制主标签
  308. "showAdd":'region11', //是否复制当前模块
  309. "itemType": 'region12',
  310. "disType": 'region13',
  311. "labelPrefix":'prefix', //前缀
  312. "labelSuffix":'suffix', //后缀
  313. "maxValue":'maxNormalVal', //前缀
  314. "minValue":'minNormalVal', //后缀
  315. "specFlag": 'specFlag', //标记状态(0:没有标记,1:主诉高亮,2:诱因记录)
  316. "selFlag":'selFlag',
  317. };
  318. if (tagFlag == 2) {
  319. maps["tagType"] = 'region2'
  320. }
  321. let obj = {};
  322. for(let it in maps){
  323. obj[maps[it]] = typeof data[it]=='number'?data[it]+'':data[it];
  324. }
  325. return obj;
  326. },
  327. /**
  328. * 模板数据提交转化函数
  329. */
  330. orderTemplateData(data){
  331. let tmpArr = [];
  332. for(let i =0;i<data.length;i++){
  333. let obj={
  334. "flag": '',
  335. "questionId": '',
  336. "relationModule": '',
  337. "text": ""
  338. };
  339. if(data[i].type == 'sub'){ //这是子模板
  340. obj.relationModule = parseInt(data[i].id)
  341. }else if(data[i].type == 'input'){ //这个是输入框文字
  342. if(data[i].text){ //去掉空输入框
  343. obj.text = data[i].text
  344. }
  345. }else{ //这个是标签
  346. obj.questionId = data[i].id
  347. }
  348. obj.flag = data[i].flag || ''
  349. tmpArr.push(obj)
  350. }
  351. return tmpArr;
  352. },
  353. /*返回n个row的数组
  354. *n:Number
  355. * row:Object
  356. * orderName:编号字段名称,默认orderNo
  357. */
  358. getInitRow(row,n,orderName){
  359. let arr = [];
  360. let obj = {};
  361. for(let i=0;i<n;i++){
  362. obj = Object.assign({},row);
  363. obj[orderName||'orderNo'] = i;
  364. arr.push(obj);
  365. }
  366. return arr;
  367. },
  368. /**
  369. * 预问诊-字段转换
  370. *
  371. * **/
  372. transPretreatKeys(org,isDpt){
  373. let maps = {
  374. name:'region4',
  375. description:'region5',
  376. itemType:'region12',
  377. sexType:'region7',
  378. ageBegin:'region8',
  379. ageEnd:'region9',
  380. tagName:'region3',
  381. //tagType:isDpt?'':'region2',
  382. //controlType:isDpt?'region2':'',
  383. type:'region1',
  384. url:'region13',
  385. specFlag:'specFlag',
  386. required: 'required',
  387. explains:'explains',
  388. flag:'flag'
  389. };
  390. isDpt?maps.controlType = 'region2':maps.tagType = 'region2';
  391. let obj = {};
  392. for(let i in org){
  393. if(maps[i]){
  394. obj[maps[i]] = org[i]+''==='null'?'':org[i]+'';
  395. }
  396. }
  397. return obj;
  398. },
  399. //获取图片名称
  400. getImgName(url){
  401. const arr = url.split("/");
  402. return arr[arr.length-1];
  403. },
  404. dragBox(dragWrap,dragTop,type,addDom,img) {
  405. let drag = $("#"+dragTop); //拖拽区域
  406. let wrap = $("#"+dragWrap); //被拖拽的元素
  407. width = getWindowInnerWidth();
  408. height = getWindowInnerHeight();
  409. addPart = addDom||null;
  410. dragWrapper = dragWrap;
  411. if (type === 'del') {
  412. $(document).off('mousemove', handleMove)
  413. $(document).off('mouseup', handleUp)
  414. } else {
  415. drag.on('mousedown', function (event) {
  416. let lenHeight = wrap.height()
  417. if(img){
  418. $("#preImg").css("height",(lenHeight)+'px')
  419. $("#dragModalWrap").css("height",(lenHeight)+'px')
  420. }
  421. let page = getPageCoordinate(event, '0');
  422. mouseX = page.boxLeft - wrap.offset().left; //鼠标到拖拽元素的左边界的距离
  423. mouseY = page.boxTop - wrap.offset().top; //鼠标到拖拽元素的上边界的距离
  424. isDrag = true
  425. })
  426. $(document).on('mousemove', handleMove)
  427. $(document).on('mouseup', handleUp)
  428. }
  429. },
  430. getCurrentPage(currentChange, total, pageSize) {
  431. const currentPage = currentChange > Math.ceil(total/pageSize) ? Math.ceil(total/pageSize) : currentChange
  432. return currentPage;
  433. }
  434. }