//年级显示方法
let gradeArr = ["学前班", "一年级", "二年级", "三年级","四年级"]
const gradeUpper = grade => {
return gradeArr[grade];
}
//时间戳转时间
function formatDate(time, flag) {
console.log(time)
const t = new Date(time);
const tf = function(i){return (i < 10 ? '0' : '') + i};
const year = t.getFullYear();
const month = tf(t.getMonth() + 1);
const day = tf(t.getDate());
const hour = tf(t.getHours());
const minute = tf(t.getMinutes());
//console.log( month + '月' + day + '日' + hour + ':' + minute);
if(flag == 1) {
return month + '月' + day + '日' + ' ' + hour + ':' + minute;
}else if(flag == 2) {
console.log(year,month,day)
return year + '-' + month + '-' + day
}else if(flag == 3){
return month + '-' + day + ' ' + hour + ':' + minute;
}else if( flag == 4) {
return year + '年' + month + '月' + day + '日';
}
}
//将秒转化为 X/XX 比如四小时三十六分 为4/36
const day = msd => {
//不到一分钟的情况;
let time = Math.floor(msd) + '秒';//整数秒
//time = "0/00";
//超过一分钟的情况
if (Math.floor(msd) > 60) {
let min = Math.floor(msd / 60);
time = min + "分";
// let hour = Math.floor(min / 60);
// let minShow = min % 60;
// time = hour + "/" + minShow ;
if (min > 60) {
min = Math.floor(msd / 60) % 60;
let hour = Math.floor(msd / 3600);
time = hour + "小时";
// time = hour + "时";
if (hour > 24) {
hour = Math.floor(msd / 3600) % 24;
let day = Math.floor(msd / 3600 / 24);
time = day + "天";
// time = day + "天";
}
}
}
return time;
}
//计算首页学过了多长时间
function studyTime (arr) {
const studyLog = [];
const time = new Date();
for(let item of arr) {
if(!item.lessonTitle) {
break;
}
let msd = (time - item.gmtCreated*1) / 1000;
studyLog.push(
{
lessonTitle: item.lessonTitle,
lessonId: item.lessonId,
gmtCreated:day(msd) + '之前'
}
)
}
return studyLog
}
//计算各个页面学了多长时间
function studyPageTime (arr) {
const studyLog = [];
const time = new Date();
for(let item of arr) {
if(!item.title) {
continue;
}
let msd = (time - item.studyDate*1) / 1000;
studyLog.push(
{
title: item.title,
lessonId: item.lessonId,
isStudy: item.isStudy,
studyDate: day(msd) + '之前'
}
)
}
return studyLog;
}
//过滤暂无资料不显示
function filter (arr) {
const studyLog = [];
for(let item of arr) {
if(!item.fileName) {
continue;
}
studyLog.push(
{
fileName: item.fileName,
warePath: item.warePath,
}
)
}
console.log(studyLog)
return studyLog;
}
//本周推荐勋章攻略 取消
function strategy (str) {
return str.split('
');
}
//判断能不能预览
function preview (arr) {
const studyLog = [];
for(let item of arr) {
if(item.warePath){
studyLog.push(
{
title: item.title,
lessonId: item.lessonId,
warePath: item.warePath
}
)
}
}
return studyLog;
}
//获取当前页面传的的值
function getUrl() {
var pages = getCurrentPages() //获取加载的页面
var currentPage = pages[pages.length-1] //获取当前页面的对象
var url = currentPage.route //当前页面url
var options = currentPage.options //如果要获取url中所带的参数可以查看options
return options
}
//判断科目唯一值columnId
function column(columnNum) {
let column = {};
switch(columnNum) {
case '1':
column.columnId = '1'
column.columnName = '语文'
break;
case '2':
column.columnId = '2'
column.columnName = '数学'
break;
case '3':
column.columnId = '3'
column.columnName = '中文'
break;
case '4':
column.columnId = '4'
column.columnName = '英语'
break;
case '5':
column.columnId = '5'
column.columnName = '科学'
break;
case '6':
column.columnId = '6'
column.columnName = '艺术'
break;
}
return column;
}
//输入跳转URL链接
function url(columnNum) {
let url = '';
switch(columnNum) {
case '1':
url = '../language/language?ind=2'
break;
case '2':
url = '../mathematics/mathematics?ind=3'
break;
case '3':
url = '../chinese/chinese?ind=4'
break;
case '4':
url = '../english/english?ind=5'
break;
case '5':
url = '../science/science?ind=6'
break;
case '6':
url = '../art/art?ind=7'
break;
}
return url;
}
//过滤返回回来的数组找出前三名重新排序
function topThree (arr) {
var arr1 = [];
for(var item of arr.slice(0,3)){
if(item.rank == 2){
arr1.unshift(item)
}
if(item.rank == 1) {
arr1.push(item)
}
if(item.rank == 3) {
arr1.push(item)
}
}
return arr1;
}
//获取回复条数
function replyNo (arr) {
let num = 0;
for( let item of arr) {
num += item.currentReplyCount
}
return num
}
//url地址转成对象
function convertObject (str) {
const arr = str.split('&');
let obj = {};
for (let item of arr) {
let key = item.split('=')[0];
let val = item.split('=')[1];
obj[key] = val;
}
return obj;
}
/*另一个小程序用到的方法 */
//时间转换
function formatTime(time) {
let currentTime = (new Date()).getTime();
let durationTime = (currentTime - Number(time)) / 1000;
let date = new Date(time)
let year = date.getFullYear()
let month = date.getMonth() + 1
let day = date.getDate()
let hour = date.getHours()
let minute = date.getMinutes()
let second = date.getSeconds()
let interval = '';
switch (true) {
case durationTime < 60:
interval = '刚刚';
break;
case durationTime < 3600:
interval = `${parseInt(durationTime / 60)}分钟前`;
break;
case durationTime < 3600 * 24:
interval = `${parseInt(durationTime / 3600)}小时前`;
break;
default:
interval = [month, day].map(formatNumber).join('/') + ' ' + [hour, minute].map(formatNumber).join(':');
}
return interval
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
// 弹出Toast
function showToast(str, mask, icon, time) {
wx.showToast({
title: str.toString(),
mask: mask,
icon: icon,
duration: +time
})
}
//隐藏Toast
function hideToast() {
wx.hideToast();
}
// 保存文件
function saveFile(tempFile, success) {
wx.saveFile({
tempFilePath: tempFile,
success: function(res) {
let svaedFile = res.savedFilePath
if (success) {
success(svaeFile)
}
}
})
}
module.exports = {
formatDate,
studyTime,
studyPageTime,
filter,
strategy,
gradeUpper,
day,
preview,
getUrl,
column,
url,
topThree,
replyNo,
convertObject,
formatTime,
showToast,
hideToast,
saveFile
}