@netang/quasar 0.0.102 → 0.0.103

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 (162) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +11 -11
  3. package/_docs/docs/.vuepress/client.js +8 -8
  4. package/_docs/docs/.vuepress/config.js +40 -40
  5. package/_docs/docs/.vuepress/configs/index.js +2 -2
  6. package/_docs/docs/.vuepress/configs/navbar/index.js +1 -1
  7. package/_docs/docs/.vuepress/configs/navbar/zh.js +16 -16
  8. package/_docs/docs/.vuepress/configs/sidebar/index.js +1 -1
  9. package/_docs/docs/.vuepress/configs/sidebar/zh.js +75 -75
  10. package/_docs/docs/.vuepress/public/css/index.css +3 -3
  11. package/_docs/docs/.vuepress/styles/index.scss +3 -3
  12. package/_docs/docs/components/column-title.md +25 -25
  13. package/_docs/docs/components/data.md +66 -66
  14. package/_docs/docs/components/dialog.md +59 -59
  15. package/_docs/docs/components/dragger.md +26 -26
  16. package/_docs/docs/components/editor-code.md +16 -16
  17. package/_docs/docs/components/empty.md +13 -13
  18. package/_docs/docs/components/field-date.md +16 -16
  19. package/_docs/docs/components/field-text.md +57 -57
  20. package/_docs/docs/components/field-tree.md +14 -14
  21. package/_docs/docs/components/img.md +25 -25
  22. package/_docs/docs/components/input-number.md +21 -21
  23. package/_docs/docs/components/list-menu-item.md +21 -21
  24. package/_docs/docs/components/list-menu.md +21 -21
  25. package/_docs/docs/components/power-page.md +21 -21
  26. package/_docs/docs/components/price.md +21 -21
  27. package/_docs/docs/components/render.md +12 -12
  28. package/_docs/docs/components/search-item.md +10 -10
  29. package/_docs/docs/components/search.md +12 -12
  30. package/_docs/docs/components/select.md +11 -11
  31. package/_docs/docs/components/splitter.md +15 -15
  32. package/_docs/docs/components/table-column-fixed.md +20 -20
  33. package/_docs/docs/components/table-pagination.md +20 -20
  34. package/_docs/docs/components/table-splitter.md +20 -20
  35. package/_docs/docs/components/table-summary.md +20 -20
  36. package/_docs/docs/components/table.md +25 -25
  37. package/_docs/docs/components/thumbnail.md +18 -18
  38. package/_docs/docs/components/toolbar.md +9 -9
  39. package/_docs/docs/components/uploader-query.md +19 -19
  40. package/_docs/docs/components/uploader.md +16 -16
  41. package/_docs/docs/components/value-format.md +26 -26
  42. package/_docs/docs/index.md +1 -1
  43. package/_docs/docs/utils/alert.md +26 -26
  44. package/_docs/docs/utils/area.md +112 -112
  45. package/_docs/docs/utils/arr.md +80 -80
  46. package/_docs/docs/utils/auth.md +101 -101
  47. package/_docs/docs/utils/bus.md +18 -18
  48. package/_docs/docs/utils/confirm.md +31 -31
  49. package/_docs/docs/utils/copy.md +22 -22
  50. package/_docs/docs/utils/dialog.md +98 -98
  51. package/_docs/docs/utils/dict.md +50 -50
  52. package/_docs/docs/utils/dictOptions.md +27 -27
  53. package/_docs/docs/utils/form.md +33 -33
  54. package/_docs/docs/utils/getData.md +60 -60
  55. package/_docs/docs/utils/getFile.md +21 -21
  56. package/_docs/docs/utils/getImage.md +33 -33
  57. package/_docs/docs/utils/getTime.md +51 -51
  58. package/_docs/docs/utils/index.md +1 -1
  59. package/_docs/docs/utils/loading.md +18 -18
  60. package/_docs/docs/utils/notify.md +29 -29
  61. package/_docs/docs/utils/power.md +353 -353
  62. package/_docs/docs/utils/previewImage.md +11 -11
  63. package/_docs/docs/utils/price.md +45 -45
  64. package/_docs/docs/utils/rule.md +30 -30
  65. package/_docs/docs/utils/ruleValid.md +31 -31
  66. package/_docs/docs/utils/symbols.md +30 -30
  67. package/_docs/docs/utils/table.md +194 -194
  68. package/_docs/docs/utils/timestamp.md +27 -27
  69. package/_docs/docs/utils/toast.md +27 -27
  70. package/_docs/docs/utils/tree.md +174 -174
  71. package/_docs/docs/utils/uploader.md +29 -29
  72. package/_docs/package.json +11 -11
  73. package/components/column-title/index.vue +37 -37
  74. package/components/data/index.vue +20 -20
  75. package/components/dialog/index.vue +372 -372
  76. package/components/dragger/index.vue +203 -203
  77. package/components/drawer/index.vue +303 -303
  78. package/components/editor-code/index.vue +289 -289
  79. package/components/empty/index.vue +71 -71
  80. package/components/field-date/index.vue +850 -850
  81. package/components/field-date/methods.js +100 -100
  82. package/components/field-table/index.vue +1222 -1222
  83. package/components/field-text/index.vue +165 -165
  84. package/components/field-tree/index.vue +103 -81
  85. package/components/img/index.vue +202 -202
  86. package/components/input-number/index.vue +546 -546
  87. package/components/list-menu/index.vue +149 -149
  88. package/components/list-menu-item/index.vue +79 -79
  89. package/components/power-page/index.vue +92 -92
  90. package/components/price/index.vue +188 -188
  91. package/components/private/components/index.js +11 -11
  92. package/components/private/components/move-to-tree/index.vue +154 -154
  93. package/components/private/edit-power-data/index.vue +816 -816
  94. package/components/private/table-visible-columns-button/index.vue +109 -109
  95. package/components/render/index.vue +150 -150
  96. package/components/search/index.vue +222 -222
  97. package/components/search-item/index.vue +210 -210
  98. package/components/splitter/index.vue +415 -415
  99. package/components/table/index.vue +456 -456
  100. package/components/table-column-fixed/index.vue +112 -112
  101. package/components/table-pagination/index.vue +192 -192
  102. package/components/table-splitter/index.vue +360 -360
  103. package/components/table-summary/index.vue +110 -110
  104. package/components/thumbnail/index.vue +72 -72
  105. package/components/toolbar/container.vue +31 -31
  106. package/components/toolbar/index.vue +136 -136
  107. package/components/uploader/index.vue +158 -158
  108. package/components/uploader-query/index.vue +758 -758
  109. package/components/value-format/index.vue +274 -274
  110. package/configs/area3.js +1 -1
  111. package/docs/css/index.css +3 -3
  112. package/package.json +1 -1
  113. package/sass/common.scss +174 -174
  114. package/sass/index.scss +14 -14
  115. package/sass/line.scss +39 -39
  116. package/sass/quasar/btn.scss +46 -46
  117. package/sass/quasar/common.scss +3 -3
  118. package/sass/quasar/dialog.scss +7 -7
  119. package/sass/quasar/drawer.scss +6 -6
  120. package/sass/quasar/field.scss +243 -243
  121. package/sass/quasar/loading.scss +6 -6
  122. package/sass/quasar/menu.scss +8 -8
  123. package/sass/quasar/table.scss +150 -150
  124. package/sass/quasar/toolbar.scss +22 -22
  125. package/store/index.js +29 -29
  126. package/utils/$auth.js +127 -127
  127. package/utils/$form.js +56 -56
  128. package/utils/$power.js +1215 -1215
  129. package/utils/$rule.js +13 -13
  130. package/utils/$ruleValid.js +10 -10
  131. package/utils/$table.js +999 -999
  132. package/utils/$tree.js +713 -713
  133. package/utils/alert.js +12 -12
  134. package/utils/area.js +400 -400
  135. package/utils/arr.js +51 -51
  136. package/utils/bus.js +6 -6
  137. package/utils/config.js +52 -52
  138. package/utils/confirm.js +11 -11
  139. package/utils/copy.js +30 -30
  140. package/utils/dialog.js +36 -36
  141. package/utils/dict.js +21 -21
  142. package/utils/dictOptions.js +28 -28
  143. package/utils/getData.js +73 -73
  144. package/utils/getFile.js +40 -40
  145. package/utils/getImage.js +153 -153
  146. package/utils/getTime.js +106 -106
  147. package/utils/index.js +61 -61
  148. package/utils/loading.js +15 -15
  149. package/utils/notify.js +13 -13
  150. package/utils/previewImage.js +10 -10
  151. package/utils/price.js +18 -18
  152. package/utils/symbols.js +18 -18
  153. package/utils/timestamp.js +18 -18
  154. package/utils/toast.js +13 -13
  155. package/utils/uploader/aliyun.js +6 -6
  156. package/utils/uploader/local.js +8 -8
  157. package/utils/uploader/qiniu.js +321 -321
  158. package/utils/uploader.js +1059 -1059
  159. package/utils/useAuth.js +30 -30
  160. package/utils/useRouter.js +47 -47
  161. package/utils/useSearch.js +0 -6
  162. package/utils/useUploader.js +53 -53
