common.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. export function remainingDays(endTime) {
  112. if (!endTime) return null;
  113. const MS_PER_DAY = 24 * 60 * 60 * 1000;
  114. // 1) 将结束时间转为 Date 对象
  115. const end = new Date(endTime);
  116. if (isNaN(end.getTime())) return null;
  117. // 2) 取今天的"零点"日期(精确到日,不考虑时分秒)
  118. const today = new Date();
  119. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate());
  120. // 3) 取结束日期的"零点"
  121. const endStart = new Date(end.getFullYear(), end.getMonth(), end.getDate());
  122. // 4) 计算天数差并 +1(含当天和结束天)
  123. const diffDays = Math.round((endStart.getTime() - todayStart.getTime()) / MS_PER_DAY);
  124. if (diffDays < 0) return 0; // 已过期:不显示剩余时间
  125. return diffDays + 1; // 18号到22号:22-18=4,加1后=5天
  126. }
  127. export function unioformDateTransform(date, param = "time"){
  128. if(date){
  129. let strDate = new Date(date).toLocaleString('zh-CN')
  130. let days = strDate.split(" ")[0]
  131. let time = strDate.split(" ")[1]
  132. let year = days.split("/")[0]
  133. let month = days.split("/")[1]
  134. let day = days.split("/")[2]
  135. if(Number(month)<10){
  136. month = '0'+ month
  137. }
  138. if(Number(day)<10){
  139. day = '0'+day
  140. }
  141. days = year+'-'+month+'-'+day
  142. switch(param){
  143. case "day":
  144. return days
  145. case "time":
  146. return days + " " + time
  147. }
  148. }else{
  149. return ''
  150. }
  151. }
  152. export const fileSizeConvert = (byte)=>{
  153. let kbSize = 0
  154. let mbSize = 0
  155. let gbSize = 0
  156. kbSize = (byte / 1024)
  157. if(kbSize > 1024){
  158. mbSize = kbSize / 1024
  159. if(mbSize > 1024){
  160. gbSize = mbSize / 1024
  161. return gbSize.toFixed(2) + 'GB'
  162. }else {
  163. return mbSize.toFixed(2) + 'MB'
  164. }
  165. }else{
  166. return kbSize.toFixed(2) + 'KB'
  167. }
  168. }
  169. // 计算文件大小方法,如果文件可以算出 MB,就返回 MB,否则返回 KB
  170. export const getFileSize = (fileSize) => {
  171. if (!fileSize) return '0 KB'
  172. if (fileSize >= 1024 * 1024) {
  173. fileSize = Math.round(fileSize / (1024 * 1024))
  174. return `${fileSize} MB`
  175. } else {
  176. fileSize = Math.round(fileSize / 1024)
  177. return `${fileSize} KB`
  178. }
  179. }