util.js 4.8 KB

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