material.js
2.64 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// packageStudy/pages/sceneMarket/components/material/material.js
import {
getMaterialById
} from '../../request'
Component({
/**
* 组件的属性列表
*/
properties: {
item: Object
},
/**
* 组件的初始数据
*/
data: {
materialTypeEnum: {
0: '图片',
1: '视频',
2: 'PDF',
3: '富文本',
4: 'Excel',
5: 'Word'
},
readStatusEnum: {
0: '已查看',
1: '未查看'
}
},
/**
* 组件的方法列表
*/
methods: {
async jump() {
try {
const {
item
} = this.data
if (item.type == 0 || item.type == 1 || item.type == 3) {
//视频、图片、富文本
wx.navigateTo({
url: `/packageStudy/pages/sceneMarket/materialPage/materialPage?id=${item.id}`,
})
} else {
wx.showToast({
title: '打开中...',
mask: true,
icon: 'loading',
duration: 99999999999
})
if (item.fileName) return this.openFile(item)
//PDF、Excel、Word
const {
code,
data,
message
} = await getMaterialById({
id: item.id
}),
_this = this
if (code == 0) {
const url = data.detailInfos[0].image
wx.downloadFile({
url,
success: function (res) {
const manage = wx.getFileSystemManager()
let file = url.split('/')
let fileName = file[file.length - 1]
manage.saveFile({
tempFilePath: res.tempFilePath,
filePath: wx.env.USER_DATA_PATH + '/' + fileName,
success: function (res) {
item.fileName = fileName
_this.openFile(item)
},
fail(err) {
throw err
}
})
},
fail(err) {
throw err
}
})
} else throw message
}
this.triggerEvent('refreshPage')
} catch (error) {
wx.$util.showToast(error)
}
},
openFile(item) {
this.setData({
item
})
wx.showToast({
title: '请点击右上角『…』选择“保存到手机”',
mask: true,
icon: 'none'
})
this.triggerEvent('browsingDoc', item.id)
setTimeout(() => {
wx.openDocument({
filePath: wx.env.USER_DATA_PATH + '/' + item.fileName,
showMenu: true,
})
}, 1e3)
}
}
})