util.js 4.8 KB

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