@aochuang/common 1.0.137 → 1.0.138

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.
@@ -9,11 +9,11 @@ export default {
9
9
  name: {
10
10
  type: String,
11
11
  required: true
12
- },
12
+ },
13
13
  size: {
14
14
  type: Number
15
15
  },
16
- color: {
16
+ color: {
17
17
  type: String
18
18
  }
19
19
  },
@@ -1,5 +1,9 @@
1
1
  import Image from './image'
2
- import { setImageClickFn, getThumbnailSrc as _getThumbnailSrc, removeThumbnailSrc as _removeThumbnailSrc } from './util'
2
+ import {
3
+ setImageClickFn,
4
+ getThumbnailSrc as _getThumbnailSrc,
5
+ removeThumbnailSrc as _removeThumbnailSrc
6
+ } from './util'
3
7
 
4
8
  Image.install = (Vue, options) => {
5
9
  options = Object.assign({
@@ -106,6 +106,39 @@ export function getThumbnailSrc (url, width) {
106
106
  }
107
107
  }
108
108
 
109
+ function decodeUrlParam (value) {
110
+ try {
111
+ return decodeURIComponent(value.replace(/\+/g, ' '))
112
+ } catch (e) {
113
+ return value
114
+ }
115
+ }
116
+
117
+ function removeThumbnailQuery (url) {
118
+ const hashIndex = url.indexOf('#')
119
+ const hash = hashIndex > -1 ? url.substr(hashIndex) : ''
120
+ const urlWithoutHash = hashIndex > -1 ? url.substr(0, hashIndex) : url
121
+ const queryIndex = urlWithoutHash.indexOf('?')
122
+
123
+ if (queryIndex < 0) {
124
+ return url
125
+ }
126
+
127
+ const baseUrl = urlWithoutHash.substr(0, queryIndex)
128
+ const query = urlWithoutHash.substr(queryIndex + 1)
129
+ const nextQuery = query.split('&').filter(param => {
130
+ const equalIndex = param.indexOf('=')
131
+ const key = decodeUrlParam(equalIndex > -1 ? param.substr(0, equalIndex) : param)
132
+ const value = decodeUrlParam(equalIndex > -1 ? param.substr(equalIndex + 1) : '')
133
+ const isAliyunThumbnail = key === 'x-oss-process' && /^image\/resize,w_[^/]+$/.test(value)
134
+ const isTencentThumbnail = /^imageMogr2\/thumbnail\/[^/]+$/.test(key) && !value
135
+
136
+ return !isAliyunThumbnail && !isTencentThumbnail
137
+ }).join('&')
138
+
139
+ return baseUrl + (nextQuery ? '?' + nextQuery : '') + hash
140
+ }
141
+
109
142
  /**
110
143
  * 移除图片缩略图裁剪参数,保留其他查询参数及 hash
111
144
  */
@@ -115,27 +148,29 @@ export function removeThumbnailSrc (url) {
115
148
  }
116
149
 
117
150
  const hashIndex = url.indexOf('#')
118
- const hash = hashIndex >= 0 ? url.substr(hashIndex) : ''
119
- let source = hashIndex >= 0 ? url.substr(0, hashIndex) : url
120
-
121
- // /oss/view 和 /oss/ 地址的真实图片在 url 参数中,递归处理后再重新编码
122
- if (source.indexOf('/oss/view') > -1 || source.indexOf('/oss/') > -1) {
123
- const marker = source.indexOf('/oss/view') > -1 ? '/oss/view' : '/oss/'
124
- const markerIndex = source.indexOf(marker)
125
- const prefix = source.substr(0, markerIndex + marker.length)
126
- const params = getUrlParams(source)
127
- if (params.url) {
128
- params.url = removeThumbnailSrc(params.url)
129
- source = buildUrl(prefix, params)
130
- return source + hash
131
- }
151
+ const hash = hashIndex > -1 ? url.substr(hashIndex) : ''
152
+ const urlWithoutHash = hashIndex > -1 ? url.substr(0, hashIndex) : url
153
+ let proxyPath = ''
154
+
155
+ if (urlWithoutHash.indexOf('/oss/view') > -1) {
156
+ proxyPath = '/oss/view'
157
+ } else if (urlWithoutHash.indexOf('/oss/') > -1) {
158
+ proxyPath = '/oss/'
159
+ }
160
+
161
+ if (!proxyPath) {
162
+ return removeThumbnailQuery(url)
163
+ }
164
+
165
+ const urlParams = getUrlParams(urlWithoutHash)
166
+ if (!urlParams.url) {
167
+ return url
132
168
  }
133
169
 
134
- // 阿里云参数是普通 query 参数;腾讯云参数是无等号的 query path
135
- source = source
136
- .replace(/([?&])x-oss-process=image\/resize(?:,[^&#]*)?(?=&|$)/ig, '$1')
137
- .replace(/([?&])imageMogr2\/thumbnail\/[^&#]*(?=&|$)/ig, '$1')
138
- .replace('?&', '?')
139
- .replace(/[?&]$/, '')
140
- return source + hash
170
+ return buildUrl(
171
+ urlWithoutHash.substr(0, urlWithoutHash.indexOf(proxyPath) + proxyPath.length),
172
+ Object.assign({}, urlParams, {
173
+ url: removeThumbnailSrc(urlParams.url)
174
+ })
175
+ ) + hash
141
176
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aochuang/common",
3
- "version": "1.0.137",
3
+ "version": "1.0.138",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "publishConfig": {