|
|
|
import {
|
|
|
|
host,
|
|
|
|
cdn
|
|
|
|
} from './config'
|
|
|
|
|
|
|
|
const formatTime = (date) => {
|
|
|
|
const year = date.getFullYear()
|
|
|
|
const month = date.getMonth() + 1
|
|
|
|
const day = date.getDate()
|
|
|
|
const hour = date.getHours()
|
|
|
|
const minute = date.getMinutes()
|
|
|
|
const second = date.getSeconds()
|
|
|
|
|
|
|
|
return `${[year, month, day].map(formatNumber).join('-')} ${[hour, minute, second].map(formatNumber).join(':')}`
|
|
|
|
}
|
|
|
|
|
|
|
|
const time = se => {
|
|
|
|
// console.log(se)
|
|
|
|
if (se === null) {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
var minutes = parseInt(se / (1000 * 60));
|
|
|
|
if (minutes < 10) {
|
|
|
|
minutes = '0' + minutes
|
|
|
|
}
|
|
|
|
var seconds = parseInt((se % (1000 * 60)) / 1000);
|
|
|
|
if (seconds < 10) {
|
|
|
|
seconds = '0' + seconds
|
|
|
|
}
|
|
|
|
// console.log(minutes + " 分钟 " + seconds + " 秒 ")
|
|
|
|
return minutes + ':' + seconds
|
|
|
|
}
|
|
|
|
|
|
|
|
const formatNumber = n => {
|
|
|
|
n = n.toString()
|
|
|
|
return n[1] ? n : `0${n}`
|
|
|
|
}
|
|
|
|
|
|
|
|
const http = (url, method, data, isLoading = true) => {
|
|
|
|
if (false) {
|
|
|
|
wx.showLoading({
|
|
|
|
title: '加载中'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
if (url === '/user/login' || url === '/user/register') {
|
|
|
|
var header = {}
|
|
|
|
} else {
|
|
|
|
var header = {
|
|
|
|
'token': wx.getStorageSync('token')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wx.request({
|
|
|
|
url: host + url,
|
|
|
|
method: method,
|
|
|
|
data: data,
|
|
|
|
header: header,
|
|
|
|
success: res => {
|
|
|
|
wx.hideLoading();
|
|
|
|
if (200 == res.data.code) {
|
|
|
|
resolve(res.data);
|
|
|
|
} else if (10008 === res.data.code && res.data.msg === "未注册!") {
|
|
|
|
resolve(res.data)
|
|
|
|
} else if (10010 === res.data.code) {
|
|
|
|
wx.login({
|
|
|
|
success: res => {
|
|
|
|
http("/user/login", "post", {
|
|
|
|
code: res.code
|
|
|
|
}).then(res => {
|
|
|
|
if (res.msg === "未注册!") {
|
|
|
|
wx.getSystemInfo({
|
|
|
|
success(res) {
|
|
|
|
if (res.environment) {
|
|
|
|
// 企业微信环境
|
|
|
|
// this.getUserProfile()
|
|
|
|
} else {
|
|
|
|
// 微信·环境
|
|
|
|
wx.showToast({
|
|
|
|
title: '初次打开请在企业微信中',
|
|
|
|
icon: 'none',
|
|
|
|
duration: 3000
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
getApp().globalData.token = res.data.token
|
|
|
|
wx.setStorage({
|
|
|
|
key: 'user',
|
|
|
|
data: res.data.user
|
|
|
|
})
|
|
|
|
wx.setStorage({
|
|
|
|
key: 'token',
|
|
|
|
data: res.data.token
|
|
|
|
})
|
|
|
|
if (res.data.user.departmentName === '' || res.data.user.departmentName === null) {
|
|
|
|
wx.getSystemInfo({
|
|
|
|
success(res) {
|
|
|
|
if (res.environment) {
|
|
|
|
// 企业微信环境
|
|
|
|
wx.qy.login({
|
|
|
|
success: function (res) {
|
|
|
|
http("/qywx/userDepartmentRefresh", "post", {
|
|
|
|
code: res.code
|
|
|
|
}).then(res => {
|
|
|
|
console.log(res)
|
|
|
|
if (res.success) {
|
|
|
|
wx.showToast({
|
|
|
|
title: '刷新成功',
|
|
|
|
icon: 'none',
|
|
|
|
duration: 3000
|
|
|
|
})
|
|
|
|
this.userlogin()
|
|
|
|
} else {}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// 微信·环境
|
|
|
|
wx.showToast({
|
|
|
|
title: '请在企业微信中打开\r\n以获取企业架构',
|
|
|
|
icon: 'none',
|
|
|
|
duration: 3000
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
wx.getSystemInfo({
|
|
|
|
success(res) {
|
|
|
|
if (res.environment) {
|
|
|
|
// 企业微信环境
|
|
|
|
wx.qy.login({
|
|
|
|
success: function (res) {
|
|
|
|
http("/qywx/userDepartmentRefresh", "post", {
|
|
|
|
code: res.code
|
|
|
|
}).then(res => {
|
|
|
|
console.log(res)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// 微信·环境
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
http(url, method, data, isLoading = true).then(res => {
|
|
|
|
console.log(res)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
wx.showToast({
|
|
|
|
title: res.data.msg,
|
|
|
|
icon: 'none',
|
|
|
|
duration: 2000
|
|
|
|
})
|
|
|
|
resolve(res.data)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
complete: res => {
|
|
|
|
wx.hideLoading();
|
|
|
|
},
|
|
|
|
fail: res => {
|
|
|
|
wx.hideLoading();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const lookup = (e) => {
|
|
|
|
console.log(e)
|
|
|
|
if (e.type === 1 || e.teachRefeType === 1) {
|
|
|
|
wx.navigateTo({
|
|
|
|
url: '/pages/lookup/loockvideo/video',
|
|
|
|
})
|
|
|
|
wx.setStorage({
|
|
|
|
key: 'video',
|
|
|
|
data: e
|
|
|
|
})
|
|
|
|
} else if (e.type === 2 || e.teachRefeType === 2) {
|
|
|
|
wx.navigateTo({
|
|
|
|
url: '/pages/lookup/lookimg/img',
|
|
|
|
})
|
|
|
|
wx.setStorage({
|
|
|
|
key: 'article',
|
|
|
|
data: e
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// wx.navigateTo({
|
|
|
|
// url: '/pages/lookup/lookfile/lookfile',
|
|
|
|
// })
|
|
|
|
// wx.setStorage({
|
|
|
|
// key: 'file',
|
|
|
|
// data: e
|
|
|
|
// })
|
|
|
|
wx.showLoading({
|
|
|
|
title: '正在加载',
|
|
|
|
mask: true
|
|
|
|
})
|
|
|
|
var url = e.fileUrl?e.fileUrl:e.teachRefeFileUrl;
|
|
|
|
//下载文件,生成临时地址
|
|
|
|
wx.downloadFile({
|
|
|
|
url: cdn + url,
|
|
|
|
success(res) {
|
|
|
|
console.log(res)
|
|
|
|
wx.openDocument({
|
|
|
|
filePath: res.tempFilePath,
|
|
|
|
showMenu: true,
|
|
|
|
success(re) {
|
|
|
|
wx.showToast({
|
|
|
|
title: '加载成功',
|
|
|
|
duration: 2000,
|
|
|
|
mask: true,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
fail(re) {
|
|
|
|
wx.showToast({
|
|
|
|
title: '加载失败',
|
|
|
|
duration: 2000,
|
|
|
|
mask: true,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
complete(re) {
|
|
|
|
wx.hideLoading();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
//保存到本地
|
|
|
|
// wx.saveFile({
|
|
|
|
// tempFilePath: res.tempFilePath,
|
|
|
|
// success: function (res) {
|
|
|
|
// console.log(res)
|
|
|
|
// console.log('保存到本地', res)
|
|
|
|
// wx.saveImageToPhotosAlbum({
|
|
|
|
// filePath: res.savedFilePath,
|
|
|
|
// complete(res) {
|
|
|
|
// wx.hideLoading();
|
|
|
|
// wx.showToast({
|
|
|
|
// title: '下载图片成功',
|
|
|
|
// duration: 2000,
|
|
|
|
// mask: true,
|
|
|
|
// });
|
|
|
|
// console.log('保存到相册', res)
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
// },
|
|
|
|
// fail: function (err) {
|
|
|
|
// wx.hideLoading();
|
|
|
|
// wx.showToast({
|
|
|
|
// title: '保存失败',
|
|
|
|
// duration: 2000,
|
|
|
|
// mask: true,
|
|
|
|
// });
|
|
|
|
// console.log('保存失败:', err)
|
|
|
|
// },
|
|
|
|
// complete(res) {
|
|
|
|
// console.log(res)
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const matchType = (fileName) => {
|
|
|
|
// 后缀获取
|
|
|
|
var suffix = '';
|
|
|
|
// 获取类型结果
|
|
|
|
var result = '';
|
|
|
|
try {
|
|
|
|
var flieArr = fileName.split('.');
|
|
|
|
suffix = flieArr[flieArr.length - 1];
|
|
|
|
} catch (err) {
|
|
|
|
suffix = '';
|
|
|
|
}
|
|
|
|
// fileName无后缀返回 false
|
|
|
|
if (!suffix) {
|
|
|
|
result = false;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
// 图片格式
|
|
|
|
var imglist = ['png', 'jpg', 'jpeg', 'bmp', 'gif'];
|
|
|
|
// 进行图片匹配
|
|
|
|
result = imglist.some(function (item) {
|
|
|
|
return item == suffix;
|
|
|
|
});
|
|
|
|
if (result) {
|
|
|
|
result = 'image';
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
// 匹配txt
|
|
|
|
var txtlist = ['txt'];
|
|
|
|
result = txtlist.some(function (item) {
|
|
|
|
return item == suffix;
|
|
|
|
});
|
|
|
|
if (result) {
|
|
|
|
result = 'txt';
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
// 匹配 excel
|
|
|
|
var excelist = ['xls', 'xlsx'];
|
|
|
|
result = excelist.some(function (item) {
|
|
|
|
return item == suffix;
|
|
|
|
});
|
|
|
|
if (result) {
|
|
|
|
result = 'excel';
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
// 匹配 word
|
|
|
|
var wordlist = ['doc', 'docx'];
|
|
|
|
result = wordlist.some(function (item) {
|
|
|
|
return item == suffix;
|
|
|
|
});
|
|
|
|
if (result) {
|
|
|
|
result = 'word';
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
// 匹配 pdf
|
|
|
|
var pdflist = ['pdf'];
|
|
|
|
result = pdflist.some(function (item) {
|
|
|
|
return item == suffix;
|
|
|
|
});
|
|
|
|
if (result) {
|
|
|
|
result = 'pdf';
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
// 匹配 ppt
|
|
|
|
var pptlist = ['ppt', 'pptx'];
|
|
|
|
result = pptlist.some(function (item) {
|
|
|
|
return item == suffix;
|
|
|
|
});
|
|
|
|
if (result) {
|
|
|
|
result = 'ppt';
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
// 匹配 视频
|
|
|
|
var videolist = ['mp4', 'm2v', 'mkv'];
|
|
|
|
result = videolist.some(function (item) {
|
|
|
|
return item == suffix;
|
|
|
|
});
|
|
|
|
if (result) {
|
|
|
|
result = 'video';
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
// 匹配 音频
|
|
|
|
var radiolist = ['mp3', 'wav', 'wmv'];
|
|
|
|
result = radiolist.some(function (item) {
|
|
|
|
return item == suffix;
|
|
|
|
});
|
|
|
|
if (result) {
|
|
|
|
result = 'radio';
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
// 其他 文件类型
|
|
|
|
result = 'other';
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
formatTime,
|
|
|
|
http,
|
|
|
|
lookup,
|
|
|
|
formatNumber,
|
|
|
|
time,
|
|
|
|
matchType
|
|
|
|
}
|