common.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. export function pxToMm(value) {
  2. // var inch = value / getDPI()[0];
  3. let scale=1754/297;
  4. // var c_value = inch * 25.4;
  5. let result=value/scale;
  6. return parseFloat(result.toFixed(4));//保留四位小数
  7. }
  8. // /**
  9. // * 像素转换成毫米
  10. // * @param {number} px - 输入的像素值
  11. // * @returns {number} - 转换后的毫米值,保留四位小数
  12. // */
  13. // export function pxToMm(px) {
  14. // if (px) {
  15. // const DPI = getDPI(); // 获取当前设备的 DPI
  16. // const MM_PER_INCH = 25.4; // 每英寸毫米数
  17. // const mmValue = (px * MM_PER_INCH) / DPI;
  18. // return parseFloat(mmValue.toFixed(4)); // 保留四位小数
  19. // } else {
  20. // console.log("px", px);
  21. // return 0;
  22. // }
  23. // }
  24. // 保留整数 小数 四舍五入
  25. export function mmToPx(num) {
  26. if(num)
  27. {
  28. // console.log("num",num);
  29. // console.log("转换后",Math.round(1754/297*num));
  30. // let scale=1754/297;
  31. // let result=(scale*num).toFixed(4);
  32. // console.log("scale",scale,result);
  33. let scale=1754/297;
  34. return parseFloat((scale*num).toFixed(4)); //高 150dpi 1754 96dpi:1120 1754*2480
  35. // return Math.round(1754/297*num)
  36. }
  37. else
  38. {
  39. console.log("num",num);
  40. return 0;
  41. }
  42. }
  43. //获取考试背景颜色值
  44. export function getExamBackgroundColor(type){
  45. const examTypesColor = {
  46. "1": '#F56C6C',//期末
  47. "2": '#3ba272',//期中
  48. "3": '#ea7acb',//模拟
  49. "4": '#fac858',//月考
  50. "5": '#995fb3',//周测
  51. "6": '#72C0DD',//平时
  52. };
  53. const result = examTypesColor[type];
  54. return result;
  55. }
  56. // 获取分析报告成绩分析中默认的20个颜色
  57. export function getScorePerformanceAnalysis() {
  58. return [
  59. "#5470C6",
  60. "#3BA272",
  61. "#EE6666",
  62. "#FAC858",
  63. "#995FB3",
  64. "#72C0DD",
  65. "#90CB75",
  66. "#EA7ACB",
  67. "#FC8451",
  68. "#65789B",
  69. "#178BD3",
  70. "#26A616",
  71. "#A6129E",
  72. "#D3A141",
  73. "#234098",
  74. "#047640",
  75. "#B43535",
  76. "#848BDC",
  77. "#D6AF83",
  78. "#84313D",
  79. ]
  80. }
  81. // 文本截断:超过指定长度显示省略号
  82. export function truncateText(text, maxLength) {
  83. if (!text) return '';
  84. if (text.length > maxLength) {
  85. return text.substring(0, maxLength) + '...';
  86. }
  87. return text;
  88. }
  89. // 去掉小数点后面为0的 如果不为0 最多只保留2位小数
  90. export function getFormatNumber(numberStr) {
  91. // 尝试将字符串转为数字
  92. const num = parseFloat(numberStr);
  93. // 检查是否为NaN(非数字情况)
  94. if (isNaN(num)) {
  95. return numberStr;
  96. }
  97. // 检查是否为整数(小数点后全为0)
  98. if (num % 1 === 0) {
  99. return num.toString();
  100. }
  101. // 保留最多2位小数,并去除末尾的0
  102. let formatted = num.toFixed(2);
  103. // 如果小数点后都是0,则返回整数形式
  104. if (formatted.endsWith('.00')) {
  105. return formatted.split('.')[0];
  106. }
  107. // 去除末尾的0(如1.50 → 1.5)
  108. formatted = formatted.replace(/\.?0+$/, '');
  109. return formatted;
  110. }
  111. /**
  112. * 格式化时间,输出:xx月xx日 时:分,去除年份、秒
  113. * @param {string|number|null} time
  114. * @returns {string}
  115. */
  116. export function formatToMonthDayHM(time) {
  117. if (time == null || time === '') {
  118. return ''
  119. }
  120. const str = String(time).trim().replace('T', ' ')
  121. const match = str.match(/^(\d{4})-(\d{2})-(\d{2})(?:\s+(\d{2}:\d{2}))?(?::\d{2})?/)
  122. if (!match) {
  123. return str
  124. }
  125. const month = match[2]
  126. const day = match[3]
  127. const hm = match[4]
  128. if (hm) {
  129. return `${month}-${day} ${hm}`
  130. }
  131. return `${month}-${day}`
  132. }
  133. /**
  134. * 去掉后端时间中的秒,页面只展示「年月日 时分」
  135. * 支持:2026-08-27 16:44:00、2026-08-27T16:44:59、2026-08-27
  136. */
  137. export function formatTimeWithoutSeconds(time) {
  138. if (time == null || time === '') {
  139. return ''
  140. }
  141. const str = String(time).trim().replace('T', ' ')
  142. const match = str.match(/^(\d{4}-\d{2}-\d{2})(?:\s+(\d{2}:\d{2}))?(?::\d{2})?/)
  143. if (!match) {
  144. return str
  145. }
  146. if (match[2]) {
  147. return `${match[1]} ${match[2]}`
  148. }
  149. return match[1]
  150. }
  151. /**
  152. * 开始时间传给后端:补写死的 00 秒
  153. * 例:2026-08-27 16:44 → 2026-08-27 16:44:00
  154. */
  155. export function toApiStartTime(time) {
  156. const base = formatTimeWithoutSeconds(time)
  157. if (!base) {
  158. return ''
  159. }
  160. if (/^\d{4}-\d{2}-\d{2}$/.test(base)) {
  161. return `${base} 00:00:00`
  162. }
  163. return `${base}:00`
  164. }
  165. /**
  166. * 结束时间传给后端:补写死的 59 秒
  167. * 例:2026-08-27 17:30 → 2026-08-27 17:30:59
  168. */
  169. export function toApiEndTime(time) {
  170. const base = formatTimeWithoutSeconds(time)
  171. if (!base) {
  172. return ''
  173. }
  174. if (/^\d{4}-\d{2}-\d{2}$/.test(base)) {
  175. return `${base} 23:59:59`
  176. }
  177. return `${base}:59`
  178. }
  179. /**
  180. * 剩余时间:结束时间减去当前时间,精确到分钟。
  181. * 满一天显示「X天X小时X分」,不足一天显示「X小时X分」或「X分」。
  182. * 已过期返回 null。
  183. *
  184. * @param {string|Date} endTime - 结束时间
  185. * @returns {string|null}
  186. */
  187. export function remainingDays(endTime) {
  188. if (!endTime) return null
  189. const end = new Date(typeof endTime === 'string' ? String(endTime).replace(/-/g, '/') : endTime)
  190. if (isNaN(end.getTime())) return null
  191. const diffMs = end.getTime() - Date.now()
  192. if (diffMs <= 0) return null
  193. const MS_PER_MINUTE = 60 * 1000
  194. const MS_PER_HOUR = 60 * MS_PER_MINUTE
  195. const MS_PER_DAY = 24 * MS_PER_HOUR
  196. const days = Math.floor(diffMs / MS_PER_DAY)
  197. const hours = Math.floor((diffMs % MS_PER_DAY) / MS_PER_HOUR)
  198. const minutes = Math.floor((diffMs % MS_PER_HOUR) / MS_PER_MINUTE)
  199. if (days > 0) {
  200. return `${days}天${hours}小时${minutes}分`
  201. }
  202. if (hours > 0) {
  203. return `${hours}小时${minutes}分`
  204. }
  205. if (minutes > 0) {
  206. return `${minutes}分`
  207. }
  208. return '不足1分'
  209. }
  210. //时间
  211. export function unioformDateTransform(date, param = "time"){
  212. if(date){
  213. let strDate = new Date(date).toLocaleString('zh-CN')
  214. let days = strDate.split(" ")[0]
  215. let time = strDate.split(" ")[1]
  216. let year = days.split("/")[0]
  217. let month = days.split("/")[1]
  218. let day = days.split("/")[2]
  219. if(Number(month)<10){
  220. month = '0'+ month
  221. }
  222. if(Number(day)<10){
  223. day = '0'+day
  224. }
  225. days = year+'-'+month+'-'+day
  226. switch(param){
  227. case "day":
  228. return days
  229. case "time":
  230. return days + " " + time
  231. }
  232. }else{
  233. return ''
  234. }
  235. }
  236. export const fileSizeConvert = (byte)=>{
  237. let kbSize = 0
  238. let mbSize = 0
  239. let gbSize = 0
  240. kbSize = (byte / 1024)
  241. if(kbSize > 1024){
  242. mbSize = kbSize / 1024
  243. if(mbSize > 1024){
  244. gbSize = mbSize / 1024
  245. return gbSize.toFixed(2) + 'GB'
  246. }else {
  247. return mbSize.toFixed(2) + 'MB'
  248. }
  249. }else{
  250. return kbSize.toFixed(2) + 'KB'
  251. }
  252. }
  253. // 计算文件大小方法,如果文件可以算出 MB,就返回 MB,否则返回 KB
  254. export const getFileSize = (fileSize) => {
  255. if (!fileSize) return '0 KB'
  256. if (fileSize >= 1024 * 1024) {
  257. fileSize = Math.round(fileSize / (1024 * 1024))
  258. return `${fileSize} MB`
  259. } else {
  260. fileSize = Math.round(fileSize / 1024)
  261. return `${fileSize} KB`
  262. }
  263. }
  264. //部门及部门下的人
  265. export const getDeptTreeData = (list) => {
  266. if (!list || list.length === 0) return []
  267. const arr = []
  268. list.forEach(one => {
  269. const { name, id, groupVOS } = one
  270. const objectOne = {
  271. label: name,
  272. id: id,
  273. children: []
  274. }
  275. arr.push(objectOne)
  276. if (groupVOS && groupVOS.length) {
  277. groupVOS.forEach(two => {
  278. const { groupId, groupName, groupUserVOS } = two
  279. const objectTwo = {
  280. label: groupName,
  281. id: groupId,
  282. children: []
  283. }
  284. objectOne.children.push(objectTwo)
  285. if (groupUserVOS && groupUserVOS.length) {
  286. groupUserVOS.forEach((three, threeIndex) => {
  287. const { teacherName, teacherId, userAccount, canDel, selected } = three
  288. objectTwo.children.push({
  289. id: `${one.id}_${two.groupId}_${teacherId}_${threeIndex}`,
  290. label: `${teacherName}(${userAccount})`,
  291. targetType: 1,
  292. targetName: teacherName,
  293. targetId: teacherId,
  294. studentCode: null,
  295. roleCode: null,
  296. roleName: null,
  297. gradeCode: null,
  298. gradeName: null,
  299. subjectCode: null,
  300. subjectName: null,
  301. classIdCode: null,
  302. className: null,
  303. classType: null,
  304. classTypeName: null,
  305. departmentId: one.id,
  306. departmentName: one.name,
  307. departmentGroupId: two.groupId,
  308. departmentGroupName: two.groupName,
  309. disabled: !canDel,
  310. selected,
  311. checked:selected==1?'checked':''
  312. })
  313. })
  314. }
  315. })
  316. }
  317. })
  318. return arr
  319. }
  320. export const subjectTreeData = (list) => {
  321. if (!list || list.length === 0) return []
  322. const arr = []
  323. list.forEach(one => {
  324. const { subjectName, subjectCode, gradePersonVos } = one
  325. const objectOne = {
  326. label: subjectName,
  327. id: subjectCode,
  328. children: []
  329. }
  330. arr.push(objectOne)
  331. if (gradePersonVos && gradePersonVos.length) {
  332. gradePersonVos.forEach(two => {
  333. const { gradeName, gradeCode, personVoList } = two
  334. const objectTwo = {
  335. label: gradeName,
  336. id: `${subjectCode}_${gradeCode}`,
  337. children: []
  338. }
  339. objectOne.children.push(objectTwo)
  340. if (personVoList && personVoList.length) {
  341. personVoList.forEach((three, threeIndex) => {
  342. const {
  343. showName,
  344. teacherName,
  345. teacherId,
  346. userAccount,
  347. roleCode,
  348. roleName,
  349. gradeCode,
  350. gradeName,
  351. classIdCode,
  352. className,
  353. canDel,
  354. selected
  355. } = three
  356. objectTwo.children.push({
  357. id: `${one.subjectCode}_${two.gradeCode}_${teacherId}_${threeIndex}`,
  358. label: `${showName}(${userAccount})`,
  359. targetType: 1,
  360. targetName: teacherName,
  361. targetId: teacherId,
  362. studentCode: null,
  363. roleCode,
  364. roleName,
  365. gradeCode,
  366. gradeName,
  367. subjectCode: one.subjectCode,
  368. subjectName: one.subjectName,
  369. classIdCode,
  370. className,
  371. classType: null,
  372. classTypeName: null,
  373. departmentId: null,
  374. departmentName: null,
  375. departmentGroupId: null,
  376. departmentGroupName: null,
  377. disabled: !canDel,
  378. selected
  379. })
  380. })
  381. }
  382. })
  383. }
  384. })
  385. return arr
  386. }
  387. export const gradTreeData = (list) => {
  388. if (!list || list.length === 0) return []
  389. const arr = []
  390. list.forEach(one => {
  391. const { gradeName, gradeCode, classTypePersonVoList } = one
  392. const objectOne = {
  393. label: gradeName,
  394. id: gradeCode,
  395. children: []
  396. }
  397. arr.push(objectOne)
  398. if (classTypePersonVoList && classTypePersonVoList.length) {
  399. classTypePersonVoList.forEach(two => {
  400. const { classTypeName, classType, classInfoPersonVoList } = two
  401. const objectTwo = {
  402. label: classTypeName,
  403. id: `${gradeCode}_${classType}`,
  404. children: []
  405. }
  406. objectOne.children.push(objectTwo)
  407. if (classInfoPersonVoList && classInfoPersonVoList.length) {
  408. classInfoPersonVoList.forEach(three => {
  409. const { className, classIdCode, personVoList } = three
  410. const objectThree = {
  411. label: className,
  412. id: `${gradeCode}_${classType}_${classIdCode}`,
  413. children: []
  414. }
  415. objectTwo.children.push(objectThree)
  416. if (personVoList && personVoList.length) {
  417. personVoList.forEach((four, fourIndex) => {
  418. const {
  419. showName,
  420. teacherName,
  421. teacherId,
  422. userAccount,
  423. roleCode,
  424. roleName,
  425. courseCode,
  426. courseName,
  427. canDel,
  428. selected
  429. } = four
  430. objectThree.children.push({
  431. id: `${one.gradeCode}_${two.classType}_${three.classIdCode}_${teacherId}_${fourIndex}`,
  432. label: `${showName}(${userAccount})`,
  433. targetType: 1,
  434. targetName: teacherName,
  435. targetId: teacherId,
  436. studentCode: null,
  437. roleCode,
  438. roleName,
  439. gradeCode,
  440. gradeName,
  441. subjectCode: courseCode,
  442. subjectName: courseName,
  443. classIdCode: three.classIdCode,
  444. className: three.className,
  445. classType: two.classType,
  446. classTypeName: two.classTypeName,
  447. departmentId: null,
  448. departmentName: null,
  449. departmentGroupId: null,
  450. departmentGroupName: null,
  451. disabled: !canDel,
  452. selected
  453. })
  454. })
  455. }
  456. })
  457. }
  458. })
  459. }
  460. })
  461. return arr
  462. }
  463. export const permTreeData = (list) => {
  464. if (!list || list.length === 0) return []
  465. const arr = []
  466. list.forEach(one => {
  467. const { roleId, roleName, personVoList } = one
  468. const objectOne = {
  469. label: roleName,
  470. id: roleId,
  471. children: []
  472. }
  473. arr.push(objectOne)
  474. if (personVoList && personVoList.length) {
  475. personVoList.forEach((two, twoIndex) => {
  476. const {
  477. showName,
  478. teacherName,
  479. teacherId,
  480. userAccount,
  481. roleCode,
  482. roleName,
  483. gradeCode,
  484. gradeName,
  485. courseCode,
  486. courseName,
  487. canDel,
  488. selected
  489. } = two
  490. objectOne.children.push({
  491. id: `${one.roleId}_${teacherId}_${twoIndex}`,
  492. label: `${showName}(${userAccount})`,
  493. targetType: 1,
  494. targetName: teacherName,
  495. targetId: teacherId,
  496. studentCode: null,
  497. roleCode,
  498. roleName,
  499. gradeCode,
  500. gradeName,
  501. subjectCode: courseCode,
  502. subjectName: courseName,
  503. classIdCode: null,
  504. className: null,
  505. classType: null,
  506. classTypeName: null,
  507. departmentId: null,
  508. departmentName: null,
  509. departmentGroupId: null,
  510. departmentGroupName: null,
  511. disabled: !canDel,
  512. selected
  513. })
  514. })
  515. }
  516. })
  517. return arr
  518. }
  519. /**
  520. * 根据文件后缀获取文件类型
  521. * @param {string} fileType 文件后缀,如 "jpg"
  522. * @returns {'image'|'video'|'audio'|'document'|''}
  523. */
  524. export const getFileTypeBySuffix = (fileType) => {
  525. // 图片后缀
  526. const IMAGE_EXTS = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'svg', 'ico', 'tiff'];
  527. // 视频后缀
  528. const VIDEO_EXTS = ['mp4', 'mov', 'avi', 'mkv', 'flv', 'wmv', 'rmvb', 'mpeg', 'mpg', 'webm'];
  529. // 音频后缀
  530. const AUDIO_EXTS = ['mp3', 'wav', 'flac', 'aac', 'ogg', 'm4a', 'wma'];
  531. // 文档后缀
  532. const DOC_EXTS = ['doc', 'docx'];
  533. const XLS_EXTS = ['xls', 'xlsx'];
  534. const PPT_EXTS = ['ppt', 'pptx'];
  535. const PDF_EXTS = ['pdf'];
  536. const TXT_EXTS = ['txt'];
  537. if (!fileType) return '';
  538. // 后缀,转小写
  539. const ext = fileType.toLowerCase();
  540. if(IMAGE_EXTS.includes(ext)){
  541. return 'image'
  542. }else if (VIDEO_EXTS.includes(ext)){
  543. return 'video'
  544. }else if (AUDIO_EXTS.includes(ext)){
  545. return 'audio'
  546. }else if (DOC_EXTS.includes(ext)){
  547. return 'word'
  548. }else if (XLS_EXTS.includes(ext)){
  549. return 'excel'
  550. }else if (PPT_EXTS.includes(ext)){
  551. return 'pptx'
  552. }else if (PDF_EXTS.includes(ext)){
  553. return 'pdf'
  554. }else if (TXT_EXTS.includes(ext)){
  555. return 'txt'
  556. }else{
  557. return ''
  558. }
  559. }