haiwei-module-admin 1.1.1 → 1.1.2

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.
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "id": 0,
3
3
  "name": "haiwei-module-admin",
4
4
  "code": "admin",
5
- "version": "1.1.1",
5
+ "version": "1.1.2",
6
6
  "description": "haiwei前端Admin模块组件",
7
7
  "author": "Eric",
8
8
  "license": "ISC",
@@ -0,0 +1,55 @@
1
+ <template>
2
+ <div :style="{ width, height }" class="nm-file-img-preview">
3
+ <img v-if="url__" :src="url__" />
4
+ <nm-icon class="no-picture" v-else name="photo" />
5
+ </div>
6
+ </template>
7
+ <script>
8
+ export default {
9
+ data() {
10
+ return {
11
+ url_: this.url,
12
+ url__: ''
13
+ }
14
+ },
15
+ props: {
16
+ url: {
17
+ type: String
18
+ },
19
+ //是否私有
20
+ private: Boolean,
21
+ width: {
22
+ type: String,
23
+ default: 'auto'
24
+ },
25
+ height: {
26
+ type: String,
27
+ default: 'auto'
28
+ }
29
+ },
30
+ watch: {
31
+ url: {
32
+ immediate: true,
33
+ handler(val) {
34
+ if (val !== this.url_) {
35
+ if (this.private && this.url) {
36
+ $api.admin.file.preview(this.url).then(url => {
37
+ this.url__ = url
38
+ })
39
+ } else {
40
+ this.url__ = val
41
+ }
42
+ }
43
+ }
44
+ }
45
+ }
46
+ }
47
+ </script>
48
+ <style lang="scss">
49
+ .nm-file-img-preview {
50
+ display: block;
51
+ img {
52
+ width: 100%;
53
+ }
54
+ }
55
+ </style>
@@ -16,6 +16,10 @@ export default {
16
16
  url: {
17
17
  type: String
18
18
  },
19
+ // 完整路径(公有图片)
20
+ fullPath: {
21
+ type: String
22
+ },
19
23
  //是否私有
20
24
  private: Boolean,
21
25
  width: {
@@ -27,19 +31,89 @@ export default {
27
31
  default: 'auto'
28
32
  }
29
33
  },
34
+ computed: {
35
+ baseUrl() {
36
+ // 优先使用全局变量,否则使用默认值
37
+ return window.__HAIWEI_API_BASE_URL__ || '/api/'
38
+ }
39
+ },
30
40
  watch: {
31
41
  url: {
32
42
  immediate: true,
33
43
  handler(val) {
34
44
  if (val !== this.url_) {
35
- if (this.private && this.url) {
36
- $api.admin.file.preview(this.url).then(url => {
37
- this.url__ = url
38
- })
45
+ this.updatePreviewUrl()
46
+ }
47
+ }
48
+ },
49
+ fullPath: {
50
+ immediate: true,
51
+ handler() {
52
+ this.updatePreviewUrl()
53
+ }
54
+ },
55
+ private: {
56
+ immediate: true,
57
+ handler() {
58
+ this.updatePreviewUrl()
59
+ }
60
+ }
61
+ },
62
+ methods: {
63
+ // 判断是否为绝对 URL
64
+ isAbsoluteUrl(path) {
65
+ return /^(https?:)?\/\//.test(path)
66
+ },
67
+
68
+ // 规范化路径拼接
69
+ normalizePath(base, path) {
70
+ // 如果 path 已经是绝对 URL,直接返回
71
+ if (this.isAbsoluteUrl(path)) {
72
+ return path
73
+ }
74
+ // 确保 base 以 / 结尾,path 不以 / 开头
75
+ let baseUrl = base.endsWith('/') ? base.slice(0, -1) : base
76
+ let fullPath = path.startsWith('/') ? path.slice(1) : path
77
+ return `${baseUrl}/${fullPath}`
78
+ },
79
+
80
+ updatePreviewUrl() {
81
+ // 重置内部状态
82
+ this.url_ = this.url
83
+
84
+ // 优先使用 fullPath
85
+ if (this.fullPath) {
86
+ if (this.private) {
87
+ // 私有图片,调用预览接口
88
+ $api.admin.file.preview(this.fullPath).then(url => {
89
+ this.url__ = url
90
+ }).catch(() => {
91
+ // 如果接口调用失败,使用原始路径
92
+ this.url__ = this.fullPath
93
+ })
94
+ } else {
95
+ // 公有图片,拼接 OSS 路径
96
+ if (this.isAbsoluteUrl(this.fullPath)) {
97
+ // 如果已经是绝对 URL,直接使用
98
+ this.url__ = this.fullPath
39
99
  } else {
40
- this.url__ = val
100
+ // 拼接 OSS 路径
101
+ this.url__ = this.normalizePath(this.baseUrl + '/oss/o', this.fullPath)
41
102
  }
42
103
  }
104
+ } else if (this.url) {
105
+ // 原有逻辑
106
+ if (this.private) {
107
+ $api.admin.file.preview(this.url).then(url => {
108
+ this.url__ = url
109
+ }).catch(() => {
110
+ this.url__ = this.url
111
+ })
112
+ } else {
113
+ this.url__ = this.url
114
+ }
115
+ } else {
116
+ this.url__ = ''
43
117
  }
44
118
  }
45
119
  }
@@ -162,7 +162,7 @@ export default {
162
162
  },
163
163
 
164
164
  downloadUrl() {
165
- console.log('this.fileInfo?.url ', this.fileInfo?.url)
165
+ // console.log('this.fileInfo?.url ', this.fileInfo?.url)
166
166
  return this.fileInfo?.url || this.fullPath
167
167
  },
168
168