@netang/quasar 0.1.95 → 0.1.97

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netang/quasar",
3
- "version": "0.1.95",
3
+ "version": "0.1.97",
4
4
  "description": "netang-quasar",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
package/utils/config.js CHANGED
@@ -11,12 +11,7 @@ import $n_get from 'lodash/get'
11
11
  // // 是否开启人民币分转元
12
12
  // priceCentToYuan: false,
13
13
  // // 上传器
14
- // uploader: {
15
- // upload: {
16
- // type: '',
17
- // domain: '',
18
- // },
19
- // },
14
+ // uploader: {},
20
15
  // },
21
16
 
22
17
  // 配置
package/utils/getFile.js CHANGED
@@ -1,7 +1,11 @@
1
+ import $n_has from 'lodash/has'
2
+
1
3
  import $n_isValidArray from '@netang/utils/isValidArray'
4
+ import $n_isValidObject from '@netang/utils/isValidObject'
2
5
  import $n_isValidString from '@netang/utils/isValidString'
6
+ import $n_split from '@netang/utils/split'
3
7
 
4
- import $n_config from './config'
8
+ import $n_uploader from './uploader'
5
9
 
6
10
  import useFileUrl from './useFileUrl'
7
11
 
@@ -12,11 +16,32 @@ export default function getFile(src) {
12
16
 
13
17
  if (src) {
14
18
 
19
+ // 如果是字符串
20
+ if ($n_isValidString(src)) {
21
+
22
+ // 则按照逗号隔开转为数租
23
+ src = $n_split(src, ',')
24
+ }
25
+
15
26
  // 如果为数组, 则获取第一个
16
27
  if ($n_isValidArray(src)) {
17
28
  src = src[0]
18
29
  }
19
30
 
31
+ // 如果为对象
32
+ if ($n_isValidObject(src)) {
33
+
34
+ if ($n_has(src, 'img')) {
35
+ src = src.img
36
+
37
+ } else if ($n_has(src, 'file')) {
38
+ src = src.file
39
+
40
+ } else if ($n_has(src, 'video')) {
41
+ src = src.video
42
+ }
43
+ }
44
+
20
45
  if ($n_isValidString(src)) {
21
46
 
22
47
  // http(s):// 或 data: 或 blob: 开头的地址
@@ -25,15 +50,11 @@ export default function getFile(src) {
25
50
  }
26
51
 
27
52
  const {
28
- type,
53
+ // type,
29
54
  domain,
30
- } = $n_config('uploader.upload')
55
+ } = $n_uploader.getUpload()
31
56
 
32
- switch (type) {
33
- // 七牛云
34
- case 'qiniu':
35
- return useFileUrl(domain, src)
36
- }
57
+ return useFileUrl(domain, src)
37
58
  }
38
59
  }
39
60
 
package/utils/getImage.js CHANGED
@@ -7,7 +7,7 @@ import $n_isValidString from '@netang/utils/isValidString'
7
7
  import $n_isNumeric from '@netang/utils/isNumeric'
8
8
  import $n_split from '@netang/utils/split'
9
9
 
10
- import $n_config from './config'
10
+ import $n_uploader from './uploader'
11
11
 
12
12
  import useFileUrl from './useFileUrl'
13
13
 
