// pages/classdetail/classdetail.js const AV = require("../../lib/av-live-query-weapp-min") const File = require("../../model/file") const Class = require("../../model/class") const { jsonify } = require('../../utils/leancloudutils'); Page({ /** * 页面的初始数据 */ class: null, pic_urls: [], data: { loading: true, files:[] }, preview_pic:function(event){ wx.showLoading({ title: '加载中', }) fileid = event.currentTarget.dataset.fileid; wx.previewImage({ urls: this.pic_urls, current: fileid, complete:wx.hideLoading }) }, del_file: function(event){ objectid = event.currentTarget.dataset.id; fileid = event.currentTarget.dataset.fileid; pull_files = this.pull_files.bind(this); wx.showModal({ title: '确认', content: '删除该资源吗?', success(res){ if(res.confirm){ file = new AV.Object.createWithoutData("File_", objectid); file.destroy().then(()=>{ wx.cloud.deleteFile({ fileList:[fileid], success:()=>{ wx.showToast({ title: '删除成功', }); pull_files(); }, fail:()=>{ wx.showToast({ title: '删除失败', icon: "none" }) } }); },()=>{ wx.showToast({ title: '操作失败', icon:"none" }) })} } }) }, download: function(event){ console.log(event); fileid = event.currentTarget.dataset.fileid; wx.showLoading({ title: '下载中', }) wx.cloud.downloadFile({ fileID: fileid, success: res => { // 返回临时文件路径 console.log(res.tempFilePath); wx.openDocument({ filePath: res.tempFilePath, success: function (res) { console.log('打开文档成功') }, fail:console.error, complete:wx.hideLoading }) // wx.saveFile({ // tempFilePath: res.tempFilePath, // success(res) { // const savedFilePath = res.savedFilePath; // console.log(savedFilePath); // } // }) }, fail: console.error }) }, upload: function(event){ console.log(event) pull_files = this.pull_files.bind(this); class_ = this.class; wx.showToast({ title: '请选择聊天文件', icon:"none" }) wx.chooseMessageFile({ count: 1, success(res){ const tempFile = res.tempFiles[0]; const filename = tempFile.name; const filepath = tempFile.path; const cloudpath = tempFile.time+"/" + filename; console.log(res); wx.showLoading({ title: '上传中', }) wx.cloud.uploadFile({ cloudPath: cloudpath, filePath: filepath, success: res => { // 返回文件 ID fileid = res.fileID; file = new File({ filename: filename, class: class_, uploader: AV.User.current(), fileid: fileid }).save().then(()=>{ wx.hideLoading(); pull_files(); wx.showToast({ title: '上传成功', }) }, ()=>{ wx.hideLoading(); wx.showToast({ title: "上传失败", icon: "none" }) }).catch(console.error); console.log(res.fileID); }, fail: error=>{ console.error(error); wx.hideLoading(); wx.showToast({ title: '上传失败', icon:"none" }) } }) } }) }, pull_files:function(){ query = new AV.Query("File_"); query.equalTo("class", this.class); query.include("uploader"); query.find().then(this.set_files); }, set_files: function(files){ files = files.map(file=>{ uploader = file.get("uploader"); file.set("uploader_name", uploader.get("name")); file.set("uploader_username", uploader.get("username")); filename = file.get("filename"); if(filename.endsWith(".png") || filename.endsWith(".jpg")){ file.set("ispic", true); this.pic_urls.push(file.get("fileid")); console.log(this.pic_urls); }else{ file.set("ispic", false); } return file; }) this.setData(jsonify({files})); this.setData({loading: false}); console.log(this.data); }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { class_id = options.class_id; this.class = AV.Object.createWithoutData("Class_", class_id); this.pull_files(); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { this.pull_files(); }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })