Commit 40ae2164 authored by Facius's avatar Facius

资料文件页面加访问缓存

parent a190be72
......@@ -64,6 +64,7 @@ export default {
mixWater: "", // 缓存水印拍照图片
},
},
fileList: {} // 文件地址缓存 {url最后一段: tmpPath}
},
onLaunch: async function () {
// 应用初始化(全局只触发一次)
......
......@@ -88,7 +88,6 @@ const install = (Vue, vm) => {
*/
// 响应拦截,判断状态码是否通过
Vue.prototype.$u.http.interceptor.response = (res) => {
console.log(res)
let result = res.data || res;
let msg = getMsg(res)
//返回__token__,设置
......@@ -102,7 +101,7 @@ const install = (Vue, vm) => {
// return result;
case 200:
return {
data:result.data == null ? result : result.data, // result.data的值为0, false, ''的时候也要算有值
data:result.data == null ? result : result.data, // result.data的值为0, false, ''的时候也要算有值;;null的情况下后台返回的是单层及{header: ', data: 数据, message:'', code: ''}
code:code,
message:msg
};
......
......@@ -68,7 +68,8 @@
'2': {} // 选中的品牌
},
loading: false, // 文件列表加载中
orderFile: false // 是否是订单详情进来
orderFile: false, // 是否是订单详情进来
fileList: {}, // 文件地址缓存 {url最后一段: tmpPath}
};
},
computed: {
......@@ -120,7 +121,7 @@
} else { // 获取所有文件资料
this.loadFileLabel('1')
}
this.fileList = getApp().globalData.fileList || {} // 文件地址缓存 {url最后一段: tmpPath}
},
onShow() {
// 从标签列表页面返回,获取选中的标签
......@@ -156,61 +157,116 @@
}
this.$u.route({url: 'pages/order/document-tag' + '?partnerCompanyId=' + partnerCompanyId + '&selectedId=' + this.selected[key].id})
},
// 分享
// 点击了分享
handleShare(item) {
// 分享资料事件
getApp().track('share_document', {
id: this.vuex_user.id,
url: item.url
});
uni.showLoading({
title: '加载中'
})
// callback 写法
wx.downloadFile({
url: item.url, // 下载url
success(res) {
uni.hideLoading()
// 下载完成后转发
wx.shareFileMessage({
filePath: res.tempFilePath,
success() {},
fail: console.error,
})
},
fail: console.error,
})
let block = (filePath) => {
wx.shareFileMessage({
filePath: filePath,
success() {},
fail: console.error,
})
}
let filePath = this.fileList[(item.url || '').split('/').pop()]
if (filePath) { // 有下载记录
wx.shareFileMessage({ // 如果用checkFile检查文件是否存在后再分享,shareFileMess报错:shareFileMessage:fail can only be invoked by user TAP gesture.
filePath: filePath,
success() {},
fail: (error) => {
console.log(error)
if (error.errMsg.indexOf('internal') != -1) { // 文件不存在重新下载
this.downloadFile(item.url, (result)=> {
wx.shareFileMessage({
filePath: result.tempFilePath,
success() {},
fail: console.error,
})
})
}
},
})
} else { // 无下载记录
this.downloadFile(item.url, (res)=> { // 下载
wx.shareFileMessage({
filePath: res.tempFilePath,
success() {},
fail: console.error,
})
})
}
},
// 查看
// 点击了查看
handlePreview(item) {
// 查看资料事件
getApp().track('preview_document', {
id: this.vuex_user.id,
url: item.url
});
});
let filePath = this.fileList[(item.url || '').split('/').pop()]
if (filePath) { // 有下载记录
this.checkFile(filePath, (res)=> { // 文件是否还存在
if (res.size && res.size > 0) { // 存在,预览文件
this.previewFile(filePath)
} else { // 文件已经不存在, 下载
this.downloadFile(item.url, (result)=> {
this.previewFile(result.tempFilePath)
})
}
})
} else { // 无下载记录
this.downloadFile(item.url, (res)=> { // 下载
this.previewFile(res.tempFilePath)
})
}
},
// 缓存文件是否存在
checkFile(filePath, complete) {
uni.getFileInfo({
filePath: filePath,
complete: complete
})
},
// 下载文件
downloadFile(url, block) {
uni.showLoading({
title: '加载中'
})
uni.downloadFile({
url: item.url,
success: function(res) {
uni.hideLoading()
var filePath = res.tempFilePath;
if (/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(filePath)) { // 查看图片
wx.previewImage({
current: filePath, // 当前显示图片的http链接
urls: [filePath] // 需要预览的图片http链接列表
})
} else { // 查看文件
uni.openDocument({
filePath: filePath,
success: function(res) {
console.log('打开文档成功');
}
});
})
uni.downloadFile({
url: url,
success: (res)=> {
uni.hideLoading()
this.fileList[url.split('/').pop()] = res.tempFilePath
block(res)
}
});
},
// 预览文件
previewFile(filePath) {
if (/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(filePath)) { // 查看图片
wx.previewImage({
current: filePath, // 当前显示图片的http链接
urls: [filePath] // 需要预览的图片http链接列表
})
} else { // 查看文件
uni.openDocument({
filePath: filePath,
success: function(res) {
console.log('打开文档成功');
}
}
});
});
}
},
// 分享文件
shareFile(filePath) {
wx.shareFileMessage({
filePath: filePath,
success() {},
fail: console.error,
})
},
// 获取合作商或品牌列表
async loadFileLabel(type, partnerCompanyId) {
......@@ -245,7 +301,6 @@
})
this.loading = false
if (res && res.code == 200) {
console.log(res.data)
this.list = res.data || []
// this.list = [...res.data,...res.data,...res.data,...res.data,...res.data,...res.data,...res.data] // 测试模拟多条数据
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment