1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
| import dayjs from 'dayjs';
|
| // 下载并打开文档
| function downloadFileAndOpen(url) {
| wx.downloadFile({
| url: url,
| success: function (res) {
| wx.hideLoading();
| const filePath = res.tempFilePath;
| wx.openDocument({
| filePath: filePath,
| success: function (res) {
| const time = dayjs().format();
| wx.setStorage({
| key: url,
| data: { filePath, time },
| });
| },
| });
| },
| });
| }
|
| // 打开office文档
| function openDoc(url) {
| wx.showLoading({
| title: '加载中',
| icom: 'none',
| mask: true,
| });
| wx.getStorage({
| key: url,
| success: res => {
| wx.hideLoading();
| const { filePath, time } = res.data;
| // console.log(filePath, time);
| if (filePath && time) {
| const now = dayjs();
| const past = dayjs(time);
| const diffDay = now.diff(past, 'day');
| // console.log('diffDay:', diffDay);
| if (diffDay > 2) {
| downloadFileAndOpen(url);
| } else {
| wx.openDocument({
| filePath: filePath,
| fail: err => {
| downloadFileAndOpen(url);
| },
| });
| }
| } else {
| downloadFileAndOpen(url);
| }
| },
| fail: err => {
| downloadFileAndOpen(url);
| },
| });
| }
|
| const FILETYPE = ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf'];
| function getSuffix(path) {
| return path.substring(path.lastIndexOf('.') + 1);
| }
| function isOfficeFile(path) {
| const suffix = getSuffix(path);
| return FILETYPE.indexOf(suffix) != -1;
| }
|
| export { openDoc, isOfficeFile, getSuffix };
|
|