@netang/quasar 0.1.75 → 0.1.76

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.
Files changed (247) hide show
  1. package/.editorconfig +12 -12
  2. package/_docs/docs/components/field-table.md +58 -58
  3. package/_docs/docs/components/field-tree.md +21 -21
  4. package/_docs/docs/components/table.md +24 -24
  5. package/_docs/docs/utils/table.md +196 -196
  6. package/components/dialog/img-viewer/index.vue +657 -657
  7. package/components/dialog/index.vue +372 -372
  8. package/components/drawer/index.vue +303 -303
  9. package/components/editor-code/index.vue +325 -325
  10. package/components/empty/index.vue +80 -80
  11. package/components/field-date/index.vue +850 -850
  12. package/components/field-tree/index.vue +754 -754
  13. package/components/img/index.vue +211 -211
  14. package/components/mixed-table/index.vue +532 -532
  15. package/components/mixed-table-splitter/index.vue +377 -377
  16. package/components/power-page/index.vue +94 -94
  17. package/components/private/components/move-to-tree/index.vue +154 -154
  18. package/components/private/edit-power-data/index.vue +846 -846
  19. package/components/private/table-visible-columns-button/index.vue +114 -114
  20. package/components/render/index.vue +123 -123
  21. package/components/search/index.vue +231 -231
  22. package/components/search-item/index.vue +212 -212
  23. package/components/select/index.vue +177 -177
  24. package/components/splitter/index.vue +422 -422
  25. package/components/table/index.vue +513 -513
  26. package/components/table-column-fixed/index.vue +110 -110
  27. package/components/table-summary/index.vue +107 -107
  28. package/components/toolbar/index.vue +146 -146
  29. package/components/tree/index.vue +1728 -1728
  30. package/components/uploader/index.vue +188 -188
  31. package/docs/404.html +33 -33
  32. package/docs/assets/404.html-60b35caa.js +1 -1
  33. package/docs/assets/404.html-d1e63d77.js +1 -1
  34. package/docs/assets/alert.html-b2a2a72f.js +5 -5
  35. package/docs/assets/alert.html-ba46d137.js +1 -1
  36. package/docs/assets/app-9f30aa4b.js +6 -6
  37. package/docs/assets/area.html-01b9b58d.js +42 -42
  38. package/docs/assets/area.html-9a4fce6a.js +1 -1
  39. package/docs/assets/arr.html-145d27e7.js +1 -1
  40. package/docs/assets/arr.html-674e65ab.js +11 -11
  41. package/docs/assets/auth.html-579fa830.js +1 -1
  42. package/docs/assets/auth.html-8544ed95.js +8 -8
  43. package/docs/assets/bus.html-c71254aa.js +1 -1
  44. package/docs/assets/bus.html-dc7d3d19.js +6 -6
  45. package/docs/assets/column-title.html-c735cb5a.js +3 -3
  46. package/docs/assets/column-title.html-e9316762.js +1 -1
  47. package/docs/assets/confirm.html-ddfdc27f.js +10 -10
  48. package/docs/assets/confirm.html-ef3e2bef.js +1 -1
  49. package/docs/assets/copy.html-d20345b6.js +1 -1
  50. package/docs/assets/copy.html-ef8c8571.js +13 -13
  51. package/docs/assets/data.html-6432175d.js +30 -30
  52. package/docs/assets/data.html-a3b05d5b.js +1 -1
  53. package/docs/assets/dialog.html-1f698e5a.js +1 -1
  54. package/docs/assets/dialog.html-62902b83.js +68 -68
  55. package/docs/assets/dialog.html-baea77c9.js +1 -1
  56. package/docs/assets/dialog.html-bb082fc4.js +1 -1
  57. package/docs/assets/dict.html-1311da3d.js +23 -23
  58. package/docs/assets/dict.html-b96fbf0c.js +1 -1
  59. package/docs/assets/dictOptions.html-7c4f40a5.js +1 -1
  60. package/docs/assets/dictOptions.html-fb99d175.js +5 -5
  61. package/docs/assets/dragger.html-668d3efa.js +1 -1
  62. package/docs/assets/dragger.html-749d585a.js +1 -1
  63. package/docs/assets/editor-code.html-6ab26ea9.js +1 -1
  64. package/docs/assets/editor-code.html-d196205d.js +1 -1
  65. package/docs/assets/empty.html-1c139131.js +1 -1
  66. package/docs/assets/empty.html-1e9c441d.js +1 -1
  67. package/docs/assets/field-date.html-069fdb13.js +1 -1
  68. package/docs/assets/field-date.html-ad204aa9.js +1 -1
  69. package/docs/assets/field-table.html-ce480f03.js +1 -1
  70. package/docs/assets/field-table.html-d9236160.js +1 -1
  71. package/docs/assets/field-text.html-7277c62f.js +1 -1
  72. package/docs/assets/field-text.html-ccb4cecf.js +1 -1
  73. package/docs/assets/field-tree.html-519bfb45.js +1 -1
  74. package/docs/assets/field-tree.html-fdc748d6.js +1 -1
  75. package/docs/assets/form.html-2b562c37.js +2 -2
  76. package/docs/assets/form.html-75104cd5.js +1 -1
  77. package/docs/assets/framework-204010b2.js +5 -5
  78. package/docs/assets/getData.html-990e3787.js +1 -1
  79. package/docs/assets/getData.html-bb72025f.js +34 -34
  80. package/docs/assets/getFile.html-42368004.js +1 -1
  81. package/docs/assets/getFile.html-99abd054.js +3 -3
  82. package/docs/assets/getImage.html-3429c5a1.js +1 -1
  83. package/docs/assets/getImage.html-4d886d83.js +3 -3
  84. package/docs/assets/getTime.html-7435f922.js +1 -1
  85. package/docs/assets/getTime.html-b37f49eb.js +20 -20
  86. package/docs/assets/img.html-7d1da657.js +1 -1
  87. package/docs/assets/img.html-fbea1105.js +1 -1
  88. package/docs/assets/index.html-1695dd7c.js +1 -1
  89. package/docs/assets/index.html-65a4aa67.js +1 -1
  90. package/docs/assets/index.html-7b98d5bd.js +1 -1
  91. package/docs/assets/index.html-c01f2648.js +1 -1
  92. package/docs/assets/input-number.html-0b250d2a.js +1 -1
  93. package/docs/assets/input-number.html-a8eb0378.js +1 -1
  94. package/docs/assets/list-menu-item.html-7f1b4611.js +1 -1
  95. package/docs/assets/list-menu-item.html-84ed5ab8.js +1 -1
  96. package/docs/assets/list-menu.html-28b4163f.js +1 -1
  97. package/docs/assets/list-menu.html-cb6ba95b.js +1 -1
  98. package/docs/assets/loading.html-dae9e39d.js +6 -6
  99. package/docs/assets/loading.html-dc74c9e6.js +1 -1
  100. package/docs/assets/notify.html-e6c4c514.js +1 -1
  101. package/docs/assets/notify.html-f2c4d914.js +8 -8
  102. package/docs/assets/power-page.html-32e02f82.js +1 -1
  103. package/docs/assets/power-page.html-485e77da.js +1 -1
  104. package/docs/assets/power.html-d258cc19.js +93 -93
  105. package/docs/assets/power.html-e490bd32.js +1 -1
  106. package/docs/assets/previewImage.html-6a6b4245.js +1 -1
  107. package/docs/assets/previewImage.html-c5b7e945.js +2 -2
  108. package/docs/assets/price.html-1882c548.js +19 -19
  109. package/docs/assets/price.html-94d3f5be.js +1 -1
  110. package/docs/assets/price.html-d213df0f.js +1 -1
  111. package/docs/assets/price.html-deaf880f.js +1 -1
  112. package/docs/assets/render.html-8efcbdd4.js +1 -1
  113. package/docs/assets/render.html-df228e38.js +1 -1
  114. package/docs/assets/rule.html-2cd57fc2.js +13 -13
  115. package/docs/assets/rule.html-61662001.js +1 -1
  116. package/docs/assets/ruleValid.html-04fe2552.js +1 -1
  117. package/docs/assets/ruleValid.html-e0a776af.js +14 -14
  118. package/docs/assets/search-0782d0d1.svg +1 -1
  119. package/docs/assets/search-item.html-3f75394c.js +1 -1
  120. package/docs/assets/search-item.html-4e942ecd.js +1 -1
  121. package/docs/assets/search.html-2807043e.js +1 -1
  122. package/docs/assets/search.html-c24f8806.js +1 -1
  123. package/docs/assets/select.html-00d0607c.js +1 -1
  124. package/docs/assets/select.html-de7731f5.js +1 -1
  125. package/docs/assets/splitter.html-56f51a70.js +1 -1
  126. package/docs/assets/splitter.html-f5c836d7.js +1 -1
  127. package/docs/assets/style-161e43ab.css +1 -1
  128. package/docs/assets/symbols.html-a6aea4bf.js +1 -1
  129. package/docs/assets/symbols.html-b1f65bad.js +21 -21
  130. package/docs/assets/table-column-fixed.html-3a69e7b2.js +1 -1
  131. package/docs/assets/table-column-fixed.html-e763c38b.js +1 -1
  132. package/docs/assets/table-pagination.html-236934d3.js +1 -1
  133. package/docs/assets/table-pagination.html-c37ee2ac.js +1 -1
  134. package/docs/assets/table-splitter.html-07eab15c.js +1 -1
  135. package/docs/assets/table-splitter.html-7670ee65.js +1 -1
  136. package/docs/assets/table-summary.html-04db434f.js +1 -1
  137. package/docs/assets/table-summary.html-943c65a0.js +1 -1
  138. package/docs/assets/table.html-36253ad7.js +1 -1
  139. package/docs/assets/table.html-7f9c5d1b.js +38 -38
  140. package/docs/assets/table.html-93d53dc8.js +1 -1
  141. package/docs/assets/table.html-ac99b9cb.js +1 -1
  142. package/docs/assets/thumbnail.html-bab1976b.js +1 -1
  143. package/docs/assets/thumbnail.html-eb64e5e8.js +1 -1
  144. package/docs/assets/timestamp.html-4e54f79b.js +13 -13
  145. package/docs/assets/timestamp.html-d0e1b88a.js +1 -1
  146. package/docs/assets/toast.html-58ecbe21.js +1 -1
  147. package/docs/assets/toast.html-c9b9d36b.js +6 -6
  148. package/docs/assets/toolbar.html-83d9f97c.js +1 -1
  149. package/docs/assets/toolbar.html-ff7b8c92.js +1 -1
  150. package/docs/assets/tree.html-d07cbe79.js +23 -23
  151. package/docs/assets/tree.html-ea04193e.js +1 -1
  152. package/docs/assets/uploader-query.html-05590718.js +1 -1
  153. package/docs/assets/uploader-query.html-3175bac5.js +1 -1
  154. package/docs/assets/uploader.html-36da4394.js +2 -2
  155. package/docs/assets/uploader.html-6b5f3079.js +1 -1
  156. package/docs/assets/uploader.html-b9340b57.js +1 -1
  157. package/docs/assets/uploader.html-bc1c22e3.js +1 -1
  158. package/docs/assets/value-format.html-8ae3d47d.js +1 -1
  159. package/docs/assets/value-format.html-afa99b3d.js +1 -1
  160. package/docs/components/column-title.html +35 -35
  161. package/docs/components/data.html +62 -62
  162. package/docs/components/dialog.html +33 -33
  163. package/docs/components/dragger.html +33 -33
  164. package/docs/components/editor-code.html +33 -33
  165. package/docs/components/empty.html +33 -33
  166. package/docs/components/field-date.html +33 -33
  167. package/docs/components/field-table.html +33 -33
  168. package/docs/components/field-text.html +33 -33
  169. package/docs/components/field-tree.html +33 -33
  170. package/docs/components/img.html +33 -33
  171. package/docs/components/input-number.html +33 -33
  172. package/docs/components/list-menu-item.html +33 -33
  173. package/docs/components/list-menu.html +33 -33
  174. package/docs/components/power-page.html +33 -33
  175. package/docs/components/price.html +33 -33
  176. package/docs/components/render.html +33 -33
  177. package/docs/components/search-item.html +33 -33
  178. package/docs/components/search.html +33 -33
  179. package/docs/components/select.html +33 -33
  180. package/docs/components/splitter.html +33 -33
  181. package/docs/components/table-column-fixed.html +33 -33
  182. package/docs/components/table-pagination.html +33 -33
  183. package/docs/components/table-splitter.html +33 -33
  184. package/docs/components/table-summary.html +33 -33
  185. package/docs/components/table.html +33 -33
  186. package/docs/components/thumbnail.html +33 -33
  187. package/docs/components/toolbar.html +33 -33
  188. package/docs/components/uploader-query.html +33 -33
  189. package/docs/components/uploader.html +33 -33
  190. package/docs/components/value-format.html +33 -33
  191. package/docs/index.html +33 -33
  192. package/docs/utils/alert.html +37 -37
  193. package/docs/utils/area.html +74 -74
  194. package/docs/utils/arr.html +43 -43
  195. package/docs/utils/auth.html +40 -40
  196. package/docs/utils/bus.html +38 -38
  197. package/docs/utils/confirm.html +42 -42
  198. package/docs/utils/copy.html +45 -45
  199. package/docs/utils/dialog.html +100 -100
  200. package/docs/utils/dict.html +55 -55
  201. package/docs/utils/dictOptions.html +37 -37
  202. package/docs/utils/form.html +34 -34
  203. package/docs/utils/getData.html +66 -66
  204. package/docs/utils/getFile.html +35 -35
  205. package/docs/utils/getImage.html +35 -35
  206. package/docs/utils/getTime.html +52 -52
  207. package/docs/utils/index.html +33 -33
  208. package/docs/utils/loading.html +38 -38
  209. package/docs/utils/notify.html +40 -40
  210. package/docs/utils/power.html +125 -125
  211. package/docs/utils/previewImage.html +34 -34
  212. package/docs/utils/price.html +51 -51
  213. package/docs/utils/rule.html +45 -45
  214. package/docs/utils/ruleValid.html +46 -46
  215. package/docs/utils/symbols.html +53 -53
  216. package/docs/utils/table.html +70 -70
  217. package/docs/utils/timestamp.html +45 -45
  218. package/docs/utils/toast.html +38 -38
  219. package/docs/utils/tree.html +55 -55
  220. package/docs/utils/uploader.html +34 -34
  221. package/package.json +1 -1
  222. package/sass/common.scss +179 -179
  223. package/sass/index.scss +12 -13
  224. package/sass/quasar/field.scss +250 -250
  225. package/sass/quasar/table.scss +168 -168
  226. package/sass/variables.scss +140 -140
  227. package/utils/$form.js +72 -72
  228. package/utils/$power.js +1430 -1430
  229. package/utils/$render.js +75 -75
  230. package/utils/$search.js +416 -416
  231. package/utils/$table.js +1206 -1206
  232. package/utils/$tree.js +664 -664
  233. package/utils/config.js +57 -57
  234. package/utils/dialog.js +36 -36
  235. package/utils/dict.js +21 -21
  236. package/utils/getData.js +88 -73
  237. package/utils/getFile.js +41 -41
  238. package/utils/getImage.js +176 -176
  239. package/utils/getTime.js +113 -113
  240. package/utils/index.js +65 -65
  241. package/utils/previewImage.js +14 -14
  242. package/utils/timestamp.js +18 -18
  243. package/utils/uploader/qiniu.js +320 -320
  244. package/utils/uploader.js +1625 -1625
  245. package/utils/useFileUrl.js +25 -25
  246. package/utils/useSearch.js +499 -499
  247. package/sass/quasar/dialog.scss +0 -7