package/utils/$table.js CHANGED
@@ -1,999 +1,999 @@
1
- import { ref, computed, provide, inject, watch } from 'vue'
2
- import { useQuasar } from 'quasar'
3
-
4
- import $n_has from 'lodash/has'
5
- import $n_get from 'lodash/get'
6
- import $n_cloneDeep from 'lodash/cloneDeep'
7
- import $n_merge from 'lodash/merge'
8
- import $n_isFunction from 'lodash/isFunction'
9
- import $n_findIndex from 'lodash/findIndex'
10
- import $n_uniq from 'lodash/uniq'
11
- import $n_concat from 'lodash/concat'
12
- import $n_isNil from 'lodash/isNil'
13
-
14
- import $n_router from '@netang/utils/vue/router'
15
-
16
- import $n_forEach from '@netang/utils/forEach'
17
- import $n_isValidArray from '@netang/utils/isValidArray'
18
- import $n_isValidString from '@netang/utils/isValidString'
19
- import $n_indexOf from '@netang/utils/indexOf'
20
- import $n_storage from '@netang/utils/storage'
21
-
22
- import $n_isRequired from '@netang/utils/isRequired'
23
- import $n_forIn from '@netang/utils/forIn'
24
- import $n_runAsync from '@netang/utils/runAsync'
25
- import $n_isValidObject from '@netang/utils/isValidObject'
26
- import $n_isValidValue from '@netang/utils/isValidValue'
27
- import $n_slash from '@netang/utils/slash'
28
- import $n_http from '@netang/utils/http'
29
-
30
- import $n_$power from './$power'
31
- import $n_dict from './dict'
32
- import $n_price from './price'
33
- import $n_getTime from './getTime'
34
-
35
- import { configs } from './config'
36
-
37
- import {
38
- // 设置单个搜索值
39
- setItemValue,
40
- // 从表格列获取原始值
41
- getRawData,
42
- // 获取参数
43
- getOptions,
44
- // 格式化值
45
- formatValue,
46
- } from './useSearch'
47
-
48
- import { NRenderKey, NPowerKey, NTableKey } from './symbols'
49
-
50
- const {
51
- // 表格配置
52
- tablesConfig,
53
- } = configs
54
-
55
- /**
56
- * 创建表格
57
- */
58
- function create(options) {
59
-
60
- // ==========【数据】=================================================================================================
61
-
62
- // quasar 对象
63
- const $q = useQuasar()
64
-
65
- // 每页显示行数选项
66
- const rowsPerPageOptions = [30, 40, 50, 100, 200, 500, 1000]
67
-
68
- // 获取参数
69
- const o = $n_merge({
70
- // 路由路径
71
- path: '',
72
- // 路由参数
73
- query: {},
74
- // 附加请求数据
75
- data: {},
76
- // 表格行唯一键值
77
- rowKey: 'id',
78
- // 选择类型, 可选值 none single(默认) multiple
79
- // selection: '',
80
- // 分隔栏, 可选值 horizontal vertical cell none
81
- separator: 'cell',
82
- // 初始已选数据
83
- selected: [],
84
- // 初始表格加载状态
85
- loading: false,
86
- // 表格列数据(对象数组)
87
- columns: [],
88
- // 初始可见列
89
- visibleColumns: [],
90
- // 表格行数据
91
- rows: [],
92
- // 表格翻页参数
93
- pagination: {
94
- // 页码
95
- page: 1,
96
- // 每页的数据条数
97
- rowsPerPage: rowsPerPageOptions[0],
98
- // 数据总数(服务器返回)
99
- rowsNumber: 1,
100
- // 排序字段
101
- sortBy: null,
102
- // sortBy: 'id',
103
- // 是否降序排列
104
- descending: true,
105
- },
106
- // 每页显示行数选项
107
- rowsPerPageOptions,
108
- // 自定义请求方法
109
- request: null,
110
- // 格式化单条数据
111
- formatRow: null,
112
- // http 设置
113
- httpSettings: {},
114
- // 是否开启初始搜素
115
- search: true,
116
- // 是否开启合计
117
- summary: false,
118
- // 从参数中获取搜索值
119
- searchFromQuery: true,
120
- // 是否显示宫格
121
- showGrid: true,
122
- // 是否显示可见列
123
- showVisibleColumns: true,
124
- // 是否开启缓存
125
- cache: true,
126
- // 是否刷新后清空已选数据
127
- refreshResetSelected: true,
128
-
129
- // 单击表格行事件
130
- rowClick: null,
131
- // 双击表格行事件
132
- rowDblClick: null,
133
- }, options)
134
-
135
- // 获取权限注入
136
- const $power = $n_has(options, '$power') ? options.$power : inject(NPowerKey)
137
- const hasPowr = !! $power
138
-
139
- // 获取渲染注入
140
- const $render = inject(NRenderKey)
141
- if (!! $render) {
142
- // 如果有表格传参, 则合并参数
143
- const tableProps = $n_get($render, 'props.tableProps')
144
- if ($n_isValidObject(tableProps)) {
145
- $n_merge(o, tableProps)
146
- }
147
- }
148
-
149
- // 获取选择类型(默认 single)
150
- if (! $n_has(o, 'selection') || ! $n_isValidString(o.selection)) {
151
- if (hasPowr) {
152
- o.selection = $n_get($power, 'powerPage.data.selection')
153
- if (! $n_isValidString(o.selection)) {
154
- o.selection = 'single'
155
- }
156
- } else {
157
- o.selection = 'single'
158
- }
159
- }
160
-
161
- // 获取权限路由
162
- const $route = $n_isValidString(o.path) ?
163
- // 如果为自定义路由
164
- $n_router.resolve({
165
- path: o.path,
166
- query: $n_isValidObject(o.query) ? o.query : {},
167
- })
168
- // 否则获取当前路由
169
- : (hasPowr ? $power.getRoute() : $n_router.getRoute())
170
-
171
- // 是否有权限按钮
172
- const hasPowerBtns = hasPowr ? $power.powerBtns.value.length : false
173
-
174
- // 表格已选数据
175
- const tableSelected = hasPowr ? $power.tableSelected : ref([])
176
- if ($n_isValidArray(o.selected)) {
177
- tableSelected.value = o.selected
178
- }
179
-
180
- // 是否开启缓存
181
- const isCache = !! o.cache
182
-
183
- // 缓存名
184
- const cacheName = $route.fullPath ? $route.fullPath : ($n_isValidString(o.cache) ? o.cache : '')
185
-
186
- // 表格列
187
- const tableColumns = []
188
-
189
- // 如果有权限按钮
190
- if (hasPowerBtns) {
191
- // 添加操作列
192
- o.columns.push({
193
- label: '操作',
194
- name: 'settings',
195
- })
196
- }
197
-
198
- // 表格图片标识数组
199
- const tableImgNames = ref([])
200
-
201
- // 设置列参数
202
- $n_forEach(o.columns, function(item) {
203
-
204
- if (
205
- ! $n_has(item, 'field')
206
- && $n_has(item, 'name')
207
- ) {
208
- item.field = item.name
209
- }
210
-
211
- if (! $n_has(item, 'align')) {
212
- item.align = 'left'
213
- }
214
-
215
- // 是否隐藏
216
- item.hide = $n_get(item, 'hide') === true
217
-
218
- // 如果有显示项
219
- if ($n_get(item, 'visible') !== false) {
220
- o.visibleColumns.push(item.field)
221
- }
222
-
223
- // 如果有时间戳
224
- if ($n_has(item, 'time')) {
225
- item.format = val => $n_getTime(val, { format: item.time === true ? `YYYY-MM-DD HH:mm` : item.time }, '-')
226
-
227
- // 如果有数据字典
228
- } else if ($n_has(item, 'dict')) {
229
- item.format = val => $n_dict(item.dict, val)
230
-
231
- // 如果有图片
232
- } else if ($n_has(item, 'img') && item.img === true) {
233
- tableImgNames.value.push(item.name)
234
-
235
- // 如果有价格
236
- } else if ($n_has(item, 'price')) {
237
- item.format = val => $n_price(val)
238
- }
239
-
240
- // 如果有路由
241
- if ($n_get(item, 'route')) {
242
- // 如果该值在当前路由路径中, 则显示
243
- if ($n_indexOf($route.fullPath, item.route) > -1) {
244
- tableColumns.push(item)
245
- }
246
-
247
- } else {
248
- tableColumns.push(item)
249
- }
250
- })
251
-
252
- // 获取可见列缓存
253
- const visibleColumnsCache = o.showVisibleColumns && isCache ? $n_storage.get('table:visible_columns:' + cacheName) : []
254
-
255
- // 表格可见列
256
- const tableVisibleColumns = ref(Array.isArray(visibleColumnsCache) ? visibleColumnsCache : $n_uniq([...o.visibleColumns]))
257
-
258
- // 表格加载状态
259
- const tableLoading = ref(o.loading)
260
-
261
- // 表格行数据
262
- const tableRows = ref(o.rows)
263
-
264
- // 表格翻页参数
265
- const tablePagination = ref($route.fullPath ? o.pagination : {})
266
-
267
- // 表格宫格
268
- const tableGrid = ref(o.showGrid && isCache ? $n_storage.get('table:grid:' + cacheName) === true : false)
269
-
270
- // 表格请求参数(将表格传参中的搜索参数剥离掉, 剩下的直接当做参数传递给服务器)
271
- let tableRequestQuery = {}
272
-
273
- // 是否请求表格合计
274
- let isRequestSummary = false
275
-
276
- // 表格合计
277
- const tableSummary = ref(null)
278
-
279
- // 表格选择类型
280
- const tableSelection = ref(o.selection)
281
-
282
- // 表格分隔栏
283
- const tableSeparator = ref(o.separator)
284
-
285
- const {
286
- // 原始参数
287
- rawQuery,
288
- // 原始表格搜索参数
289
- rawSearchOptions,
290
- // 原始表格搜索值(空表格搜索值, 用于搜索重置)
291
- rawTableSearchValue,
292
- // 首次表格搜索值(如果表格搜索参数中带了初始值, 则设置初始值)
293
- firstTableSearchValue,
294
- // 表格搜索值(如果表格搜索参数中带了初始值, 则设置初始值)
295
- } = getRawData(tableColumns, Object.assign({}, $route.query), o.searchFromQuery)
296
-
297
- // 表格搜索数据值
298
- const tableSearchValue = ref($route.fullPath ? firstTableSearchValue : [])
299
-
300
- // 表格搜索参数
301
- const tableSearchOptions = ref()
302
-
303
- // 是否已加载
304
- let _isTableLoaded = false
305
-
306
- // ==========【计算属性】=============================================================================================
307
-
308
- /**
309
- * 固定在表格右边的权限按钮列表
310
- */
311
- const tableFixedPowerBtns = ! hasPowerBtns ? ref([]) : computed(function () {
312
-
313
- const lists = []
314
-
315
- // 先格式化权限按钮列表
316
- $n_forEach($n_$power.formatBtns($power.powerBtns.value), function(item) {
317
- // 如果是固定按钮
318
- if (item.fixed) {
319
- lists.push(item)
320
- }
321
- })
322
-
323
- return lists
324
- })
325
-
326
- /**
327
- * 获取权限按钮中可双击的按钮
328
- */
329
- const tableDbClickPowerBtn = ! hasPowerBtns ? ref(null) : computed(function () {
330
- if (
331
- // 非手机模式
332
- ! $q.platform.is.mobile
333
- // 有权限列表
334
- && $n_isValidArray($power.powerBtns.value)
335
- ) {
336
- for (const item of $power.powerBtns.value) {
337
- if ($n_has(item, 'data.dbclick') === true) {
338
- return item
339
- }
340
- }
341
- }
342
- })
343
-
344
- /**
345
- * 是否显示固定在右边的权限按钮列表
346
- */
347
- const showTableFixed = computed(function () {
348
- return $n_indexOf(tableVisibleColumns.value, 'settings') > -1
349
- })
350
-
351
- // ==========【监听数据】=============================================================================================
352
-
353
- /**
354
- * 监听表格宫格模式
355
- */
356
- if (o.showGrid && isCache) {
357
- watch(tableGrid, function(val) {
358
-
359
- // 设置宫格模式缓存(永久缓存)
360
- // #if ! IS_DEV
361
- $n_storage.set('table:grid:' + cacheName, val, 0)
362
- // #endif
363
- })
364
- }
365
-
366
- /**
367
- * 监听表格可见列
368
- */
369
- if (o.showVisibleColumns && isCache) {
370
- watch(tableVisibleColumns, function(val) {
371
-
372
- // 设置可见列缓存(永久缓存)
373
- // #if ! IS_DEV
374
- $n_storage.set('table:visible_columns:' + cacheName, val, 0)
375
- // #endif
376
- })
377
- }
378
-
379
- /**
380
- * 监听固定在右边的权限按钮列表
381
- */
382
- if (hasPowerBtns) {
383
- watch(tableFixedPowerBtns, function (lists) {
384
-
385
- const index = $n_indexOf(tableVisibleColumns.value, 'settings')
386
-
387
- // 如果有固定在右边的权限按钮列表
388
- if ($n_isValidArray(lists)) {
389
-
390
- // 如果设置不在可见列中
391
- if (index === -1) {
392
-
393
- // 如果非手机模式
394
- if (! $q.platform.is.mobile) {
395
-
396
- // 则将设置加入可见列中
397
- tableVisibleColumns.value.push('settings')
398
- }
399
-
400
- // 否则在可见列中 && 如果是手机模式
401
- } else if ($q.platform.is.mobile) {
402
-
403
- // 则将设置从可见列中删除
404
- tableVisibleColumns.value.splice(index, 1)
405
- }
406
-
407
- // 否则如果设置在可见列中
408
- } else if (index > -1) {
409
-
410
- // 则将设置从可见列中删除
411
- tableVisibleColumns.value.splice(index, 1)
412
- }
413
-
414
- }, {
415
- // 立即执行
416
- immediate: true,
417
- })
418
- }
419
-
420
- // ==========【方法】================================================================================================
421
-
422
- /**
423
- * 设置表格传参
424
- */
425
- function setQuery(query) {
426
-
427
- if ($n_isValidObject(query)) {
428
-
429
- query = $n_cloneDeep(query)
430
-
431
- // 搜索参数键值数组
432
- const searchQueryKey = []
433
-
434
- // 搜索键值数组
435
- const NSearchKeys = []
436
- // 搜索数组
437
- const NSearchValues = []
438
-
439
- // 参数中是否有自定义搜索参数
440
- const hasNSearch = $n_has(query, 'n_search')
441
- if (hasNSearch) {
442
- // 删除在搜索中存在的参数键值
443
- $n_forIn(query.n_search, function (item, key) {
444
- if ($n_has(query, key)) {
445
- delete query[key]
446
- }
447
- })
448
- }
449
-
450
- // 如果允许从参数中获取搜索值
451
- if (o.searchFromQuery) {
452
-
453
- $n_forEach(rawSearchOptions, function (item, index) {
454
-
455
- const valueItem = tableSearchValue.value[index]
456
-
457
- // 如果传参在搜素 n_search 参数中
458
- if (hasNSearch && $n_has(query.n_search, item.name)) {
459
- const newSearchItem = query.n_search[item.name]
460
- if ($n_isValidArray(newSearchItem)) {
461
- valueItem[0].compare = newSearchItem[0].compare
462
- valueItem[0].value = newSearchItem[0].value
463
-
464
- if (newSearchItem.length > 1) {
465
- valueItem[1].compare = newSearchItem[1].compare
466
- valueItem[1].value = newSearchItem[1].value
467
- }
468
- }
469
- // 设置搜索的 key
470
- NSearchKeys.push(item.name)
471
-
472
- // 如果传参在搜索参数中
473
- } else if ($n_has(query, item.name)) {
474
- // 设置单个搜索值
475
- setItemValue(valueItem, $n_isRequired(query[item.name]) ? query[item.name] : '')
476
- // 设置参数中搜索的 key
477
- searchQueryKey.push(item.name)
478
- }
479
- })
480
-
481
- $n_forEach(searchQueryKey, function (key) {
482
- delete query[key]
483
- })
484
-
485
- if (hasNSearch) {
486
- $n_forIn(query.n_search, function(item, key) {
487
- if (
488
- NSearchKeys.indexOf(key) === -1
489
- && $n_isValidArray(item)
490
- ) {
491
- item[0].field = key
492
- NSearchValues.push(item[0])
493
-
494
- if (item.length > 1) {
495
- item[1].field = key
496
- NSearchValues.push(item[1])
497
- }
498
- }
499
- })
500
- }
501
-
502
- } else {
503
- $n_forIn(query.n_search, function(item, key) {
504
- if ($n_isValidArray(item)) {
505
- item[0].field = key
506
- NSearchValues.push(item[0])
507
- if (item.length > 1) {
508
- item[1].field = key
509
- NSearchValues.push(item[1])
510
- }
511
- }
512
- })
513
- }
514
-
515
- if (NSearchValues.length) {
516
- query.n_search = NSearchValues
517
- } else if (hasNSearch) {
518
- delete query.n_search
519
- }
520
-
521
- tableRequestQuery = query
522
- return
523
- }
524
-
525
- tableRequestQuery = {}
526
- }
527
-
528
-
529
- /**
530
- * 表格是否已加载
531
- */
532
- function isTableLoaded() {
533
- return _isTableLoaded
534
- }
535
-
536
- /**
537
- * 表格加载(只加载一次)
538
- */
539
- async function tableLoad() {
540
-
541
- // 如果表格已加载过了
542
- if (_isTableLoaded) {
543
- // 则无任何操作
544
- return
545
- }
546
-
547
- // 表格重新加载
548
- await tableReload()
549
- }
550
-
551
- /**
552
- * 表格重新加载
553
- */
554
- async function tableReload() {
555
-
556
- // 表格已加载
557
- _isTableLoaded = true
558
-
559
- if (! $route.fullPath) {
560
- return
561
- }
562
-
563
- // 请求表格合计
564
- if (o.summary) {
565
- isRequestSummary = true
566
- }
567
-
568
- // 请求表格数据
569
- // $tableRef?.requestServerInteraction({
570
- // pagination: o.pagination,
571
- // })
572
- await tableRequest({
573
- pagination: o.pagination,
574
- })
575
-
576
- // 清空表格已选数据
577
- if (o.refreshResetSelected) {
578
- tableSelected.value = []
579
- }
580
- }
581
-
582
- /**
583
- * 表格刷新
584
- */
585
- async function tableRefresh() {
586
-
587
- if (! $route.fullPath) {
588
- return
589
- }
590
-
591
- // 请求表格合计
592
- if (o.summary) {
593
- isRequestSummary = true
594
- }
595
-
596
- // 请求表格数据
597
- // $tableRef.requestServerInteraction()
598
- await tableRequest({
599
- pagination: tablePagination.value,
600
- })
601
-
602
- // 清空表格已选数据
603
- if (o.refreshResetSelected) {
604
- tableSelected.value = []
605
- }
606
- }
607
-
608
- /**
609
- * 表格搜索重置
610
- */
611
- function tableSearchReset(reload = true) {
612
-
613
- const newValue = []
614
-
615
- $n_forEach(rawSearchOptions, function (item, index) {
616
- // 如果该搜索条件是隐藏的
617
- if (item.hide) {
618
- newValue.push(tableSearchValue.value[index])
619
- // 否则为初始值
620
- } else {
621
- newValue.push(rawTableSearchValue[index])
622
- }
623
- })
624
-
625
- // 还原表格搜索数据
626
- tableSearchValue.value = $n_cloneDeep(newValue)
627
-
628
- // 表格重新加载
629
- if (reload) {
630
- tableReload().finally()
631
- }
632
- }
633
-
634
- /**
635
- * 获取表格请求数据
636
- */
637
- function getTableRequestData(props, isSummary = undefined) {
638
-
639
- // 解构数据
640
- const {
641
- // filter,
642
- pagination: {
643
- // 页码
644
- page,
645
- // 每页的数据条数
646
- rowsPerPage,
647
- // 排序字段
648
- sortBy,
649
- // 是否降序排列
650
- descending,
651
- }
652
- } = props
653
-
654
- // 请求数据
655
- const data = {
656
- // 页码
657
- page,
658
- // 每页的数据条数
659
- per_page: rowsPerPage,
660
- }
661
-
662
- // 如果排序字段是有效值
663
- if ($n_isValidValue(sortBy)) {
664
- Object.assign(data, {
665
- // 排序字段
666
- order_by: sortBy,
667
- // 是否降序排列
668
- is_desc: descending ? 1 : 0,
669
- })
670
- }
671
-
672
- // 合并参数
673
- $n_forIn(Object.assign({}, rawQuery, tableRequestQuery, o.data), function(value, key) {
674
- // 如果有值
675
- if ($n_isRequired(value)) {
676
- data[key] = value
677
- }
678
- })
679
-
680
- // 获取搜索值
681
- const search = formatValue(rawSearchOptions, tableSearchValue.value)
682
- if ($n_isValidArray(search)) {
683
- data.n_search = $n_has(data, 'n_search') ? $n_concat(data.n_search, search) : search
684
- }
685
-
686
- if ($n_isNil(isSummary)) {
687
- isSummary = isRequestSummary
688
- }
689
-
690
- // 如果请求表格合计
691
- if (isSummary) {
692
- data.summary = 1
693
- }
694
-
695
- return data
696
- }
697
-
698
- /**
699
- * 请求数据
700
- */
701
- async function tableRequest(props) {
702
-
703
- // 加载
704
- tableLoading.value = true
705
-
706
- // 解构数据
707
- const {
708
- // filter,
709
- pagination: {
710
- // 页码
711
- page,
712
- // 每页的数据条数
713
- rowsPerPage,
714
- // 排序字段
715
- sortBy,
716
- // 是否降序排列
717
- descending,
718
- }
719
- } = props
720
-
721
- // http 请求参数
722
- const httpOptions = Object.assign({
723
- // 请求数据
724
- url: $route.path,
725
- // 请求数据
726
- data: getTableRequestData(props, isRequestSummary),
727
- // ~~~~~~ 先开启防抖, 如果后期遇到表格加载不出来的情况, 再关闭防抖
728
- // 关闭防抖(允许重复请求)
729
- // debounce: false,
730
- }, o.httpSettings)
731
-
732
- const { status, data: res } = $n_isFunction(o.request)
733
- // 如果有自定义请求方法
734
- ? await $n_runAsync(o.request)({
735
- // http 请求参数
736
- httpOptions,
737
- // 表格声明属性
738
- props,
739
- // 表格行数据
740
- rows: tableRows,
741
- // 表格已选数据
742
- selected: tableSelected,
743
- })
744
- // 否则请求服务器
745
- : await $n_http(httpOptions)
746
-
747
- // 请求成功
748
- if (status) {
749
-
750
- const {
751
- // 返回数据
752
- rows,
753
- // 数据总数
754
- total,
755
- } = res
756
-
757
- // 如果请求表格合计
758
- if (isRequestSummary) {
759
- const summary = $n_get(res, 'summary')
760
- tableSummary.value = $n_isValidObject(summary) ? summary : null
761
- }
762
-
763
- // 更新页码
764
- tablePagination.value.page = page
765
- // 更新每页的数据条数
766
- tablePagination.value.rowsPerPage = rowsPerPage
767
- // 更新数据总数
768
- tablePagination.value.rowsNumber = total
769
- // 更新排序字段
770
- tablePagination.value.sortBy = sortBy
771
- // 更新是否降序排列
772
- tablePagination.value.descending = descending
773
-
774
- // 格式化单条数据
775
- if ($n_isFunction(o.formatRow)) {
776
- $n_forEach(rows, function(row) {
777
- o.formatRow({
778
- row,
779
- rows: tableRows,
780
- selected: tableSelected,
781
- })
782
- })
783
- }
784
-
785
- // 清除现有数据并添加新数据
786
- tableRows.value.splice(0, tableRows.value.length, ...rows)
787
- }
788
-
789
- // 取消请求表格合计
790
- isRequestSummary = false
791
-
792
- // 取消加载
793
- tableLoading.value = false
794
- }
795
-
796
- /**
797
- * 单击表格行
798
- */
799
- function _tableRowClick(e, row) {
800
-
801
- // 如果选择类型为无
802
- if (tableSelection.value === 'none') {
803
- // 则无任何操作
804
- return
805
- }
806
-
807
- const opt = {}
808
- opt[o.rowKey] = row[o.rowKey]
809
-
810
- // 获取当前数据索引
811
- const itemIndex = $n_findIndex(tableSelected.value, opt)
812
-
813
- // 如果不存在, 则添加
814
- if (itemIndex === -1) {
815
-
816
- // 如果选择类型为单选
817
- if (tableSelection.value === 'single') {
818
- tableSelected.value = [ row ]
819
-
820
- // 否则为多选
821
- } else {
822
- tableSelected.value.push(row)
823
- }
824
-
825
- // 否则删除
826
- } else {
827
- tableSelected.value.splice(itemIndex, 1)
828
- }
829
- }
830
- function tableRowClick(...e) {
831
-
832
- // 单击表格行
833
- _tableRowClick(...e)
834
-
835
- // 如果有自定义单击事件
836
- if ($n_isFunction(o.rowClick)) {
837
- o.rowClick(...e)
838
- }
839
- }
840
-
841
- /**
842
- * 双击表格行
843
- */
844
- function _tableRowDblclick(e, row) {
845
-
846
- // 如果选择类型为无
847
- if (tableSelection.value === 'none') {
848
- // 则无任何操作
849
- return
850
- }
851
-
852
- if (
853
- // 有权限
854
- hasPowr
855
- // 有双击的权限按钮
856
- && tableDbClickPowerBtn.value
857
- ) {
858
- $power.powerBtnClick(tableDbClickPowerBtn.value, [ row ])
859
- }
860
- }
861
- function tableRowDblclick(...e) {
862
-
863
- // 双击表格行
864
- _tableRowDblclick(...e)
865
-
866
- // 如果有自定义双击表格行事件
867
- if ($n_isFunction(o.tableRowDblclick)) {
868
- o.tableRowDblclick(...e)
869
- }
870
- }
871
-
872
- /**
873
- * 设置表格搜索参数
874
- */
875
- async function setTableSearchOptions(format) {
876
- tableSearchOptions.value = await getOptions(rawSearchOptions, format)
877
- }
878
-
879
- /**
880
- * 是否有表格搜索值
881
- */
882
- function hasTableSearchValue() {
883
- return !! formatValue(rawSearchOptions, tableSearchValue.value).length
884
- }
885
-
886
- // 如果开启搜索
887
- if (o.search) {
888
- // 设置表格搜索参数
889
- setTableSearchOptions()
890
- .finally()
891
- }
892
-
893
- // ==========【返回】=================================================================================================
894
-
895
- const resTable = {
896
- // 当前路由全路径
897
- routeFullPath: $route.fullPath,
898
- // 当前路由路径
899
- routePath: $route.path,
900
- // 当前路由参数
901
- routeQuery: $route.query,
902
- // 获取当前路由
903
- getRoute() {
904
- return $route
905
- },
906
-
907
- // 表格加载状态
908
- tableLoading,
909
- // 表格行唯一键值
910
- tableRowKey: o.rowKey,
911
- // 表格选择类型
912
- tableSelection,
913
- // 表格分隔栏
914
- tableSeparator,
915
- // 表格每页显示行数选项
916
- tableRowsPerPageOptions: rowsPerPageOptions,
917
- // 表格列数据(对象数组)
918
- tableColumns,
919
- // 表格可见列
920
- tableVisibleColumns,
921
- // 表格行数据
922
- tableRows,
923
- // 表格翻页参数
924
- tablePagination,
925
- // 表格已选数据
926
- tableSelected,
927
- // 固定在右边的权限按钮列表
928
- tableFixedPowerBtns,
929
- // 是否显示固定在右边的权限按钮列表
930
- showTableFixed,
931
- // 表格图片标识
932
- tableImgNames,
933
-
934
- // 表格宫格
935
- tableGrid,
936
- // 表格合计
937
- tableSummary,
938
- // 表格搜索数据
939
- tableSearchValue,
940
- // 表格搜索参数
941
- tableSearchOptions,
942
-
943
- // 设置表格传参
944
- setQuery,
945
- // 表格是否已加载
946
- isTableLoaded,
947
- // 表格加载(只加载一次)
948
- tableLoad,
949
- // 表格重新加载
950
- tableReload,
951
- // 表格刷新
952
- tableRefresh,
953
- // 表格搜索重置
954
- tableSearchReset,
955
- // 获取表格请求数据
956
- getTableRequestData,
957
- // 表格请求数据
958
- tableRequest,
959
- // 表格单击表格行
960
- tableRowClick,
961
- // 表格双击表格行
962
- tableRowDblclick,
963
- // 设置表格搜索参数
964
- setTableSearchOptions,
965
-
966
- // 是否有表格搜索值
967
- hasTableSearchValue,
968
- }
969
-
970
- if (hasPowr) {
971
- $power.update(function(data, _data) {
972
- _data.$table = resTable
973
- })
974
- }
975
-
976
- // 提供可以被后代组件注入的值
977
- provide(NTableKey, resTable)
978
-
979
- return resTable
980
- }
981
-
982
- /**
983
- * 获取表格配置
984
- */
985
- function config(routePath, path, defaultValue) {
986
- return $n_cloneDeep($n_get(tablesConfig, $n_slash(routePath, 'start', false) + (path ? '.' + path : ''), defaultValue))
987
- }
988
-
989
- /**
990
- * 业务表格
991
- */
992
- const $table = {
993
- // 创建表格
994
- create,
995
- // 获取表格配置
996
- config,
997
- }
998
-
999
- export default $table
1
+ import { ref, computed, provide, inject, watch } from 'vue'
2
+ import { useQuasar } from 'quasar'
3
+
4
+ import $n_has from 'lodash/has'
5
+ import $n_get from 'lodash/get'
6
+ import $n_cloneDeep from 'lodash/cloneDeep'
7
+ import $n_merge from 'lodash/merge'
8
+ import $n_isFunction from 'lodash/isFunction'
9
+ import $n_findIndex from 'lodash/findIndex'
10
+ import $n_uniq from 'lodash/uniq'
11
+ import $n_concat from 'lodash/concat'
12
+ import $n_isNil from 'lodash/isNil'
13
+
14
+ import $n_router from '@netang/utils/vue/router'
15
+
16
+ import $n_forEach from '@netang/utils/forEach'
17
+ import $n_isValidArray from '@netang/utils/isValidArray'
18
+ import $n_isValidString from '@netang/utils/isValidString'
19
+ import $n_indexOf from '@netang/utils/indexOf'
20
+ import $n_storage from '@netang/utils/storage'
21
+
22
+ import $n_isRequired from '@netang/utils/isRequired'
23
+ import $n_forIn from '@netang/utils/forIn'
24
+ import $n_runAsync from '@netang/utils/runAsync'
25
+ import $n_isValidObject from '@netang/utils/isValidObject'
26
+ import $n_isValidValue from '@netang/utils/isValidValue'
27
+ import $n_slash from '@netang/utils/slash'
28
+ import $n_http from '@netang/utils/http'
29
+
30
+ import $n_$power from './$power'
31
+ import $n_dict from './dict'
32
+ import $n_price from './price'
33
+ import $n_getTime from './getTime'
34
+
35
+ import { configs } from './config'
36
+
37
+ import {
38
+ // 设置单个搜索值
39
+ setItemValue,
40
+ // 从表格列获取原始值
41
+ getRawData,
42
+ // 获取参数
43
+ getOptions,
44
+ // 格式化值
45
+ formatValue,
46
+ } from './useSearch'
47
+
48
+ import { NRenderKey, NPowerKey, NTableKey } from './symbols'
49
+
50
+ const {
51
+ // 表格配置
52
+ tablesConfig,
53
+ } = configs
54
+
55
+ /**
56
+ * 创建表格
57
+ */
58
+ function create(options) {
59
+
60
+ // ==========【数据】=================================================================================================
61
+
62
+ // quasar 对象
63
+ const $q = useQuasar()
64
+
65
+ // 每页显示行数选项
66
+ const rowsPerPageOptions = [30, 40, 50, 100, 200, 500, 1000]
67
+
68
+ // 获取参数
69
+ const o = $n_merge({
70
+ // 路由路径
71
+ path: '',
72
+ // 路由参数
73
+ query: {},
74
+ // 附加请求数据
75
+ data: {},
76
+ // 表格行唯一键值
77
+ rowKey: 'id',
78
+ // 选择类型, 可选值 none single(默认) multiple
79
+ // selection: '',
80
+ // 分隔栏, 可选值 horizontal vertical cell none
81
+ separator: 'cell',
82
+ // 初始已选数据
83
+ selected: [],
84
+ // 初始表格加载状态
85
+ loading: false,
86
+ // 表格列数据(对象数组)
87
+ columns: [],
88
+ // 初始可见列
89
+ visibleColumns: [],
90
+ // 表格行数据
91
+ rows: [],
92
+ // 表格翻页参数
93
+ pagination: {
94
+ // 页码
95
+ page: 1,
96
+ // 每页的数据条数
97
+ rowsPerPage: rowsPerPageOptions[0],
98
+ // 数据总数(服务器返回)
99
+ rowsNumber: 1,
100
+ // 排序字段
101
+ sortBy: null,
102
+ // sortBy: 'id',
103
+ // 是否降序排列
104
+ descending: true,
105
+ },
106
+ // 每页显示行数选项
107
+ rowsPerPageOptions,
108
+ // 自定义请求方法
109
+ request: null,
110
+ // 格式化单条数据
111
+ formatRow: null,
112
+ // http 设置
113
+ httpSettings: {},
114
+ // 是否开启初始搜素
115
+ search: true,
116
+ // 是否开启合计
117
+ summary: false,
118
+ // 从参数中获取搜索值
119
+ searchFromQuery: true,
120
+ // 是否显示宫格
121
+ showGrid: true,
122
+ // 是否显示可见列
123
+ showVisibleColumns: true,
124
+ // 是否开启缓存
125
+ cache: true,
126
+ // 是否刷新后清空已选数据
127
+ refreshResetSelected: true,
128
+
129
+ // 单击表格行事件
130
+ rowClick: null,
131
+ // 双击表格行事件
132
+ rowDblClick: null,
133
+ }, options)
134
+
135
+ // 获取权限注入
136
+ const $power = $n_has(options, '$power') ? options.$power : inject(NPowerKey)
137
+ const hasPowr = !! $power
138
+
139
+ // 获取渲染注入
140
+ const $render = inject(NRenderKey)
141
+ if (!! $render) {
142
+ // 如果有表格传参, 则合并参数
143
+ const tableProps = $n_get($render, 'props.tableProps')
144
+ if ($n_isValidObject(tableProps)) {
145
+ $n_merge(o, tableProps)
146
+ }
147
+ }
148
+
149
+ // 获取选择类型(默认 single)
150
+ if (! $n_has(o, 'selection') || ! $n_isValidString(o.selection)) {
151
+ if (hasPowr) {
152
+ o.selection = $n_get($power, 'powerPage.data.selection')
153
+ if (! $n_isValidString(o.selection)) {
154
+ o.selection = 'single'
155
+ }
156
+ } else {
157
+ o.selection = 'single'
158
+ }
159
+ }
160
+
161
+ // 获取权限路由
162
+ const $route = $n_isValidString(o.path) ?
163
+ // 如果为自定义路由
164
+ $n_router.resolve({
165
+ path: o.path,
166
+ query: $n_isValidObject(o.query) ? o.query : {},
167
+ })
168
+ // 否则获取当前路由
169
+ : (hasPowr ? $power.getRoute() : $n_router.getRoute())
170
+
171
+ // 是否有权限按钮
172
+ const hasPowerBtns = hasPowr ? $power.powerBtns.value.length : false
173
+
174
+ // 表格已选数据
175
+ const tableSelected = hasPowr ? $power.tableSelected : ref([])
176
+ if ($n_isValidArray(o.selected)) {
177
+ tableSelected.value = o.selected
178
+ }
179
+
180
+ // 是否开启缓存
181
+ const isCache = !! o.cache
182
+
183
+ // 缓存名
184
+ const cacheName = $route.fullPath ? $route.fullPath : ($n_isValidString(o.cache) ? o.cache : '')
185
+
186
+ // 表格列
187
+ const tableColumns = []
188
+
189
+ // 如果有权限按钮
190
+ if (hasPowerBtns) {
191
+ // 添加操作列
192
+ o.columns.push({
193
+ label: '操作',
194
+ name: 'settings',
195
+ })
196
+ }
197
+
198
+ // 表格图片标识数组
199
+ const tableImgNames = ref([])
200
+
201
+ // 设置列参数
202
+ $n_forEach(o.columns, function(item) {
203
+
204
+ if (
205
+ ! $n_has(item, 'field')
206
+ && $n_has(item, 'name')
207
+ ) {
208
+ item.field = item.name
209
+ }
210
+
211
+ if (! $n_has(item, 'align')) {
212
+ item.align = 'left'
213
+ }
214
+
215
+ // 是否隐藏
216
+ item.hide = $n_get(item, 'hide') === true
217
+
218
+ // 如果有显示项
219
+ if ($n_get(item, 'visible') !== false) {
220
+ o.visibleColumns.push(item.field)
221
+ }
222
+
223
+ // 如果有时间戳
224
+ if ($n_has(item, 'time')) {
225
+ item.format = val => $n_getTime(val, { format: item.time === true ? `YYYY-MM-DD HH:mm` : item.time }, '-')
226
+
227
+ // 如果有数据字典
228
+ } else if ($n_has(item, 'dict')) {
229
+ item.format = val => $n_dict(item.dict, val)
230
+
231
+ // 如果有图片
232
+ } else if ($n_has(item, 'img') && item.img === true) {
233
+ tableImgNames.value.push(item.name)
234
+
235
+ // 如果有价格
236
+ } else if ($n_has(item, 'price')) {
237
+ item.format = val => $n_price(val)
238
+ }
239
+
240
+ // 如果有路由
241
+ if ($n_get(item, 'route')) {
242
+ // 如果该值在当前路由路径中, 则显示
243
+ if ($n_indexOf($route.fullPath, item.route) > -1) {
244
+ tableColumns.push(item)
245
+ }
246
+
247
+ } else {
248
+ tableColumns.push(item)
249
+ }
250
+ })
251
+
252
+ // 获取可见列缓存
253
+ const visibleColumnsCache = o.showVisibleColumns && isCache ? $n_storage.get('table:visible_columns:' + cacheName) : []
254
+
255
+ // 表格可见列
256
+ const tableVisibleColumns = ref(Array.isArray(visibleColumnsCache) ? visibleColumnsCache : $n_uniq([...o.visibleColumns]))
257
+
258
+ // 表格加载状态
259
+ const tableLoading = ref(o.loading)
260
+
261
+ // 表格行数据
262
+ const tableRows = ref(o.rows)
263
+
264
+ // 表格翻页参数
265
+ const tablePagination = ref($route.fullPath ? o.pagination : {})
266
+
267
+ // 表格宫格
268
+ const tableGrid = ref(o.showGrid && isCache ? $n_storage.get('table:grid:' + cacheName) === true : false)
269
+
270
+ // 表格请求参数(将表格传参中的搜索参数剥离掉, 剩下的直接当做参数传递给服务器)
271
+ let tableRequestQuery = {}
272
+
273
+ // 是否请求表格合计
274
+ let isRequestSummary = false
275
+
276
+ // 表格合计
277
+ const tableSummary = ref(null)
278
+
279
+ // 表格选择类型
280
+ const tableSelection = ref(o.selection)
281
+
282
+ // 表格分隔栏
283
+ const tableSeparator = ref(o.separator)
284
+
285
+ const {
286
+ // 原始参数
287
+ rawQuery,
288
+ // 原始表格搜索参数
289
+ rawSearchOptions,
290
+ // 原始表格搜索值(空表格搜索值, 用于搜索重置)
291
+ rawTableSearchValue,
292
+ // 首次表格搜索值(如果表格搜索参数中带了初始值, 则设置初始值)
293
+ firstTableSearchValue,
294
+ // 表格搜索值(如果表格搜索参数中带了初始值, 则设置初始值)
295
+ } = getRawData(tableColumns, Object.assign({}, $route.query), o.searchFromQuery)
296
+
297
+ // 表格搜索数据值
298
+ const tableSearchValue = ref($route.fullPath ? firstTableSearchValue : [])
299
+
300
+ // 表格搜索参数
301
+ const tableSearchOptions = ref()
302
+
303
+ // 是否已加载
304
+ let _isTableLoaded = false
305
+
306
+ // ==========【计算属性】=============================================================================================
307
+
308
+ /**
309
+ * 固定在表格右边的权限按钮列表
310
+ */
311
+ const tableFixedPowerBtns = ! hasPowerBtns ? ref([]) : computed(function () {
312
+
313
+ const lists = []
314
+
315
+ // 先格式化权限按钮列表
316
+ $n_forEach($n_$power.formatBtns($power.powerBtns.value), function(item) {
317
+ // 如果是固定按钮
318
+ if (item.fixed) {
319
+ lists.push(item)
320
+ }
321
+ })
322
+
323
+ return lists
324
+ })
325
+
326
+ /**
327
+ * 获取权限按钮中可双击的按钮
328
+ */
329
+ const tableDbClickPowerBtn = ! hasPowerBtns ? ref(null) : computed(function () {
330
+ if (
331
+ // 非手机模式
332
+ ! $q.platform.is.mobile
333
+ // 有权限列表
334
+ && $n_isValidArray($power.powerBtns.value)
335
+ ) {
336
+ for (const item of $power.powerBtns.value) {
337
+ if ($n_has(item, 'data.dbclick') === true) {
338
+ return item
339
+ }
340
+ }
341
+ }
342
+ })
343
+
344
+ /**
345
+ * 是否显示固定在右边的权限按钮列表
346
+ */
347
+ const showTableFixed = computed(function () {
348
+ return $n_indexOf(tableVisibleColumns.value, 'settings') > -1
349
+ })
350
+
351
+ // ==========【监听数据】=============================================================================================
352
+
353
+ /**
354
+ * 监听表格宫格模式
355
+ */
356
+ if (o.showGrid && isCache) {
357
+ watch(tableGrid, function(val) {
358
+
359
+ // 设置宫格模式缓存(永久缓存)
360
+ // #if ! IS_DEV
361
+ $n_storage.set('table:grid:' + cacheName, val, 0)
362
+ // #endif
363
+ })
364
+ }
365
+
366
+ /**
367
+ * 监听表格可见列
368
+ */
369
+ if (o.showVisibleColumns && isCache) {
370
+ watch(tableVisibleColumns, function(val) {
371
+
372
+ // 设置可见列缓存(永久缓存)
373
+ // #if ! IS_DEV
374
+ $n_storage.set('table:visible_columns:' + cacheName, val, 0)
375
+ // #endif
376
+ })
377
+ }
378
+
379
+ /**
380
+ * 监听固定在右边的权限按钮列表
381
+ */
382
+ if (hasPowerBtns) {
383
+ watch(tableFixedPowerBtns, function (lists) {
384
+
385
+ const index = $n_indexOf(tableVisibleColumns.value, 'settings')
386
+
387
+ // 如果有固定在右边的权限按钮列表
388
+ if ($n_isValidArray(lists)) {
389
+
390
+ // 如果设置不在可见列中
391
+ if (index === -1) {
392
+
393
+ // 如果非手机模式
394
+ if (! $q.platform.is.mobile) {
395
+
396
+ // 则将设置加入可见列中
397
+ tableVisibleColumns.value.push('settings')
398
+ }
399
+
400
+ // 否则在可见列中 && 如果是手机模式
401
+ } else if ($q.platform.is.mobile) {
402
+
403
+ // 则将设置从可见列中删除
404
+ tableVisibleColumns.value.splice(index, 1)
405
+ }
406
+
407
+ // 否则如果设置在可见列中
408
+ } else if (index > -1) {
409
+
410
+ // 则将设置从可见列中删除
411
+ tableVisibleColumns.value.splice(index, 1)
412
+ }
413
+
414
+ }, {
415
+ // 立即执行
416
+ immediate: true,
417
+ })
418
+ }
419
+
420
+ // ==========【方法】================================================================================================
421
+
422
+ /**
423
+ * 设置表格传参
424
+ */
425
+ function setQuery(query) {
426
+
427
+ if ($n_isValidObject(query)) {
428
+
429
+ query = $n_cloneDeep(query)
430
+
431
+ // 搜索参数键值数组
432
+ const searchQueryKey = []
433
+
434
+ // 搜索键值数组
435
+ const NSearchKeys = []
436
+ // 搜索数组
437
+ const NSearchValues = []
438
+
439
+ // 参数中是否有自定义搜索参数
440
+ const hasNSearch = $n_has(query, 'n_search')
441
+ if (hasNSearch) {
442
+ // 删除在搜索中存在的参数键值
443
+ $n_forIn(query.n_search, function (item, key) {
444
+ if ($n_has(query, key)) {
445
+ delete query[key]
446
+ }
447
+ })
448
+ }
449
+
450
+ // 如果允许从参数中获取搜索值
451
+ if (o.searchFromQuery) {
452
+
453
+ $n_forEach(rawSearchOptions, function (item, index) {
454
+
455
+ const valueItem = tableSearchValue.value[index]
456
+
457
+ // 如果传参在搜素 n_search 参数中
458
+ if (hasNSearch && $n_has(query.n_search, item.name)) {
459
+ const newSearchItem = query.n_search[item.name]
460
+ if ($n_isValidArray(newSearchItem)) {
461
+ valueItem[0].compare = newSearchItem[0].compare
462
+ valueItem[0].value = newSearchItem[0].value
463
+
464
+ if (newSearchItem.length > 1) {
465
+ valueItem[1].compare = newSearchItem[1].compare
466
+ valueItem[1].value = newSearchItem[1].value
467
+ }
468
+ }
469
+ // 设置搜索的 key
470
+ NSearchKeys.push(item.name)
471
+
472
+ // 如果传参在搜索参数中
473
+ } else if ($n_has(query, item.name)) {
474
+ // 设置单个搜索值
475
+ setItemValue(valueItem, $n_isRequired(query[item.name]) ? query[item.name] : '')
476
+ // 设置参数中搜索的 key
477
+ searchQueryKey.push(item.name)
478
+ }
479
+ })
480
+
481
+ $n_forEach(searchQueryKey, function (key) {
482
+ delete query[key]
483
+ })
484
+
485
+ if (hasNSearch) {
486
+ $n_forIn(query.n_search, function(item, key) {
487
+ if (
488
+ NSearchKeys.indexOf(key) === -1
489
+ && $n_isValidArray(item)
490
+ ) {
491
+ item[0].field = key
492
+ NSearchValues.push(item[0])
493
+
494
+ if (item.length > 1) {
495
+ item[1].field = key
496
+ NSearchValues.push(item[1])
497
+ }
498
+ }
499
+ })
500
+ }
501
+
502
+ } else {
503
+ $n_forIn(query.n_search, function(item, key) {
504
+ if ($n_isValidArray(item)) {
505
+ item[0].field = key
506
+ NSearchValues.push(item[0])
507
+ if (item.length > 1) {
508
+ item[1].field = key
509
+ NSearchValues.push(item[1])
510
+ }
511
+ }
512
+ })
513
+ }
514
+
515
+ if (NSearchValues.length) {
516
+ query.n_search = NSearchValues
517
+ } else if (hasNSearch) {
518
+ delete query.n_search
519
+ }
520
+
521
+ tableRequestQuery = query
522
+ return
523
+ }
524
+
525
+ tableRequestQuery = {}
526
+ }
527
+
528
+
529
+ /**
530
+ * 表格是否已加载
531
+ */
532
+ function isTableLoaded() {
533
+ return _isTableLoaded
534
+ }
535
+
536
+ /**
537
+ * 表格加载(只加载一次)
538
+ */
539
+ async function tableLoad() {
540
+
541
+ // 如果表格已加载过了
542
+ if (_isTableLoaded) {
543
+ // 则无任何操作
544
+ return
545
+ }
546
+
547
+ // 表格重新加载
548
+ await tableReload()
549
+ }
550
+
551
+ /**
552
+ * 表格重新加载
553
+ */
554
+ async function tableReload() {
555
+
556
+ // 表格已加载
557
+ _isTableLoaded = true
558
+
559
+ if (! $route.fullPath) {
560
+ return
561
+ }
562
+
563
+ // 请求表格合计
564
+ if (o.summary) {
565
+ isRequestSummary = true
566
+ }
567
+
568
+ // 请求表格数据
569
+ // $tableRef?.requestServerInteraction({
570
+ // pagination: o.pagination,
571
+ // })
572
+ await tableRequest({
573
+ pagination: o.pagination,
574
+ })
575
+
576
+ // 清空表格已选数据
577
+ if (o.refreshResetSelected) {
578
+ tableSelected.value = []
579
+ }
580
+ }
581
+
582
+ /**
583
+ * 表格刷新
584
+ */
585
+ async function tableRefresh() {
586
+
587
+ if (! $route.fullPath) {
588
+ return
589
+ }
590
+
591
+ // 请求表格合计
592
+ if (o.summary) {
593
+ isRequestSummary = true
594
+ }
595
+
596
+ // 请求表格数据
597
+ // $tableRef.requestServerInteraction()
598
+ await tableRequest({
599
+ pagination: tablePagination.value,
600
+ })
601
+
602
+ // 清空表格已选数据
603
+ if (o.refreshResetSelected) {
604
+ tableSelected.value = []
605
+ }
606
+ }
607
+
608
+ /**
609
+ * 表格搜索重置
610
+ */
611
+ function tableSearchReset(reload = true) {
612
+
613
+ const newValue = []
614
+
615
+ $n_forEach(rawSearchOptions, function (item, index) {
616
+ // 如果该搜索条件是隐藏的
617
+ if (item.hide) {
618
+ newValue.push(tableSearchValue.value[index])
619
+ // 否则为初始值
620
+ } else {
621
+ newValue.push(rawTableSearchValue[index])
622
+ }
623
+ })
624
+
625
+ // 还原表格搜索数据
626
+ tableSearchValue.value = $n_cloneDeep(newValue)
627
+
628
+ // 表格重新加载
629
+ if (reload) {
630
+ tableReload().finally()
631
+ }
632
+ }
633
+
634
+ /**
635
+ * 获取表格请求数据
636
+ */
637
+ function getTableRequestData(props, isSummary = undefined) {
638
+
639
+ // 解构数据
640
+ const {
641
+ // filter,
642
+ pagination: {
643
+ // 页码
644
+ page,
645
+ // 每页的数据条数
646
+ rowsPerPage,
647
+ // 排序字段
648
+ sortBy,
649
+ // 是否降序排列
650
+ descending,
651
+ }
652
+ } = props
653
+
654
+ // 请求数据
655
+ const data = {
656
+ // 页码
657
+ page,
658
+ // 每页的数据条数
659
+ per_page: rowsPerPage,
660
+ }
661
+
662
+ // 如果排序字段是有效值
663
+ if ($n_isValidValue(sortBy)) {
664
+ Object.assign(data, {
665
+ // 排序字段
666
+ order_by: sortBy,
667
+ // 是否降序排列
668
+ is_desc: descending ? 1 : 0,
669
+ })
670
+ }
671
+
672
+ // 合并参数
673
+ $n_forIn(Object.assign({}, rawQuery, tableRequestQuery, o.data), function(value, key) {
674
+ // 如果有值
675
+ if ($n_isRequired(value)) {
676
+ data[key] = value
677
+ }
678
+ })
679
+
680
+ // 获取搜索值
681
+ const search = formatValue(rawSearchOptions, tableSearchValue.value)
682
+ if ($n_isValidArray(search)) {
683
+ data.n_search = $n_has(data, 'n_search') ? $n_concat(data.n_search, search) : search
684
+ }
685
+
686
+ if ($n_isNil(isSummary)) {
687
+ isSummary = isRequestSummary
688
+ }
689
+
690
+ // 如果请求表格合计
691
+ if (isSummary) {
692
+ data.summary = 1
693
+ }
694
+
695
+ return data
696
+ }
697
+
698
+ /**
699
+ * 请求数据
700
+ */
701
+ async function tableRequest(props) {
702
+
703
+ // 加载
704
+ tableLoading.value = true
705
+
706
+ // 解构数据
707
+ const {
708
+ // filter,
709
+ pagination: {
710
+ // 页码
711
+ page,
712
+ // 每页的数据条数
713
+ rowsPerPage,
714
+ // 排序字段
715
+ sortBy,
716
+ // 是否降序排列
717
+ descending,
718
+ }
719
+ } = props
720
+
721
+ // http 请求参数
722
+ const httpOptions = Object.assign({
723
+ // 请求数据
724
+ url: $route.path,
725
+ // 请求数据
726
+ data: getTableRequestData(props, isRequestSummary),
727
+ // ~~~~~~ 先开启防抖, 如果后期遇到表格加载不出来的情况, 再关闭防抖
728
+ // 关闭防抖(允许重复请求)
729
+ // debounce: false,
730
+ }, o.httpSettings)
731
+
732
+ const { status, data: res } = $n_isFunction(o.request)
733
+ // 如果有自定义请求方法
734
+ ? await $n_runAsync(o.request)({
735
+ // http 请求参数
736
+ httpOptions,
737
+ // 表格声明属性
738
+ props,
739
+ // 表格行数据
740
+ rows: tableRows,
741
+ // 表格已选数据
742
+ selected: tableSelected,
743
+ })
744
+ // 否则请求服务器
745
+ : await $n_http(httpOptions)
746
+
747
+ // 请求成功
748
+ if (status) {
749
+
750
+ const {
751
+ // 返回数据
752
+ rows,
753
+ // 数据总数
754
+ total,
755
+ } = res
756
+
757
+ // 如果请求表格合计
758
+ if (isRequestSummary) {
759
+ const summary = $n_get(res, 'summary')
760
+ tableSummary.value = $n_isValidObject(summary) ? summary : null
761
+ }
762
+
763
+ // 更新页码
764
+ tablePagination.value.page = page
765
+ // 更新每页的数据条数
766
+ tablePagination.value.rowsPerPage = rowsPerPage
767
+ // 更新数据总数
768
+ tablePagination.value.rowsNumber = total
769
+ // 更新排序字段
770
+ tablePagination.value.sortBy = sortBy
771
+ // 更新是否降序排列
772
+ tablePagination.value.descending = descending
773
+
774
+ // 格式化单条数据
775
+ if ($n_isFunction(o.formatRow)) {
776
+ $n_forEach(rows, function(row) {
777
+ o.formatRow({
778
+ row,
779
+ rows: tableRows,
780
+ selected: tableSelected,
781
+ })
782
+ })
783
+ }
784
+
785
+ // 清除现有数据并添加新数据
786
+ tableRows.value.splice(0, tableRows.value.length, ...rows)
787
+ }
788
+
789
+ // 取消请求表格合计
790
+ isRequestSummary = false
791
+
792
+ // 取消加载
793
+ tableLoading.value = false
794
+ }
795
+
796
+ /**
797
+ * 单击表格行
798
+ */
799
+ function _tableRowClick(e, row) {
800
+
801
+ // 如果选择类型为无
802
+ if (tableSelection.value === 'none') {
803
+ // 则无任何操作
804
+ return
805
+ }
806
+
807
+ const opt = {}
808
+ opt[o.rowKey] = row[o.rowKey]
809
+
810
+ // 获取当前数据索引
811
+ const itemIndex = $n_findIndex(tableSelected.value, opt)
812
+
813
+ // 如果不存在, 则添加
814
+ if (itemIndex === -1) {
815
+
816
+ // 如果选择类型为单选
817
+ if (tableSelection.value === 'single') {
818
+ tableSelected.value = [ row ]
819
+
820
+ // 否则为多选
821
+ } else {
822
+ tableSelected.value.push(row)
823
+ }
824
+
825
+ // 否则删除
826
+ } else {
827
+ tableSelected.value.splice(itemIndex, 1)
828
+ }
829
+ }
830
+ function tableRowClick(...e) {
831
+
832
+ // 单击表格行
833
+ _tableRowClick(...e)
834
+
835
+ // 如果有自定义单击事件
836
+ if ($n_isFunction(o.rowClick)) {
837
+ o.rowClick(...e)
838
+ }
839
+ }
840
+
841
+ /**
842
+ * 双击表格行
843
+ */
844
+ function _tableRowDblclick(e, row) {
845
+
846
+ // 如果选择类型为无
847
+ if (tableSelection.value === 'none') {
848
+ // 则无任何操作
849
+ return
850
+ }
851
+
852
+ if (
853
+ // 有权限
854
+ hasPowr
855
+ // 有双击的权限按钮
856
+ && tableDbClickPowerBtn.value
857
+ ) {
858
+ $power.powerBtnClick(tableDbClickPowerBtn.value, [ row ])
859
+ }
860
+ }
861
+ function tableRowDblclick(...e) {
862
+
863
+ // 双击表格行
864
+ _tableRowDblclick(...e)
865
+
866
+ // 如果有自定义双击表格行事件
867
+ if ($n_isFunction(o.tableRowDblclick)) {
868
+ o.tableRowDblclick(...e)
869
+ }
870
+ }
871
+
872
+ /**
873
+ * 设置表格搜索参数
874
+ */
875
+ async function setTableSearchOptions(format) {
876
+ tableSearchOptions.value = await getOptions(rawSearchOptions, format)
877
+ }
878
+
879
+ /**
880
+ * 是否有表格搜索值
881
+ */
882
+ function hasTableSearchValue() {
883
+ return !! formatValue(rawSearchOptions, tableSearchValue.value).length
884
+ }
885
+
886
+ // 如果开启搜索
887
+ if (o.search) {
888
+ // 设置表格搜索参数
889
+ setTableSearchOptions()
890
+ .finally()
891
+ }
892
+
893
+ // ==========【返回】=================================================================================================
894
+
895
+ const resTable = {
896
+ // 当前路由全路径
897
+ routeFullPath: $route.fullPath,
898
+ // 当前路由路径
899
+ routePath: $route.path,
900
+ // 当前路由参数
901
+ routeQuery: $route.query,
902
+ // 获取当前路由
903
+ getRoute() {
904
+ return $route
905
+ },
906
+
907
+ // 表格加载状态
908
+ tableLoading,
909
+ // 表格行唯一键值
910
+ tableRowKey: o.rowKey,
911
+ // 表格选择类型
912
+ tableSelection,
913
+ // 表格分隔栏
914
+ tableSeparator,
915
+ // 表格每页显示行数选项
916
+ tableRowsPerPageOptions: rowsPerPageOptions,
917
+ // 表格列数据(对象数组)
918
+ tableColumns,
919
+ // 表格可见列
920
+ tableVisibleColumns,
921
+ // 表格行数据
922
+ tableRows,
923
+ // 表格翻页参数
924
+ tablePagination,
925
+ // 表格已选数据
926
+ tableSelected,
927
+ // 固定在右边的权限按钮列表
928
+ tableFixedPowerBtns,
929
+ // 是否显示固定在右边的权限按钮列表
930
+ showTableFixed,
931
+ // 表格图片标识
932
+ tableImgNames,
933
+
934
+ // 表格宫格
935
+ tableGrid,
936
+ // 表格合计
937
+ tableSummary,
938
+ // 表格搜索数据
939
+ tableSearchValue,
940
+ // 表格搜索参数
941
+ tableSearchOptions,
942
+
943
+ // 设置表格传参
944
+ setQuery,
945
+ // 表格是否已加载
946
+ isTableLoaded,
947
+ // 表格加载(只加载一次)
948
+ tableLoad,
949
+ // 表格重新加载
950
+ tableReload,
951
+ // 表格刷新
952
+ tableRefresh,
953
+ // 表格搜索重置
954
+ tableSearchReset,
955
+ // 获取表格请求数据
956
+ getTableRequestData,
957
+ // 表格请求数据
958
+ tableRequest,
959
+ // 表格单击表格行
960
+ tableRowClick,
961
+ // 表格双击表格行
962
+ tableRowDblclick,
963
+ // 设置表格搜索参数
964
+ setTableSearchOptions,
965
+
966
+ // 是否有表格搜索值
967
+ hasTableSearchValue,
968
+ }
969
+
970
+ if (hasPowr) {
971
+ $power.update(function(data, _data) {
972
+ _data.$table = resTable
973
+ })
974
+ }
975
+
976
+ // 提供可以被后代组件注入的值
977
+ provide(NTableKey, resTable)
978
+
979
+ return resTable
980
+ }
981
+
982
+ /**
983
+ * 获取表格配置
984
+ */
985
+ function config(routePath, path, defaultValue) {
986
+ return $n_cloneDeep($n_get(tablesConfig, $n_slash(routePath, 'start', false) + (path ? '.' + path : ''), defaultValue))
987
+ }
988
+
989
+ /**
990
+ * 业务表格
991
+ */
992
+ const $table = {
993
+ // 创建表格
994
+ create,
995
+ // 获取表格配置
996
+ config,
997
+ }
998
+
999
+ export default $table