ylyx-cli 1.0.16 → 1.1.1

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 (289) hide show
  1. package/README.md +313 -276
  2. package/bin/ylyx.js +176 -168
  3. package/lib/config.js +325 -283
  4. package/lib/deploy.js +509 -529
  5. package/lib/environments.js +111 -0
  6. package/lib/generator.js +192 -192
  7. package/lib/index.js +113 -113
  8. package/lib/interactive.js +87 -87
  9. package/lib/remote.js +179 -179
  10. package/lib/template.js +174 -174
  11. package/lib/utils.js +148 -148
  12. package/package.json +61 -59
  13. package/templates/react-app/files/.gitignore +22 -0
  14. package/templates/react-app/files/README.md +23 -23
  15. package/templates/react-app/files/package.json +28 -28
  16. package/templates/react-app/files/public/index.html +11 -11
  17. package/templates/react-app/files/src/App.css +15 -15
  18. package/templates/react-app/files/src/App.js +15 -15
  19. package/templates/react-app/files/src/index.css +10 -10
  20. package/templates/react-app/files/src/index.js +11 -11
  21. package/templates/react-app/template.json +11 -11
  22. package/templates/react-component/files/Component.module.css +5 -5
  23. package/templates/react-component/files/Component.tsx +31 -31
  24. package/templates/react-component/files/index.ts +2 -2
  25. package/templates/react-component/template.json +48 -48
  26. package/templates/vue-app/files/.editorconfig +14 -14
  27. package/templates/vue-app/files/.env.development +21 -21
  28. package/templates/vue-app/files/.env.production +18 -18
  29. package/templates/vue-app/files/.eslintignore +5 -5
  30. package/templates/vue-app/files/.eslintrc.js +267 -267
  31. package/templates/vue-app/files/.gitignore +24 -0
  32. package/templates/vue-app/files/.prettierignore +11 -11
  33. package/templates/vue-app/files/.travis.yml +5 -5
  34. package/templates/vue-app/files/.vscode/extensions.json +7 -7
  35. package/templates/vue-app/files/.vscode/settings.json +39 -39
  36. package/templates/vue-app/files/LICENSE +191 -191
  37. package/templates/vue-app/files/README.md +87 -87
  38. package/templates/vue-app/files/babel.config.js +11 -11
  39. package/templates/vue-app/files/jest.config.js +24 -24
  40. package/templates/vue-app/files/package.json +136 -136
  41. package/templates/vue-app/files/plopfile.js +7 -7
  42. package/templates/vue-app/files/postcss.config.js +5 -5
  43. package/templates/vue-app/files/public/default.js +25 -25
  44. package/templates/vue-app/files/public/index.html +26 -26
  45. package/templates/vue-app/files/src/App.vue +54 -54
  46. package/templates/vue-app/files/src/api/bigDataV/index.js +198 -198
  47. package/templates/vue-app/files/src/api/data.js +42 -42
  48. package/templates/vue-app/files/src/api/dataArchive/index.js +186 -186
  49. package/templates/vue-app/files/src/api/generator/genConfig.js +16 -16
  50. package/templates/vue-app/files/src/api/generator/generator.js +32 -32
  51. package/templates/vue-app/files/src/api/indexManagement/index.js +39 -39
  52. package/templates/vue-app/files/src/api/institutionMonitor/index.js +396 -396
  53. package/templates/vue-app/files/src/api/institutionSet/index.js +120 -120
  54. package/templates/vue-app/files/src/api/login.js +44 -44
  55. package/templates/vue-app/files/src/api/selfStatistics/index.js +50 -50
  56. package/templates/vue-app/files/src/api/system/code.js +15 -15
  57. package/templates/vue-app/files/src/api/system/dict.js +34 -34
  58. package/templates/vue-app/files/src/api/system/dictDetail.js +52 -52
  59. package/templates/vue-app/files/src/api/system/equipment.js +35 -35
  60. package/templates/vue-app/files/src/api/system/examManage.js +49 -49
  61. package/templates/vue-app/files/src/api/system/examProject.js +48 -48
  62. package/templates/vue-app/files/src/api/system/hosModel.js +35 -35
  63. package/templates/vue-app/files/src/api/system/hospital.js +88 -88
  64. package/templates/vue-app/files/src/api/system/job.js +40 -40
  65. package/templates/vue-app/files/src/api/system/menu.js +67 -67
  66. package/templates/vue-app/files/src/api/system/model.js +42 -42
  67. package/templates/vue-app/files/src/api/system/risConfig.js +9 -9
  68. package/templates/vue-app/files/src/api/system/role.js +64 -64
  69. package/templates/vue-app/files/src/api/system/sysSet.js +19 -19
  70. package/templates/vue-app/files/src/api/system/timing.js +41 -41
  71. package/templates/vue-app/files/src/api/system/user.js +121 -121
  72. package/templates/vue-app/files/src/api/tools/alipay.js +25 -25
  73. package/templates/vue-app/files/src/api/tools/email.js +24 -24
  74. package/templates/vue-app/files/src/api/tools/localStorage.js +27 -27
  75. package/templates/vue-app/files/src/api/tools/qiniu.js +40 -40
  76. package/templates/vue-app/files/src/assets/css/font-awesome.css +2342 -2342
  77. package/templates/vue-app/files/src/assets/css/font-awesome.min.css +2336 -2336
  78. package/templates/vue-app/files/src/assets/fonts/fontawesome-webfont.svg +2671 -2671
  79. package/templates/vue-app/files/src/assets/icons/index.js +9 -9
  80. package/templates/vue-app/files/src/assets/icons/svg/hospital.svg +1 -1
  81. package/templates/vue-app/files/src/assets/icons/svg/hospital1.svg +1 -1
  82. package/templates/vue-app/files/src/assets/icons/svg/loginOne.svg +35 -35
  83. package/templates/vue-app/files/src/assets/icons/svg/loginOneWhite.svg +19 -19
  84. package/templates/vue-app/files/src/assets/icons/svg/loginOr.svg +93 -93
  85. package/templates/vue-app/files/src/assets/icons/svg/loginUp.svg +96 -96
  86. package/templates/vue-app/files/src/assets/icons/svg/monitor.svg +1 -1
  87. package/templates/vue-app/files/src/assets/icons/svg/offline.svg +1 -1
  88. package/templates/vue-app/files/src/assets/icons/svg/online.svg +1 -1
  89. package/templates/vue-app/files/src/assets/icons/svg/system.svg +1 -1
  90. package/templates/vue-app/files/src/assets/icons/svgo.yml +21 -21
  91. package/templates/vue-app/files/src/assets/styles/btn.scss +103 -103
  92. package/templates/vue-app/files/src/assets/styles/eladmin.scss +124 -124
  93. package/templates/vue-app/files/src/assets/styles/element-ui.scss +217 -217
  94. package/templates/vue-app/files/src/assets/styles/element-variables.scss +31 -31
  95. package/templates/vue-app/files/src/assets/styles/global.scss +73 -73
  96. package/templates/vue-app/files/src/assets/styles/index.scss +220 -220
  97. package/templates/vue-app/files/src/assets/styles/mixin.scss +60 -60
  98. package/templates/vue-app/files/src/assets/styles/publicStyle.scss +224 -224
  99. package/templates/vue-app/files/src/assets/styles/sidebar.scss +239 -239
  100. package/templates/vue-app/files/src/assets/styles/transition.scss +48 -48
  101. package/templates/vue-app/files/src/assets/styles/variables.scss +44 -44
  102. package/templates/vue-app/files/src/common/flexible.js +145 -145
  103. package/templates/vue-app/files/src/components/Breadcrumb/index.vue +87 -87
  104. package/templates/vue-app/files/src/components/Crud/CRUD.operation.vue +248 -248
  105. package/templates/vue-app/files/src/components/Crud/Pagination.vue +20 -20
  106. package/templates/vue-app/files/src/components/Crud/RR.operation.vue +39 -39
  107. package/templates/vue-app/files/src/components/Crud/UD.operation.vue +106 -106
  108. package/templates/vue-app/files/src/components/Crud/crud.js +907 -907
  109. package/templates/vue-app/files/src/components/Crud/user/UR.operation.vue +154 -154
  110. package/templates/vue-app/files/src/components/DateRangePicker/index.vue +42 -42
  111. package/templates/vue-app/files/src/components/DateRangePickerProhibitNext/index.vue +42 -42
  112. package/templates/vue-app/files/src/components/DateRangePickerProhibitTodayNext/index.vue +48 -48
  113. package/templates/vue-app/files/src/components/Dict/Dict.js +31 -31
  114. package/templates/vue-app/files/src/components/Dict/index.js +29 -29
  115. package/templates/vue-app/files/src/components/Doc/index.vue +16 -16
  116. package/templates/vue-app/files/src/components/Echarts/BarChart.vue +115 -115
  117. package/templates/vue-app/files/src/components/Echarts/Category.vue +768 -768
  118. package/templates/vue-app/files/src/components/Echarts/Funnel.vue +120 -120
  119. package/templates/vue-app/files/src/components/Echarts/Gauge.vue +74 -74
  120. package/templates/vue-app/files/src/components/Echarts/Graph.vue +101 -101
  121. package/templates/vue-app/files/src/components/Echarts/HeatMap.vue +301 -301
  122. package/templates/vue-app/files/src/components/Echarts/Line3D.vue +110 -110
  123. package/templates/vue-app/files/src/components/Echarts/PieChart.vue +84 -84
  124. package/templates/vue-app/files/src/components/Echarts/Point.vue +161 -161
  125. package/templates/vue-app/files/src/components/Echarts/RadarChart.vue +123 -123
  126. package/templates/vue-app/files/src/components/Echarts/Rich.vue +160 -160
  127. package/templates/vue-app/files/src/components/Echarts/Sankey.vue +114 -114
  128. package/templates/vue-app/files/src/components/Echarts/Scatter.vue +192 -192
  129. package/templates/vue-app/files/src/components/Echarts/Sunburst.vue +127 -127
  130. package/templates/vue-app/files/src/components/Echarts/ThemeRiver.vue +233 -233
  131. package/templates/vue-app/files/src/components/Echarts/WordCloud.vue +201 -201
  132. package/templates/vue-app/files/src/components/GithubCorner/index.vue +55 -55
  133. package/templates/vue-app/files/src/components/Hamburger/index.vue +44 -44
  134. package/templates/vue-app/files/src/components/HeaderSearch/index.vue +196 -196
  135. package/templates/vue-app/files/src/components/IconSelect/index.vue +74 -74
  136. package/templates/vue-app/files/src/components/IconSelect/requireIcons.js +10 -10
  137. package/templates/vue-app/files/src/components/Iframe/index.vue +30 -30
  138. package/templates/vue-app/files/src/components/JavaEdit/index.vue +78 -78
  139. package/templates/vue-app/files/src/components/Pagination/index.vue +100 -100
  140. package/templates/vue-app/files/src/components/PanThumb/index.vue +149 -149
  141. package/templates/vue-app/files/src/components/ParentView/index.vue +3 -3
  142. package/templates/vue-app/files/src/components/Permission/index.js +13 -13
  143. package/templates/vue-app/files/src/components/Permission/permission.js +21 -21
  144. package/templates/vue-app/files/src/components/RightPanel/index.vue +149 -149
  145. package/templates/vue-app/files/src/components/Screenfull/index.vue +60 -60
  146. package/templates/vue-app/files/src/components/SizeSelect/index.vue +60 -60
  147. package/templates/vue-app/files/src/components/SvgIcon/index.vue +66 -66
  148. package/templates/vue-app/files/src/components/ThemePicker/index.vue +177 -177
  149. package/templates/vue-app/files/src/components/UploadExcel/index.vue +149 -149
  150. package/templates/vue-app/files/src/components/WaterMark/setWaterMark.js +68 -68
  151. package/templates/vue-app/files/src/components/WaterMark/waterMark.vue +69 -69
  152. package/templates/vue-app/files/src/components/Xgplayer/index.vue +49 -49
  153. package/templates/vue-app/files/src/components/YamlEdit/index.vue +81 -81
  154. package/templates/vue-app/files/src/layout/components/AppMain.vue +67 -67
  155. package/templates/vue-app/files/src/layout/components/Navbar.vue +209 -209
  156. package/templates/vue-app/files/src/layout/components/Settings/index.vue +125 -125
  157. package/templates/vue-app/files/src/layout/components/Sidebar/FixiOSBug.js +26 -26
  158. package/templates/vue-app/files/src/layout/components/Sidebar/Item.vue +29 -29
  159. package/templates/vue-app/files/src/layout/components/Sidebar/Link.vue +35 -35
  160. package/templates/vue-app/files/src/layout/components/Sidebar/Logo.vue +93 -93
  161. package/templates/vue-app/files/src/layout/components/Sidebar/SidebarItem.vue +104 -104
  162. package/templates/vue-app/files/src/layout/components/Sidebar/index.vue +79 -79
  163. package/templates/vue-app/files/src/layout/components/TagsView/ScrollPane.vue +90 -90
  164. package/templates/vue-app/files/src/layout/components/TagsView/index.vue +299 -299
  165. package/templates/vue-app/files/src/layout/components/index.js +5 -5
  166. package/templates/vue-app/files/src/layout/index.vue +119 -119
  167. package/templates/vue-app/files/src/layout/mixin/ResizeHandler.js +45 -45
  168. package/templates/vue-app/files/src/main.js +89 -89
  169. package/templates/vue-app/files/src/mixins/crud.js +348 -348
  170. package/templates/vue-app/files/src/router/index.js +156 -156
  171. package/templates/vue-app/files/src/router/routers.js +203 -203
  172. package/templates/vue-app/files/src/settings.js +57 -57
  173. package/templates/vue-app/files/src/store/getters.js +29 -29
  174. package/templates/vue-app/files/src/store/index.js +25 -25
  175. package/templates/vue-app/files/src/store/modules/api.js +33 -33
  176. package/templates/vue-app/files/src/store/modules/app.js +55 -55
  177. package/templates/vue-app/files/src/store/modules/permission.js +79 -79
  178. package/templates/vue-app/files/src/store/modules/register.js +17 -17
  179. package/templates/vue-app/files/src/store/modules/settings.js +46 -46
  180. package/templates/vue-app/files/src/store/modules/tagsView.js +168 -168
  181. package/templates/vue-app/files/src/store/modules/user.js +119 -119
  182. package/templates/vue-app/files/src/utils/auth.js +18 -18
  183. package/templates/vue-app/files/src/utils/base64ToFile.js +27 -27
  184. package/templates/vue-app/files/src/utils/clipboard.js +36 -36
  185. package/templates/vue-app/files/src/utils/datetime.js +289 -289
  186. package/templates/vue-app/files/src/utils/getBlobFile.js +10 -10
  187. package/templates/vue-app/files/src/utils/getTime.js +29 -29
  188. package/templates/vue-app/files/src/utils/index.js +520 -520
  189. package/templates/vue-app/files/src/utils/permission.js +29 -29
  190. package/templates/vue-app/files/src/utils/pinyin.js +450 -450
  191. package/templates/vue-app/files/src/utils/request.js +139 -139
  192. package/templates/vue-app/files/src/utils/rsaEncrypt.js +37 -37
  193. package/templates/vue-app/files/src/utils/shortcuts.js +210 -210
  194. package/templates/vue-app/files/src/utils/upload.js +12 -12
  195. package/templates/vue-app/files/src/utils/validate.js +262 -262
  196. package/templates/vue-app/files/src/views/components/Echarts.vue +124 -124
  197. package/templates/vue-app/files/src/views/components/Editor.vue +75 -75
  198. package/templates/vue-app/files/src/views/components/MarkDown.vue +44 -44
  199. package/templates/vue-app/files/src/views/components/UVideo.vue +37 -37
  200. package/templates/vue-app/files/src/views/components/YamlEdit.vue +206 -206
  201. package/templates/vue-app/files/src/views/components/camera/camera.vue +115 -115
  202. package/templates/vue-app/files/src/views/components/camera/cameraTest.vue +158 -158
  203. package/templates/vue-app/files/src/views/components/excel/upload-excel.vue +41 -41
  204. package/templates/vue-app/files/src/views/components/icons/element-icons.js +74 -74
  205. package/templates/vue-app/files/src/views/components/icons/index.vue +106 -106
  206. package/templates/vue-app/files/src/views/components/icons/svg-icons.js +10 -10
  207. package/templates/vue-app/files/src/views/components/scroll/index.vue +107 -107
  208. package/templates/vue-app/files/src/views/components/scrollFull/index.vue +99 -99
  209. package/templates/vue-app/files/src/views/dashboard/LineChart.vue +138 -138
  210. package/templates/vue-app/files/src/views/dashboard/PanelGroup.vue +173 -173
  211. package/templates/vue-app/files/src/views/dashboard/mixins/resize.js +57 -57
  212. package/templates/vue-app/files/src/views/dataArchive/delTable.vue +354 -354
  213. package/templates/vue-app/files/src/views/dataArchive/exportDetail.vue +121 -121
  214. package/templates/vue-app/files/src/views/dataArchive/exportList.vue +160 -160
  215. package/templates/vue-app/files/src/views/dataArchive/index.vue +2104 -2104
  216. package/templates/vue-app/files/src/views/features/401.vue +83 -83
  217. package/templates/vue-app/files/src/views/features/404.vue +226 -226
  218. package/templates/vue-app/files/src/views/features/redirect.vue +12 -12
  219. package/templates/vue-app/files/src/views/generator/config.vue +348 -348
  220. package/templates/vue-app/files/src/views/generator/index.vue +143 -143
  221. package/templates/vue-app/files/src/views/generator/preview.vue +39 -39
  222. package/templates/vue-app/files/src/views/home.vue +80 -80
  223. package/templates/vue-app/files/src/views/indexManagement/index.vue +422 -422
  224. package/templates/vue-app/files/src/views/indexManagement/other.vue +286 -286
  225. package/templates/vue-app/files/src/views/institutionMonitor/components/LineChart.vue +191 -191
  226. package/templates/vue-app/files/src/views/institutionMonitor/index.scss +339 -339
  227. package/templates/vue-app/files/src/views/institutionMonitor/index.vue +1385 -1385
  228. package/templates/vue-app/files/src/views/institutionMonitor/mixins/resize.js +57 -57
  229. package/templates/vue-app/files/src/views/institutionMonitor/monitorDetail/checkProjectCode.vue +267 -267
  230. package/templates/vue-app/files/src/views/institutionMonitor/monitorDetail/imageAbnormalMonitor.vue +361 -361
  231. package/templates/vue-app/files/src/views/institutionMonitor/monitorDetail/index.vue +686 -686
  232. package/templates/vue-app/files/src/views/institutionMonitor/monitorDetail/leakageAnomaly.vue +270 -270
  233. package/templates/vue-app/files/src/views/institutionMonitor/monitorDetail/moveMonitore.vue +114 -114
  234. package/templates/vue-app/files/src/views/institutionMonitor/monitorDetail/qualityMonitore.vue +338 -338
  235. package/templates/vue-app/files/src/views/institutionSet/cloudSet.vue +534 -534
  236. package/templates/vue-app/files/src/views/institutionSet/index.vue +2550 -2550
  237. package/templates/vue-app/files/src/views/login.vue +243 -243
  238. package/templates/vue-app/files/src/views/qualityLogin.vue +280 -280
  239. package/templates/vue-app/files/src/views/selfStatistics/components/LineChart.vue +296 -296
  240. package/templates/vue-app/files/src/views/selfStatistics/hospitalDetail/index.vue +533 -533
  241. package/templates/vue-app/files/src/views/selfStatistics/index.scss +447 -447
  242. package/templates/vue-app/files/src/views/selfStatistics/index.vue +903 -903
  243. package/templates/vue-app/files/src/views/selfStatistics/mixins/resize.js +57 -57
  244. package/templates/vue-app/files/src/views/selfStatistics/monitorDetail/index.vue +285 -285
  245. package/templates/vue-app/files/src/views/selfStatistics/offlineDetail/index.vue +104 -104
  246. package/templates/vue-app/files/src/views/selfStatistics/selfActiveDetail/index.vue +104 -104
  247. package/templates/vue-app/files/src/views/selfStatistics/selfMonitorDetail/index.vue +420 -420
  248. package/templates/vue-app/files/src/views/selfStatistics/setWaterMark/setWaterMark.js +68 -68
  249. package/templates/vue-app/files/src/views/selfStatistics/setWaterMark/waterMark.vue +69 -69
  250. package/templates/vue-app/files/src/views/system/dept/index.vue +144 -144
  251. package/templates/vue-app/files/src/views/system/dict/dictDetail.vue +146 -146
  252. package/templates/vue-app/files/src/views/system/dict/index.vue +159 -159
  253. package/templates/vue-app/files/src/views/system/examModality/index.vue +174 -174
  254. package/templates/vue-app/files/src/views/system/examPart/index.vue +184 -184
  255. package/templates/vue-app/files/src/views/system/examProject/index.vue +321 -321
  256. package/templates/vue-app/files/src/views/system/examine/index.vue +66 -66
  257. package/templates/vue-app/files/src/views/system/job/index.vue +119 -119
  258. package/templates/vue-app/files/src/views/system/job/module/form.vue +72 -72
  259. package/templates/vue-app/files/src/views/system/job/module/header.vue +48 -48
  260. package/templates/vue-app/files/src/views/system/menu/index.vue +327 -327
  261. package/templates/vue-app/files/src/views/system/model/index.vue +285 -285
  262. package/templates/vue-app/files/src/views/system/role/index.vue +434 -434
  263. package/templates/vue-app/files/src/views/system/sysSet/index.vue +84 -84
  264. package/templates/vue-app/files/src/views/system/timing/index.vue +289 -289
  265. package/templates/vue-app/files/src/views/system/timing/log.vue +143 -143
  266. package/templates/vue-app/files/src/views/system/user/admin.vue +414 -414
  267. package/templates/vue-app/files/src/views/system/user/center/updateEmail.vue +146 -146
  268. package/templates/vue-app/files/src/views/system/user/center/updatePass.vue +168 -168
  269. package/templates/vue-app/files/src/views/system/user/center.vue +140 -140
  270. package/templates/vue-app/files/src/views/system/user/index.vue +972 -972
  271. package/templates/vue-app/files/src/views/systemSet/role/index.vue +314 -314
  272. package/templates/vue-app/files/src/views/systemSet/user/admin.vue +414 -414
  273. package/templates/vue-app/files/src/views/systemSet/user/center/updateEmail.vue +146 -146
  274. package/templates/vue-app/files/src/views/systemSet/user/center/updatePass.vue +115 -115
  275. package/templates/vue-app/files/src/views/systemSet/user/center.vue +351 -351
  276. package/templates/vue-app/files/src/views/systemSet/user/index.vue +928 -928
  277. package/templates/vue-app/files/src/views/tools/aliPay/config.vue +102 -102
  278. package/templates/vue-app/files/src/views/tools/aliPay/index.vue +48 -48
  279. package/templates/vue-app/files/src/views/tools/aliPay/toPay.vue +91 -91
  280. package/templates/vue-app/files/src/views/tools/email/config.vue +91 -91
  281. package/templates/vue-app/files/src/views/tools/email/index.vue +42 -42
  282. package/templates/vue-app/files/src/views/tools/email/send.vue +159 -159
  283. package/templates/vue-app/files/src/views/tools/storage/index.vue +35 -35
  284. package/templates/vue-app/files/src/views/tools/storage/local/index.vue +207 -207
  285. package/templates/vue-app/files/src/views/tools/storage/qiniu/form.vue +108 -108
  286. package/templates/vue-app/files/src/views/tools/storage/qiniu/index.vue +241 -241
  287. package/templates/vue-app/files/src/views/tools/swagger/index.vue +14 -14
  288. package/templates/vue-app/files/vue.config.js +173 -173
  289. package/templates/vue-app/template.json +29 -29
