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

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.
@@ -1,96 +1,92 @@
1
1
  <template>
2
2
  <span class="file-preview">
3
3
  <span class="file-name">{{ showFileName ? filename : $t("login.framePro.label.attachment") }}</span>
4
- <el-button v-if="isPreview" type="primary" link @click="handlePreview">
5
- {{$t("login.framePro.label.preview")}}
6
- </el-button>
7
- <el-button v-if="isDownload" type="primary" link @click="handleDownload">
8
- {{$t("login.framePro.label.download")}}
9
- </el-button>
4
+ <span v-if="isPreview" class="file-action" @click="handlePreview">{{ $t("login.framePro.label.preview") }}</span>
5
+ <span v-if="isDownload" class="file-action" @click="handleDownload">{{ $t("login.framePro.label.download") }}</span>
10
6
  </span>
11
7
  </template>
12
8
 
13
- <script setup>
14
- import { computed } from 'vue'
9
+ <script>
15
10
  import 'js-base64'
16
11
 
17
12
  const Base64 = window.Base64
18
13
 
19
- const props = defineProps({
20
- // 文件地址
21
- url: {
22
- type: String,
23
- required: true
14
+ export default {
15
+ props: {
16
+ // 文件地址
17
+ url: {
18
+ type: String,
19
+ required: true
20
+ },
21
+ // 文件地址的域名前缀,未传入时默认使用当前站点 origin
22
+ fileDomain: {
23
+ type: String,
24
+ default: ''
25
+ },
26
+ // 是否显示预览按钮
27
+ isPreview: {
28
+ type: Boolean,
29
+ default: true
30
+ },
31
+ // 是否显示下载按钮
32
+ isDownload: {
33
+ type: Boolean,
34
+ default: true
35
+ },
36
+ // 是否显示文件名,false 时显示“附件”
37
+ showFileName: {
38
+ type: Boolean,
39
+ default: true
40
+ }
24
41
  },
25
- // 文件地址的域名前缀,未传入时默认使用当前站点 origin
26
- fileDomain: {
27
- type: String,
28
- default: ''
42
+ computed: {
43
+ // 从 url 中截取文件名
44
+ filename() {
45
+ const value = this.url || ''
46
+ const raw = value.substring(value.lastIndexOf('/') + 1).split('?')[0]
47
+ try {
48
+ return decodeURIComponent(raw)
49
+ } catch (e) {
50
+ return raw
51
+ }
52
+ }
29
53
  },
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 ''
54
+ methods: {
55
+ // 从 localStorage.webConfig 读取预览服务域名
56
+ getPreviewDomain() {
57
+ try {
58
+ const webConfig = JSON.parse(localStorage.getItem('webConfig') || '{}')
59
+ return webConfig?.httpConfig?.configPreviewDomain || ''
60
+ } catch (e) {
61
+ return ''
62
+ }
63
+ },
64
+ // 预览:使用预览服务器打开文件
65
+ handlePreview() {
66
+ const domain = this.getPreviewDomain().replace(/\/+$/, '') || ''
67
+ if (!domain) {
68
+ console.warn('[file-preview] 未配置预览服务域名')
69
+ return
70
+ }
71
+ // 文件地址需带上域名:优先使用外部传入的域名,否则使用当前站点 origin
72
+ const fileDomain = (this.fileDomain || window.location.origin).replace(/\/+$/, '')
73
+ const fileUrl = /^https?:\/\//i.test(this.url)
74
+ ? this.url
75
+ : `${fileDomain}/${String(this.url).replace(/^\/+/, '')}`
76
+ const previewUrl = `${domain}/onlinePreview?url=${encodeURIComponent(Base64.encode(fileUrl))}`
77
+ window.open(previewUrl)
78
+ },
79
+ // 下载
80
+ handleDownload() {
81
+ const link = document.createElement('a')
82
+ link.href = this.url
83
+ link.download = this.filename
84
+ document.body.appendChild(link)
85
+ link.click()
86
+ document.body.removeChild(link)
87
+ }
65
88
  }
66
89
  }
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
90
  </script>
95
91
 
96
92
  <style scoped>
@@ -108,4 +104,13 @@ function handleDownload() {
108
104
  margin-right: 12px;
109
105
  line-height: 16px;
110
106
  }
107
+
108
+ .file-action {
109
+ cursor: pointer;
110
+ color: var(--gw-color-primary);
111
+ white-space: nowrap;
112
+ &+.file-action {
113
+ margin-left: 6px;
114
+ }
115
+ }
111
116
  </style>
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.57",
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
  },