@@ -28,9 +28,10 @@ export default function getImage(src, options) {
28
28
  // 如果为数组, 则获取第一个
29
29
  if ($n_isValidArray(src)) {
30
30
  src = src[0]
31
+ }
31
32
 
32
33
  // 如果为对象
33
- } else if ($n_isValidObject(src)) {
34
+ if ($n_isValidObject(src)) {
34
35
 
35
36
  if ($n_has(src, 'options')) {
36
37
  options = src.options
@@ -77,10 +78,10 @@ export default function getImage(src, options) {
77
78
  w = Number(w)
78
79
  if (w > 0) {
79
80
 
80
- // 获取设备像素比
81
81
  /* #if IS_WEB */
82
82
  // 如果开启缩放
83
83
  if (zoom) {
84
+ // 获取设备像素比
84
85
  const devicePixelRatio = window.devicePixelRatio || 1
85
86
  if (devicePixelRatio > 2) {
86
87
  w *= 2
@@ -104,7 +105,6 @@ export default function getImage(src, options) {
104
105
  }
105
106
  }
106
107
  }
107
- // --------------------------------------------------
108
108
 
109
109
  } else {
110
110
  options = {}
@@ -113,13 +113,15 @@ export default function getImage(src, options) {
113
113
  const {
114
114
  type,
115
115
  domain,
116
- } = $n_config('uploader.upload')
116
+ } = $n_uploader.getUpload()
117
117
 
118
118
  // 判断图片上传方式
119
119
  switch (type) {
120
120
 
121
121
  // 七牛云
122
122
  case 'qiniu':
123
+ // minio
124
+ case 'minio':
123
125
 
124
126
  const {
125
127
  compress,
@@ -166,9 +168,10 @@ export default function getImage(src, options) {
166
168
  src += '/format/' + format
167
169
  }
168
170
  }
169
-
170
- return useFileUrl(domain, src)
171
+ break
171
172
  }
173
+
174
+ return useFileUrl(domain, src)
172
175
  }
173
176
  }
174
177
 
package/utils/uploader.js CHANGED
@@ -45,8 +45,8 @@ import {
45
45
  FilE_NAME,
46
46
  // 上传状态
47
47
  UPLOAD_STATUS,
48
- // 上传器
49
- UPLOADERS,
48
+ // 上传至服务器
49
+ uploadServer,
50
50
  } from './useUploader'
51
51
 
52
52
  // 文件数量
@@ -114,16 +114,15 @@ function create(options) {
114
114
  /**
115
115
  * 上传配置
116
116
  */
117
- const configUpload = Object.assign(
117
+ const configUpload = getUpload()
118
+
119
+ const configLimit = Object.assign(
118
120
  {
119
- type: 'local',
121
+ maxSize: 100,
122
+ exts: [],
120
123
  },
121
- $n_config('uploader.upload')
124
+ $n_config('uploader.limit.' + props.type, {})
122
125
  )
123
- const configLimit = Object.assign({
124
- maxSize: 100,
125
- exts: [],
126
- }, $n_config('uploader.limit.' + props.type))
127
126
 
128
127
  // 如果有单个文件的最大大小
129
128
  if (props.maxSize) {
@@ -871,23 +870,8 @@ function create(options) {
871
870
  /**
872
871
  * 上传
873
872
  */
874
- let _upload = null
875
873
  async function upload() {
876
874
  try {
877
- if (! _upload) {
878
- const run = $n_get(UPLOADERS, configUpload.type)
879
- if (run) {
880
- _upload = (await run()).default
881
- }
882
- if (! _upload) {
883
- // 错误提示
884
- $n_alert({
885
- message: '没有定义上传器',
886
- })
887
- return
888
- }
889
- }
890
-
891
875
  // 待上传文件列表
892
876
  const waitUploadFileLists = []
893
877
  for (const fileItem of uploadFileLists.value) {
@@ -900,12 +884,13 @@ function create(options) {
900
884
  return
901
885
  }
902
886
 
903
- // 上传
904
- await _upload({
905
- config: configUpload,
887
+ // 上传至服务器
888
+ await uploadServer({
889
+ fileType: FilE_TYPE[props.type],
890
+ configUpload,
906
891
  waitUploadFileLists,
907
- uploadFileLists,
908
- checkFileError,
892
+ // uploadFileLists,
893
+ // checkFileError,
909
894
  setFileSuccess,
910
895
  setFileFail,
911
896
  })
@@ -1614,12 +1599,29 @@ function create(options) {
1614
1599
  }
1615
1600
  }
1616
1601
 
1602
+ /**
1603
+ * 获取上传方式
1604
+ */
1605
+ export function getUpload(userConfig = null, defaultUpload = '') {
1606
+ const uploadConfig = $n_get((userConfig ? userConfig : configs.userConfig), 'uploader.upload', {})
1607
+ const type = defaultUpload ? defaultUpload : uploadConfig.default
1608
+ return Object.assign(
1609
+ {},
1610
+ $n_get(uploadConfig, type, {}),
1611
+ {
1612
+ type,
1613
+ }
1614
+ )
1615
+ }
1616
+
1617
1617
  /**
1618
1618
  * 上传器
1619
1619
  */
1620
1620
  const uploader = {
1621
1621
  // 创建对话框
1622
1622
  create,
1623
+ // 获取上传方式
1624
+ getUpload,
1623
1625
  }
1624
1626
 
1625
1627
  export default uploader
@@ -14,6 +14,7 @@ export default function useFileUrl(domain, src) {
14
14
  const {
15
15
  getFileUrl,
16
16
  } = configs
17
+
17
18
  if ($n_isFunction(getFileUrl)) {
18
19
  const res = getFileUrl(domain, src)
19
20
  if ($n_isValidString(res)) {
@@ -1,53 +1,303 @@
1
- /**
2
- * 文件类型映射
3
- */
4
- export const FilE_TYPE = {
5
- file: 1,
6
- image: 2,
7
- video: 3,
8
- audio: 4,
9
- }
10
-
11
- /**
12
- * 文件名称映射
13
- */
14
- export const FilE_NAME = {
15
- 1: '文件',
16
- 2: '图片',
17
- 3: '视频',
18
- 4: '音频',
19
- }
20
-
21
- /**
22
- * 上传状态
23
- */
24
- export const UPLOAD_STATUS = {
25
- // 等待上传中
26
- waiting: 1,
27
- // 检查 hash 中
28
- hashChecking: 2,
29
- // 检查 hash 完成
30
- hashChecked: 3,
31
- // 检查是否存在服务器中
32
- existChecking: 4,
33
- // 检查是否存在服务器完成
34
- existChecked: 5,
35
- // 上传中
36
- uploading: 6,
37
- // 上传完成
38
- success: 7,
39
- // 上传失败
40
- fail: 8,
41
- }
42
-
43
- /**
44
- * 上传器
45
- */
46
- export const UPLOADERS = {
47
- // 本地上传
48
- 'local': ()=>import('./uploader/local'),
49
- // 七牛云上传
50
- 'qiniu': ()=>import('./uploader/qiniu'),
51
- // 阿里云上传
52
- 'aliyun': ()=>import('./uploader/aliyun'),
53
- }
1
+ import $n_storage from '@netang/utils/storage'
2
+ import $n_isValidObject from '@netang/utils/isValidObject'
3
+ import $n_isValidString from '@netang/utils/isValidString'
4
+ import $n_http from '@netang/utils/http'
5
+ import $n_numberDeep from '@netang/utils/numberDeep'
6
+ import $n_json from '@netang/utils/json'
7
+
8
+ import { getUpload } from './uploader'
9
+
10
+ import $n_toast from './toast'
11
+ import $n_config from './config'
12
+
13
+ /**
14
+ * 文件类型映射
15
+ */
16
+ export const FilE_TYPE = {
17
+ file: 1,
18
+ image: 2,
19
+ video: 3,
20
+ audio: 4,
21
+ }
22
+
23
+ /**
24
+ * 文件名称映射
25
+ */
26
+ export const FilE_NAME = {
27
+ 1: '文件',
28
+ 2: '图片',
29
+ 3: '视频',
30
+ 4: '音频',
31
+ }
32
+
33
+ /**
34
+ * 上传状态
35
+ */
36
+ export const UPLOAD_STATUS = {
37
+ // 等待上传中
38
+ waiting: 1,
39
+ // 检查 hash 中
40
+ hashChecking: 2,
41
+ // 检查 hash 完成
42
+ hashChecked: 3,
43
+ // 检查是否存在服务器中
44
+ existChecking: 4,
45
+ // 检查是否存在服务器完成
46
+ existChecked: 5,
47
+ // 上传中
48
+ uploading: 6,
49
+ // 上传完成
50
+ success: 7,
51
+ // 上传失败
52
+ fail: 8,
53
+ }
54
+
55
+ /**
56
+ * 获取上传参数
57
+ */
58
+ export async function getUploadParams(type, bucket = 'public') {
59
+
60
+ // 缓存名
61
+ const cacheName = `upload_params_${type}_${bucket}`
62
+
63
+ // 获取缓存
64
+ const cache = $n_storage.get(cacheName)
65
+ if (cache !== null) {
66
+ return cache
67
+ }
68
+
69
+ // 请求数据
70
+ const { status, data } = await $n_http({
71
+ url: $n_config('apiFileUrl') + 'get_upload_params',
72
+ data: {
73
+ // 类型
74
+ type,
75
+ // 空间名称
76
+ bucket,
77
+ },
78
+ })
79
+
80
+ // 如果成功
81
+ if (! status || ! $n_isValidObject(data)) {
82
+ return false
83
+ }
84
+
85
+ // 【生产模式】
86
+ // --------------------------------------------------
87
+ // #ifdef IS_PRO
88
+ // 保存缓存(6 小时)
89
+ $n_storage.set(cacheName, data, 21600000)
90
+ // #endif
91
+ // --------------------------------------------------
92
+
93
+ return data
94
+ }
95
+
96
+ /**
97
+ * 删除上传参数缓存
98
+ */
99
+ export function deleteUploadParams(type, bucket = 'public') {
100
+ $n_storage.delete(`upload_params_${type}_${bucket}`)
101
+ }
102
+
103
+ /**
104
+ * 上传至服务器
105
+ */
106
+ export async function uploadServer(params) {
107
+
108
+ const {
109
+ fileType,
110
+ configUpload,
111
+ waitUploadFileLists,
112
+ // uploadFileLists,
113
+ // checkFileError,
114
+ setFileSuccess,
115
+ setFileFail,
116
+ } = params
117
+
118
+ // 获取上传参数
119
+ const uploadParams = await getUploadParams(configUpload.type)
120
+ if (uploadParams === false) {
121
+ for (const fileItem of waitUploadFileLists) {
122
+ setFileFail(fileItem, '上传失败')
123
+ }
124
+ $n_toast({
125
+ message: `获取[${configUpload.type}]上传参数失败`,
126
+ })
127
+ return
128
+ }
129
+
130
+ // 是否上传 minio 备份
131
+ // --------------------------------------------------
132
+ let backupParams = null
133
+ let backupConfig = null
134
+ if (configUpload.type !== 'minio') {
135
+ backupConfig = getUpload(null, 'minio')
136
+ // 如果同步
137
+ if (backupConfig.sync === true) {
138
+ backupParams = await getUploadParams(backupConfig.type)
139
+ if (backupParams === false) {
140
+ for (const fileItem of waitUploadFileLists) {
141
+ setFileFail(fileItem, '上传失败')
142
+ }
143
+ $n_toast({
144
+ message: `获取[${backupConfig.type}]上传参数失败`,
145
+ })
146
+ return
147
+ }
148
+ }
149
+ }
150
+ // --------------------------------------------------
151
+
152
+ // 批量上传
153
+ for (const fileItem of waitUploadFileLists) {
154
+ // 上传单个文件
155
+ await uploadFileItem(fileItem)
156
+ }
157
+
158
+ /**
159
+ * 上传单个文件
160
+ */
161
+ async function uploadFileItem(fileItem) {
162
+
163
+ // 设置文件状态
164
+ fileItem.status = UPLOAD_STATUS.uploading
165
+
166
+ // 上传文件
167
+ const upload = async function(configUpload, uploadParams, startPercent, halfPercent) {
168
+
169
+ const {
170
+ // 上传地址
171
+ url,
172
+ // 文件名
173
+ fileName,
174
+ // 键值名
175
+ keyName,
176
+ // 上传数据
177
+ data,
178
+ } = uploadParams
179
+
180
+ // 请求数据
181
+ const httpData = Object.assign({}, data)
182
+ // 文件
183
+ httpData[fileName] = fileItem.file
184
+ // 自定义文件 key
185
+ httpData[keyName] = fileItem.hash
186
+
187
+ const { status, data: res } = await $n_http({
188
+ // 上传地址
189
+ url,
190
+ // 数据
191
+ data: httpData,
192
+ // 关闭错误提醒
193
+ warn: false,
194
+ // 关闭检查结果 code
195
+ checkCode: false,
196
+ // 不包含头部鉴权认证
197
+ token: false,
198
+ // 开启上传
199
+ upload: true,
200
+ // 取消请求
201
+ onCancel(cancel) {
202
+ // 设置中断上传
203
+ fileItem.abort = function(msg) {
204
+ cancel($n_isValidString(msg) ? msg : '已取消')
205
+ }
206
+ },
207
+ // 监听上传进度
208
+ onUploadProgress(percent) {
209
+ // 设置上传进度
210
+ fileItem.progress = Math.round(startPercent + (halfPercent ? percent / 2 : percent))
211
+ },
212
+ })
213
+
214
+ // 如果请求失败
215
+ if (! status) {
216
+ // 设置文件上传失败
217
+ setFileFail(fileItem, res.msg)
218
+ // 删除上传参数缓存
219
+ deleteUploadParams(configUpload.type)
220
+ return false
221
+ }
222
+
223
+ return res
224
+ }
225
+ const resUpload = await upload(configUpload, uploadParams, 0, !! backupParams)
226
+ if (resUpload === false) {
227
+ return false
228
+ }
229
+
230
+ // 是否已备份
231
+ let is_backup = 0
232
+
233
+ // 上传至备份服务器
234
+ // --------------------------------------------------
235
+ if (backupParams) {
236
+ const res = await upload(backupConfig, backupParams, 50, true)
237
+ if (res === false) {
238
+ return false
239
+ }
240
+ is_backup = 1
241
+ }
242
+ // --------------------------------------------------
243
+
244
+ const {
245
+ title,
246
+ type,
247
+ hash,
248
+ ext,
249
+ size,
250
+ json,
251
+ } = fileItem
252
+
253
+ // 请求 - 上传文件至 cdn
254
+ const { status: statusCallback, data: resCallback } = await $n_http({
255
+ url: $n_config('apiFileUrl') + 'upload_callback',
256
+ data: {
257
+ // 类型
258
+ type: configUpload.type,
259
+ // 需上传的文件类型
260
+ file_type: fileType,
261
+ // 文件
262
+ file: {
263
+ // 标题
264
+ title: title || '',
265
+ // 类型
266
+ type,
267
+ // hash
268
+ hash,
269
+ // 后缀
270
+ ext,
271
+ // 文件大小
272
+ size,
273
+ // 文件 json
274
+ json,
275
+ // 是否已备份
276
+ is_backup,
277
+ },
278
+ // 结果
279
+ result: $n_isValidObject(resUpload) ? resUpload : {},
280
+ },
281
+ // 关闭错误提示
282
+ warn: false,
283
+ })
284
+
285
+ // 请求失败
286
+ if (! statusCallback || ! $n_isValidObject(resCallback)) {
287
+ // 设置文件上传失败
288
+ setFileFail(fileItem, resCallback.msg || '上传失败')
289
+ return false
290
+ }
291
+
292
+ // 格式化 json
293
+ const _json = $n_json.parse(resCallback.json)
294
+ Object.assign(fileItem, resCallback, {
295
+ json: $n_isValidObject(_json) ? $n_numberDeep(_json) : {},
296
+ })
297
+
298
+ // 设置文件上传成功
299
+ setFileSuccess(fileItem)
300
+
301
+ return true
302
+ }
303
+ }
@@ -1,6 +0,0 @@
1
- /**
2
- * 七牛云上传
3
- */
4
- export default async function () {
5
-
6
- }
@@ -1,8 +0,0 @@
1
- /**
2
- * 七牛云上传
3
- */
4
- export default async function () {
5
-
6
-
7
-
8
- }
@@ -1,326 +0,0 @@
1
- import $n_has from 'lodash/has'
2
- import $n_isString from 'lodash/isString'
3
- import $n_findIndex from 'lodash/findIndex'
4
- import $n_trim from 'lodash/trim'
5
-
6
- import $n_isValidObject from '@netang/utils/isValidObject'
7
- import $n_isValidString from '@netang/utils/isValidString'
8
- import $n_http from '@netang/utils/http'
9
- import $n_trimString from '@netang/utils/trimString'
10
-
11
- import $n_toast from '../toast'
12
- import $n_config from '../config'
13
-
14
- import {
15
- UPLOAD_STATUS,
16
- } from '../useUploader'
17
-
18
- /**
19
- * 获取七牛云上传 token
20
- */
21
- async function getQiniuToken(bucket = 'public') {
22
-
23
- // 请求数据
24
- const { status, data } = await $n_http({
25
- url: $n_config('apiFileUrl') + 'get_qiniu_token',
26
- data: {
27
- bucket,
28
- },
29
-
30
- // 【生产模式】
31
- // --------------------------------------------------
32
- // #ifdef IS_PRO
33
- // 开启缓存
34
- cache: 'qiniu_token_' + bucket,
35
- // 缓存时间(6 小时)
36
- cacheTime: 21600000,
37
- // #endif
38
- // --------------------------------------------------
39
- })
40
-
41
- // 如果成功
42
- if (! status) {
43
- return false
44
- }
45
-
46
- return data
47
- }
48
-
49
- /**
50
- * 七牛云上传
51
- */
52
- export default async function ({ waitUploadFileLists, uploadFileLists, checkFileError, setFileSuccess, setFileFail }) {
53
-
54
- // 获取七牛云上传 token
55
- const token = await getQiniuToken()
56
- if (! token) {
57
- for (const fileItem of waitUploadFileLists) {
58
- setFileFail(fileItem, '上传失败')
59
- }
60
- $n_toast({
61
- message: '获取上传参数失败',
62
- })
63
- return
64
- }
65
-
66
- // 批量上传
67
- for (const fileItem of waitUploadFileLists) {
68
- // 上传单个文件
69
- await uploadFileItem(fileItem, token)
70
- }
71
-
72
- /**
73
- * 上传单个文件
74
- */
75
- async function uploadFileItem(fileItem, token) {
76
-
77
- // 设置文件状态
78
- fileItem.status = UPLOAD_STATUS.uploading
79
-
80
- // 请求上传文件到七牛云
81
- const { status, data: resUpload } = await $n_http({
82
- // 上传地址
83
- url: 'https://upload.qiniup.com/',
84
- // 数据
85
- data: {
86
- // 七牛云上传 token
87
- token,
88
- // 文件
89
- file: fileItem.file,
90
- // 自定义文件 key
91
- key: fileItem.hash,
92
- },
93
- // 关闭错误提醒
94
- warn: false,
95
- // 关闭检查结果 code
96
- checkCode: false,
97
- // 不包含头部鉴权认证
98
- token: false,
99
- // 开启上传
100
- upload: true,
101
- // 取消请求
102
- onCancel(cancel) {
103
- // 设置中断上传
104
- fileItem.abort = function(msg) {
105
- cancel($n_isValidString(msg) ? msg : '已取消')
106
- }
107
- },
108
- // 监听上传进度
109
- onUploadProgress(percent) {
110
- // 设置上传进度
111
- fileItem.progress = percent
112
- },
113
- })
114
-
115
- // 如果请求失败
116
- if (! status) {
117
- // 设置文件上传失败
118
- setFileFail(fileItem, resUpload.msg)
119
- return
120
- }
121
-
122
- // 如果检查七牛回调成功
123
- const query = await checkQiniuCallback(resUpload, fileItem)
124
- if (! query) {
125
- return
126
- }
127
-
128
- // 请求 - 上传文件至 cdn
129
- const { status: statusCallback, data: resCallback } = await $n_http({
130
- url: $n_config('apiFileUrl') + 'upload_cdn_callback',
131
- data: query,
132
- // 关闭错误提示
133
- warn: false,
134
- })
135
-
136
- // 请求失败
137
- if (! statusCallback) {
138
- // 设置文件上传失败
139
- setFileFail(fileItem, resCallback.msg || '上传失败')
140
- return
141
- }
142
-
143
- // 设置文件上传成功
144
- setFileSuccess(fileItem)
145
- }
146
-
147
- /**
148
- * 检查七牛回调成功
149
- */
150
- function checkQiniuCallback(res, fileItem) {
151
-
152
- // 如果文件被删除
153
- if ($n_findIndex(uploadFileLists.value, { hash: fileItem.hash }) === -1) {
154
- // 设置文件上传失败
155
- setFileFail(fileItem, '上传失败')
156
- return false
157
- }
158
-
159
- // 返回示例
160
- // ------------------------------
161
- // format: "null"
162
- // width: null
163
- // height: null
164
- // key: "d038dce5892840636b2c1f6d241f33ad"
165
- // hash: "lrYGlELBAgN9OgKV_i5gbHOdZMvt"
166
- // orientation: null
167
- // size: 6620454
168
- // aduration: 57.258005
169
- // vwidth: 1280
170
- // vheight: 720
171
- // vrotate: null
172
- // vduration: 57.291992
173
-
174
- let {
175
- format,
176
- width,
177
- height,
178
- key,
179
- orientation,
180
- size,
181
- aduration,
182
- vwidth,
183
- vheight,
184
- vrotate,
185
- vduration,
186
- } = res
187
-
188
- format = $n_trimString(format)
189
- if (format) {
190
- format = format.toLowerCase()
191
- }
192
-
193
- const query = {
194
- // 标题
195
- title: fileItem.title,
196
- // 类型(1:文件,2:图片,3:视频,4:音频)
197
- type: 0,
198
- // hash
199
- hash: key,
200
- // 文件大小
201
- size,
202
- // 后缀
203
- ext: fileItem.ext,
204
- }
205
-
206
- // json 信息
207
- const json = {}
208
-
209
- // 【1】先判断是否为视频(有时长 && 有宽 && 有高 && 时长 > 0, 则为视频)
210
- if (vduration && vwidth && vheight) {
211
-
212
- // 类型(3:视频)
213
- query.type = 3
214
-
215
- // 视频旋转角度
216
- // vrotate: 无 ==> 1: 手机右横屏(宽高不变)
217
- // vrotate: 90 ==> 2: 手机垂直(宽高反转)
218
- // vrotate: 180 ==> 3: 手机左横屏(宽高不变)
219
- // vrotate: 270 ==> 4: 手机倒过来垂直(宽高反转)
220
- const rotates = {
221
- 90: 2,
222
- 180: 3,
223
- 270: 4,
224
- }
225
-
226
- // 设置 json 信息
227
- Object.assign(json, {
228
- w: vwidth,
229
- h: vheight,
230
- d: vduration,
231
- })
232
-
233
- if ($n_has(rotates, vrotate)) {
234
- json.o = rotates[vrotate]
235
- }
236
-
237
- // 【2】再判断是否为音频(有音频时长 && 无视频时长 && 音频时长 > 0, 则为音频)
238
- } else if (aduration && ! vduration) {
239
-
240
- // 类型(4:音频)
241
- query.type = 4
242
-
243
- // 设置 json 信息
244
- json.d = aduration
245
-
246
- // 【3】再判断是否为图片(有宽 && 有高 && 大小 < 20M, 为图片)
247
- } else if (width && height) {
248
-
249
- // 类型(2:图片)
250
- query.type = 2
251
-
252
- // 图片后缀名
253
- query.ext = format === 'png' || format === 'gif' ? format : 'jpg'
254
-
255
- // 如果大小 < 20M, 则为可用图片
256
- if (size < 20971520) {
257
-
258
- // 设置 json 数据
259
- json.w = width
260
- json.h = height
261
-
262
- // 图片垂直角度
263
- const orientations = {
264
- // 【1】相机原始位置(宽高不变)
265
- // 'top-left': 1,
266
- // 【2】等于 1 的垂直镜像(几乎无用)(宽高不变)
267
- 'top-right': 2,
268
- // 【3】旋转 180度(宽高不变)
269
- 'bottom-right': 3,
270
- // 【4】等于 3 的垂直镜像(几乎无用)(宽高不变)
271
- 'bottom-left': 4,
272
- // 【5】等于 8 的水平镜像(几乎无用)(宽高反转)
273
- 'left-top': 5,
274
- // 【6】旋转 90 度(宽高反转)
275
- 'right-top': 6,
276
- // 【7】等于 8 的垂直镜像(几乎无用)(宽高反转)
277
- 'right-bottom': 7,
278
- // 【8】旋转 270 度(宽高反转)
279
- 'left-bottom': 8,
280
- }
281
- if (orientation && $n_isString(orientation)) {
282
- const key = $n_trim(orientation).toLowerCase()
283
- if ($n_has(orientations, key)) {
284
- json.o = orientations[key]
285
- }
286
- }
287
-
288
- // 否则为类型为文件
289
- } else {
290
- // 检查文件错误
291
- const errMsg = checkFileError(query)
292
- if (errMsg) {
293
- // 设置文件上传失败
294
- setFileFail(fileItem, errMsg)
295
- return false
296
- }
297
-
298
- // 类型(1:文件)
299
- query.type = 1
300
- }
301
-
302
- // 否则为类型为文件
303
- } else {
304
- // 类型(1:文件)
305
- query.type = 1
306
- }
307
-
308
- // 检查文件错误
309
- const errMsg = checkFileError(query)
310
- if (errMsg) {
311
- // 设置文件上传失败
312
- setFileFail(fileItem, errMsg)
313
- return false
314
- }
315
-
316
- // json 信息
317
- query.json = json
318
-
319
- // 设置文件
320
- Object.assign(fileItem, query)
321
-
322
- return Object.assign({}, query, {
323
- json: $n_isValidObject(json) ? JSON.stringify(json) : ''
324
- })
325
- }
326
- }