@@ -1,907 +1,907 @@
1
- import { initData, initDataPost, download } from '@/api/data';
2
- import { parseTime, downloadFile } from '@/utils/index';
3
- import Vue from 'vue';
4
-
5
- /**
6
- * CRUD配置
7
- * @author moxun
8
- * @param {*} options <br>
9
- * @return crud instance.
10
- * @example
11
- * 要使用多crud时,请在关联crud的组件处使用crud-tag进行标记,如:<jobForm :job-status="dict.job_status" crud-tag="job" />
12
- */
13
- function CRUD(options) {
14
- const defaultOptions = {
15
- tag: 'default',
16
- // id字段名
17
- idField: 'id',
18
- // 标题
19
- title: '',
20
- // 请求数据的url
21
- url: '',
22
- // 表格数据
23
- data: [],
24
- // 选择项
25
- selections: [],
26
- // 待查询的对象
27
- query: {},
28
- // 查询数据的参数
29
- params: {},
30
- // Form 表单
31
- form: {},
32
- // 重置表单
33
- defaultForm: () => {},
34
- // 排序规则,默认 id 降序, 支持多字段排序 ['id,desc', 'createTime,asc']
35
- sort: ['id,desc'],
36
- // 等待时间
37
- time: 50,
38
- // CRUD Method
39
- crudMethod: {
40
- add: form => {},
41
- del: id => {},
42
- edit: form => {},
43
- get: id => {},
44
- },
45
- // 主页操作栏显示哪些按钮
46
- optShow: {
47
- add: true,
48
- edit: false,
49
- del: false,
50
- download: false,
51
- reset: false,
52
- },
53
- // 自定义一些扩展属性
54
- props: {},
55
- // 在主页准备
56
- queryOnPresenterCreated: true,
57
- // 调试开关
58
- debug: false,
59
- type: '',
60
- };
61
- options = mergeOptions(defaultOptions, options);
62
- const data = {
63
- ...options,
64
- // 记录数据状态
65
- dataStatus: {},
66
- status: {
67
- add: CRUD.STATUS.NORMAL,
68
- edit: CRUD.STATUS.NORMAL,
69
- // 添加或编辑状态
70
- get cu() {
71
- if (this.add === CRUD.STATUS.NORMAL && this.edit === CRUD.STATUS.NORMAL) {
72
- return CRUD.STATUS.NORMAL;
73
- } else if (this.add === CRUD.STATUS.PREPARED || this.edit === CRUD.STATUS.PREPARED) {
74
- return CRUD.STATUS.PREPARED;
75
- } else if (this.add === CRUD.STATUS.PROCESSING || this.edit === CRUD.STATUS.PROCESSING) {
76
- return CRUD.STATUS.PROCESSING;
77
- }
78
- throw new Error("wrong crud's cu status");
79
- },
80
- // 标题
81
- get title() {
82
- return this.add > CRUD.STATUS.NORMAL
83
- ? `新增${crud.title}`
84
- : this.edit > CRUD.STATUS.NORMAL
85
- ? `编辑${crud.title}`
86
- : crud.title;
87
- },
88
- },
89
- msg: {
90
- submit: '提交成功',
91
- add: '新增成功',
92
- edit: '编辑成功',
93
- del: '删除成功',
94
- },
95
- page: {
96
- // 页码
97
- page: 0,
98
- // 每页数据条数
99
- size: 10,
100
- // 总数据条数
101
- total: 0,
102
- },
103
- // 整体loading
104
- loading: false,
105
- // 导出的 Loading
106
- downloadLoading: false,
107
- // 删除的 Loading
108
- delAllLoading: false,
109
- };
110
- const methods = {
111
- /**
112
- * 通用的提示
113
- */
114
- submitSuccessNotify() {
115
- crud.notify(crud.msg.submit, CRUD.NOTIFICATION_TYPE.SUCCESS);
116
- },
117
- addSuccessNotify() {
118
- crud.notify(crud.msg.add, CRUD.NOTIFICATION_TYPE.SUCCESS);
119
- },
120
- editSuccessNotify() {
121
- crud.notify(crud.msg.edit, CRUD.NOTIFICATION_TYPE.SUCCESS);
122
- },
123
- delSuccessNotify() {
124
- crud.notify(crud.msg.del, CRUD.NOTIFICATION_TYPE.SUCCESS);
125
- },
126
- // 搜索
127
- toQuery() {
128
- crud.page.page = 1;
129
- crud.refresh();
130
- },
131
- // 刷新
132
- refresh() {
133
- if (!callVmHook(crud, CRUD.HOOK.beforeRefresh)) {
134
- return;
135
- }
136
- // 没有地址的时候 阻止进行下一步
137
- if (!crud.url) return;
138
- return new Promise((resolve, reject) => {
139
- crud.loading = true;
140
- // 请求数据
141
- let initDataType = crud.type == 'post' ? initDataPost : initData;
142
- initDataType(crud.url, crud.getQueryParams())
143
- .then(data => {
144
- const table = crud.getTable();
145
- if (table && table.lazy) {
146
- // 懒加载子节点数据,清掉已加载的数据
147
- table.store.states.treeData = {};
148
- table.store.states.lazyTreeNodeMap = {};
149
- }
150
- crud.page.total = data.totalElements;
151
- crud.data = data.content;
152
- crud.resetDataStatus();
153
- // time 毫秒后显示表格
154
- setTimeout(() => {
155
- crud.loading = false;
156
- callVmHook(crud, CRUD.HOOK.afterRefresh);
157
- }, crud.time);
158
- resolve(data);
159
- })
160
- .catch(err => {
161
- crud.loading = false;
162
- reject(err);
163
- });
164
- });
165
- },
166
- /**
167
- * 启动添加
168
- */
169
- toAdd() {
170
- crud.resetForm();
171
- if (
172
- !(
173
- callVmHook(crud, CRUD.HOOK.beforeToAdd, crud.form) &&
174
- callVmHook(crud, CRUD.HOOK.beforeToCU, crud.form)
175
- )
176
- ) {
177
- return;
178
- }
179
- crud.status.add = CRUD.STATUS.PREPARED;
180
- callVmHook(crud, CRUD.HOOK.afterToAdd, crud.form);
181
- callVmHook(crud, CRUD.HOOK.afterToCU, crud.form);
182
- },
183
- /**
184
- * 启动编辑
185
- * @param {*} data 数据项
186
- */
187
- toEdit(data) {
188
- crud.resetForm(JSON.parse(JSON.stringify(data)));
189
- if (
190
- !(
191
- callVmHook(crud, CRUD.HOOK.beforeToEdit, crud.form) &&
192
- callVmHook(crud, CRUD.HOOK.beforeToCU, crud.form)
193
- )
194
- ) {
195
- return;
196
- }
197
- crud.status.edit = CRUD.STATUS.PREPARED;
198
- crud.getDataStatus(crud.getDataId(data)).edit = CRUD.STATUS.PREPARED;
199
- callVmHook(crud, CRUD.HOOK.afterToEdit, crud.form);
200
- callVmHook(crud, CRUD.HOOK.afterToCU, crud.form);
201
- },
202
- /**
203
- * 启动删除
204
- * @param {*} data 数据项
205
- */
206
- toDelete(data) {
207
- crud.getDataStatus(crud.getDataId(data)).delete = CRUD.STATUS.PREPARED;
208
- },
209
- /**
210
- * 取消删除
211
- * @param {*} data 数据项
212
- */
213
- cancelDelete(data) {
214
- if (!callVmHook(crud, CRUD.HOOK.beforeDeleteCancel, data)) {
215
- return;
216
- }
217
- crud.getDataStatus(crud.getDataId(data)).delete = CRUD.STATUS.NORMAL;
218
- callVmHook(crud, CRUD.HOOK.afterDeleteCancel, data);
219
- },
220
- /**
221
- * 取消新增/编辑
222
- */
223
- cancelCU() {
224
- const addStatus = crud.status.add;
225
- const editStatus = crud.status.edit;
226
- if (addStatus === CRUD.STATUS.PREPARED) {
227
- if (!callVmHook(crud, CRUD.HOOK.beforeAddCancel, crud.form)) {
228
- return;
229
- }
230
- crud.status.add = CRUD.STATUS.NORMAL;
231
- }
232
- if (editStatus === CRUD.STATUS.PREPARED) {
233
- if (!callVmHook(crud, CRUD.HOOK.beforeEditCancel, crud.form)) {
234
- return;
235
- }
236
- crud.status.edit = CRUD.STATUS.NORMAL;
237
- crud.getDataStatus(crud.getDataId(crud.form)).edit = CRUD.STATUS.NORMAL;
238
- }
239
- crud.resetForm();
240
- if (addStatus === CRUD.STATUS.PREPARED) {
241
- callVmHook(crud, CRUD.HOOK.afterAddCancel, crud.form);
242
- }
243
- if (editStatus === CRUD.STATUS.PREPARED) {
244
- callVmHook(crud, CRUD.HOOK.afterEditCancel, crud.form);
245
- }
246
- // 清除表单验证
247
- if (crud.findVM('form').$refs['form']) {
248
- crud.findVM('form').$refs['form'].clearValidate();
249
- }
250
- },
251
- /**
252
- * 提交新增/编辑
253
- */
254
- submitCU() {
255
- if (!callVmHook(crud, CRUD.HOOK.beforeValidateCU)) {
256
- return;
257
- }
258
- crud.findVM('form').$refs['form'].validate(valid => {
259
- if (!valid) {
260
- return;
261
- }
262
- if (!callVmHook(crud, CRUD.HOOK.afterValidateCU)) {
263
- return;
264
- }
265
- if (crud.status.add === CRUD.STATUS.PREPARED) {
266
- crud.doAdd();
267
- } else if (crud.status.edit === CRUD.STATUS.PREPARED) {
268
- crud.doEdit();
269
- }
270
- });
271
- },
272
- /**
273
- * 执行添加
274
- */
275
- doAdd() {
276
- if (!callVmHook(crud, CRUD.HOOK.beforeSubmit)) {
277
- return;
278
- }
279
- crud.status.add = CRUD.STATUS.PROCESSING;
280
- crud.crudMethod
281
- .add(crud.form)
282
- .then(() => {
283
- crud.status.add = CRUD.STATUS.NORMAL;
284
- crud.resetForm();
285
- crud.addSuccessNotify();
286
- callVmHook(crud, CRUD.HOOK.afterSubmit);
287
- crud.toQuery();
288
- })
289
- .catch(() => {
290
- crud.status.add = CRUD.STATUS.PREPARED;
291
- callVmHook(crud, CRUD.HOOK.afterAddError);
292
- });
293
- },
294
- /**
295
- * 执行编辑
296
- */
297
- doEdit() {
298
- if (!callVmHook(crud, CRUD.HOOK.beforeSubmit)) {
299
- return;
300
- }
301
- crud.status.edit = CRUD.STATUS.PROCESSING;
302
- crud.crudMethod
303
- .edit(crud.form)
304
- .then(() => {
305
- crud.status.edit = CRUD.STATUS.NORMAL;
306
- crud.getDataStatus(crud.getDataId(crud.form)).edit = CRUD.STATUS.NORMAL;
307
- crud.editSuccessNotify();
308
- crud.resetForm();
309
- callVmHook(crud, CRUD.HOOK.afterSubmit);
310
- crud.refresh();
311
- })
312
- .catch(() => {
313
- crud.status.edit = CRUD.STATUS.PREPARED;
314
- callVmHook(crud, CRUD.HOOK.afterEditError);
315
- });
316
- },
317
- /**
318
- * 执行删除
319
- * @param {*} data 数据项
320
- */
321
- doDelete(data) {
322
- let delAll = false;
323
- let dataStatus;
324
- const ids = [];
325
- if (data instanceof Array) {
326
- delAll = true;
327
- data.forEach(val => {
328
- ids.push(this.getDataId(val));
329
- });
330
- } else {
331
- ids.push(this.getDataId(data));
332
- dataStatus = crud.getDataStatus(this.getDataId(data));
333
- }
334
- if (!callVmHook(crud, CRUD.HOOK.beforeDelete, data)) {
335
- return;
336
- }
337
- if (!delAll) {
338
- dataStatus.delete = CRUD.STATUS.PROCESSING;
339
- }
340
- return crud.crudMethod
341
- .del(ids)
342
- .then(() => {
343
- if (delAll) {
344
- crud.delAllLoading = false;
345
- } else dataStatus.delete = CRUD.STATUS.PREPARED;
346
- crud.dleChangePage(1);
347
- crud.delSuccessNotify();
348
- callVmHook(crud, CRUD.HOOK.afterDelete, data);
349
- crud.refresh();
350
- })
351
- .catch(() => {
352
- if (delAll) {
353
- crud.delAllLoading = false;
354
- } else dataStatus.delete = CRUD.STATUS.PREPARED;
355
- });
356
- },
357
- /**
358
- * 通用导出
359
- */
360
- doExport() {
361
- crud.downloadLoading = true;
362
- download(crud.url + '/download', crud.getQueryParams())
363
- .then(result => {
364
- crud.downloadLoading = false;
365
- })
366
- .catch(result => {
367
- downloadFile(result, crud.title + '数据', 'xlsx');
368
- crud.downloadLoading = false;
369
- });
370
- },
371
- /**
372
- * 获取查询参数
373
- */
374
- getQueryParams: function () {
375
- //字符串清空前后空格
376
- Object.keys(crud.query).map(key => {
377
- if (typeof crud.query[key] == 'string') {
378
- crud.query[key] = crud.query[key].trim();
379
- }
380
- });
381
- // 清除参数无值的情况
382
- Object.keys(crud.query).length !== 0 &&
383
- Object.keys(crud.query).forEach(item => {
384
- if (crud.query[item] === null || crud.query[item] === '') crud.query[item] = undefined;
385
- });
386
- Object.keys(crud.params).length !== 0 &&
387
- Object.keys(crud.params).forEach(item => {
388
- if (crud.params[item] === null || crud.params[item] === '') crud.params[item] = undefined;
389
- });
390
- return {
391
- page: crud.page.page - 1,
392
- size: crud.page.size,
393
- sort: crud.sort,
394
- ...crud.query,
395
- ...crud.params,
396
- };
397
- },
398
- // 当前页改变
399
- pageChangeHandler(e) {
400
- crud.page.page = e;
401
- crud.refresh();
402
- },
403
- // 每页条数改变
404
- sizeChangeHandler(e) {
405
- crud.page.size = e;
406
- crud.page.page = 1;
407
- crud.refresh();
408
- },
409
- // 预防删除第二页最后一条数据时,或者多选删除第二页的数据时,页码错误导致请求无数据
410
- dleChangePage(size) {
411
- if (crud.data.length === size && crud.page.page !== 1) {
412
- crud.page.page -= 1;
413
- }
414
- },
415
- // 选择改变
416
- selectionChangeHandler(val) {
417
- crud.selections = val;
418
- },
419
- /**
420
- * 重置查询参数
421
- * @param {Boolean} toQuery 重置后进行查询操作
422
- */
423
- resetQuery(toQuery = true) {
424
- const defaultQuery = JSON.parse(JSON.stringify(crud.defaultQuery));
425
- const query = crud.query;
426
- Object.keys(query).forEach(key => {
427
- query[key] = defaultQuery[key];
428
- });
429
- // 重置参数
430
- this.params = {};
431
- if (toQuery) {
432
- crud.toQuery();
433
- }
434
- },
435
- /**
436
- * 重置表单
437
- * @param {Array} data 数据
438
- */
439
- resetForm(data) {
440
- const form =
441
- data ||
442
- (typeof crud.defaultForm === 'object'
443
- ? JSON.parse(JSON.stringify(crud.defaultForm))
444
- : crud.defaultForm.apply(crud.findVM('form')));
445
- const crudFrom = crud.form;
446
- for (const key in form) {
447
- if (crudFrom.hasOwnProperty(key)) {
448
- crudFrom[key] = form[key];
449
- } else {
450
- Vue.set(crudFrom, key, form[key]);
451
- }
452
- }
453
- // add by ghl 2020-10-04 页面重复添加信息时,下拉框的校验会存在,需要找工取消
454
- if (crud.findVM('form').$refs['form']) {
455
- crud.findVM('form').$refs['form'].clearValidate();
456
- }
457
- },
458
- /**
459
- * 重置数据状态
460
- */
461
- resetDataStatus() {
462
- const dataStatus = {};
463
- function resetStatus(datas) {
464
- datas = datas || [];
465
- datas.forEach(e => {
466
- dataStatus[crud.getDataId(e)] = {
467
- delete: 0,
468
- edit: 0,
469
- };
470
- if (e.children) {
471
- resetStatus(e.children);
472
- }
473
- });
474
- }
475
- resetStatus(crud.data);
476
- crud.dataStatus = dataStatus;
477
- },
478
- /**
479
- * 获取数据状态
480
- * @param {Number | String} id 数据项id
481
- */
482
- getDataStatus(id) {
483
- return crud.dataStatus[id];
484
- },
485
- /**
486
- * 用于树形表格多选, 选中所有
487
- * @param selection
488
- */
489
- selectAllChange(selection) {
490
- // 如果选中的数目与请求到的数目相同就选中子节点,否则就清空选中
491
- if (selection && selection.length === crud.data.length) {
492
- selection.forEach(val => {
493
- crud.selectChange(selection, val);
494
- });
495
- } else {
496
- crud.getTable().clearSelection();
497
- }
498
- },
499
- /**
500
- * 用于树形表格多选,单选的封装
501
- * @param selection
502
- * @param row
503
- */
504
- selectChange(selection, row) {
505
- // 如果selection中存在row代表是选中,否则是取消选中
506
- if (
507
- selection.find(val => {
508
- return crud.getDataId(val) === crud.getDataId(row);
509
- })
510
- ) {
511
- if (row.children) {
512
- row.children.forEach(val => {
513
- crud.getTable().toggleRowSelection(val, true);
514
- selection.push(val);
515
- if (val.children) {
516
- crud.selectChange(selection, val);
517
- }
518
- });
519
- }
520
- } else {
521
- crud.toggleRowSelection(selection, row);
522
- }
523
- },
524
- /**
525
- * 切换选中状态
526
- * @param selection
527
- * @param data
528
- */
529
- toggleRowSelection(selection, data) {
530
- if (data.children) {
531
- data.children.forEach(val => {
532
- crud.getTable().toggleRowSelection(val, false);
533
- if (val.children) {
534
- crud.toggleRowSelection(selection, val);
535
- }
536
- });
537
- }
538
- },
539
- findVM(type) {
540
- return crud.vms.find(vm => vm && vm.type === type).vm;
541
- },
542
- notify(title, type = CRUD.NOTIFICATION_TYPE.INFO) {
543
- crud.vms[0].vm.$notify({
544
- title,
545
- type,
546
- duration: 2500,
547
- });
548
- },
549
- updateProp(name, value) {
550
- Vue.set(crud.props, name, value);
551
- },
552
- getDataId(data) {
553
- return data[this.idField];
554
- },
555
- getTable() {
556
- return this.findVM('presenter').$refs.table;
557
- },
558
- attchTable() {
559
- const table = this.getTable();
560
- this.updateProp('table', table);
561
- const that = this;
562
- table.$on('expand-change', (row, expanded) => {
563
- if (!expanded) {
564
- return;
565
- }
566
- const lazyTreeNodeMap = table.store.states.lazyTreeNodeMap;
567
- row.children = lazyTreeNodeMap[crud.getDataId(row)];
568
- if (row.children) {
569
- row.children.forEach(ele => {
570
- const id = crud.getDataId(ele);
571
- if (that.dataStatus[id] === undefined) {
572
- that.dataStatus[id] = {
573
- delete: 0,
574
- edit: 0,
575
- };
576
- }
577
- });
578
- }
579
- });
580
- },
581
- };
582
- const crud = Object.assign({}, data);
583
- // 可观测化
584
- Vue.observable(crud);
585
- // 附加方法
586
- Object.assign(crud, methods);
587
- // 记录初始默认的查询参数,后续重置查询时使用
588
- Object.assign(crud, {
589
- defaultQuery: JSON.parse(JSON.stringify(data.query)),
590
- // 预留4位存储:组件 主页、头部、分页、表单,调试查看也方便找
591
- vms: Array(4),
592
- /**
593
- * 注册组件实例
594
- * @param {String} type 类型
595
- * @param {*} vm 组件实例
596
- * @param {Number} index 该参数内部使用
597
- */
598
- registerVM(type, vm, index = -1) {
599
- const vmObj = {
600
- type,
601
- vm: vm,
602
- };
603
- if (index < 0) {
604
- this.vms.push(vmObj);
605
- return;
606
- }
607
- if (index < 4) {
608
- // 内置预留vm数
609
- this.vms[index] = vmObj;
610
- return;
611
- }
612
- this.vms.length = Math.max(this.vms.length, index);
613
- this.vms.splice(index, 1, vmObj);
614
- },
615
- /**
616
- * 取消注册组件实例
617
- * @param {*} vm 组件实例
618
- */
619
- unregisterVM(type, vm) {
620
- for (let i = this.vms.length - 1; i >= 0; i--) {
621
- if (this.vms[i] === undefined) {
622
- continue;
623
- }
624
- if (this.vms[i].type === type && this.vms[i].vm === vm) {
625
- if (i < 4) {
626
- // 内置预留vm数
627
- this.vms[i] = undefined;
628
- } else {
629
- this.vms.splice(i, 1);
630
- }
631
- break;
632
- }
633
- }
634
- },
635
- });
636
- // 冻结处理,需要扩展数据的话,使用crud.updateProp(name, value),以crud.props.name形式访问,这个是响应式的,可以做数据绑定
637
- Object.freeze(crud);
638
- return crud;
639
- }
640
-
641
- // hook VM
642
- function callVmHook(crud, hook) {
643
- const tagHook = crud.tag ? hook + '$' + crud.tag : null;
644
- let ret = true;
645
- const nargs = [crud];
646
- for (let i = 2; i < arguments.length; ++i) {
647
- nargs.push(arguments[i]);
648
- }
649
- // 有些组件扮演了多个角色,调用钩子时,需要去重
650
- const vmSet = new Set();
651
- crud.vms.forEach(vm => vm && vmSet.add(vm.vm));
652
- vmSet.forEach(vm => {
653
- if (vm[hook]) {
654
- ret = vm[hook].apply(vm, nargs) !== false && ret;
655
- }
656
- if (tagHook && vm[tagHook]) {
657
- ret = vm[tagHook].apply(vm, nargs) !== false && ret;
658
- }
659
- });
660
- return ret;
661
- }
662
-
663
- function mergeOptions(src, opts) {
664
- const optsRet = {
665
- ...src,
666
- };
667
- for (const key in src) {
668
- if (opts.hasOwnProperty(key)) {
669
- optsRet[key] = opts[key];
670
- }
671
- }
672
- return optsRet;
673
- }
674
-
675
- /**
676
- * 查找crud
677
- * @param {*} vm
678
- * @param {string} tag
679
- */
680
- function lookupCrud(vm, tag) {
681
- tag = tag || vm.$attrs['crud-tag'] || 'default';
682
- // function lookupCrud(vm, tag) {
683
- if (vm.$crud) {
684
- const ret = vm.$crud[tag];
685
- if (ret) {
686
- return ret;
687
- }
688
- }
689
- return vm.$parent ? lookupCrud(vm.$parent, tag) : undefined;
690
- }
691
-
692
- /**
693
- * crud主页
694
- */
695
- function presenter(crud) {
696
- if (crud) {
697
- console.warn(
698
- '[CRUD warn]: ' + 'please use $options.cruds() { return CRUD(...) or [CRUD(...), ...] }'
699
- );
700
- }
701
- return {
702
- data() {
703
- // 在data中返回crud,是为了将crud与当前实例关联,组件观测crud相关属性变化
704
- return {
705
- crud: this.crud,
706
- };
707
- },
708
- beforeCreate() {
709
- this.$crud = this.$crud || {};
710
- let cruds = this.$options.cruds instanceof Function ? this.$options.cruds() : crud;
711
- if (!(cruds instanceof Array)) {
712
- cruds = [cruds];
713
- }
714
- cruds.forEach(ele => {
715
- if (this.$crud[ele.tag]) {
716
- console.error('[CRUD error]: ' + 'crud with tag [' + ele.tag + ' is already exist');
717
- }
718
- this.$crud[ele.tag] = ele;
719
- ele.registerVM('presenter', this, 0);
720
- });
721
- this.crud = this.$crud['defalut'] || cruds[0];
722
- },
723
- methods: {
724
- parseTime,
725
- },
726
- created() {
727
- for (const k in this.$crud) {
728
- if (this.$crud[k].queryOnPresenterCreated) {
729
- this.$crud[k].toQuery();
730
- }
731
- }
732
- },
733
- destroyed() {
734
- for (const k in this.$crud) {
735
- this.$crud[k].unregisterVM('presenter', this);
736
- }
737
- },
738
- mounted() {
739
- // 如果table未实例化(例如使用了v-if),请稍后在适当时机crud.attchTable刷新table信息
740
- if (this.$refs.table !== undefined) {
741
- this.crud.attchTable();
742
- }
743
- },
744
- };
745
- }
746
-
747
- /**
748
- * 头部
749
- */
750
- function header() {
751
- return {
752
- data() {
753
- return {
754
- crud: this.crud,
755
- query: this.crud.query,
756
- };
757
- },
758
- beforeCreate() {
759
- this.crud = lookupCrud(this);
760
- this.crud.registerVM('header', this, 1);
761
- },
762
- destroyed() {
763
- this.crud.unregisterVM('header', this);
764
- },
765
- };
766
- }
767
-
768
- /**
769
- * 分页
770
- */
771
- function pagination() {
772
- return {
773
- data() {
774
- return {
775
- crud: this.crud,
776
- page: this.crud.page,
777
- };
778
- },
779
- beforeCreate() {
780
- this.crud = lookupCrud(this);
781
- this.crud.registerVM('pagination', this, 2);
782
- },
783
- destroyed() {
784
- this.crud.unregisterVM('pagination', this);
785
- },
786
- };
787
- }
788
-
789
- /**
790
- * 表单
791
- */
792
- function form(defaultForm) {
793
- return {
794
- data() {
795
- return {
796
- crud: this.crud,
797
- form: this.crud.form,
798
- };
799
- },
800
- beforeCreate() {
801
- this.crud = lookupCrud(this);
802
- this.crud.registerVM('form', this, 3);
803
- },
804
- created() {
805
- this.crud.defaultForm = defaultForm;
806
- this.crud.resetForm();
807
- },
808
- destroyed() {
809
- this.crud.unregisterVM('form', this);
810
- },
811
- };
812
- }
813
-
814
- /**
815
- * crud
816
- */
817
- function crud(options = {}) {
818
- const defaultOptions = {
819
- type: undefined,
820
- };
821
- options = mergeOptions(defaultOptions, options);
822
- return {
823
- data() {
824
- return {
825
- crud: this.crud,
826
- };
827
- },
828
- beforeCreate() {
829
- this.crud = lookupCrud(this);
830
- this.crud.registerVM(options.type, this);
831
- },
832
- destroyed() {
833
- this.crud.unregisterVM(options.type, this);
834
- },
835
- };
836
- }
837
-
838
- /**
839
- * CRUD钩子
840
- */
841
- CRUD.HOOK = {
842
- /** 刷新 - 之前 */
843
- beforeRefresh: 'beforeCrudRefresh',
844
- /** 刷新 - 之后 */
845
- afterRefresh: 'afterCrudRefresh',
846
- /** 删除 - 之前 */
847
- beforeDelete: 'beforeCrudDelete',
848
- /** 删除 - 之后 */
849
- afterDelete: 'afterCrudDelete',
850
- /** 删除取消 - 之前 */
851
- beforeDeleteCancel: 'beforeCrudDeleteCancel',
852
- /** 删除取消 - 之后 */
853
- afterDeleteCancel: 'afterCrudDeleteCancel',
854
- /** 新建 - 之前 */
855
- beforeToAdd: 'beforeCrudToAdd',
856
- /** 新建 - 之后 */
857
- afterToAdd: 'afterCrudToAdd',
858
- /** 编辑 - 之前 */
859
- beforeToEdit: 'beforeCrudToEdit',
860
- /** 编辑 - 之后 */
861
- afterToEdit: 'afterCrudToEdit',
862
- /** 开始 "新建/编辑" - 之前 */
863
- beforeToCU: 'beforeCrudToCU',
864
- /** 开始 "新建/编辑" - 之后 */
865
- afterToCU: 'afterCrudToCU',
866
- /** "新建/编辑" 验证 - 之前 */
867
- beforeValidateCU: 'beforeCrudValidateCU',
868
- /** "新建/编辑" 验证 - 之后 */
869
- afterValidateCU: 'afterCrudValidateCU',
870
- /** 添加取消 - 之前 */
871
- beforeAddCancel: 'beforeCrudAddCancel',
872
- /** 添加取消 - 之后 */
873
- afterAddCancel: 'afterCrudAddCancel',
874
- /** 编辑取消 - 之前 */
875
- beforeEditCancel: 'beforeCrudEditCancel',
876
- /** 编辑取消 - 之后 */
877
- afterEditCancel: 'afterCrudEditCancel',
878
- /** 提交 - 之前 */
879
- beforeSubmit: 'beforeCrudSubmitCU',
880
- /** 提交 - 之后 */
881
- afterSubmit: 'afterCrudSubmitCU',
882
- afterAddError: 'afterCrudAddError',
883
- afterEditError: 'afterCrudEditError',
884
- };
885
-
886
- /**
887
- * CRUD状态
888
- */
889
- CRUD.STATUS = {
890
- NORMAL: 0,
891
- PREPARED: 1,
892
- PROCESSING: 2,
893
- };
894
-
895
- /**
896
- * CRUD通知类型
897
- */
898
- CRUD.NOTIFICATION_TYPE = {
899
- SUCCESS: 'success',
900
- WARNING: 'warning',
901
- INFO: 'info',
902
- ERROR: 'error',
903
- };
904
-
905
- export default CRUD;
906
-
907
- export { presenter, header, form, pagination, crud };
1
+ import { initData, initDataPost, download } from '@/api/data';
2
+ import { parseTime, downloadFile } from '@/utils/index';
3
+ import Vue from 'vue';
4
+
5
+ /**
6
+ * CRUD配置
7
+ * @author moxun
8
+ * @param {*} options <br>
9
+ * @return crud instance.
10
+ * @example
11
+ * 要使用多crud时,请在关联crud的组件处使用crud-tag进行标记,如:<jobForm :job-status="dict.job_status" crud-tag="job" />
12
+ */
13
+ function CRUD(options) {
14
+ const defaultOptions = {
15
+ tag: 'default',
16
+ // id字段名
17
+ idField: 'id',
18
+ // 标题
19
+ title: '',
20
+ // 请求数据的url
21
+ url: '',
22
+ // 表格数据
23
+ data: [],
24
+ // 选择项
25
+ selections: [],
26
+ // 待查询的对象
27
+ query: {},
28
+ // 查询数据的参数
29
+ params: {},
30
+ // Form 表单
31
+ form: {},
32
+ // 重置表单
33
+ defaultForm: () => {},
34
+ // 排序规则,默认 id 降序, 支持多字段排序 ['id,desc', 'createTime,asc']
35
+ sort: ['id,desc'],
36
+ // 等待时间
37
+ time: 50,
38
+ // CRUD Method
39
+ crudMethod: {
40
+ add: form => {},
41
+ del: id => {},
42
+ edit: form => {},
43
+ get: id => {},
44
+ },
45
+ // 主页操作栏显示哪些按钮
46
+ optShow: {
47
+ add: true,
48
+ edit: false,
49
+ del: false,
50
+ download: false,
51
+ reset: false,
52
+ },
53
+ // 自定义一些扩展属性
54
+ props: {},
55
+ // 在主页准备
56
+ queryOnPresenterCreated: true,
57
+ // 调试开关
58
+ debug: false,
59
+ type: '',
60
+ };
61
+ options = mergeOptions(defaultOptions, options);
62
+ const data = {
63
+ ...options,
64
+ // 记录数据状态
65
+ dataStatus: {},
66
+ status: {
67
+ add: CRUD.STATUS.NORMAL,
68
+ edit: CRUD.STATUS.NORMAL,
69
+ // 添加或编辑状态
70
+ get cu() {
71
+ if (this.add === CRUD.STATUS.NORMAL && this.edit === CRUD.STATUS.NORMAL) {
72
+ return CRUD.STATUS.NORMAL;
73
+ } else if (this.add === CRUD.STATUS.PREPARED || this.edit === CRUD.STATUS.PREPARED) {
74
+ return CRUD.STATUS.PREPARED;
75
+ } else if (this.add === CRUD.STATUS.PROCESSING || this.edit === CRUD.STATUS.PROCESSING) {
76
+ return CRUD.STATUS.PROCESSING;
77
+ }
78
+ throw new Error("wrong crud's cu status");
79
+ },
80
+ // 标题
81
+ get title() {
82
+ return this.add > CRUD.STATUS.NORMAL
83
+ ? `新增${crud.title}`
84
+ : this.edit > CRUD.STATUS.NORMAL
85
+ ? `编辑${crud.title}`
86
+ : crud.title;
87
+ },
88
+ },
89
+ msg: {
90
+ submit: '提交成功',
91
+ add: '新增成功',
92
+ edit: '编辑成功',
93
+ del: '删除成功',
94
+ },
95
+ page: {
96
+ // 页码
97
+ page: 0,
98
+ // 每页数据条数
99
+ size: 10,
100
+ // 总数据条数
101
+ total: 0,
102
+ },
103
+ // 整体loading
104
+ loading: false,
105
+ // 导出的 Loading
106
+ downloadLoading: false,
107
+ // 删除的 Loading
108
+ delAllLoading: false,
109
+ };
110
+ const methods = {
111
+ /**
112
+ * 通用的提示
113
+ */
114
+ submitSuccessNotify() {
115
+ crud.notify(crud.msg.submit, CRUD.NOTIFICATION_TYPE.SUCCESS);
116
+ },
117
+ addSuccessNotify() {
118
+ crud.notify(crud.msg.add, CRUD.NOTIFICATION_TYPE.SUCCESS);
119
+ },
120
+ editSuccessNotify() {
121
+ crud.notify(crud.msg.edit, CRUD.NOTIFICATION_TYPE.SUCCESS);
122
+ },
123
+ delSuccessNotify() {
124
+ crud.notify(crud.msg.del, CRUD.NOTIFICATION_TYPE.SUCCESS);
125
+ },
126
+ // 搜索
127
+ toQuery() {
128
+ crud.page.page = 1;
129
+ crud.refresh();
130
+ },
131
+ // 刷新
132
+ refresh() {
133
+ if (!callVmHook(crud, CRUD.HOOK.beforeRefresh)) {
134
+ return;
135
+ }
136
+ // 没有地址的时候 阻止进行下一步
137
+ if (!crud.url) return;
138
+ return new Promise((resolve, reject) => {
139
+ crud.loading = true;
140
+ // 请求数据
141
+ let initDataType = crud.type == 'post' ? initDataPost : initData;
142
+ initDataType(crud.url, crud.getQueryParams())
143
+ .then(data => {
144
+ const table = crud.getTable();
145
+ if (table && table.lazy) {
146
+ // 懒加载子节点数据,清掉已加载的数据
147
+ table.store.states.treeData = {};
148
+ table.store.states.lazyTreeNodeMap = {};
149
+ }
150
+ crud.page.total = data.totalElements;
151
+ crud.data = data.content;
152
+ crud.resetDataStatus();
153
+ // time 毫秒后显示表格
154
+ setTimeout(() => {
155
+ crud.loading = false;
156
+ callVmHook(crud, CRUD.HOOK.afterRefresh);
157
+ }, crud.time);
158
+ resolve(data);
159
+ })
160
+ .catch(err => {
161
+ crud.loading = false;
162
+ reject(err);
163
+ });
164
+ });
165
+ },
166
+ /**
167
+ * 启动添加
168
+ */
169
+ toAdd() {
170
+ crud.resetForm();
171
+ if (
172
+ !(
173
+ callVmHook(crud, CRUD.HOOK.beforeToAdd, crud.form) &&
174
+ callVmHook(crud, CRUD.HOOK.beforeToCU, crud.form)
175
+ )
176
+ ) {
177
+ return;
178
+ }
179
+ crud.status.add = CRUD.STATUS.PREPARED;
180
+ callVmHook(crud, CRUD.HOOK.afterToAdd, crud.form);
181
+ callVmHook(crud, CRUD.HOOK.afterToCU, crud.form);
182
+ },
183
+ /**
184
+ * 启动编辑
185
+ * @param {*} data 数据项
186
+ */
187
+ toEdit(data) {
188
+ crud.resetForm(JSON.parse(JSON.stringify(data)));
189
+ if (
190
+ !(
191
+ callVmHook(crud, CRUD.HOOK.beforeToEdit, crud.form) &&
192
+ callVmHook(crud, CRUD.HOOK.beforeToCU, crud.form)
193
+ )
194
+ ) {
195
+ return;
196
+ }
197
+ crud.status.edit = CRUD.STATUS.PREPARED;
198
+ crud.getDataStatus(crud.getDataId(data)).edit = CRUD.STATUS.PREPARED;
199
+ callVmHook(crud, CRUD.HOOK.afterToEdit, crud.form);
200
+ callVmHook(crud, CRUD.HOOK.afterToCU, crud.form);
201
+ },
202
+ /**
203
+ * 启动删除
204
+ * @param {*} data 数据项
205
+ */
206
+ toDelete(data) {
207
+ crud.getDataStatus(crud.getDataId(data)).delete = CRUD.STATUS.PREPARED;
208
+ },
209
+ /**
210
+ * 取消删除
211
+ * @param {*} data 数据项
212
+ */
213
+ cancelDelete(data) {
214
+ if (!callVmHook(crud, CRUD.HOOK.beforeDeleteCancel, data)) {
215
+ return;
216
+ }
217
+ crud.getDataStatus(crud.getDataId(data)).delete = CRUD.STATUS.NORMAL;
218
+ callVmHook(crud, CRUD.HOOK.afterDeleteCancel, data);
219
+ },
220
+ /**
221
+ * 取消新增/编辑
222
+ */
223
+ cancelCU() {
224
+ const addStatus = crud.status.add;
225
+ const editStatus = crud.status.edit;
226
+ if (addStatus === CRUD.STATUS.PREPARED) {
227
+ if (!callVmHook(crud, CRUD.HOOK.beforeAddCancel, crud.form)) {
228
+ return;
229
+ }
230
+ crud.status.add = CRUD.STATUS.NORMAL;
231
+ }
232
+ if (editStatus === CRUD.STATUS.PREPARED) {
233
+ if (!callVmHook(crud, CRUD.HOOK.beforeEditCancel, crud.form)) {
234
+ return;
235
+ }
236
+ crud.status.edit = CRUD.STATUS.NORMAL;
237
+ crud.getDataStatus(crud.getDataId(crud.form)).edit = CRUD.STATUS.NORMAL;
238
+ }
239
+ crud.resetForm();
240
+ if (addStatus === CRUD.STATUS.PREPARED) {
241
+ callVmHook(crud, CRUD.HOOK.afterAddCancel, crud.form);
242
+ }
243
+ if (editStatus === CRUD.STATUS.PREPARED) {
244
+ callVmHook(crud, CRUD.HOOK.afterEditCancel, crud.form);
245
+ }
246
+ // 清除表单验证
247
+ if (crud.findVM('form').$refs['form']) {
248
+ crud.findVM('form').$refs['form'].clearValidate();
249
+ }
250
+ },
251
+ /**
252
+ * 提交新增/编辑
253
+ */
254
+ submitCU() {
255
+ if (!callVmHook(crud, CRUD.HOOK.beforeValidateCU)) {
256
+ return;
257
+ }
258
+ crud.findVM('form').$refs['form'].validate(valid => {
259
+ if (!valid) {
260
+ return;
261
+ }
262
+ if (!callVmHook(crud, CRUD.HOOK.afterValidateCU)) {
263
+ return;
264
+ }
265
+ if (crud.status.add === CRUD.STATUS.PREPARED) {
266
+ crud.doAdd();
267
+ } else if (crud.status.edit === CRUD.STATUS.PREPARED) {
268
+ crud.doEdit();
269
+ }
270
+ });
271
+ },
272
+ /**
273
+ * 执行添加
274
+ */
275
+ doAdd() {
276
+ if (!callVmHook(crud, CRUD.HOOK.beforeSubmit)) {
277
+ return;
278
+ }
279
+ crud.status.add = CRUD.STATUS.PROCESSING;
280
+ crud.crudMethod
281
+ .add(crud.form)
282
+ .then(() => {
283
+ crud.status.add = CRUD.STATUS.NORMAL;
284
+ crud.resetForm();
285
+ crud.addSuccessNotify();
286
+ callVmHook(crud, CRUD.HOOK.afterSubmit);
287
+ crud.toQuery();
288
+ })
289
+ .catch(() => {
290
+ crud.status.add = CRUD.STATUS.PREPARED;
291
+ callVmHook(crud, CRUD.HOOK.afterAddError);
292
+ });
293
+ },
294
+ /**
295
+ * 执行编辑
296
+ */
297
+ doEdit() {
298
+ if (!callVmHook(crud, CRUD.HOOK.beforeSubmit)) {
299
+ return;
300
+ }
301
+ crud.status.edit = CRUD.STATUS.PROCESSING;
302
+ crud.crudMethod
303
+ .edit(crud.form)
304
+ .then(() => {
305
+ crud.status.edit = CRUD.STATUS.NORMAL;
306
+ crud.getDataStatus(crud.getDataId(crud.form)).edit = CRUD.STATUS.NORMAL;
307
+ crud.editSuccessNotify();
308
+ crud.resetForm();
309
+ callVmHook(crud, CRUD.HOOK.afterSubmit);
310
+ crud.refresh();
311
+ })
312
+ .catch(() => {
313
+ crud.status.edit = CRUD.STATUS.PREPARED;
314
+ callVmHook(crud, CRUD.HOOK.afterEditError);
315
+ });
316
+ },
317
+ /**
318
+ * 执行删除
319
+ * @param {*} data 数据项
320
+ */
321
+ doDelete(data) {
322
+ let delAll = false;
323
+ let dataStatus;
324
+ const ids = [];
325
+ if (data instanceof Array) {
326
+ delAll = true;
327
+ data.forEach(val => {
328
+ ids.push(this.getDataId(val));
329
+ });
330
+ } else {
331
+ ids.push(this.getDataId(data));
332
+ dataStatus = crud.getDataStatus(this.getDataId(data));
333
+ }
334
+ if (!callVmHook(crud, CRUD.HOOK.beforeDelete, data)) {
335
+ return;
336
+ }
337
+ if (!delAll) {
338
+ dataStatus.delete = CRUD.STATUS.PROCESSING;
339
+ }
340
+ return crud.crudMethod
341
+ .del(ids)
342
+ .then(() => {
343
+ if (delAll) {
344
+ crud.delAllLoading = false;
345
+ } else dataStatus.delete = CRUD.STATUS.PREPARED;
346
+ crud.dleChangePage(1);
347
+ crud.delSuccessNotify();
348
+ callVmHook(crud, CRUD.HOOK.afterDelete, data);
349
+ crud.refresh();
350
+ })
351
+ .catch(() => {
352
+ if (delAll) {
353
+ crud.delAllLoading = false;
354
+ } else dataStatus.delete = CRUD.STATUS.PREPARED;
355
+ });
356
+ },
357
+ /**
358
+ * 通用导出
359
+ */
360
+ doExport() {
361
+ crud.downloadLoading = true;
362
+ download(crud.url + '/download', crud.getQueryParams())
363
+ .then(result => {
364
+ crud.downloadLoading = false;
365
+ })
366
+ .catch(result => {
367
+ downloadFile(result, crud.title + '数据', 'xlsx');
368
+ crud.downloadLoading = false;
369
+ });
370
+ },
371
+ /**
372
+ * 获取查询参数
373
+ */
374
+ getQueryParams: function () {
375
+ //字符串清空前后空格
376
+ Object.keys(crud.query).map(key => {
377
+ if (typeof crud.query[key] == 'string') {
378
+ crud.query[key] = crud.query[key].trim();
379
+ }
380
+ });
381
+ // 清除参数无值的情况
382
+ Object.keys(crud.query).length !== 0 &&
383
+ Object.keys(crud.query).forEach(item => {
384
+ if (crud.query[item] === null || crud.query[item] === '') crud.query[item] = undefined;
385
+ });
386
+ Object.keys(crud.params).length !== 0 &&
387
+ Object.keys(crud.params).forEach(item => {
388
+ if (crud.params[item] === null || crud.params[item] === '') crud.params[item] = undefined;
389
+ });
390
+ return {
391
+ page: crud.page.page - 1,
392
+ size: crud.page.size,
393
+ sort: crud.sort,
394
+ ...crud.query,
395
+ ...crud.params,
396
+ };
397
+ },
398
+ // 当前页改变
399
+ pageChangeHandler(e) {
400
+ crud.page.page = e;
401
+ crud.refresh();
402
+ },
403
+ // 每页条数改变
404
+ sizeChangeHandler(e) {
405
+ crud.page.size = e;
406
+ crud.page.page = 1;
407
+ crud.refresh();
408
+ },
409
+ // 预防删除第二页最后一条数据时,或者多选删除第二页的数据时,页码错误导致请求无数据
410
+ dleChangePage(size) {
411
+ if (crud.data.length === size && crud.page.page !== 1) {
412
+ crud.page.page -= 1;
413
+ }
414
+ },
415
+ // 选择改变
416
+ selectionChangeHandler(val) {
417
+ crud.selections = val;
418
+ },
419
+ /**
420
+ * 重置查询参数
421
+ * @param {Boolean} toQuery 重置后进行查询操作
422
+ */
423
+ resetQuery(toQuery = true) {
424
+ const defaultQuery = JSON.parse(JSON.stringify(crud.defaultQuery));
425
+ const query = crud.query;
426
+ Object.keys(query).forEach(key => {
427
+ query[key] = defaultQuery[key];
428
+ });
429
+ // 重置参数
430
+ this.params = {};
431
+ if (toQuery) {
432
+ crud.toQuery();
433
+ }
434
+ },
435
+ /**
436
+ * 重置表单
437
+ * @param {Array} data 数据
438
+ */
439
+ resetForm(data) {
440
+ const form =
441
+ data ||
442
+ (typeof crud.defaultForm === 'object'
443
+ ? JSON.parse(JSON.stringify(crud.defaultForm))
444
+ : crud.defaultForm.apply(crud.findVM('form')));
445
+ const crudFrom = crud.form;
446
+ for (const key in form) {
447
+ if (crudFrom.hasOwnProperty(key)) {
448
+ crudFrom[key] = form[key];
449
+ } else {
450
+ Vue.set(crudFrom, key, form[key]);
451
+ }
452
+ }
453
+ // add by ghl 2020-10-04 页面重复添加信息时,下拉框的校验会存在,需要找工取消
454
+ if (crud.findVM('form').$refs['form']) {
455
+ crud.findVM('form').$refs['form'].clearValidate();
456
+ }
457
+ },
458
+ /**
459
+ * 重置数据状态
460
+ */
461
+ resetDataStatus() {
462
+ const dataStatus = {};
463
+ function resetStatus(datas) {
464
+ datas = datas || [];
465
+ datas.forEach(e => {
466
+ dataStatus[crud.getDataId(e)] = {
467
+ delete: 0,
468
+ edit: 0,
469
+ };
470
+ if (e.children) {
471
+ resetStatus(e.children);
472
+ }
473
+ });
474
+ }
475
+ resetStatus(crud.data);
476
+ crud.dataStatus = dataStatus;
477
+ },
478
+ /**
479
+ * 获取数据状态
480
+ * @param {Number | String} id 数据项id
481
+ */
482
+ getDataStatus(id) {
483
+ return crud.dataStatus[id];
484
+ },
485
+ /**
486
+ * 用于树形表格多选, 选中所有
487
+ * @param selection
488
+ */
489
+ selectAllChange(selection) {
490
+ // 如果选中的数目与请求到的数目相同就选中子节点,否则就清空选中
491
+ if (selection && selection.length === crud.data.length) {
492
+ selection.forEach(val => {
493
+ crud.selectChange(selection, val);
494
+ });
495
+ } else {
496
+ crud.getTable().clearSelection();
497
+ }
498
+ },
499
+ /**
500
+ * 用于树形表格多选,单选的封装
501
+ * @param selection
502
+ * @param row
503
+ */
504
+ selectChange(selection, row) {
505
+ // 如果selection中存在row代表是选中,否则是取消选中
506
+ if (
507
+ selection.find(val => {
508
+ return crud.getDataId(val) === crud.getDataId(row);
509
+ })
510
+ ) {
511
+ if (row.children) {
512
+ row.children.forEach(val => {
513
+ crud.getTable().toggleRowSelection(val, true);
514
+ selection.push(val);
515
+ if (val.children) {
516
+ crud.selectChange(selection, val);
517
+ }
518
+ });
519
+ }
520
+ } else {
521
+ crud.toggleRowSelection(selection, row);
522
+ }
523
+ },
524
+ /**
525
+ * 切换选中状态
526
+ * @param selection
527
+ * @param data
528
+ */
529
+ toggleRowSelection(selection, data) {
530
+ if (data.children) {
531
+ data.children.forEach(val => {
532
+ crud.getTable().toggleRowSelection(val, false);
533
+ if (val.children) {
534
+ crud.toggleRowSelection(selection, val);
535
+ }
536
+ });
537
+ }
538
+ },
539
+ findVM(type) {
540
+ return crud.vms.find(vm => vm && vm.type === type).vm;
541
+ },
542
+ notify(title, type = CRUD.NOTIFICATION_TYPE.INFO) {
543
+ crud.vms[0].vm.$notify({
544
+ title,
545
+ type,
546
+ duration: 2500,
547
+ });
548
+ },
549
+ updateProp(name, value) {
550
+ Vue.set(crud.props, name, value);
551
+ },
552
+ getDataId(data) {
553
+ return data[this.idField];
554
+ },
555
+ getTable() {
556
+ return this.findVM('presenter').$refs.table;
557
+ },
558
+ attchTable() {
559
+ const table = this.getTable();
560
+ this.updateProp('table', table);
561
+ const that = this;
562
+ table.$on('expand-change', (row, expanded) => {
563
+ if (!expanded) {
564
+ return;
565
+ }
566
+ const lazyTreeNodeMap = table.store.states.lazyTreeNodeMap;
567
+ row.children = lazyTreeNodeMap[crud.getDataId(row)];
568
+ if (row.children) {
569
+ row.children.forEach(ele => {
570
+ const id = crud.getDataId(ele);
571
+ if (that.dataStatus[id] === undefined) {
572
+ that.dataStatus[id] = {
573
+ delete: 0,
574
+ edit: 0,
575
+ };
576
+ }
577
+ });
578
+ }
579
+ });
580
+ },
581
+ };
582
+ const crud = Object.assign({}, data);
583
+ // 可观测化
584
+ Vue.observable(crud);
585
+ // 附加方法
586
+ Object.assign(crud, methods);
587
+ // 记录初始默认的查询参数,后续重置查询时使用
588
+ Object.assign(crud, {
589
+ defaultQuery: JSON.parse(JSON.stringify(data.query)),
590
+ // 预留4位存储:组件 主页、头部、分页、表单,调试查看也方便找
591
+ vms: Array(4),
592
+ /**
593
+ * 注册组件实例
594
+ * @param {String} type 类型
595
+ * @param {*} vm 组件实例
596
+ * @param {Number} index 该参数内部使用
597
+ */
598
+ registerVM(type, vm, index = -1) {
599
+ const vmObj = {
600
+ type,
601
+ vm: vm,
602
+ };
603
+ if (index < 0) {
604
+ this.vms.push(vmObj);
605
+ return;
606
+ }
607
+ if (index < 4) {
608
+ // 内置预留vm数
609
+ this.vms[index] = vmObj;
610
+ return;
611
+ }
612
+ this.vms.length = Math.max(this.vms.length, index);
613
+ this.vms.splice(index, 1, vmObj);
614
+ },
615
+ /**
616
+ * 取消注册组件实例
617
+ * @param {*} vm 组件实例
618
+ */
619
+ unregisterVM(type, vm) {
620
+ for (let i = this.vms.length - 1; i >= 0; i--) {
621
+ if (this.vms[i] === undefined) {
622
+ continue;
623
+ }
624
+ if (this.vms[i].type === type && this.vms[i].vm === vm) {
625
+ if (i < 4) {
626
+ // 内置预留vm数
627
+ this.vms[i] = undefined;
628
+ } else {
629
+ this.vms.splice(i, 1);
630
+ }
631
+ break;
632
+ }
633
+ }
634
+ },
635
+ });
636
+ // 冻结处理,需要扩展数据的话,使用crud.updateProp(name, value),以crud.props.name形式访问,这个是响应式的,可以做数据绑定
637
+ Object.freeze(crud);
638
+ return crud;
639
+ }
640
+
641
+ // hook VM
642
+ function callVmHook(crud, hook) {
643
+ const tagHook = crud.tag ? hook + '$' + crud.tag : null;
644
+ let ret = true;
645
+ const nargs = [crud];
646
+ for (let i = 2; i < arguments.length; ++i) {
647
+ nargs.push(arguments[i]);
648
+ }
649
+ // 有些组件扮演了多个角色,调用钩子时,需要去重
650
+ const vmSet = new Set();
651
+ crud.vms.forEach(vm => vm && vmSet.add(vm.vm));
652
+ vmSet.forEach(vm => {
653
+ if (vm[hook]) {
654
+ ret = vm[hook].apply(vm, nargs) !== false && ret;
655
+ }
656
+ if (tagHook && vm[tagHook]) {
657
+ ret = vm[tagHook].apply(vm, nargs) !== false && ret;
658
+ }
659
+ });
660
+ return ret;
661
+ }
662
+
663
+ function mergeOptions(src, opts) {
664
+ const optsRet = {
665
+ ...src,
666
+ };
667
+ for (const key in src) {
668
+ if (opts.hasOwnProperty(key)) {
669
+ optsRet[key] = opts[key];
670
+ }
671
+ }
672
+ return optsRet;
673
+ }
674
+
675
+ /**
676
+ * 查找crud
677
+ * @param {*} vm
678
+ * @param {string} tag
679
+ */
680
+ function lookupCrud(vm, tag) {
681
+ tag = tag || vm.$attrs['crud-tag'] || 'default';
682
+ // function lookupCrud(vm, tag) {
683
+ if (vm.$crud) {
684
+ const ret = vm.$crud[tag];
685
+ if (ret) {
686
+ return ret;
687
+ }
688
+ }
689
+ return vm.$parent ? lookupCrud(vm.$parent, tag) : undefined;
690
+ }
691
+
692
+ /**
693
+ * crud主页
694
+ */
695
+ function presenter(crud) {
696
+ if (crud) {
697
+ console.warn(
698
+ '[CRUD warn]: ' + 'please use $options.cruds() { return CRUD(...) or [CRUD(...), ...] }'
699
+ );
700
+ }
701
+ return {
702
+ data() {
703
+ // 在data中返回crud,是为了将crud与当前实例关联,组件观测crud相关属性变化
704
+ return {
705
+ crud: this.crud,
706
+ };
707
+ },
708
+ beforeCreate() {
709
+ this.$crud = this.$crud || {};
710
+ let cruds = this.$options.cruds instanceof Function ? this.$options.cruds() : crud;
711
+ if (!(cruds instanceof Array)) {
712
+ cruds = [cruds];
713
+ }
714
+ cruds.forEach(ele => {
715
+ if (this.$crud[ele.tag]) {
716
+ console.error('[CRUD error]: ' + 'crud with tag [' + ele.tag + ' is already exist');
717
+ }
718
+ this.$crud[ele.tag] = ele;
719
+ ele.registerVM('presenter', this, 0);
720
+ });
721
+ this.crud = this.$crud['defalut'] || cruds[0];
722
+ },
723
+ methods: {
724
+ parseTime,
725
+ },
726
+ created() {
727
+ for (const k in this.$crud) {
728
+ if (this.$crud[k].queryOnPresenterCreated) {
729
+ this.$crud[k].toQuery();
730
+ }
731
+ }
732
+ },
733
+ destroyed() {
734
+ for (const k in this.$crud) {
735
+ this.$crud[k].unregisterVM('presenter', this);
736
+ }
737
+ },
738
+ mounted() {
739
+ // 如果table未实例化(例如使用了v-if),请稍后在适当时机crud.attchTable刷新table信息
740
+ if (this.$refs.table !== undefined) {
741
+ this.crud.attchTable();
742
+ }
743
+ },
744
+ };
745
+ }
746
+
747
+ /**
748
+ * 头部
749
+ */
750
+ function header() {
751
+ return {
752
+ data() {
753
+ return {
754
+ crud: this.crud,
755
+ query: this.crud.query,
756
+ };
757
+ },
758
+ beforeCreate() {
759
+ this.crud = lookupCrud(this);
760
+ this.crud.registerVM('header', this, 1);
761
+ },
762
+ destroyed() {
763
+ this.crud.unregisterVM('header', this);
764
+ },
765
+ };
766
+ }
767
+
768
+ /**
769
+ * 分页
770
+ */
771
+ function pagination() {
772
+ return {
773
+ data() {
774
+ return {
775
+ crud: this.crud,
776
+ page: this.crud.page,
777
+ };
778
+ },
779
+ beforeCreate() {
780
+ this.crud = lookupCrud(this);
781
+ this.crud.registerVM('pagination', this, 2);
782
+ },
783
+ destroyed() {
784
+ this.crud.unregisterVM('pagination', this);
785
+ },
786
+ };
787
+ }
788
+
789
+ /**
790
+ * 表单
791
+ */
792
+ function form(defaultForm) {
793
+ return {
794
+ data() {
795
+ return {
796
+ crud: this.crud,
797
+ form: this.crud.form,
798
+ };
799
+ },
800
+ beforeCreate() {
801
+ this.crud = lookupCrud(this);
802
+ this.crud.registerVM('form', this, 3);
803
+ },
804
+ created() {
805
+ this.crud.defaultForm = defaultForm;
806
+ this.crud.resetForm();
807
+ },
808
+ destroyed() {
809
+ this.crud.unregisterVM('form', this);
810
+ },
811
+ };
812
+ }
813
+
814
+ /**
815
+ * crud
816
+ */
817
+ function crud(options = {}) {
818
+ const defaultOptions = {
819
+ type: undefined,
820
+ };
821
+ options = mergeOptions(defaultOptions, options);
822
+ return {
823
+ data() {
824
+ return {
825
+ crud: this.crud,
826
+ };
827
+ },
828
+ beforeCreate() {
829
+ this.crud = lookupCrud(this);
830
+ this.crud.registerVM(options.type, this);
831
+ },
832
+ destroyed() {
833
+ this.crud.unregisterVM(options.type, this);
834
+ },
835
+ };
836
+ }
837
+
838
+ /**
839
+ * CRUD钩子
840
+ */
841
+ CRUD.HOOK = {
842
+ /** 刷新 - 之前 */
843
+ beforeRefresh: 'beforeCrudRefresh',
844
+ /** 刷新 - 之后 */
845
+ afterRefresh: 'afterCrudRefresh',
846
+ /** 删除 - 之前 */
847
+ beforeDelete: 'beforeCrudDelete',
848
+ /** 删除 - 之后 */
849
+ afterDelete: 'afterCrudDelete',
850
+ /** 删除取消 - 之前 */
851
+ beforeDeleteCancel: 'beforeCrudDeleteCancel',
852
+ /** 删除取消 - 之后 */
853
+ afterDeleteCancel: 'afterCrudDeleteCancel',
854
+ /** 新建 - 之前 */
855
+ beforeToAdd: 'beforeCrudToAdd',
856
+ /** 新建 - 之后 */
857
+ afterToAdd: 'afterCrudToAdd',
858
+ /** 编辑 - 之前 */
859
+ beforeToEdit: 'beforeCrudToEdit',
860
+ /** 编辑 - 之后 */
861
+ afterToEdit: 'afterCrudToEdit',
862
+ /** 开始 "新建/编辑" - 之前 */
863
+ beforeToCU: 'beforeCrudToCU',
864
+ /** 开始 "新建/编辑" - 之后 */
865
+ afterToCU: 'afterCrudToCU',
866
+ /** "新建/编辑" 验证 - 之前 */
867
+ beforeValidateCU: 'beforeCrudValidateCU',
868
+ /** "新建/编辑" 验证 - 之后 */
869
+ afterValidateCU: 'afterCrudValidateCU',
870
+ /** 添加取消 - 之前 */
871
+ beforeAddCancel: 'beforeCrudAddCancel',
872
+ /** 添加取消 - 之后 */
873
+ afterAddCancel: 'afterCrudAddCancel',
874
+ /** 编辑取消 - 之前 */
875
+ beforeEditCancel: 'beforeCrudEditCancel',
876
+ /** 编辑取消 - 之后 */
877
+ afterEditCancel: 'afterCrudEditCancel',
878
+ /** 提交 - 之前 */
879
+ beforeSubmit: 'beforeCrudSubmitCU',
880
+ /** 提交 - 之后 */
881
+ afterSubmit: 'afterCrudSubmitCU',
882
+ afterAddError: 'afterCrudAddError',
883
+ afterEditError: 'afterCrudEditError',
884
+ };
885
+
886
+ /**
887
+ * CRUD状态
888
+ */
889
+ CRUD.STATUS = {
890
+ NORMAL: 0,
891
+ PREPARED: 1,
892
+ PROCESSING: 2,
893
+ };
894
+
895
+ /**
896
+ * CRUD通知类型
897
+ */
898
+ CRUD.NOTIFICATION_TYPE = {
899
+ SUCCESS: 'success',
900
+ WARNING: 'warning',
901
+ INFO: 'info',
902
+ ERROR: 'error',
903
+ };
904
+
905
+ export default CRUD;
906
+
907
+ export { presenter, header, form, pagination, crud };