examtest_mp/pages/material/index.js

176 lines
3.9 KiB
JavaScript

const api = require("../../utils/request.js");
Page({
/**
* 页面的初始数据
*/
data: {
search:'',
active:0,
results: [
// {id:34232,name:'test',path:'',description:'test'},
// {id:34232,name:'test',path:'',description:'test'},
],
cateData:['标准文件','文献','指南','未分类'],
query: {
page: 1,
limit: 10,
type:'文档',
cate:'标准文件'
}
},
onLoad: function () {
},
onReady: function () {
},
onShow: function () {
var that = this
that.getList(that.data.query)
},
bindKeyInput(e){
// debugger;
let that = this
console.log(e.detail.value)
that.setData({
['query.search']:e.detail.value
})
that.getList(that.data.query)
},
searchFile(){
let that = this;
this.setData({
['query.search']:''
})
that.getList(that.data.query)
},
typeChange(e){
this.setData({
['query.cate']:e.target.dataset.type,
['query.page']:1
})
this.getList();
},
itemClick(e){
console.log(e.currentTarget.dataset)
},
getList: function () {
var that = this
api.request('/cms/material/', 'GET', that.data.query).then(res => {
if (that.data.query.page == 1) {
that.data.results = res.data.results
} else {
that.data.results = that.data.results.concat(res.data.results)
}
that.setData({
results: that.data.results,
count: res.data.count
})
})
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
var that = this
that.data.query.page = 1;
that.getList();
wx.stopPullDownRefresh();
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
var that = this
if (that.data.count <= that.data.query.page * that.data.query.limit) {
wx.showToast({
title: '没有更多了',
icon: 'none'
})
} else {
that.data.query.page = that.data.query.page + 1
that.getList()
}
},
downFile:function(e){
var that = this
wx.showLoading({
title: '正在下载...',
})
api.request('/cms/material/'+e.currentTarget.dataset.id+'/down/', 'GET').then(res => {
that.data.results[e.currentTarget.dataset.index].down_count = res.data.down_count;
let hostUrl = getApp().globalData.host.slice(0,-4);
var fileurl = hostUrl + res.data.path;
wx.downloadFile({
url: fileurl,
success(res) {
wx.showLoading({
title: '成功,正在打开...',
})
const filePath = res.tempFilePath
var filetype
if (fileurl.indexOf(".docx") != -1) {
filetype = 'docx'
}
else if (fileurl.indexOf(".doc") != -1) {
filetype = 'doc'
}
else if (fileurl.indexOf(".xlsx") != -1) {
filetype = 'xlsx'
}
else if (fileurl.indexOf(".xls") != -1) {
filetype = 'xls'
}
else if (fileurl.indexOf(".pptx") != -1) {
filetype = 'pptx'
}
else if (fileurl.indexOf(".ppt") != -1) {
filetype = 'ppt'
}
else if (fileurl.indexOf(".pdf") != -1) {
filetype = 'pdf'
} else {
wx.hideLoading()
return
}
wx.openDocument({
filePath,
fileType: filetype,
showMenu: true,
success(res) {
wx.hideLoading()
console.log('打开文档成功')
}, fail: function (e) {
wx.hideLoading()
}
})
}
})
that.setData({
results:that.data.results
})
})
}
})