@ganwei-web/gw-base-components-plus 1.0.55 → 1.0.56

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,87 +10,87 @@
10
10
  </span>
11
11
  </template>
12
12
 
13
- <script setup>
14
- import { computed } from 'vue'
13
+ <script>
15
14
  import 'js-base64'
16
15
 
17
16
  const Base64 = window.Base64
18
17
 
19
- const props = defineProps({
20
- // 文件地址
21
- url: {
22
- type: String,
23
- required: true
18
+ export default {
19
+ props: {
20
+ // 文件地址
21
+ url: {
22
+ type: String,
23
+ required: true
24
+ },
25
+ // 文件地址的域名前缀,未传入时默认使用当前站点 origin
26
+ fileDomain: {
27
+ type: String,
28
+ default: ''
29
+ },
30
+ // 是否显示预览按钮
31
+ isPreview: {
32
+ type: Boolean,
33
+ default: true
34
+ },
35
+ // 是否显示下载按钮
36
+ isDownload: {
37
+ type: Boolean,
38
+ default: true
39
+ },
40
+ // 是否显示文件名,false 时显示“附件”
41
+ showFileName: {
42
+ type: Boolean,
43
+ default: true
44
+ }
24
45
  },
25
- // 文件地址的域名前缀,未传入时默认使用当前站点 origin
26
- fileDomain: {
27
- type: String,
28
- default: ''
46
+ computed: {
47
+ // 从 url 中截取文件名
48
+ filename() {
49
+ const value = this.url || ''
50
+ const raw = value.substring(value.lastIndexOf('/') + 1).split('?')[0]
51
+ try {
52
+ return decodeURIComponent(raw)
53
+ } catch (e) {
54
+ return raw
55
+ }
56
+ }
29
57
  },
30
- // 是否显示预览按钮
31
- isPreview: {
32
- type: Boolean,
33
- default: true
34
- },
35
- // 是否显示下载按钮
36
- isDownload: {
37
- type: Boolean,
38
- default: true
39
- },
40
- // 是否显示文件名,false 时显示“附件”
41
- showFileName: {
42
- type: Boolean,
43
- default: true
44
- }
45
- })
46
-
47
- // url 中截取文件名
48
- const filename = computed(() => {
49
- const value = props.url || ''
50
- const raw = value.substring(value.lastIndexOf('/') + 1).split('?')[0]
51
- try {
52
- return decodeURIComponent(raw)
53
- } catch (e) {
54
- return raw
55
- }
56
- })
57
-
58
- // localStorage.webConfig 读取预览服务域名
59
- function getPreviewDomain() {
60
- try {
61
- const webConfig = JSON.parse(localStorage.getItem('webConfig') || '{}')
62
- return webConfig?.httpConfig?.configPreviewDomain || ''
63
- } catch (e) {
64
- return ''
58
+ methods: {
59
+ // 从 localStorage.webConfig 读取预览服务域名
60
+ getPreviewDomain() {
61
+ try {
62
+ const webConfig = JSON.parse(localStorage.getItem('webConfig') || '{}')
63
+ return webConfig?.httpConfig?.configPreviewDomain || ''
64
+ } catch (e) {
65
+ return ''
66
+ }
67
+ },
68
+ // 预览:使用预览服务器打开文件
69
+ handlePreview() {
70
+ const domain = this.getPreviewDomain().replace(/\/+$/, '') || ''
71
+ if (!domain) {
72
+ console.warn('[file-preview] 未配置预览服务域名')
73
+ return
74
+ }
75
+ // 文件地址需带上域名:优先使用外部传入的域名,否则使用当前站点 origin
76
+ const fileDomain = (this.fileDomain || window.location.origin).replace(/\/+$/, '')
77
+ const fileUrl = /^https?:\/\//i.test(this.url)
78
+ ? this.url
79
+ : `${fileDomain}/${String(this.url).replace(/^\/+/, '')}`
80
+ const previewUrl = `${domain}/onlinePreview?url=${encodeURIComponent(Base64.encode(fileUrl))}`
81
+ window.open(previewUrl)
82
+ },
83
+ // 下载
84
+ handleDownload() {
85
+ const link = document.createElement('a')
86
+ link.href = this.url
87
+ link.download = this.filename
88
+ document.body.appendChild(link)
89
+ link.click()
90
+ document.body.removeChild(link)
91
+ }
65
92
  }
66
93
  }
67
-
68
- // 预览:使用预览服务器打开文件
69
- function handlePreview() {
70
- const domain = getPreviewDomain().replace(/\/+$/, '') || ""
71
- if (!domain) {
72
- console.warn('[file-preview] 未配置预览服务域名')
73
- return
74
- }
75
- // 文件地址需带上域名:优先使用外部传入的域名,否则使用当前站点 origin
76
- const fileDomain = (props.fileDomain || window.location.origin).replace(/\/+$/, '')
77
- const fileUrl = /^https?:\/\//i.test(props.url)
78
- ? props.url
79
- : `${fileDomain}/${String(props.url).replace(/^\/+/, '')}`
80
- console.log('fileUrl', fileUrl)
81
- const previewUrl = `${domain}/onlinePreview?url=${encodeURIComponent(Base64.encode(fileUrl))}`
82
- window.open(previewUrl)
83
- }
84
-
85
- // 下载
86
- function handleDownload() {
87
- const link = document.createElement('a')
88
- link.href = props.url
89
- link.download = filename.value
90
- document.body.appendChild(link)
91
- link.click()
92
- document.body.removeChild(link)
93
- }
94
94
  </script>
95
95
 
96
96
  <style scoped>
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "@ganwei-web/gw-base-components-plus",
3
- "version": "1.0.55",
3
+ "version": "1.0.56",
4
4
  "description": "components",
5
5
  "main": "./dist/index.es.js",
6
+ "dependencies": {
7
+ "js-base64": "^2.6.4"
8
+ },
6
9
  "scripts": {
7
10
  "test": "echo \"Error: no test specified\" && exit 1"
8
11
  },