package/utils/uploader.js CHANGED
@@ -1,1625 +1,1625 @@
1
- import { ref, isRef, } from 'vue'
2
- import { useQuasar } from 'quasar'
3
- import SparkMD5 from 'spark-md5'
4
-
5
- import $n_has from 'lodash/has'
6
- import $n_get from 'lodash/get'
7
- import $n_toLower from 'lodash/toLower'
8
- import $n_findIndex from 'lodash/findIndex'
9
- import $n_uniq from 'lodash/uniq'
10
- import $n_find from 'lodash/find'
11
- import $n_isFunction from 'lodash/isFunction'
12
-
13
- import $n_isValidArray from '@netang/utils/isValidArray'
14
- import $n_isValidObject from '@netang/utils/isValidObject'
15
- import $n_isValidString from '@netang/utils/isValidString'
16
- import $n_forEach from '@netang/utils/forEach'
17
- import $n_indexOf from '@netang/utils/indexOf'
18
- import $n_json from '@netang/utils/json'
19
- import $n_join from '@netang/utils/join'
20
- import $n_split from '@netang/utils/split'
21
- import $n_trimString from '@netang/utils/trimString'
22
- import $n_run from '@netang/utils/run'
23
- import $n_isValidValue from '@netang/utils/isValidValue'
24
- import $n_http from '@netang/utils/http'
25
- import $n_getThrowMessage from '@netang/utils/getThrowMessage'
26
- import $n_runAsync from '@netang/utils/runAsync'
27
-
28
- import $n_$ruleValid from './$ruleValid'
29
- import $n_toast from './toast'
30
- import $n_confirm from './confirm'
31
- import $n_alert from './alert'
32
- import $n_previewImage from './previewImage'
33
- import $n_getImage from './getImage'
34
- import $n_getFile from './getFile'
35
- import $n_config from './config'
36
-
37
- import { configs } from './config'
38
-
39
- import copy from './copy'
40
-
41
- import {
42
- // 文件类型映射
43
- FilE_TYPE,
44
- // 文件名称映射
45
- FilE_NAME,
46
- // 上传状态
47
- UPLOAD_STATUS,
48
- // 上传器
49
- UPLOADERS,
50
- } from './useUploader'
51
-
52
- // 文件数量
53
- let _fileNum = 0
54
-
55
- /**
56
- * 创建上传器
57
- */
58
- function create(options) {
59
-
60
- // ==========【数据】=========================================================================================
61
-
62
- // quasar 对象
63
- const $q = useQuasar()
64
-
65
- const {
66
- // 上传文件输入框节点
67
- fileRef,
68
- // 更新值方法(初始化上传列表时不更新值)
69
- onUpdateModelValue,
70
- // 更新方法
71
- onUpdate,
72
-
73
- } = Object.assign({
74
- // 更新值方法
75
- onUpdateModelValue: null,
76
- // 更新方法
77
- onUpdate: null,
78
- }, options)
79
-
80
- const optionsProps = $n_get(options, 'props')
81
-
82
- // 声明属性
83
- const props = Object.assign({
84
- // 值
85
- modelValue: '',
86
- // 上传文件类型, 可选值 file image video audio
87
- type: 'image',
88
- // 上传文件数量(0:不限)
89
- count: 0,
90
- // 单个文件的最大大小(单位: MB)
91
- maxSize: 0,
92
- // 单个文件的限制后缀
93
- exts: [],
94
- // true: 值格式为数组, 如 ['xxxxxx', 'xxxxxx', 'xxxxxx']
95
- // false: 值格式为字符串, 如 xxxxxx,xxxxxx,xxxxxx
96
- valueArray: false,
97
- // 是否去重
98
- unique: false,
99
- // 是否初始加载文件信息(仅图片有效, 其他类型自动会加载文件信息)
100
- loadInfo: false,
101
- // 单文件上传提示
102
- confirm: false,
103
- }, optionsProps)
104
-
105
- // options 中是否存在 props.modelValue
106
- const hasPropsModelValue = $n_has(optionsProps, 'modelValue')
107
-
108
- // 上传文件列表
109
- const uploadFileLists = $n_has(options, 'uploadFileLists') && isRef(options.uploadFileLists) ? options.uploadFileLists : ref([])
110
-
111
- // 上传网络外链回调
112
- let uploadNetCallback
113
-
114
- /**
115
- * 上传配置
116
- */
117
- const configUpload = Object.assign(
118
- {
119
- type: 'local',
120
- },
121
- $n_config('uploader.upload')
122
- )
123
- const configLimit = Object.assign({
124
- maxSize: 100,
125
- exts: [],
126
- }, $n_config('uploader.limit.' + props.type))
127
-
128
- // 如果有单个文件的最大大小
129
- if (props.maxSize) {
130
- configLimit.maxSize = props.maxSize
131
- }
132
-
133
- // 如果有单个文件的限制后缀
134
- if ($n_isValidArray(props.exts)) {
135
- configLimit.exts = props.exts
136
- }
137
-
138
- // ==========【计算属性】=========================================================================================
139
-
140
- /**
141
- * 上传文件后缀名
142
- */
143
- // const accept = computed(function () {
144
- //
145
- // })
146
-
147
- // ==========【监听数据】==============================================================================================
148
-
149
- /**
150
- * 监听上传文件列表
151
- */
152
- // if (props.watchModelValue && hasPropsModelValue) {
153
- // watch(()=>options.props.modelValue, function() {
154
- // // 初始化上传列表
155
- // initUploadFileLists()
156
- // .finally()
157
- // })
158
- // }
159
-
160
- // ==========【方法】=================================================================================================
161
-
162
- /**
163
- * 获取值
164
- */
165
- function getValue() {
166
-
167
- const hashs = []
168
- const files = []
169
-
170
- for (const fileItem of uploadFileLists.value) {
171
- if (fileItem.status === UPLOAD_STATUS.success) {
172
- hashs.push(fileItem.hash)
173
- files.push(fileItem)
174
- }
175
- }
176
-
177
- const hashsString = $n_join(hashs, ',')
178
-
179
- return {
180
- value: props.valueArray ? hashs : hashsString,
181
- hashsString,
182
- hashs,
183
- files,
184
- query: uploadFileLists,
185
- }
186
- }
187
-
188
- /**
189
- * 更新值
190
- */
191
- function updateValue() {
192
-
193
- // 获取值
194
- const result = getValue()
195
-
196
- // 更新值
197
- $n_run(onUpdateModelValue)(result)
198
-
199
- // 更新
200
- $n_run(onUpdate)(result)
201
-
202
- // 上传网络外链回调
203
- $n_run(uploadNetCallback)()
204
- }
205
-
206
- /**
207
- * 更新
208
- */
209
- function update() {
210
- // 更新
211
- $n_run(onUpdate)(getValue())
212
- }
213
-
214
- /**
215
- * 初始化上传列表
216
- */
217
- async function initUploadFileLists() {
218
-
219
- const modelValue = hasPropsModelValue ? options.props.modelValue : props.modelValue
220
-
221
- // 值数组
222
- const hashs = []
223
-
224
- // hash all
225
- const hashAll = {}
226
-
227
- // 获取值数组
228
- const lists = props.valueArray ? modelValue : $n_split(modelValue, ',')
229
-
230
- if (
231
- // 如果只能上传一个
232
- props.count === 1
233
- // 如果为空
234
- && ! $n_isValidArray(lists)
235
- ) {
236
- // 更新上传文件列表
237
- uploadFileLists.value = []
238
- return
239
- }
240
-
241
- // 新上传文件列表
242
- const newUploadFileLists = []
243
-
244
- // 新列表
245
- const newLists = []
246
-
247
- // 是否更新
248
- let isUpdate = false
249
-
250
- // 合并当前上传列表中未上传成功的文件
251
- for (const fileItem of uploadFileLists.value) {
252
- if (fileItem.status !== UPLOAD_STATUS.success) {
253
- newUploadFileLists.push(fileItem)
254
- hashAll[fileItem.hash] = fileItem
255
- }
256
- }
257
-
258
- $n_forEach(lists, function(hash) {
259
- if ($n_isValidString(hash)) {
260
-
261
- const hasItem = $n_find(uploadFileLists.value, { hash })
262
-
263
- // 如果在当前上传文件列表中已存在
264
- if (hasItem) {
265
- hashAll[hash] = hasItem
266
-
267
- } else if (! $n_has(hashAll, 'hash')) {
268
-
269
- // 如果是外链
270
- if (/^http(s)?:\/\//i.test(hash)) {
271
- hashs.push(hash)
272
- hashAll[hash] = {
273
- hash,
274
- __img: hash,
275
- isNet: true,
276
- isNetUploaded: false,
277
- }
278
-
279
- // 否则为 hash 文件
280
- } else {
281
- hashs.push(hash)
282
- hashAll[hash] = {
283
- hash,
284
- isNet: false,
285
- }
286
- }
287
- }
288
-
289
- // 新列表
290
- newLists.push(hash)
291
- }
292
- })
293
-
294
- // 如果类型不是图片 || 初始加载文件信息, 则请求文件信息
295
- if (
296
- (props.type !== 'image' || props.loadInfo)
297
- && hashs.length
298
- ) {
299
- // 请求 - 获取文件
300
- const { status, data: resExisted } = await $n_http({
301
- url: $n_config('apiFileUrl') + 'get_file',
302
- data: {
303
- hashs: $n_uniq(hashs),
304
- },
305
- // 关闭错误
306
- warn: false,
307
- // 关闭防抖(可以重复请求)
308
- debounce: false,
309
- })
310
- if (status) {
311
-
312
- $n_forEach(resExisted, function (existedItem) {
313
-
314
- // 创建原始单个文件
315
- const fileItem = createRawFileItem()
316
-
317
- // 设置已存在文件
318
- setExistedFileItem(fileItem, existedItem)
319
-
320
- // 添加至 hash all
321
- hashAll[fileItem.hash] = Object.assign(fileItem, {
322
- key: fileItem.hash,
323
- })
324
- })
325
-
326
- // 需要更新
327
- isUpdate = true
328
- }
329
- }
330
-
331
- for (const hash of newLists) {
332
-
333
- let hasItem = hashAll[hash]
334
-
335
- // 如果该 hash 已存在
336
- if ($n_has(hasItem, 'id')) {
337
-
338
- // 如果新列表中存在 该 id
339
- if ($n_findIndex(newUploadFileLists, { id: hasItem.id }) > -1) {
340
-
341
- // 更新 id
342
- hasItem = Object.assign({}, hasItem, {
343
- id: ++_fileNum
344
- })
345
- }
346
-
347
- // 否则该 hash 不存在
348
- } else {
349
- hasItem = Object.assign(createRawFileItem(), hasItem, {
350
- // 文件唯一 key
351
- key: hash,
352
- // hash
353
- hash,
354
- // 状态
355
- status: UPLOAD_STATUS.success,
356
- // 进度
357
- progress: 100,
358
- // 信息
359
- msg: '',
360
- })
361
- hashAll[hash] = hasItem
362
- }
363
-
364
- // 添加至新列表中
365
- newUploadFileLists.push(hasItem)
366
- }
367
-
368
- // 更新上传文件列表
369
- uploadFileLists.value = newUploadFileLists
370
-
371
- if (isUpdate) {
372
- // 更新
373
- update()
374
- }
375
- }
376
-
377
- /**
378
- * 初始化上传网络外链列表
379
- */
380
- function initUploadNetLists(callback) {
381
-
382
- // 如果提交时禁止上传网络外链文件
383
- if ($n_get(optionsProps, 'submitUploadNet') !== true) {
384
- return
385
- }
386
-
387
- uploadNetCallback = callback
388
-
389
- for (const fileItem of uploadFileLists.value) {
390
- if (
391
- fileItem.isNet
392
- && fileItem.status === UPLOAD_STATUS.success
393
- ) {
394
- // 将文件状态修改为: 等待上传中
395
- fileItem.status = UPLOAD_STATUS.waiting
396
- }
397
- }
398
- }
399
-
400
- /**
401
- * 上传网络外链文件
402
- */
403
- async function uploadNet(submitUploadNet = false) {
404
-
405
- // 如果提交时禁止上传网络外链文件
406
- if (submitUploadNet === true || $n_get(optionsProps, 'submitUploadNet') === true) {
407
-
408
- const promises = []
409
-
410
- for (const fileItem of uploadFileLists.value) {
411
- if (
412
- fileItem.isNet
413
- && fileItem.status === UPLOAD_STATUS.waiting
414
- ) {
415
- // 设置网络图片 file
416
- promises.push(setNetFile(fileItem))
417
- }
418
- }
419
-
420
- if (! promises.length) {
421
- return
422
- }
423
- await Promise.all(promises)
424
-
425
- // 检查待上传文件在服务器上是否存在
426
- // --------------------------------------------------
427
- if (! await checkWaitUploadFileExists()) {
428
- return
429
- }
430
-
431
- // 上传
432
- await upload()
433
- }
434
- }
435
-
436
- /**
437
- * 获取上传网络外链进度
438
- */
439
- // function getUploadNetProgress() {
440
- //
441
- // let total = 0
442
- // let loaded = 0
443
- //
444
- // // 如果提交时允许上传网络外链文件
445
- // if ($n_get(optionsProps, 'submitUploadNet') === true) {
446
- //
447
- // for (const fileItem of uploadFileLists.value) {
448
- // if (fileItem.isNet && fileItem.status <= UPLOAD_STATUS.success) {
449
- // total++
450
- // if (fileItem.isNetUploaded) {
451
- // loaded++
452
- // }
453
- // }
454
- // }
455
- //
456
- // // if (total && loaded < total) {
457
- // // return {
458
- // // loaded,
459
- // // total,
460
- // // // progress: Math.round(loaded * 100 / total),
461
- // // }
462
- // // }
463
- // }
464
- //
465
- // return {
466
- // loaded,
467
- // total,
468
- // // progress: 100,
469
- // }
470
- // }
471
-
472
- /**
473
- * 设置网络图片 file
474
- */
475
- async function setNetFile(fileItem) {
476
-
477
- // 设置文件状态
478
- fileItem.status = UPLOAD_STATUS.hashChecking
479
- // 设置文件检查进度
480
- fileItem.progress = 0
481
-
482
- try {
483
- const r = await fetch(fileItem.__img, {
484
- method: 'GET',
485
- })
486
- const arrayBuffer = await r.arrayBuffer()
487
- const blob = new Blob([arrayBuffer], { type: r.headers.get('Content-Type') })
488
-
489
- // -------- axios
490
- // const r = await axios({
491
- // method: 'GET',
492
- // url: fileItem.__img,
493
- // responseType: 'arraybuffer'
494
- // })
495
- // console.log(r)
496
- // const arrayBuffer = r.data
497
- // const blob = new Blob([arrayBuffer], { type: r.headers['content-type'] })
498
- // -------- axios
499
-
500
- // 如果有类型
501
- if (blob.type) {
502
-
503
- // 后缀名
504
- let ext = ''
505
-
506
- // 如果为图片
507
- if (
508
- props.type === 'image'
509
- || $n_indexOf(blob.type, 'image/') > -1
510
- ) {
511
- switch (blob.type) {
512
- case 'image/png':
513
- ext = 'png'
514
- break
515
- case 'image/gif':
516
- ext = 'gif'
517
- break
518
- default:
519
- ext = 'jpg'
520
- break
521
- }
522
-
523
- // 如果为视频
524
- } else if (props.type === 'video') {
525
- ext = 'mp4'
526
-
527
- // 如果为音频
528
- } else if (props.type === 'audio') {
529
- ext = 'mp3'
530
-
531
- // 否则为文件
532
- } else {
533
- const arr = $n_split(props.type, '/')
534
- if (arr.length > 0) {
535
- ext = arr[1]
536
- }
537
- }
538
-
539
- // 如果有后缀名
540
- if (ext) {
541
- // 设置文件
542
- fileItem.file = new File([blob], '', { type: blob.type })
543
- // 设置后缀名
544
- fileItem.ext = ext
545
-
546
- const {
547
- size,
548
- } = fileItem.file
549
-
550
- // 文件大小
551
- fileItem.size = size
552
-
553
- // 检查文件错误
554
- const errMsg = checkFileError(fileItem)
555
- if (errMsg) {
556
- // 设置文件上传失败
557
- setFileFail(fileItem, errMsg)
558
- return
559
- }
560
-
561
- // 初始化 SparkMD5
562
- const spark = new SparkMD5.ArrayBuffer()
563
- spark.append(arrayBuffer)
564
-
565
- // 获取文件 hash
566
- const hash = spark.end(false)
567
- if (hash) {
568
- // 设置文件 hash
569
- fileItem.hash = hash
570
- // 标题
571
- fileItem.title = fileItem.hash
572
- // 设置文件状态
573
- fileItem.status = UPLOAD_STATUS.hashChecked
574
- // 设置文件检查进度
575
- fileItem.progress = 100
576
- return
577
- }
578
- }
579
- }
580
-
581
- } catch (e) {}
582
-
583
- // 设置文件上传失败
584
- setFileFail(fileItem)
585
- }
586
-
587
- /**
588
- * 检查是否正在上传文件
589
- */
590
- function checkUploading() {
591
- for (const fileItem of uploadFileLists.value) {
592
- if (fileItem.status < UPLOAD_STATUS.success) {
593
- return true
594
- }
595
- }
596
- return false
597
- }
598
-
599
- /**
600
- * 选择文件上传
601
- */
602
- function chooseUpload() {
603
- // 点击文件输入框
604
- fileRef.value.click()
605
- }
606
-
607
- /**
608
- * 选择网络外链上传
609
- */
610
- function chooseUploadNet() {
611
- $q.dialog({
612
- title: `添加网络${FilE_NAME[FilE_TYPE[props.type]]}`,
613
- style: 'min-width:600px;',
614
- // message: `添加网络${FilE_NAME[FilE_TYPE[props.type]]}`,
615
- prompt: {
616
- model: '',
617
- isValid: $n_$ruleValid('required'),
618
- type: 'textarea',
619
- placeholder: '多个链接,使用中英文逗号、空格、换行隔开',
620
- outlined: true,
621
- },
622
- cancel: true,
623
- persistent: true,
624
- })
625
- .onOk(async function(value) {
626
-
627
- let files = $n_isValidString(value)
628
- ? $n_uniq($n_split(value.replace(/\n|,|,/g, ','), ','))
629
- .map(e => $n_trimString(e))
630
- .filter(
631
- val => val && /^(http(s)?:\/\/)/i.test(val)
632
- )
633
- : []
634
-
635
- if (! files.length) {
636
- // 轻提示
637
- $n_toast({
638
- message: '请输入正确的链接'
639
- })
640
- return
641
- }
642
-
643
- // 格式化上传网络链接
644
- // --------------------------------------------------
645
- const {
646
- formatUploadNet,
647
- } = configs
648
- if ($n_isFunction(formatUploadNet)) {
649
- files = formatUploadNet(files, props.type === 'image')
650
- }
651
- // --------------------------------------------------
652
-
653
- if (! $n_isValidArray(files)) {
654
- // 轻提示
655
- $n_toast({
656
- message: '请输入正确的链接'
657
- })
658
- return
659
- }
660
-
661
- // 添加上传网络外链至文件列表
662
- function addFileNetItem(hash) {
663
- uploadFileLists.value.push(Object.assign(createRawFileItem(), {
664
- // id
665
- id: ++_fileNum,
666
- // 文件唯一 key
667
- key: hash,
668
- // hash
669
- hash,
670
- // 将文件状态修改为: 等待上传中
671
- status: UPLOAD_STATUS.waiting,
672
- // 进度
673
- progress: 100,
674
- // 信息
675
- msg: '',
676
-
677
- __img: hash,
678
- isNet: true,
679
- isNetUploaded: false,
680
- }))
681
- }
682
-
683
- // 遍历选择的文件列表
684
- for (const file of files) {
685
-
686
- // 如果只能上传一个
687
- if (props.count === 1) {
688
-
689
- // 如果有上传文件列表
690
- if (uploadFileLists.value.length) {
691
-
692
- // 如果开启单文件上传提示
693
- if (props.confirm) {
694
-
695
- // 确认框
696
- $n_confirm({
697
- message: '最多只能上传1个文件,确认上传并替换吗?',
698
- })
699
- // 点击确认执行
700
- .onOk(function () {
701
-
702
- // 删除所有文件
703
- deleteAll()
704
-
705
- // 添加上传网络外链至文件列表
706
- addFileNetItem(file)
707
-
708
- // 上传网络外链文件
709
- uploadNet(true)
710
- .finally()
711
- })
712
- return
713
- }
714
-
715
- // 删除所有文件
716
- deleteAll()
717
- }
718
-
719
- } else if (
720
- // 如果有上传数量限制
721
- props.count > 1
722
- // 上传文件列表数量 === 上传数量限制
723
- && uploadFileLists.value.length >= props.count
724
- ) {
725
- // 轻提示
726
- $n_toast({
727
- message: `最多只能上传${props.count}个文件,请先删除后再上传`,
728
- })
729
- return
730
- }
731
-
732
- // 添加上传网络外链至文件列表
733
- addFileNetItem(file)
734
- }
735
-
736
- // 上传网络外链文件
737
- uploadNet(true)
738
- .finally()
739
- })
740
- }
741
-
742
- /**
743
- * 文件输入框更新
744
- */
745
- function fileChange(e) {
746
-
747
- try {
748
- // 获取上传文件
749
- const files = Array.from(e.target.files)
750
-
751
- // 清空上传文件输入框内容
752
- fileRef.value.value = ''
753
-
754
- // 如果没有选择文件
755
- if (! files.length) {
756
- // 则无任何操作
757
- return
758
- }
759
-
760
- // 遍历选择的文件列表
761
- for (const file of files) {
762
-
763
- // 创建单个文件
764
- const fileItem = createFileItem(file)
765
- if (fileItem !== false) {
766
-
767
- // 如果只能上传一个
768
- if (props.count === 1) {
769
-
770
- // 如果有上传文件列表
771
- if (uploadFileLists.value.length) {
772
-
773
- // 如果开启单文件上传提示
774
- if (props.confirm) {
775
-
776
- // 确认框
777
- $n_confirm({
778
- message: '最多只能上传1个文件,确认上传并替换吗?',
779
- })
780
- // 点击确认执行
781
- .onOk(function () {
782
-
783
- // 删除所有文件
784
- deleteAll()
785
-
786
- // 添加至上传文件列表
787
- uploadFileLists.value.push(fileItem)
788
-
789
- // 开始上传
790
- startUpload()
791
- .finally()
792
- })
793
- return
794
- }
795
-
796
- // 删除所有文件
797
- deleteAll()
798
- }
799
-
800
- } else if (
801
- // 如果有上传数量限制
802
- props.count > 1
803
- // 上传文件列表数量 === 上传数量限制
804
- && uploadFileLists.value.length >= props.count
805
- ) {
806
- // 轻提示
807
- $n_toast({
808
- message: `最多只能上传${props.count}个文件,请先删除后再上传`,
809
- })
810
- return
811
- }
812
-
813
- // 添加至上传文件列表
814
- uploadFileLists.value.push(fileItem)
815
- }
816
- }
817
-
818
- // 开始上传
819
- startUpload()
820
- .finally()
821
-
822
- } catch (e) {
823
-
824
- // 错误提示
825
- $n_alert({
826
- message: $n_getThrowMessage(e),
827
- })
828
- }
829
-
830
- }
831
-
832
- /**
833
- * 开始上传
834
- */
835
- async function startUpload() {
836
-
837
- // 【设置待上传文件的 hash】
838
- // --------------------------------------------------
839
- const promises = []
840
- for (const fileItem of uploadFileLists.value) {
841
- // 如果是等待上传的文件
842
- if (fileItem.status === UPLOAD_STATUS.waiting) {
843
-
844
- // 检查文件错误
845
- const errMsg = checkFileError(fileItem)
846
- if (errMsg) {
847
- // 设置文件上传失败
848
- setFileFail(fileItem, errMsg)
849
-
850
- } else {
851
- // 设置文件 hash
852
- promises.push(setFileHash(fileItem))
853
- }
854
- }
855
- }
856
-
857
- if (promises.length) {
858
- await Promise.all(promises)
859
- }
860
-
861
- // 检查待上传文件在服务器上是否存在
862
- // --------------------------------------------------
863
- if (! await checkWaitUploadFileExists()) {
864
- return
865
- }
866
-
867
- // 上传
868
- await upload()
869
- }
870
-
871
- /**
872
- * 上传
873
- */
874
- let _upload = null
875
- async function upload() {
876
- 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
- // 待上传文件列表
892
- const waitUploadFileLists = []
893
- for (const fileItem of uploadFileLists.value) {
894
- // 检查存在服务器完成
895
- if (fileItem.status === UPLOAD_STATUS.existChecked) {
896
- waitUploadFileLists.push(fileItem)
897
- }
898
- }
899
- if (! waitUploadFileLists.length) {
900
- return
901
- }
902
-
903
- // 上传
904
- await _upload({
905
- config: configUpload,
906
- waitUploadFileLists,
907
- uploadFileLists,
908
- checkFileError,
909
- setFileSuccess,
910
- setFileFail,
911
- })
912
-
913
- } catch (e) {
914
- // 错误提示
915
- $n_alert({
916
- message: $n_getThrowMessage(e),
917
- })
918
- }
919
- }
920
-
921
- /**
922
- * 设置文件上传成功
923
- */
924
- const setFileSuccess = (fileItem) => {
925
-
926
- // 设置文件状态
927
- fileItem.status = UPLOAD_STATUS.success
928
- // 设置文件信息
929
- fileItem.msg = ''
930
- // 设置文件检查进度
931
- fileItem.progress = 0
932
- // 是否网络文件已上传
933
- if (fileItem.isNet) {
934
- fileItem.isNetUploaded = true
935
- }
936
-
937
- // // 单个文件上传结束回调
938
- // uploadQueryCallback(fileItem)
939
- //
940
- // // 上传更新回调
941
- // uploadChange(getResultData())
942
-
943
- // 更新值
944
- updateValue()
945
- }
946
-
947
- /**
948
- * 设置文件上传失败
949
- */
950
- function setFileFail(fileItem, errMsg) {
951
-
952
- // 设置文件状态
953
- fileItem.status = UPLOAD_STATUS.fail
954
- // 设置文件信息
955
- fileItem.msg = errMsg || `无效${FilE_NAME[fileItem.type]}`
956
- // 设置文件检查进度
957
- fileItem.progress = 0
958
-
959
- // // 单个文件上传结束回调
960
- // uploadQueryCallback(fileItem)
961
-
962
- // 上传网络外链回调
963
- $n_run(uploadNetCallback)()
964
- }
965
-
966
- /**
967
- * 设置文件 hash
968
- */
969
- function setFileHash(fileItem) {
970
- return new Promise(function (resolve) {
971
-
972
- // 设置文件状态
973
- fileItem.status = UPLOAD_STATUS.hashChecking
974
- // 设置文件检查进度
975
- fileItem.progress = 0
976
-
977
- const {
978
- file,
979
- } = fileItem
980
-
981
- // blob 切片
982
- const blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice
983
- // 以 2MB 的分片读取
984
- const chunkSize = 2097152
985
- // 总分片数量
986
- const chunks = Math.ceil(file.size / chunkSize)
987
- // 初始化 SparkMD5
988
- const spark = new SparkMD5.ArrayBuffer()
989
- // 当前分片数
990
- let currentChunk = 0
991
- // 创建文件读取器
992
- const fileReader = new FileReader()
993
-
994
- // 文件加载
995
- fileReader.onload = function(e) {
996
-
997
- // 追加 array buffer
998
- spark.append(e.target.result)
999
-
1000
- // 当前分片数++
1001
- currentChunk++
1002
-
1003
- // 如果分块数量 < 总分片数量
1004
- if (currentChunk < chunks) {
1005
-
1006
- // 设置文件检查进度
1007
- fileItem.progress = Math.floor((currentChunk / chunks) * 100)
1008
-
1009
- // 读取下一个分片
1010
- loadNext()
1011
-
1012
- } else {
1013
-
1014
- // 获取文件 hash
1015
- const hash = spark.end(false)
1016
- if (! hash) {
1017
- // 设置文件上传失败
1018
- setFileFail(fileItem)
1019
- // 完成回调
1020
- resolve()
1021
- return
1022
- }
1023
-
1024
- // 下一步
1025
- function next(hash) {
1026
- if (
1027
- // 如果开启去重
1028
- props.unique
1029
- // 如果该文件 hash 在上传文件列表中
1030
- && $n_findIndex(uploadFileLists.value, { hash }) > -1
1031
- ) {
1032
- // 轻提示
1033
- $n_toast({
1034
- message: '该文件已存在,不可重复上传',
1035
- })
1036
-
1037
- // 设置文件上传失败
1038
- setFileFail(fileItem, '已存在')
1039
-
1040
- // 删除单个文件
1041
- deleteFileItem(fileItem)
1042
-
1043
- } else {
1044
- // 设置文件 hash
1045
- fileItem.hash = hash
1046
- // 设置文件状态
1047
- fileItem.status = UPLOAD_STATUS.hashChecked
1048
- // 设置文件检查进度
1049
- fileItem.progress = 100
1050
- }
1051
-
1052
- // 完成回调
1053
- resolve()
1054
- }
1055
-
1056
- // 格式化上传文件 hash
1057
- // --------------------------------------------------
1058
- const {
1059
- formatUploadFileHash,
1060
- } = configs
1061
- if ($n_isFunction(formatUploadFileHash)) {
1062
- $n_runAsync(formatUploadFileHash)(hash, fileItem)
1063
- .then(function (newHash) {
1064
- // 下一步
1065
- next($n_isValidString(newHash) ? newHash : hash)
1066
- })
1067
- return
1068
- }
1069
- // --------------------------------------------------
1070
-
1071
- // 下一步
1072
- next(hash)
1073
- }
1074
- }
1075
-
1076
- // 文件加载错误
1077
- fileReader.onerror = function() {
1078
- // 设置文件上传失败
1079
- setFileFail(fileItem)
1080
- // 完成回调
1081
- resolve()
1082
- }
1083
-
1084
- /**
1085
- * 读取下一个分片
1086
- */
1087
- function loadNext() {
1088
- const start = currentChunk * chunkSize
1089
- const end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize
1090
- fileReader.readAsArrayBuffer(blobSlice.call(file, start, end))
1091
- }
1092
-
1093
- // 读取下一个分片
1094
- loadNext()
1095
- })
1096
- }
1097
-
1098
- /**
1099
- * 检查文件错误
1100
- */
1101
- function checkFileError({ type, size, ext }) {
1102
-
1103
- // 需要上传的文件类型
1104
- const fileType = FilE_TYPE[props.type]
1105
-
1106
- // 如果 类型为文件, 则不受限制 || 当前上传的类型允许
1107
- if (props.type === 'file' || type === fileType) {
1108
-
1109
- const {
1110
- maxSize,
1111
- exts,
1112
- } = configLimit
1113
-
1114
- // 检查文件后缀名
1115
- if (
1116
- // 如果没有后缀名, 则无效
1117
- ! ext
1118
- // 如果后缀名不在允许范围内, 则无效
1119
- || (
1120
- $n_isValidArray(exts)
1121
- && exts.indexOf(ext) === -1
1122
- )
1123
- ) {
1124
- return '后缀名不允许'
1125
- }
1126
-
1127
- // 检查文件大小
1128
- if (size > 0) {
1129
- if (
1130
- maxSize > 0
1131
- && size > (maxSize * 1048576)
1132
- ) {
1133
- return `${FilE_NAME[fileType]}大于${maxSize}M`
1134
- }
1135
- return
1136
- }
1137
- }
1138
-
1139
- return `无效${FilE_NAME[fileType]}`
1140
- }
1141
-
1142
- /**
1143
- * 检查待上传文件在服务器上是否存在
1144
- */
1145
- async function checkWaitUploadFileExists() {
1146
-
1147
- // 需检查的 hashs
1148
- const checkHashs = []
1149
- // 需检查的文件列表
1150
- const checkFileLists = []
1151
-
1152
- for (const fileItem of uploadFileLists.value) {
1153
- if (fileItem.status === UPLOAD_STATUS.hashChecked) {
1154
- // 设置文件状态
1155
- fileItem.status = UPLOAD_STATUS.existChecking
1156
- // 添加检查 hash 数组
1157
- checkHashs.push(fileItem.hash)
1158
- // 添加检查文件列表
1159
- checkFileLists.push(fileItem)
1160
- }
1161
- }
1162
-
1163
- // 如果没有需要检查的 hash
1164
- if (! checkHashs.length) {
1165
- // 返回失败
1166
- return false
1167
- }
1168
-
1169
- // 请求 - 检查文件是否存在 hash
1170
- const { status, data: resExisted } = await $n_http({
1171
- url: $n_config('apiFileUrl') + 'check_exist',
1172
- data: {
1173
- hashs: $n_uniq(checkHashs),
1174
- },
1175
- // 关闭错误
1176
- warn: false,
1177
- // 关闭防抖(可以重复请求)
1178
- debounce: false,
1179
- })
1180
-
1181
- // 如果请求失败
1182
- if (! status) {
1183
- // 设置文件上传失败
1184
- for (const fileItem of checkFileLists) {
1185
- setFileFail(fileItem, resExisted.msg)
1186
- }
1187
- // 返回失败
1188
- return false
1189
- }
1190
-
1191
- // 设置文件状态
1192
- for (const fileItem of checkFileLists) {
1193
- fileItem.status = UPLOAD_STATUS.existChecked
1194
- }
1195
-
1196
- // 如果有存在的文件列表
1197
- if ($n_isValidArray(resExisted)) {
1198
-
1199
- // 已存在文件数量
1200
- let existedNum = 0
1201
- // 不存在文件数量
1202
- let noExistNum = 0
1203
-
1204
- for (const fileItem of checkFileLists) {
1205
-
1206
- // 如果文件已存在(已经上传过了, 就是检查是否秒传文件)
1207
- const existedItem = $n_find(resExisted, { hash: fileItem.hash })
1208
- if (existedItem) {
1209
-
1210
- // 如果 类型为文件, 则不受限制 || 文件类型正确
1211
- if (props.type === 'file' || fileItem.type === $n_get(existedItem, 'type')) {
1212
-
1213
- // 设置已存在文件
1214
- setExistedFileItem(fileItem, existedItem)
1215
-
1216
- // 设置文件为非网络外链
1217
- fileItem.isNet = false
1218
-
1219
- // 单个文件上传结束回调
1220
- // uploadQueryCallback(fileItem)
1221
-
1222
- // 已存在文件数量++
1223
- existedNum++
1224
-
1225
- // 否则有不存在文件
1226
- } else {
1227
- // 不存在文件数量++
1228
- noExistNum++
1229
- }
1230
-
1231
- // 否则有不存在文件
1232
- } else {
1233
- // 不存在文件数量++
1234
- noExistNum++
1235
- }
1236
- }
1237
-
1238
- // 如果有已存在的文件
1239
- if (existedNum) {
1240
-
1241
- // 更新值
1242
- updateValue()
1243
-
1244
- // 上传更新回调
1245
- // uploadChange(getResultData())
1246
- }
1247
-
1248
- return noExistNum > 0
1249
- }
1250
-
1251
- return true
1252
- }
1253
-
1254
- /**
1255
- * 创建原始单个文件
1256
- */
1257
- function createRawFileItem() {
1258
- return {
1259
- // id
1260
- id: ++_fileNum,
1261
- // 文件唯一 key
1262
- // key,
1263
- // 文件名
1264
- title: '',
1265
- // 文件原始数据
1266
- // file,
1267
- // 类型(1:文件,2:图片,3:视频,4:音频)
1268
- type: FilE_TYPE[props.type],
1269
- // 后缀
1270
- ext: '',
1271
- // 大小
1272
- size: 0,
1273
- // hash
1274
- hash: '',
1275
- // 信息
1276
- json: {},
1277
- // 状态: waiting checking checked uploading success fail
1278
- status: UPLOAD_STATUS.waiting,
1279
- // 进度
1280
- progress: 0,
1281
- // 信息
1282
- msg: '待上传',
1283
- // 是否为网络文件
1284
- isNet: false,
1285
- // 是否网络文件已上传
1286
- isNetUploaded: false,
1287
- // 中断上传
1288
- abort() {},
1289
- }
1290
- }
1291
-
1292
- /**
1293
- * 创建单个文件
1294
- */
1295
- function createFileItem(file) {
1296
-
1297
- // 单个文件示例
1298
- // name: "123.jpg"
1299
- // size: 101206
1300
- // type: "image/jpeg"
1301
- // lastModified: 1629099994411
1302
- // lastModifiedDate: Mon Aug 16 2021 15:46:34 GMT+0800 (中国标准时间) {}
1303
- // webkitRelativePath: ""
1304
-
1305
- const {
1306
- name,
1307
- size,
1308
- lastModified,
1309
- webkitRelativePath,
1310
- } = file
1311
-
1312
- // 文件唯一 key
1313
- const key = webkitRelativePath + lastModified + name + size
1314
-
1315
- if (
1316
- // 如果开启去重
1317
- props.unique
1318
- // 如果该文件 key 在上传文件列表中
1319
- && $n_findIndex(uploadFileLists.value, { key }) > -1
1320
- ) {
1321
- // 轻提示
1322
- $n_toast({
1323
- message: '该文件已存在,不可重复上传',
1324
- })
1325
-
1326
- // 则返回 false
1327
- return false
1328
- }
1329
-
1330
- // 文件名
1331
- let title = name
1332
- // 文件后缀名
1333
- let ext = ''
1334
-
1335
- const index = name.lastIndexOf('.')
1336
- if (index > -1) {
1337
- title = name.substring(0, index)
1338
- ext = $n_toLower(name.substring(index + 1))
1339
- }
1340
-
1341
- // 创建单个文件
1342
- const fileItem = Object.assign(createRawFileItem(), {
1343
- // 文件唯一 key
1344
- key,
1345
- // 文件原始数据
1346
- file,
1347
- // 文件名
1348
- title,
1349
- // 后缀
1350
- ext,
1351
- // 大小
1352
- size,
1353
- })
1354
-
1355
- // 如果上传类型为图片
1356
- if (
1357
- props.type === 'image'
1358
- && file.type.toLowerCase().startsWith('image')
1359
- ) {
1360
- // 获取图片预览地址
1361
- const img = new Image()
1362
- img.src = window.URL.createObjectURL(file)
1363
- fileItem.__img = img.src
1364
- }
1365
-
1366
- return fileItem
1367
- }
1368
-
1369
- /**
1370
- * 设置已存在文件
1371
- */
1372
- function setExistedFileItem(fileItem, existedItem) {
1373
-
1374
- // 返回数据示例
1375
- // ------------------------------
1376
- // title: 1
1377
- // type: 2
1378
- // hash: "799e6bb77f638e192b9079e6a55bc2de"
1379
- // ext: "jpg"
1380
- // size: 306037
1381
- // json: "{\"w\":1920,\"h\":1200,\"o\":1}"
1382
- // status: 1
1383
-
1384
- const {
1385
- // 标题
1386
- title,
1387
- // 类型(1:文件,2:图片,3:视频,4:音频)
1388
- type,
1389
- // hash
1390
- hash,
1391
- // 后缀
1392
- ext,
1393
- // 大小
1394
- size,
1395
- // 信息
1396
- json,
1397
- } = existedItem
1398
-
1399
- const fileJson = $n_json.parse(json)
1400
-
1401
- // 设置文件
1402
- Object.assign(fileItem, {
1403
- // 标题
1404
- title,
1405
- // 类型(1:文件,2:图片,3:视频,4:音频)
1406
- type,
1407
- // hash
1408
- hash,
1409
- // 后缀
1410
- ext,
1411
- // 大小
1412
- size,
1413
- // 信息
1414
- json: $n_isValidObject(fileJson) ? fileJson : {},
1415
- // 状态
1416
- status: UPLOAD_STATUS.success,
1417
- // 进度
1418
- progress: 100,
1419
- // 信息
1420
- msg: '',
1421
- })
1422
- }
1423
-
1424
- /**
1425
- * 删除所有文件
1426
- */
1427
- function deleteAll() {
1428
-
1429
- // 如果有上传文件列表
1430
- if (uploadFileLists.value.length) {
1431
-
1432
- for (const fileItem of uploadFileLists.value) {
1433
- // 退出上传
1434
- fileItem.abort()
1435
- }
1436
-
1437
- // 清空上传文件列表
1438
- uploadFileLists.value = []
1439
- }
1440
- }
1441
-
1442
- /**
1443
- * 删除单个文件
1444
- */
1445
- function deleteFileItem(fileItem) {
1446
- const index = $n_findIndex(uploadFileLists.value, { id: fileItem.id })
1447
- if (index > -1) {
1448
-
1449
- const {
1450
- status,
1451
- } = fileItem
1452
-
1453
- // 退出上传
1454
- fileItem.abort()
1455
-
1456
- // 从上传文件列表中删除
1457
- uploadFileLists.value.splice(index, 1)
1458
-
1459
- // 如果该文件已上传成功
1460
- if (status === UPLOAD_STATUS.success) {
1461
- // 更新值
1462
- updateValue()
1463
- }
1464
- }
1465
- }
1466
-
1467
- /**
1468
- * 预览图片
1469
- */
1470
- function previewImage(fileItem) {
1471
- // 预览图片
1472
- if (fileItem.type === FilE_TYPE.image) {
1473
- $n_previewImage({
1474
- images: [
1475
- $n_has(fileItem, '__img') ? fileItem.__img : fileItem.hash,
1476
- ],
1477
- })
1478
- }
1479
- }
1480
-
1481
- /**
1482
- * 修改文件名
1483
- */
1484
- async function editFileTitle(newTitle, fileItem) {
1485
-
1486
- if ($n_isValidValue(newTitle)) {
1487
-
1488
- newTitle = $n_trimString(newTitle)
1489
-
1490
- const {
1491
- hash,
1492
- title,
1493
- } = fileItem
1494
-
1495
- if (title === newTitle) {
1496
- return
1497
- }
1498
-
1499
- // 先设置新文件名
1500
- fileItem.title = newTitle
1501
-
1502
- // 请求 - 修改文件名
1503
- const { status } = await $n_http({
1504
- url: $n_config('apiFileUrl') + 'edit_file_title',
1505
- data: {
1506
- hash,
1507
- title: newTitle,
1508
- },
1509
- })
1510
-
1511
- // 如果修改失败
1512
- if (! status) {
1513
- // 还原文件名
1514
- fileItem.title = title
1515
- return
1516
- }
1517
-
1518
- // 轻提示
1519
- $n_toast({
1520
- type: 'positive',
1521
- message: '修改成功',
1522
- })
1523
- }
1524
- }
1525
-
1526
- /**
1527
- * 播放
1528
- */
1529
- function play({ hash, __img }) {
1530
-
1531
- const src = __img ? __img : $n_getFile(hash)
1532
-
1533
- let width
1534
- let height
1535
- let fullWidth = false
1536
- let fullHeight = false
1537
- let ok = true
1538
- let style = ''
1539
-
1540
- if ($q.platform.is.mobile) {
1541
- width = $q.screen.width - 48 - 32
1542
- height = $q.screen.height - 48 - 32 - 6 - 52
1543
- fullWidth = true
1544
- fullHeight = true
1545
- } else {
1546
- width = 800 - 32
1547
- height = 400 - 32 - 6
1548
- ok = false
1549
- style = 'width:800px;max-width:800px;height:400px;max-height:400px;'
1550
- }
1551
-
1552
- $q.dialog({
1553
- message: `<video style="width:${width}px;height:${height}px;" playsinline autoplay controls src="${src}" type="video/mp4" muted="muted"></video>`,
1554
- style,
1555
- html: true,
1556
- dark: true,
1557
- ok,
1558
- fullWidth,
1559
- fullHeight,
1560
- })
1561
- }
1562
-
1563
- /**
1564
- * 复制地址
1565
- */
1566
- function copyUrl({ type, hash }) {
1567
-
1568
- const _url = type === FilE_TYPE.image ?
1569
- // 如果是图片
1570
- $n_getImage(hash)
1571
- // 否则是文件
1572
- : $n_getFile(hash)
1573
-
1574
- if ($n_isValidString(_url)) {
1575
- copy(_url, '复制地址成功')
1576
- }
1577
- }
1578
-
1579
- return {
1580
- // 上传文件列表
1581
- query: uploadFileLists,
1582
- // 更新值
1583
- updateValue,
1584
- // 初始化上传列表
1585
- initUploadFileLists,
1586
-
1587
- // 初始化上传网络外链列表
1588
- initUploadNetLists,
1589
- // 上传网络外链文件
1590
- uploadNet,
1591
- // 获取上传网络外链进度
1592
- // getUploadNetProgress,
1593
-
1594
- // 检查是否正在上传文件
1595
- checkUploading,
1596
- // 选择文件上传
1597
- chooseUpload,
1598
- // 选择网络外链上传
1599
- chooseUploadNet,
1600
- // 文件输入框更新
1601
- fileChange,
1602
- // 删除所有文件
1603
- deleteAll,
1604
- // 删除单个文件
1605
- deleteFileItem,
1606
- // 预览图片
1607
- previewImage,
1608
- // 修改文件名
1609
- editFileTitle,
1610
- // 播放视频/音频
1611
- play,
1612
- // 复制地址
1613
- copyUrl,
1614
- }
1615
- }
1616
-
1617
- /**
1618
- * 上传器
1619
- */
1620
- const uploader = {
1621
- // 创建对话框
1622
- create,
1623
- }
1624
-
1625
- export default uploader
1
+ import { ref, isRef, } from 'vue'
2
+ import { useQuasar } from 'quasar'
3
+ import SparkMD5 from 'spark-md5'
4
+
5
+ import $n_has from 'lodash/has'
6
+ import $n_get from 'lodash/get'
7
+ import $n_toLower from 'lodash/toLower'
8
+ import $n_findIndex from 'lodash/findIndex'
9
+ import $n_uniq from 'lodash/uniq'
10
+ import $n_find from 'lodash/find'
11
+ import $n_isFunction from 'lodash/isFunction'
12
+
13
+ import $n_isValidArray from '@netang/utils/isValidArray'
14
+ import $n_isValidObject from '@netang/utils/isValidObject'
15
+ import $n_isValidString from '@netang/utils/isValidString'
16
+ import $n_forEach from '@netang/utils/forEach'
17
+ import $n_indexOf from '@netang/utils/indexOf'
18
+ import $n_json from '@netang/utils/json'
19
+ import $n_join from '@netang/utils/join'
20
+ import $n_split from '@netang/utils/split'
21
+ import $n_trimString from '@netang/utils/trimString'
22
+ import $n_run from '@netang/utils/run'
23
+ import $n_isValidValue from '@netang/utils/isValidValue'
24
+ import $n_http from '@netang/utils/http'
25
+ import $n_getThrowMessage from '@netang/utils/getThrowMessage'
26
+ import $n_runAsync from '@netang/utils/runAsync'
27
+
28
+ import $n_$ruleValid from './$ruleValid'
29
+ import $n_toast from './toast'
30
+ import $n_confirm from './confirm'
31
+ import $n_alert from './alert'
32
+ import $n_previewImage from './previewImage'
33
+ import $n_getImage from './getImage'
34
+ import $n_getFile from './getFile'
35
+ import $n_config from './config'
36
+
37
+ import { configs } from './config'
38
+
39
+ import copy from './copy'
40
+
41
+ import {
42
+ // 文件类型映射
43
+ FilE_TYPE,
44
+ // 文件名称映射
45
+ FilE_NAME,
46
+ // 上传状态
47
+ UPLOAD_STATUS,
48
+ // 上传器
49
+ UPLOADERS,
50
+ } from './useUploader'
51
+
52
+ // 文件数量
53
+ let _fileNum = 0
54
+
55
+ /**
56
+ * 创建上传器
57
+ */
58
+ function create(options) {
59
+
60
+ // ==========【数据】=========================================================================================
61
+
62
+ // quasar 对象
63
+ const $q = useQuasar()
64
+
65
+ const {
66
+ // 上传文件输入框节点
67
+ fileRef,
68
+ // 更新值方法(初始化上传列表时不更新值)
69
+ onUpdateModelValue,
70
+ // 更新方法
71
+ onUpdate,
72
+
73
+ } = Object.assign({
74
+ // 更新值方法
75
+ onUpdateModelValue: null,
76
+ // 更新方法
77
+ onUpdate: null,
78
+ }, options)
79
+
80
+ const optionsProps = $n_get(options, 'props')
81
+
82
+ // 声明属性
83
+ const props = Object.assign({
84
+ // 值
85
+ modelValue: '',
86
+ // 上传文件类型, 可选值 file image video audio
87
+ type: 'image',
88
+ // 上传文件数量(0:不限)
89
+ count: 0,
90
+ // 单个文件的最大大小(单位: MB)
91
+ maxSize: 0,
92
+ // 单个文件的限制后缀
93
+ exts: [],
94
+ // true: 值格式为数组, 如 ['xxxxxx', 'xxxxxx', 'xxxxxx']
95
+ // false: 值格式为字符串, 如 xxxxxx,xxxxxx,xxxxxx
96
+ valueArray: false,
97
+ // 是否去重
98
+ unique: false,
99
+ // 是否初始加载文件信息(仅图片有效, 其他类型自动会加载文件信息)
100
+ loadInfo: false,
101
+ // 单文件上传提示
102
+ confirm: false,
103
+ }, optionsProps)
104
+
105
+ // options 中是否存在 props.modelValue
106
+ const hasPropsModelValue = $n_has(optionsProps, 'modelValue')
107
+
108
+ // 上传文件列表
109
+ const uploadFileLists = $n_has(options, 'uploadFileLists') && isRef(options.uploadFileLists) ? options.uploadFileLists : ref([])
110
+
111
+ // 上传网络外链回调
112
+ let uploadNetCallback
113
+
114
+ /**
115
+ * 上传配置
116
+ */
117
+ const configUpload = Object.assign(
118
+ {
119
+ type: 'local',
120
+ },
121
+ $n_config('uploader.upload')
122
+ )
123
+ const configLimit = Object.assign({
124
+ maxSize: 100,
125
+ exts: [],
126
+ }, $n_config('uploader.limit.' + props.type))
127
+
128
+ // 如果有单个文件的最大大小
129
+ if (props.maxSize) {
130
+ configLimit.maxSize = props.maxSize
131
+ }
132
+
133
+ // 如果有单个文件的限制后缀
134
+ if ($n_isValidArray(props.exts)) {
135
+ configLimit.exts = props.exts
136
+ }
137
+
138
+ // ==========【计算属性】=========================================================================================
139
+
140
+ /**
141
+ * 上传文件后缀名
142
+ */
143
+ // const accept = computed(function () {
144
+ //
145
+ // })
146
+
147
+ // ==========【监听数据】==============================================================================================
148
+
149
+ /**
150
+ * 监听上传文件列表
151
+ */
152
+ // if (props.watchModelValue && hasPropsModelValue) {
153
+ // watch(()=>options.props.modelValue, function() {
154
+ // // 初始化上传列表
155
+ // initUploadFileLists()
156
+ // .finally()
157
+ // })
158
+ // }
159
+
160
+ // ==========【方法】=================================================================================================
161
+
162
+ /**
163
+ * 获取值
164
+ */
165
+ function getValue() {
166
+
167
+ const hashs = []
168
+ const files = []
169
+
170
+ for (const fileItem of uploadFileLists.value) {
171
+ if (fileItem.status === UPLOAD_STATUS.success) {
172
+ hashs.push(fileItem.hash)
173
+ files.push(fileItem)
174
+ }
175
+ }
176
+
177
+ const hashsString = $n_join(hashs, ',')
178
+
179
+ return {
180
+ value: props.valueArray ? hashs : hashsString,
181
+ hashsString,
182
+ hashs,
183
+ files,
184
+ query: uploadFileLists,
185
+ }
186
+ }
187
+
188
+ /**
189
+ * 更新值
190
+ */
191
+ function updateValue() {
192
+
193
+ // 获取值
194
+ const result = getValue()
195
+
196
+ // 更新值
197
+ $n_run(onUpdateModelValue)(result)
198
+
199
+ // 更新
200
+ $n_run(onUpdate)(result)
201
+
202
+ // 上传网络外链回调
203
+ $n_run(uploadNetCallback)()
204
+ }
205
+
206
+ /**
207
+ * 更新
208
+ */
209
+ function update() {
210
+ // 更新
211
+ $n_run(onUpdate)(getValue())
212
+ }
213
+
214
+ /**
215
+ * 初始化上传列表
216
+ */
217
+ async function initUploadFileLists() {
218
+
219
+ const modelValue = hasPropsModelValue ? options.props.modelValue : props.modelValue
220
+
221
+ // 值数组
222
+ const hashs = []
223
+
224
+ // hash all
225
+ const hashAll = {}
226
+
227
+ // 获取值数组
228
+ const lists = props.valueArray ? modelValue : $n_split(modelValue, ',')
229
+
230
+ if (
231
+ // 如果只能上传一个
232
+ props.count === 1
233
+ // 如果为空
234
+ && ! $n_isValidArray(lists)
235
+ ) {
236
+ // 更新上传文件列表
237
+ uploadFileLists.value = []
238
+ return
239
+ }
240
+
241
+ // 新上传文件列表
242
+ const newUploadFileLists = []
243
+
244
+ // 新列表
245
+ const newLists = []
246
+
247
+ // 是否更新
248
+ let isUpdate = false
249
+
250
+ // 合并当前上传列表中未上传成功的文件
251
+ for (const fileItem of uploadFileLists.value) {
252
+ if (fileItem.status !== UPLOAD_STATUS.success) {
253
+ newUploadFileLists.push(fileItem)
254
+ hashAll[fileItem.hash] = fileItem
255
+ }
256
+ }
257
+
258
+ $n_forEach(lists, function(hash) {
259
+ if ($n_isValidString(hash)) {
260
+
261
+ const hasItem = $n_find(uploadFileLists.value, { hash })
262
+
263
+ // 如果在当前上传文件列表中已存在
264
+ if (hasItem) {
265
+ hashAll[hash] = hasItem
266
+
267
+ } else if (! $n_has(hashAll, 'hash')) {
268
+
269
+ // 如果是外链
270
+ if (/^http(s)?:\/\//i.test(hash)) {
271
+ hashs.push(hash)
272
+ hashAll[hash] = {
273
+ hash,
274
+ __img: hash,
275
+ isNet: true,
276
+ isNetUploaded: false,
277
+ }
278
+
279
+ // 否则为 hash 文件
280
+ } else {
281
+ hashs.push(hash)
282
+ hashAll[hash] = {
283
+ hash,
284
+ isNet: false,
285
+ }
286
+ }
287
+ }
288
+
289
+ // 新列表
290
+ newLists.push(hash)
291
+ }
292
+ })
293
+
294
+ // 如果类型不是图片 || 初始加载文件信息, 则请求文件信息
295
+ if (
296
+ (props.type !== 'image' || props.loadInfo)
297
+ && hashs.length
298
+ ) {
299
+ // 请求 - 获取文件
300
+ const { status, data: resExisted } = await $n_http({
301
+ url: $n_config('apiFileUrl') + 'get_file',
302
+ data: {
303
+ hashs: $n_uniq(hashs),
304
+ },
305
+ // 关闭错误
306
+ warn: false,
307
+ // 关闭防抖(可以重复请求)
308
+ debounce: false,
309
+ })
310
+ if (status) {
311
+
312
+ $n_forEach(resExisted, function (existedItem) {
313
+
314
+ // 创建原始单个文件
315
+ const fileItem = createRawFileItem()
316
+
317
+ // 设置已存在文件
318
+ setExistedFileItem(fileItem, existedItem)
319
+
320
+ // 添加至 hash all
321
+ hashAll[fileItem.hash] = Object.assign(fileItem, {
322
+ key: fileItem.hash,
323
+ })
324
+ })
325
+
326
+ // 需要更新
327
+ isUpdate = true
328
+ }
329
+ }
330
+
331
+ for (const hash of newLists) {
332
+
333
+ let hasItem = hashAll[hash]
334
+
335
+ // 如果该 hash 已存在
336
+ if ($n_has(hasItem, 'id')) {
337
+
338
+ // 如果新列表中存在 该 id
339
+ if ($n_findIndex(newUploadFileLists, { id: hasItem.id }) > -1) {
340
+
341
+ // 更新 id
342
+ hasItem = Object.assign({}, hasItem, {
343
+ id: ++_fileNum
344
+ })
345
+ }
346
+
347
+ // 否则该 hash 不存在
348
+ } else {
349
+ hasItem = Object.assign(createRawFileItem(), hasItem, {
350
+ // 文件唯一 key
351
+ key: hash,
352
+ // hash
353
+ hash,
354
+ // 状态
355
+ status: UPLOAD_STATUS.success,
356
+ // 进度
357
+ progress: 100,
358
+ // 信息
359
+ msg: '',
360
+ })
361
+ hashAll[hash] = hasItem
362
+ }
363
+
364
+ // 添加至新列表中
365
+ newUploadFileLists.push(hasItem)
366
+ }
367
+
368
+ // 更新上传文件列表
369
+ uploadFileLists.value = newUploadFileLists
370
+
371
+ if (isUpdate) {
372
+ // 更新
373
+ update()
374
+ }
375
+ }
376
+
377
+ /**
378
+ * 初始化上传网络外链列表
379
+ */
380
+ function initUploadNetLists(callback) {
381
+
382
+ // 如果提交时禁止上传网络外链文件
383
+ if ($n_get(optionsProps, 'submitUploadNet') !== true) {
384
+ return
385
+ }
386
+
387
+ uploadNetCallback = callback
388
+
389
+ for (const fileItem of uploadFileLists.value) {
390
+ if (
391
+ fileItem.isNet
392
+ && fileItem.status === UPLOAD_STATUS.success
393
+ ) {
394
+ // 将文件状态修改为: 等待上传中
395
+ fileItem.status = UPLOAD_STATUS.waiting
396
+ }
397
+ }
398
+ }
399
+
400
+ /**
401
+ * 上传网络外链文件
402
+ */
403
+ async function uploadNet(submitUploadNet = false) {
404
+
405
+ // 如果提交时禁止上传网络外链文件
406
+ if (submitUploadNet === true || $n_get(optionsProps, 'submitUploadNet') === true) {
407
+
408
+ const promises = []
409
+
410
+ for (const fileItem of uploadFileLists.value) {
411
+ if (
412
+ fileItem.isNet
413
+ && fileItem.status === UPLOAD_STATUS.waiting
414
+ ) {
415
+ // 设置网络图片 file
416
+ promises.push(setNetFile(fileItem))
417
+ }
418
+ }
419
+
420
+ if (! promises.length) {
421
+ return
422
+ }
423
+ await Promise.all(promises)
424
+
425
+ // 检查待上传文件在服务器上是否存在
426
+ // --------------------------------------------------
427
+ if (! await checkWaitUploadFileExists()) {
428
+ return
429
+ }
430
+
431
+ // 上传
432
+ await upload()
433
+ }
434
+ }
435
+
436
+ /**
437
+ * 获取上传网络外链进度
438
+ */
439
+ // function getUploadNetProgress() {
440
+ //
441
+ // let total = 0
442
+ // let loaded = 0
443
+ //
444
+ // // 如果提交时允许上传网络外链文件
445
+ // if ($n_get(optionsProps, 'submitUploadNet') === true) {
446
+ //
447
+ // for (const fileItem of uploadFileLists.value) {
448
+ // if (fileItem.isNet && fileItem.status <= UPLOAD_STATUS.success) {
449
+ // total++
450
+ // if (fileItem.isNetUploaded) {
451
+ // loaded++
452
+ // }
453
+ // }
454
+ // }
455
+ //
456
+ // // if (total && loaded < total) {
457
+ // // return {
458
+ // // loaded,
459
+ // // total,
460
+ // // // progress: Math.round(loaded * 100 / total),
461
+ // // }
462
+ // // }
463
+ // }
464
+ //
465
+ // return {
466
+ // loaded,
467
+ // total,
468
+ // // progress: 100,
469
+ // }
470
+ // }
471
+
472
+ /**
473
+ * 设置网络图片 file
474
+ */
475
+ async function setNetFile(fileItem) {
476
+
477
+ // 设置文件状态
478
+ fileItem.status = UPLOAD_STATUS.hashChecking
479
+ // 设置文件检查进度
480
+ fileItem.progress = 0
481
+
482
+ try {
483
+ const r = await fetch(fileItem.__img, {
484
+ method: 'GET',
485
+ })
486
+ const arrayBuffer = await r.arrayBuffer()
487
+ const blob = new Blob([arrayBuffer], { type: r.headers.get('Content-Type') })
488
+
489
+ // -------- axios
490
+ // const r = await axios({
491
+ // method: 'GET',
492
+ // url: fileItem.__img,
493
+ // responseType: 'arraybuffer'
494
+ // })
495
+ // console.log(r)
496
+ // const arrayBuffer = r.data
497
+ // const blob = new Blob([arrayBuffer], { type: r.headers['content-type'] })
498
+ // -------- axios
499
+
500
+ // 如果有类型
501
+ if (blob.type) {
502
+
503
+ // 后缀名
504
+ let ext = ''
505
+
506
+ // 如果为图片
507
+ if (
508
+ props.type === 'image'
509
+ || $n_indexOf(blob.type, 'image/') > -1
510
+ ) {
511
+ switch (blob.type) {
512
+ case 'image/png':
513
+ ext = 'png'
514
+ break
515
+ case 'image/gif':
516
+ ext = 'gif'
517
+ break
518
+ default:
519
+ ext = 'jpg'
520
+ break
521
+ }
522
+
523
+ // 如果为视频
524
+ } else if (props.type === 'video') {
525
+ ext = 'mp4'
526
+
527
+ // 如果为音频
528
+ } else if (props.type === 'audio') {
529
+ ext = 'mp3'
530
+
531
+ // 否则为文件
532
+ } else {
533
+ const arr = $n_split(props.type, '/')
534
+ if (arr.length > 0) {
535
+ ext = arr[1]
536
+ }
537
+ }
538
+
539
+ // 如果有后缀名
540
+ if (ext) {
541
+ // 设置文件
542
+ fileItem.file = new File([blob], '', { type: blob.type })
543
+ // 设置后缀名
544
+ fileItem.ext = ext
545
+
546
+ const {
547
+ size,
548
+ } = fileItem.file
549
+
550
+ // 文件大小
551
+ fileItem.size = size
552
+
553
+ // 检查文件错误
554
+ const errMsg = checkFileError(fileItem)
555
+ if (errMsg) {
556
+ // 设置文件上传失败
557
+ setFileFail(fileItem, errMsg)
558
+ return
559
+ }
560
+
561
+ // 初始化 SparkMD5
562
+ const spark = new SparkMD5.ArrayBuffer()
563
+ spark.append(arrayBuffer)
564
+
565
+ // 获取文件 hash
566
+ const hash = spark.end(false)
567
+ if (hash) {
568
+ // 设置文件 hash
569
+ fileItem.hash = hash
570
+ // 标题
571
+ fileItem.title = fileItem.hash
572
+ // 设置文件状态
573
+ fileItem.status = UPLOAD_STATUS.hashChecked
574
+ // 设置文件检查进度
575
+ fileItem.progress = 100
576
+ return
577
+ }
578
+ }
579
+ }
580
+
581
+ } catch (e) {}
582
+
583
+ // 设置文件上传失败
584
+ setFileFail(fileItem)
585
+ }
586
+
587
+ /**
588
+ * 检查是否正在上传文件
589
+ */
590
+ function checkUploading() {
591
+ for (const fileItem of uploadFileLists.value) {
592
+ if (fileItem.status < UPLOAD_STATUS.success) {
593
+ return true
594
+ }
595
+ }
596
+ return false
597
+ }
598
+
599
+ /**
600
+ * 选择文件上传
601
+ */
602
+ function chooseUpload() {
603
+ // 点击文件输入框
604
+ fileRef.value.click()
605
+ }
606
+
607
+ /**
608
+ * 选择网络外链上传
609
+ */
610
+ function chooseUploadNet() {
611
+ $q.dialog({
612
+ title: `添加网络${FilE_NAME[FilE_TYPE[props.type]]}`,
613
+ style: 'min-width:600px;',
614
+ // message: `添加网络${FilE_NAME[FilE_TYPE[props.type]]}`,
615
+ prompt: {
616
+ model: '',
617
+ isValid: $n_$ruleValid('required'),
618
+ type: 'textarea',
619
+ placeholder: '多个链接,使用中英文逗号、空格、换行隔开',
620
+ outlined: true,
621
+ },
622
+ cancel: true,
623
+ persistent: true,
624
+ })
625
+ .onOk(async function(value) {
626
+
627
+ let files = $n_isValidString(value)
628
+ ? $n_uniq($n_split(value.replace(/\n|,|,/g, ','), ','))
629
+ .map(e => $n_trimString(e))
630
+ .filter(
631
+ val => val && /^(http(s)?:\/\/)/i.test(val)
632
+ )
633
+ : []
634
+
635
+ if (! files.length) {
636
+ // 轻提示
637
+ $n_toast({
638
+ message: '请输入正确的链接'
639
+ })
640
+ return
641
+ }
642
+
643
+ // 格式化上传网络链接
644
+ // --------------------------------------------------
645
+ const {
646
+ formatUploadNet,
647
+ } = configs
648
+ if ($n_isFunction(formatUploadNet)) {
649
+ files = formatUploadNet(files, props.type === 'image')
650
+ }
651
+ // --------------------------------------------------
652
+
653
+ if (! $n_isValidArray(files)) {
654
+ // 轻提示
655
+ $n_toast({
656
+ message: '请输入正确的链接'
657
+ })
658
+ return
659
+ }
660
+
661
+ // 添加上传网络外链至文件列表
662
+ function addFileNetItem(hash) {
663
+ uploadFileLists.value.push(Object.assign(createRawFileItem(), {
664
+ // id
665
+ id: ++_fileNum,
666
+ // 文件唯一 key
667
+ key: hash,
668
+ // hash
669
+ hash,
670
+ // 将文件状态修改为: 等待上传中
671
+ status: UPLOAD_STATUS.waiting,
672
+ // 进度
673
+ progress: 100,
674
+ // 信息
675
+ msg: '',
676
+
677
+ __img: hash,
678
+ isNet: true,
679
+ isNetUploaded: false,
680
+ }))
681
+ }
682
+
683
+ // 遍历选择的文件列表
684
+ for (const file of files) {
685
+
686
+ // 如果只能上传一个
687
+ if (props.count === 1) {
688
+
689
+ // 如果有上传文件列表
690
+ if (uploadFileLists.value.length) {
691
+
692
+ // 如果开启单文件上传提示
693
+ if (props.confirm) {
694
+
695
+ // 确认框
696
+ $n_confirm({
697
+ message: '最多只能上传1个文件,确认上传并替换吗?',
698
+ })
699
+ // 点击确认执行
700
+ .onOk(function () {
701
+
702
+ // 删除所有文件
703
+ deleteAll()
704
+
705
+ // 添加上传网络外链至文件列表
706
+ addFileNetItem(file)
707
+
708
+ // 上传网络外链文件
709
+ uploadNet(true)
710
+ .finally()
711
+ })
712
+ return
713
+ }
714
+
715
+ // 删除所有文件
716
+ deleteAll()
717
+ }
718
+
719
+ } else if (
720
+ // 如果有上传数量限制
721
+ props.count > 1
722
+ // 上传文件列表数量 === 上传数量限制
723
+ && uploadFileLists.value.length >= props.count
724
+ ) {
725
+ // 轻提示
726
+ $n_toast({
727
+ message: `最多只能上传${props.count}个文件,请先删除后再上传`,
728
+ })
729
+ return
730
+ }
731
+
732
+ // 添加上传网络外链至文件列表
733
+ addFileNetItem(file)
734
+ }
735
+
736
+ // 上传网络外链文件
737
+ uploadNet(true)
738
+ .finally()
739
+ })
740
+ }
741
+
742
+ /**
743
+ * 文件输入框更新
744
+ */
745
+ function fileChange(e) {
746
+
747
+ try {
748
+ // 获取上传文件
749
+ const files = Array.from(e.target.files)
750
+
751
+ // 清空上传文件输入框内容
752
+ fileRef.value.value = ''
753
+
754
+ // 如果没有选择文件
755
+ if (! files.length) {
756
+ // 则无任何操作
757
+ return
758
+ }
759
+
760
+ // 遍历选择的文件列表
761
+ for (const file of files) {
762
+
763
+ // 创建单个文件
764
+ const fileItem = createFileItem(file)
765
+ if (fileItem !== false) {
766
+
767
+ // 如果只能上传一个
768
+ if (props.count === 1) {
769
+
770
+ // 如果有上传文件列表
771
+ if (uploadFileLists.value.length) {
772
+
773
+ // 如果开启单文件上传提示
774
+ if (props.confirm) {
775
+
776
+ // 确认框
777
+ $n_confirm({
778
+ message: '最多只能上传1个文件,确认上传并替换吗?',
779
+ })
780
+ // 点击确认执行
781
+ .onOk(function () {
782
+
783
+ // 删除所有文件
784
+ deleteAll()
785
+
786
+ // 添加至上传文件列表
787
+ uploadFileLists.value.push(fileItem)
788
+
789
+ // 开始上传
790
+ startUpload()
791
+ .finally()
792
+ })
793
+ return
794
+ }
795
+
796
+ // 删除所有文件
797
+ deleteAll()
798
+ }
799
+
800
+ } else if (
801
+ // 如果有上传数量限制
802
+ props.count > 1
803
+ // 上传文件列表数量 === 上传数量限制
804
+ && uploadFileLists.value.length >= props.count
805
+ ) {
806
+ // 轻提示
807
+ $n_toast({
808
+ message: `最多只能上传${props.count}个文件,请先删除后再上传`,
809
+ })
810
+ return
811
+ }
812
+
813
+ // 添加至上传文件列表
814
+ uploadFileLists.value.push(fileItem)
815
+ }
816
+ }
817
+
818
+ // 开始上传
819
+ startUpload()
820
+ .finally()
821
+
822
+ } catch (e) {
823
+
824
+ // 错误提示
825
+ $n_alert({
826
+ message: $n_getThrowMessage(e),
827
+ })
828
+ }
829
+
830
+ }
831
+
832
+ /**
833
+ * 开始上传
834
+ */
835
+ async function startUpload() {
836
+
837
+ // 【设置待上传文件的 hash】
838
+ // --------------------------------------------------
839
+ const promises = []
840
+ for (const fileItem of uploadFileLists.value) {
841
+ // 如果是等待上传的文件
842
+ if (fileItem.status === UPLOAD_STATUS.waiting) {
843
+
844
+ // 检查文件错误
845
+ const errMsg = checkFileError(fileItem)
846
+ if (errMsg) {
847
+ // 设置文件上传失败
848
+ setFileFail(fileItem, errMsg)
849
+
850
+ } else {
851
+ // 设置文件 hash
852
+ promises.push(setFileHash(fileItem))
853
+ }
854
+ }
855
+ }
856
+
857
+ if (promises.length) {
858
+ await Promise.all(promises)
859
+ }
860
+
861
+ // 检查待上传文件在服务器上是否存在
862
+ // --------------------------------------------------
863
+ if (! await checkWaitUploadFileExists()) {
864
+ return
865
+ }
866
+
867
+ // 上传
868
+ await upload()
869
+ }
870
+
871
+ /**
872
+ * 上传
873
+ */
874
+ let _upload = null
875
+ async function upload() {
876
+ 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
+ // 待上传文件列表
892
+ const waitUploadFileLists = []
893
+ for (const fileItem of uploadFileLists.value) {
894
+ // 检查存在服务器完成
895
+ if (fileItem.status === UPLOAD_STATUS.existChecked) {
896
+ waitUploadFileLists.push(fileItem)
897
+ }
898
+ }
899
+ if (! waitUploadFileLists.length) {
900
+ return
901
+ }
902
+
903
+ // 上传
904
+ await _upload({
905
+ config: configUpload,
906
+ waitUploadFileLists,
907
+ uploadFileLists,
908
+ checkFileError,
909
+ setFileSuccess,
910
+ setFileFail,
911
+ })
912
+
913
+ } catch (e) {
914
+ // 错误提示
915
+ $n_alert({
916
+ message: $n_getThrowMessage(e),
917
+ })
918
+ }
919
+ }
920
+
921
+ /**
922
+ * 设置文件上传成功
923
+ */
924
+ const setFileSuccess = (fileItem) => {
925
+
926
+ // 设置文件状态
927
+ fileItem.status = UPLOAD_STATUS.success
928
+ // 设置文件信息
929
+ fileItem.msg = ''
930
+ // 设置文件检查进度
931
+ fileItem.progress = 0
932
+ // 是否网络文件已上传
933
+ if (fileItem.isNet) {
934
+ fileItem.isNetUploaded = true
935
+ }
936
+
937
+ // // 单个文件上传结束回调
938
+ // uploadQueryCallback(fileItem)
939
+ //
940
+ // // 上传更新回调
941
+ // uploadChange(getResultData())
942
+
943
+ // 更新值
944
+ updateValue()
945
+ }
946
+
947
+ /**
948
+ * 设置文件上传失败
949
+ */
950
+ function setFileFail(fileItem, errMsg) {
951
+
952
+ // 设置文件状态
953
+ fileItem.status = UPLOAD_STATUS.fail
954
+ // 设置文件信息
955
+ fileItem.msg = errMsg || `无效${FilE_NAME[fileItem.type]}`
956
+ // 设置文件检查进度
957
+ fileItem.progress = 0
958
+
959
+ // // 单个文件上传结束回调
960
+ // uploadQueryCallback(fileItem)
961
+
962
+ // 上传网络外链回调
963
+ $n_run(uploadNetCallback)()
964
+ }
965
+
966
+ /**
967
+ * 设置文件 hash
968
+ */
969
+ function setFileHash(fileItem) {
970
+ return new Promise(function (resolve) {
971
+
972
+ // 设置文件状态
973
+ fileItem.status = UPLOAD_STATUS.hashChecking
974
+ // 设置文件检查进度
975
+ fileItem.progress = 0
976
+
977
+ const {
978
+ file,
979
+ } = fileItem
980
+
981
+ // blob 切片
982
+ const blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice
983
+ // 以 2MB 的分片读取
984
+ const chunkSize = 2097152
985
+ // 总分片数量
986
+ const chunks = Math.ceil(file.size / chunkSize)
987
+ // 初始化 SparkMD5
988
+ const spark = new SparkMD5.ArrayBuffer()
989
+ // 当前分片数
990
+ let currentChunk = 0
991
+ // 创建文件读取器
992
+ const fileReader = new FileReader()
993
+
994
+ // 文件加载
995
+ fileReader.onload = function(e) {
996
+
997
+ // 追加 array buffer
998
+ spark.append(e.target.result)
999
+
1000
+ // 当前分片数++
1001
+ currentChunk++
1002
+
1003
+ // 如果分块数量 < 总分片数量
1004
+ if (currentChunk < chunks) {
1005
+
1006
+ // 设置文件检查进度
1007
+ fileItem.progress = Math.floor((currentChunk / chunks) * 100)
1008
+
1009
+ // 读取下一个分片
1010
+ loadNext()
1011
+
1012
+ } else {
1013
+
1014
+ // 获取文件 hash
1015
+ const hash = spark.end(false)
1016
+ if (! hash) {
1017
+ // 设置文件上传失败
1018
+ setFileFail(fileItem)
1019
+ // 完成回调
1020
+ resolve()
1021
+ return
1022
+ }
1023
+
1024
+ // 下一步
1025
+ function next(hash) {
1026
+ if (
1027
+ // 如果开启去重
1028
+ props.unique
1029
+ // 如果该文件 hash 在上传文件列表中
1030
+ && $n_findIndex(uploadFileLists.value, { hash }) > -1
1031
+ ) {
1032
+ // 轻提示
1033
+ $n_toast({
1034
+ message: '该文件已存在,不可重复上传',
1035
+ })
1036
+
1037
+ // 设置文件上传失败
1038
+ setFileFail(fileItem, '已存在')
1039
+
1040
+ // 删除单个文件
1041
+ deleteFileItem(fileItem)
1042
+
1043
+ } else {
1044
+ // 设置文件 hash
1045
+ fileItem.hash = hash
1046
+ // 设置文件状态
1047
+ fileItem.status = UPLOAD_STATUS.hashChecked
1048
+ // 设置文件检查进度
1049
+ fileItem.progress = 100
1050
+ }
1051
+
1052
+ // 完成回调
1053
+ resolve()
1054
+ }
1055
+
1056
+ // 格式化上传文件 hash
1057
+ // --------------------------------------------------
1058
+ const {
1059
+ formatUploadFileHash,
1060
+ } = configs
1061
+ if ($n_isFunction(formatUploadFileHash)) {
1062
+ $n_runAsync(formatUploadFileHash)(hash, fileItem)
1063
+ .then(function (newHash) {
1064
+ // 下一步
1065
+ next($n_isValidString(newHash) ? newHash : hash)
1066
+ })
1067
+ return
1068
+ }
1069
+ // --------------------------------------------------
1070
+
1071
+ // 下一步
1072
+ next(hash)
1073
+ }
1074
+ }
1075
+
1076
+ // 文件加载错误
1077
+ fileReader.onerror = function() {
1078
+ // 设置文件上传失败
1079
+ setFileFail(fileItem)
1080
+ // 完成回调
1081
+ resolve()
1082
+ }
1083
+
1084
+ /**
1085
+ * 读取下一个分片
1086
+ */
1087
+ function loadNext() {
1088
+ const start = currentChunk * chunkSize
1089
+ const end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize
1090
+ fileReader.readAsArrayBuffer(blobSlice.call(file, start, end))
1091
+ }
1092
+
1093
+ // 读取下一个分片
1094
+ loadNext()
1095
+ })
1096
+ }
1097
+
1098
+ /**
1099
+ * 检查文件错误
1100
+ */
1101
+ function checkFileError({ type, size, ext }) {
1102
+
1103
+ // 需要上传的文件类型
1104
+ const fileType = FilE_TYPE[props.type]
1105
+
1106
+ // 如果 类型为文件, 则不受限制 || 当前上传的类型允许
1107
+ if (props.type === 'file' || type === fileType) {
1108
+
1109
+ const {
1110
+ maxSize,
1111
+ exts,
1112
+ } = configLimit
1113
+
1114
+ // 检查文件后缀名
1115
+ if (
1116
+ // 如果没有后缀名, 则无效
1117
+ ! ext
1118
+ // 如果后缀名不在允许范围内, 则无效
1119
+ || (
1120
+ $n_isValidArray(exts)
1121
+ && exts.indexOf(ext) === -1
1122
+ )
1123
+ ) {
1124
+ return '后缀名不允许'
1125
+ }
1126
+
1127
+ // 检查文件大小
1128
+ if (size > 0) {
1129
+ if (
1130
+ maxSize > 0
1131
+ && size > (maxSize * 1048576)
1132
+ ) {
1133
+ return `${FilE_NAME[fileType]}大于${maxSize}M`
1134
+ }
1135
+ return
1136
+ }
1137
+ }
1138
+
1139
+ return `无效${FilE_NAME[fileType]}`
1140
+ }
1141
+
1142
+ /**
1143
+ * 检查待上传文件在服务器上是否存在
1144
+ */
1145
+ async function checkWaitUploadFileExists() {
1146
+
1147
+ // 需检查的 hashs
1148
+ const checkHashs = []
1149
+ // 需检查的文件列表
1150
+ const checkFileLists = []
1151
+
1152
+ for (const fileItem of uploadFileLists.value) {
1153
+ if (fileItem.status === UPLOAD_STATUS.hashChecked) {
1154
+ // 设置文件状态
1155
+ fileItem.status = UPLOAD_STATUS.existChecking
1156
+ // 添加检查 hash 数组
1157
+ checkHashs.push(fileItem.hash)
1158
+ // 添加检查文件列表
1159
+ checkFileLists.push(fileItem)
1160
+ }
1161
+ }
1162
+
1163
+ // 如果没有需要检查的 hash
1164
+ if (! checkHashs.length) {
1165
+ // 返回失败
1166
+ return false
1167
+ }
1168
+
1169
+ // 请求 - 检查文件是否存在 hash
1170
+ const { status, data: resExisted } = await $n_http({
1171
+ url: $n_config('apiFileUrl') + 'check_exist',
1172
+ data: {
1173
+ hashs: $n_uniq(checkHashs),
1174
+ },
1175
+ // 关闭错误
1176
+ warn: false,
1177
+ // 关闭防抖(可以重复请求)
1178
+ debounce: false,
1179
+ })
1180
+
1181
+ // 如果请求失败
1182
+ if (! status) {
1183
+ // 设置文件上传失败
1184
+ for (const fileItem of checkFileLists) {
1185
+ setFileFail(fileItem, resExisted.msg)
1186
+ }
1187
+ // 返回失败
1188
+ return false
1189
+ }
1190
+
1191
+ // 设置文件状态
1192
+ for (const fileItem of checkFileLists) {
1193
+ fileItem.status = UPLOAD_STATUS.existChecked
1194
+ }
1195
+
1196
+ // 如果有存在的文件列表
1197
+ if ($n_isValidArray(resExisted)) {
1198
+
1199
+ // 已存在文件数量
1200
+ let existedNum = 0
1201
+ // 不存在文件数量
1202
+ let noExistNum = 0
1203
+
1204
+ for (const fileItem of checkFileLists) {
1205
+
1206
+ // 如果文件已存在(已经上传过了, 就是检查是否秒传文件)
1207
+ const existedItem = $n_find(resExisted, { hash: fileItem.hash })
1208
+ if (existedItem) {
1209
+
1210
+ // 如果 类型为文件, 则不受限制 || 文件类型正确
1211
+ if (props.type === 'file' || fileItem.type === $n_get(existedItem, 'type')) {
1212
+
1213
+ // 设置已存在文件
1214
+ setExistedFileItem(fileItem, existedItem)
1215
+
1216
+ // 设置文件为非网络外链
1217
+ fileItem.isNet = false
1218
+
1219
+ // 单个文件上传结束回调
1220
+ // uploadQueryCallback(fileItem)
1221
+
1222
+ // 已存在文件数量++
1223
+ existedNum++
1224
+
1225
+ // 否则有不存在文件
1226
+ } else {
1227
+ // 不存在文件数量++
1228
+ noExistNum++
1229
+ }
1230
+
1231
+ // 否则有不存在文件
1232
+ } else {
1233
+ // 不存在文件数量++
1234
+ noExistNum++
1235
+ }
1236
+ }
1237
+
1238
+ // 如果有已存在的文件
1239
+ if (existedNum) {
1240
+
1241
+ // 更新值
1242
+ updateValue()
1243
+
1244
+ // 上传更新回调
1245
+ // uploadChange(getResultData())
1246
+ }
1247
+
1248
+ return noExistNum > 0
1249
+ }
1250
+
1251
+ return true
1252
+ }
1253
+
1254
+ /**
1255
+ * 创建原始单个文件
1256
+ */
1257
+ function createRawFileItem() {
1258
+ return {
1259
+ // id
1260
+ id: ++_fileNum,
1261
+ // 文件唯一 key
1262
+ // key,
1263
+ // 文件名
1264
+ title: '',
1265
+ // 文件原始数据
1266
+ // file,
1267
+ // 类型(1:文件,2:图片,3:视频,4:音频)
1268
+ type: FilE_TYPE[props.type],
1269
+ // 后缀
1270
+ ext: '',
1271
+ // 大小
1272
+ size: 0,
1273
+ // hash
1274
+ hash: '',
1275
+ // 信息
1276
+ json: {},
1277
+ // 状态: waiting checking checked uploading success fail
1278
+ status: UPLOAD_STATUS.waiting,
1279
+ // 进度
1280
+ progress: 0,
1281
+ // 信息
1282
+ msg: '待上传',
1283
+ // 是否为网络文件
1284
+ isNet: false,
1285
+ // 是否网络文件已上传
1286
+ isNetUploaded: false,
1287
+ // 中断上传
1288
+ abort() {},
1289
+ }
1290
+ }
1291
+
1292
+ /**
1293
+ * 创建单个文件
1294
+ */
1295
+ function createFileItem(file) {
1296
+
1297
+ // 单个文件示例
1298
+ // name: "123.jpg"
1299
+ // size: 101206
1300
+ // type: "image/jpeg"
1301
+ // lastModified: 1629099994411
1302
+ // lastModifiedDate: Mon Aug 16 2021 15:46:34 GMT+0800 (中国标准时间) {}
1303
+ // webkitRelativePath: ""
1304
+
1305
+ const {
1306
+ name,
1307
+ size,
1308
+ lastModified,
1309
+ webkitRelativePath,
1310
+ } = file
1311
+
1312
+ // 文件唯一 key
1313
+ const key = webkitRelativePath + lastModified + name + size
1314
+
1315
+ if (
1316
+ // 如果开启去重
1317
+ props.unique
1318
+ // 如果该文件 key 在上传文件列表中
1319
+ && $n_findIndex(uploadFileLists.value, { key }) > -1
1320
+ ) {
1321
+ // 轻提示
1322
+ $n_toast({
1323
+ message: '该文件已存在,不可重复上传',
1324
+ })
1325
+
1326
+ // 则返回 false
1327
+ return false
1328
+ }
1329
+
1330
+ // 文件名
1331
+ let title = name
1332
+ // 文件后缀名
1333
+ let ext = ''
1334
+
1335
+ const index = name.lastIndexOf('.')
1336
+ if (index > -1) {
1337
+ title = name.substring(0, index)
1338
+ ext = $n_toLower(name.substring(index + 1))
1339
+ }
1340
+
1341
+ // 创建单个文件
1342
+ const fileItem = Object.assign(createRawFileItem(), {
1343
+ // 文件唯一 key
1344
+ key,
1345
+ // 文件原始数据
1346
+ file,
1347
+ // 文件名
1348
+ title,
1349
+ // 后缀
1350
+ ext,
1351
+ // 大小
1352
+ size,
1353
+ })
1354
+
1355
+ // 如果上传类型为图片
1356
+ if (
1357
+ props.type === 'image'
1358
+ && file.type.toLowerCase().startsWith('image')
1359
+ ) {
1360
+ // 获取图片预览地址
1361
+ const img = new Image()
1362
+ img.src = window.URL.createObjectURL(file)
1363
+ fileItem.__img = img.src
1364
+ }
1365
+
1366
+ return fileItem
1367
+ }
1368
+
1369
+ /**
1370
+ * 设置已存在文件
1371
+ */
1372
+ function setExistedFileItem(fileItem, existedItem) {
1373
+
1374
+ // 返回数据示例
1375
+ // ------------------------------
1376
+ // title: 1
1377
+ // type: 2
1378
+ // hash: "799e6bb77f638e192b9079e6a55bc2de"
1379
+ // ext: "jpg"
1380
+ // size: 306037
1381
+ // json: "{\"w\":1920,\"h\":1200,\"o\":1}"
1382
+ // status: 1
1383
+
1384
+ const {
1385
+ // 标题
1386
+ title,
1387
+ // 类型(1:文件,2:图片,3:视频,4:音频)
1388
+ type,
1389
+ // hash
1390
+ hash,
1391
+ // 后缀
1392
+ ext,
1393
+ // 大小
1394
+ size,
1395
+ // 信息
1396
+ json,
1397
+ } = existedItem
1398
+
1399
+ const fileJson = $n_json.parse(json)
1400
+
1401
+ // 设置文件
1402
+ Object.assign(fileItem, {
1403
+ // 标题
1404
+ title,
1405
+ // 类型(1:文件,2:图片,3:视频,4:音频)
1406
+ type,
1407
+ // hash
1408
+ hash,
1409
+ // 后缀
1410
+ ext,
1411
+ // 大小
1412
+ size,
1413
+ // 信息
1414
+ json: $n_isValidObject(fileJson) ? fileJson : {},
1415
+ // 状态
1416
+ status: UPLOAD_STATUS.success,
1417
+ // 进度
1418
+ progress: 100,
1419
+ // 信息
1420
+ msg: '',
1421
+ })
1422
+ }
1423
+
1424
+ /**
1425
+ * 删除所有文件
1426
+ */
1427
+ function deleteAll() {
1428
+
1429
+ // 如果有上传文件列表
1430
+ if (uploadFileLists.value.length) {
1431
+
1432
+ for (const fileItem of uploadFileLists.value) {
1433
+ // 退出上传
1434
+ fileItem.abort()
1435
+ }
1436
+
1437
+ // 清空上传文件列表
1438
+ uploadFileLists.value = []
1439
+ }
1440
+ }
1441
+
1442
+ /**
1443
+ * 删除单个文件
1444
+ */
1445
+ function deleteFileItem(fileItem) {
1446
+ const index = $n_findIndex(uploadFileLists.value, { id: fileItem.id })
1447
+ if (index > -1) {
1448
+
1449
+ const {
1450
+ status,
1451
+ } = fileItem
1452
+
1453
+ // 退出上传
1454
+ fileItem.abort()
1455
+
1456
+ // 从上传文件列表中删除
1457
+ uploadFileLists.value.splice(index, 1)
1458
+
1459
+ // 如果该文件已上传成功
1460
+ if (status === UPLOAD_STATUS.success) {
1461
+ // 更新值
1462
+ updateValue()
1463
+ }
1464
+ }
1465
+ }
1466
+
1467
+ /**
1468
+ * 预览图片
1469
+ */
1470
+ function previewImage(fileItem) {
1471
+ // 预览图片
1472
+ if (fileItem.type === FilE_TYPE.image) {
1473
+ $n_previewImage({
1474
+ images: [
1475
+ $n_has(fileItem, '__img') ? fileItem.__img : fileItem.hash,
1476
+ ],
1477
+ })
1478
+ }
1479
+ }
1480
+
1481
+ /**
1482
+ * 修改文件名
1483
+ */
1484
+ async function editFileTitle(newTitle, fileItem) {
1485
+
1486
+ if ($n_isValidValue(newTitle)) {
1487
+
1488
+ newTitle = $n_trimString(newTitle)
1489
+
1490
+ const {
1491
+ hash,
1492
+ title,
1493
+ } = fileItem
1494
+
1495
+ if (title === newTitle) {
1496
+ return
1497
+ }
1498
+
1499
+ // 先设置新文件名
1500
+ fileItem.title = newTitle
1501
+
1502
+ // 请求 - 修改文件名
1503
+ const { status } = await $n_http({
1504
+ url: $n_config('apiFileUrl') + 'edit_file_title',
1505
+ data: {
1506
+ hash,
1507
+ title: newTitle,
1508
+ },
1509
+ })
1510
+
1511
+ // 如果修改失败
1512
+ if (! status) {
1513
+ // 还原文件名
1514
+ fileItem.title = title
1515
+ return
1516
+ }
1517
+
1518
+ // 轻提示
1519
+ $n_toast({
1520
+ type: 'positive',
1521
+ message: '修改成功',
1522
+ })
1523
+ }
1524
+ }
1525
+
1526
+ /**
1527
+ * 播放
1528
+ */
1529
+ function play({ hash, __img }) {
1530
+
1531
+ const src = __img ? __img : $n_getFile(hash)
1532
+
1533
+ let width
1534
+ let height
1535
+ let fullWidth = false
1536
+ let fullHeight = false
1537
+ let ok = true
1538
+ let style = ''
1539
+
1540
+ if ($q.platform.is.mobile) {
1541
+ width = $q.screen.width - 48 - 32
1542
+ height = $q.screen.height - 48 - 32 - 6 - 52
1543
+ fullWidth = true
1544
+ fullHeight = true
1545
+ } else {
1546
+ width = 800 - 32
1547
+ height = 400 - 32 - 6
1548
+ ok = false
1549
+ style = 'width:800px;max-width:800px;height:400px;max-height:400px;'
1550
+ }
1551
+
1552
+ $q.dialog({
1553
+ message: `<video style="width:${width}px;height:${height}px;" playsinline autoplay controls src="${src}" type="video/mp4" muted="muted"></video>`,
1554
+ style,
1555
+ html: true,
1556
+ dark: true,
1557
+ ok,
1558
+ fullWidth,
1559
+ fullHeight,
1560
+ })
1561
+ }
1562
+
1563
+ /**
1564
+ * 复制地址
1565
+ */
1566
+ function copyUrl({ type, hash }) {
1567
+
1568
+ const _url = type === FilE_TYPE.image ?
1569
+ // 如果是图片
1570
+ $n_getImage(hash)
1571
+ // 否则是文件
1572
+ : $n_getFile(hash)
1573
+
1574
+ if ($n_isValidString(_url)) {
1575
+ copy(_url, '复制地址成功')
1576
+ }
1577
+ }
1578
+
1579
+ return {
1580
+ // 上传文件列表
1581
+ query: uploadFileLists,
1582
+ // 更新值
1583
+ updateValue,
1584
+ // 初始化上传列表
1585
+ initUploadFileLists,
1586
+
1587
+ // 初始化上传网络外链列表
1588
+ initUploadNetLists,
1589
+ // 上传网络外链文件
1590
+ uploadNet,
1591
+ // 获取上传网络外链进度
1592
+ // getUploadNetProgress,
1593
+
1594
+ // 检查是否正在上传文件
1595
+ checkUploading,
1596
+ // 选择文件上传
1597
+ chooseUpload,
1598
+ // 选择网络外链上传
1599
+ chooseUploadNet,
1600
+ // 文件输入框更新
1601
+ fileChange,
1602
+ // 删除所有文件
1603
+ deleteAll,
1604
+ // 删除单个文件
1605
+ deleteFileItem,
1606
+ // 预览图片
1607
+ previewImage,
1608
+ // 修改文件名
1609
+ editFileTitle,
1610
+ // 播放视频/音频
1611
+ play,
1612
+ // 复制地址
1613
+ copyUrl,
1614
+ }
1615
+ }
1616
+
1617
+ /**
1618
+ * 上传器
1619
+ */
1620
+ const uploader = {
1621
+ // 创建对话框
1622
+ create,
1623
+ }
1624
+
1625
+ export default uploader