util.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. const formatTime = date => {
  2. const year = date.getFullYear()
  3. const month = date.getMonth() + 1
  4. const day = date.getDate()
  5. const hour = date.getHours()
  6. const minute = date.getMinutes()
  7. const second = date.getSeconds()
  8. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  9. }
  10. const formatNumber = n => {
  11. n = n.toString()
  12. return n[1] ? n : '0' + n
  13. }
  14. //年级显示方法
  15. let gradeArr = ["学前班", "一年级", "二年级", "三年级"]
  16. const gradeUpper = grade => {
  17. return gradeArr[grade];
  18. }
  19. //将秒转化为 X/XX 比如四小时三十六分 为4/36
  20. const day = msd => {
  21. //不到一分钟的情况;
  22. let time = Math.floor(msd);//整数秒
  23. time = "0/00";
  24. //超过一分钟的情况
  25. if( Math.floor(msd )> 60){
  26. let min = Math.floor(msd / 60);
  27. let hour = Math.floor(min / 60);
  28. let minShow = min % 60;
  29. time = hour + "/" + minShow ;
  30. // if( min > 60 ){
  31. // min = Math.floor(msd / 60) % 60;
  32. // let hour = Math.floor( msd / 3600 );
  33. // // time = hour + "小时" + min + "分";
  34. // time = hour + "时";
  35. // if( hour > 24 ){
  36. // hour = Math.floor( msd / 3600 ) % 24;
  37. // let day = Math.floor(msd / 3600 / 24);
  38. // // time = day + "天" + hour + "小时";
  39. // time = day + "天";
  40. // }
  41. // }
  42. }
  43. return time;
  44. }
  45. //计算首页学过了多长时间
  46. function studyTime (arr) {
  47. const studyLog = [];
  48. const time = new Date();
  49. for(let item of arr) {
  50. if(!item.lessonTitle) {
  51. break;
  52. }
  53. let msd = (time - item.gmtCreated*1) / 1000;
  54. studyLog.push(
  55. {
  56. lessonTitle: item.lessonTitle,
  57. lessonId: item.lessonId,
  58. gmtCreated:day(msd) + '之前'
  59. }
  60. )
  61. }
  62. return studyLog
  63. }
  64. //计算各个页面学了多长时间
  65. function studyPageTime (arr) {
  66. const studyLog = [];
  67. const time = new Date();
  68. for(let item of arr) {
  69. if(!item.title) {
  70. break;
  71. }
  72. let msd = (time - item.studyDate*1) / 1000;
  73. studyLog.push(
  74. {
  75. title: item.title,
  76. lessonId: item.lessonId,
  77. isStudy: item.isStudy,
  78. studyDate: day(msd) + '之前'
  79. }
  80. )
  81. }
  82. return studyLog;
  83. }
  84. //本周推荐勋章攻略 取消<br>
  85. function strategy (str) {
  86. return str.split('<br/>');
  87. }
  88. //判断能不能预览
  89. function preview (arr) {
  90. const studyLog = [];
  91. for(let item of arr) {
  92. if(item.warePath){
  93. studyLog.push(
  94. {
  95. title: item.title,
  96. lessonId: item.lessonId,
  97. warePath: item.warePath
  98. }
  99. )
  100. }
  101. }
  102. return studyLog;
  103. }
  104. //获取当前页面传的的值
  105. function getUrl() {
  106. var pages = getCurrentPages() //获取加载的页面
  107. var currentPage = pages[pages.length-1] //获取当前页面的对象
  108. var url = currentPage.route //当前页面url
  109. var options = currentPage.options //如果要获取url中所带的参数可以查看options
  110. return options
  111. }
  112. //判断科目唯一值columnId
  113. function column(columnNum) {
  114. let column = {};
  115. switch(columnNum) {
  116. case '1':
  117. column.columnId = '1'
  118. column.columnName = '语文'
  119. break;
  120. case '2':
  121. column.columnId = '2'
  122. column.columnName = '数学'
  123. break;
  124. case '3':
  125. column.columnId = '3'
  126. column.columnName = '中文'
  127. break;
  128. case '4':
  129. column.columnId = '4'
  130. column.columnName = '英语'
  131. break;
  132. case '5':
  133. column.columnId = '5'
  134. column.columnName = '科学'
  135. break;
  136. case '6':
  137. column.columnId = '6'
  138. column.columnName = '艺术'
  139. break;
  140. }
  141. return column;
  142. }
  143. //输入跳转URL链接
  144. function url(columnNum) {
  145. let url = '';
  146. switch(columnNum) {
  147. case '1':
  148. url = '../language/language?ind=2'
  149. break;
  150. case '2':
  151. url = '../mathematics/mathematics?ind=3'
  152. break;
  153. case '3':
  154. url = '../chinese/chinese?ind=4'
  155. break;
  156. case '4':
  157. url = '../english/english?ind=5'
  158. break;
  159. case '5':
  160. url = '../science/science?ind=6'
  161. break;
  162. case '6':
  163. url = '../art/art?ind=7'
  164. break;
  165. }
  166. return url;
  167. }
  168. //过滤返回回来的数组找出前三名重新排序
  169. function topThree (arr) {
  170. var arr1 = [];
  171. for(var item of arr.slice(0,3)){
  172. if(item.rank == 2){
  173. arr1.unshift(item)
  174. }
  175. if(item.rank == 1) {
  176. arr1.push(item)
  177. }
  178. if(item.rank == 3) {
  179. arr1.push(item)
  180. }
  181. }
  182. return arr1;
  183. }
  184. //获取回复条数
  185. function replyNo (arr) {
  186. let num = 0;
  187. for( let item of arr) {
  188. num += item.currentReplyCount
  189. }
  190. return num
  191. }
  192. module.exports = {
  193. formatTime: formatTime,
  194. studyTime,
  195. studyPageTime,
  196. strategy,
  197. gradeUpper,
  198. day,
  199. preview,
  200. getUrl,
  201. column,
  202. url,
  203. topThree,
  204. replyNo
  205. }