util.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //年级显示方法
  2. let gradeArr = ["学前班", "一年级", "二年级", "三年级","四年级"]
  3. const gradeUpper = grade => {
  4. return gradeArr[grade];
  5. }
  6. //时间戳转时间
  7. function formatDate(time, flag) {
  8. console.log(time)
  9. const t = new Date(time);
  10. const tf = function(i){return (i < 10 ? '0' : '') + i};
  11. const year = t.getFullYear();
  12. const month = tf(t.getMonth() + 1);
  13. const day = tf(t.getDate());
  14. const hour = tf(t.getHours());
  15. const minute = tf(t.getMinutes());
  16. //console.log( month + '月' + day + '日' + hour + ':' + minute);
  17. if(flag == 1) {
  18. return month + '月' + day + '日' + ' ' + hour + ':' + minute;
  19. }else if(flag == 2) {
  20. console.log(year,month,day)
  21. return year + '-' + month + '-' + day
  22. }else if(flag == 3){
  23. return month + '-' + day + ' ' + hour + ':' + minute;
  24. }else if( flag == 4) {
  25. return year + '年' + month + '月' + day + '日';
  26. }
  27. }
  28. //将秒转化为 X/XX 比如四小时三十六分 为4/36
  29. const day = msd => {
  30. //不到一分钟的情况;
  31. let time = Math.floor(msd) + '秒';//整数秒
  32. //time = "0/00";
  33. //超过一分钟的情况
  34. if (Math.floor(msd) > 60) {
  35. let min = Math.floor(msd / 60);
  36. time = min + "分";
  37. // let hour = Math.floor(min / 60);
  38. // let minShow = min % 60;
  39. // time = hour + "/" + minShow ;
  40. if (min > 60) {
  41. min = Math.floor(msd / 60) % 60;
  42. let hour = Math.floor(msd / 3600);
  43. time = hour + "小时";
  44. // time = hour + "时";
  45. if (hour > 24) {
  46. hour = Math.floor(msd / 3600) % 24;
  47. let day = Math.floor(msd / 3600 / 24);
  48. time = day + "天";
  49. // time = day + "天";
  50. }
  51. }
  52. }
  53. return time;
  54. }
  55. //计算首页学过了多长时间
  56. function studyTime (arr) {
  57. const studyLog = [];
  58. const time = new Date();
  59. for(let item of arr) {
  60. if(!item.lessonTitle) {
  61. break;
  62. }
  63. let msd = (time - item.gmtCreated*1) / 1000;
  64. studyLog.push(
  65. {
  66. lessonTitle: item.lessonTitle,
  67. lessonId: item.lessonId,
  68. gmtCreated:day(msd) + '之前'
  69. }
  70. )
  71. }
  72. return studyLog
  73. }
  74. //计算各个页面学了多长时间
  75. function studyPageTime (arr) {
  76. const studyLog = [];
  77. const time = new Date();
  78. for(let item of arr) {
  79. if(!item.title) {
  80. continue;
  81. }
  82. let msd = (time - item.studyDate*1) / 1000;
  83. studyLog.push(
  84. {
  85. title: item.title,
  86. lessonId: item.lessonId,
  87. isStudy: item.isStudy,
  88. studyDate: day(msd) + '之前'
  89. }
  90. )
  91. }
  92. return studyLog;
  93. }
  94. //过滤暂无资料不显示
  95. function filter (arr) {
  96. const studyLog = [];
  97. for(let item of arr) {
  98. if(!item.fileName) {
  99. continue;
  100. }
  101. studyLog.push(
  102. {
  103. fileName: item.fileName,
  104. warePath: item.warePath,
  105. }
  106. )
  107. }
  108. console.log(studyLog)
  109. return studyLog;
  110. }
  111. //本周推荐勋章攻略 取消<br>
  112. function strategy (str) {
  113. return str.split('<br/>');
  114. }
  115. //判断能不能预览
  116. function preview (arr) {
  117. const studyLog = [];
  118. for(let item of arr) {
  119. if(item.warePath){
  120. studyLog.push(
  121. {
  122. title: item.title,
  123. lessonId: item.lessonId,
  124. warePath: item.warePath
  125. }
  126. )
  127. }
  128. }
  129. return studyLog;
  130. }
  131. //获取当前页面传的的值
  132. function getUrl() {
  133. var pages = getCurrentPages() //获取加载的页面
  134. var currentPage = pages[pages.length-1] //获取当前页面的对象
  135. var url = currentPage.route //当前页面url
  136. var options = currentPage.options //如果要获取url中所带的参数可以查看options
  137. return options
  138. }
  139. //判断科目唯一值columnId
  140. function column(columnNum) {
  141. let column = {};
  142. switch(columnNum) {
  143. case '1':
  144. column.columnId = '1'
  145. column.columnName = '语文'
  146. break;
  147. case '2':
  148. column.columnId = '2'
  149. column.columnName = '数学'
  150. break;
  151. case '3':
  152. column.columnId = '3'
  153. column.columnName = '中文'
  154. break;
  155. case '4':
  156. column.columnId = '4'
  157. column.columnName = '英语'
  158. break;
  159. case '5':
  160. column.columnId = '5'
  161. column.columnName = '科学'
  162. break;
  163. case '6':
  164. column.columnId = '6'
  165. column.columnName = '艺术'
  166. break;
  167. }
  168. return column;
  169. }
  170. //输入跳转URL链接
  171. function url(columnNum) {
  172. let url = '';
  173. switch(columnNum) {
  174. case '1':
  175. url = '../language/language?ind=2'
  176. break;
  177. case '2':
  178. url = '../mathematics/mathematics?ind=3'
  179. break;
  180. case '3':
  181. url = '../chinese/chinese?ind=4'
  182. break;
  183. case '4':
  184. url = '../english/english?ind=5'
  185. break;
  186. case '5':
  187. url = '../science/science?ind=6'
  188. break;
  189. case '6':
  190. url = '../art/art?ind=7'
  191. break;
  192. }
  193. return url;
  194. }
  195. //过滤返回回来的数组找出前三名重新排序
  196. function topThree (arr) {
  197. var arr1 = [];
  198. for(var item of arr.slice(0,3)){
  199. if(item.rank == 2){
  200. arr1.unshift(item)
  201. }
  202. if(item.rank == 1) {
  203. arr1.push(item)
  204. }
  205. if(item.rank == 3) {
  206. arr1.push(item)
  207. }
  208. }
  209. return arr1;
  210. }
  211. //获取回复条数
  212. function replyNo (arr) {
  213. let num = 0;
  214. for( let item of arr) {
  215. num += item.currentReplyCount
  216. }
  217. return num
  218. }
  219. //url地址转成对象
  220. function convertObject (str) {
  221. const arr = str.split('&');
  222. let obj = {};
  223. for (let item of arr) {
  224. let key = item.split('=')[0];
  225. let val = item.split('=')[1];
  226. obj[key] = val;
  227. }
  228. return obj;
  229. }
  230. /*另一个小程序用到的方法 */
  231. //时间转换
  232. function formatTime(time) {
  233. let currentTime = (new Date()).getTime();
  234. let durationTime = (currentTime - Number(time)) / 1000;
  235. let date = new Date(time)
  236. let year = date.getFullYear()
  237. let month = date.getMonth() + 1
  238. let day = date.getDate()
  239. let hour = date.getHours()
  240. let minute = date.getMinutes()
  241. let second = date.getSeconds()
  242. let interval = '';
  243. switch (true) {
  244. case durationTime < 60:
  245. interval = '刚刚';
  246. break;
  247. case durationTime < 3600:
  248. interval = `${parseInt(durationTime / 60)}分钟前`;
  249. break;
  250. case durationTime < 3600 * 24:
  251. interval = `${parseInt(durationTime / 3600)}小时前`;
  252. break;
  253. default:
  254. interval = [month, day].map(formatNumber).join('/') + ' ' + [hour, minute].map(formatNumber).join(':');
  255. }
  256. return interval
  257. }
  258. const formatNumber = n => {
  259. n = n.toString()
  260. return n[1] ? n : '0' + n
  261. }
  262. // 弹出Toast
  263. function showToast(str, mask, icon, time) {
  264. wx.showToast({
  265. title: str.toString(),
  266. mask: mask,
  267. icon: icon,
  268. duration: +time
  269. })
  270. }
  271. //隐藏Toast
  272. function hideToast() {
  273. wx.hideToast();
  274. }
  275. // 保存文件
  276. function saveFile(tempFile, success) {
  277. wx.saveFile({
  278. tempFilePath: tempFile,
  279. success: function(res) {
  280. let svaedFile = res.savedFilePath
  281. if (success) {
  282. success(svaeFile)
  283. }
  284. }
  285. })
  286. }
  287. module.exports = {
  288. formatDate,
  289. studyTime,
  290. studyPageTime,
  291. filter,
  292. strategy,
  293. gradeUpper,
  294. day,
  295. preview,
  296. getUrl,
  297. column,
  298. url,
  299. topThree,
  300. replyNo,
  301. convertObject,
  302. formatTime,
  303. showToast,
  304. hideToast,
  305. saveFile
  306. }