util.js 4.7 KB

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