gen-pages.mjs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * 生成各分包薄壳页面(通用模板页,module 参数由注册表驱动)
  3. * 用法:node scripts/gen-pages.mjs
  4. */
  5. import fs from 'node:fs'
  6. import path from 'node:path'
  7. import { fileURLToPath } from 'node:url'
  8. const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
  9. const BILL_PKGS = [
  10. 'packageBasic', 'packageMaterial', 'packagePurchase', 'packageSales',
  11. 'packageRetail', 'packageWarehouse', 'packageFinance', 'packageProductionLogistics'
  12. ]
  13. const REPORT_PKGS = ['packageReport']
  14. const LIST_VUE = `<template>
  15. <view class="pkg-list-page">
  16. <BillListView ref="viewRef" :module-key="moduleKey" />
  17. <!-- 新增入口(P2 写入能力,表单页按 schema 判定是否可编辑) -->
  18. <view v-if="canCreate" class="fab" @click="goCreate">
  19. <text class="fab__text">+</text>
  20. </view>
  21. </view>
  22. </template>
  23. <script setup lang="ts">
  24. /**
  25. * 分包通用列表页壳:注册表 module 参数驱动,挂接页面级生命周期
  26. */
  27. import { ref, computed } from 'vue'
  28. import { onLoad, onReachBottom, onPullDownRefresh } from '@dcloudio/uni-app'
  29. import BillListView from '@/modules/bill/BillListView.vue'
  30. import { getModule } from '@/modules/registry'
  31. import { getEntitySchema, BILL_ITEM_MODULES } from '@/modules/bill/formSchemas'
  32. import { moduleEditPage } from '@/constants/routes'
  33. const moduleKey = ref('')
  34. const viewRef = ref<InstanceType<typeof BillListView> | null>(null)
  35. const canCreate = computed(() => {
  36. const m = getModule(moduleKey.value)
  37. if (!m || m.shape === 'report') return false
  38. if (m.shape === 'entity') return !!getEntitySchema(m.key)
  39. return BILL_ITEM_MODULES.has(m.key)
  40. })
  41. onLoad((options) => {
  42. moduleKey.value = options?.module || ''
  43. })
  44. onReachBottom(() => {
  45. viewRef.value?.loadMore()
  46. })
  47. onPullDownRefresh(async () => {
  48. await viewRef.value?.refresh()
  49. uni.stopPullDownRefresh()
  50. })
  51. function goCreate() {
  52. const m = getModule(moduleKey.value)
  53. if (!m) return
  54. uni.navigateTo({ url: \`\${moduleEditPage(m.pkg)}?module=\${m.key}\` })
  55. }
  56. </script>
  57. <style lang="scss" scoped>
  58. .pkg-list-page {
  59. position: relative;
  60. }
  61. .fab {
  62. position: fixed;
  63. right: 32rpx;
  64. bottom: calc(64rpx + env(safe-area-inset-bottom));
  65. width: 96rpx;
  66. height: 96rpx;
  67. border-radius: 50%;
  68. background: #2979ff;
  69. display: flex;
  70. align-items: center;
  71. justify-content: center;
  72. box-shadow: 0 8rpx 24rpx rgba(41, 121, 255, 0.35);
  73. &:active {
  74. opacity: 0.85;
  75. }
  76. &__text {
  77. color: #fff;
  78. font-size: 48rpx;
  79. line-height: 1;
  80. }
  81. }
  82. </style>
  83. `
  84. const DETAIL_VUE = `<template>
  85. <BillDetailView :module-key="moduleKey" :record-id="recordId" />
  86. </template>
  87. <script setup lang="ts">
  88. /**
  89. * 分包通用详情页壳:module + id 参数驱动
  90. */
  91. import { ref } from 'vue'
  92. import { onLoad } from '@dcloudio/uni-app'
  93. import BillDetailView from '@/modules/bill/BillDetailView.vue'
  94. const moduleKey = ref('')
  95. const recordId = ref('')
  96. onLoad((options) => {
  97. moduleKey.value = options?.module || ''
  98. recordId.value = String(options?.id || '')
  99. })
  100. </script>
  101. `
  102. const EDIT_VUE = `<template>
  103. <BillFormView :module-key="moduleKey" :record-id="recordId" />
  104. </template>
  105. <script setup lang="ts">
  106. /**
  107. * 分包通用编辑页壳:module + id(可选)参数驱动
  108. */
  109. import { ref } from 'vue'
  110. import { onLoad } from '@dcloudio/uni-app'
  111. import BillFormView from '@/modules/bill/BillFormView.vue'
  112. const moduleKey = ref('')
  113. const recordId = ref('')
  114. onLoad((options) => {
  115. moduleKey.value = options?.module || ''
  116. recordId.value = String(options?.id || '')
  117. })
  118. </script>
  119. `
  120. const REPORT_LIST_VUE = `<template>
  121. <BillListView ref="viewRef" :module-key="moduleKey" />
  122. </template>
  123. <script setup lang="ts">
  124. /**
  125. * 报表列表页壳(只读):module 参数驱动
  126. */
  127. import { ref } from 'vue'
  128. import { onLoad, onReachBottom, onPullDownRefresh } from '@dcloudio/uni-app'
  129. import BillListView from '@/modules/bill/BillListView.vue'
  130. const moduleKey = ref('')
  131. const viewRef = ref<InstanceType<typeof BillListView> | null>(null)
  132. onLoad((options) => {
  133. moduleKey.value = options?.module || ''
  134. })
  135. onReachBottom(() => {
  136. viewRef.value?.loadMore()
  137. })
  138. onPullDownRefresh(async () => {
  139. await viewRef.value?.refresh()
  140. uni.stopPullDownRefresh()
  141. })
  142. </script>
  143. `
  144. function write(rel, content) {
  145. const file = path.join(root, 'src', rel)
  146. fs.mkdirSync(path.dirname(file), { recursive: true })
  147. fs.writeFileSync(file, content, 'utf8')
  148. console.log('generated:', rel)
  149. }
  150. for (const pkg of BILL_PKGS) {
  151. write(`${pkg}/pages/list.vue`, LIST_VUE)
  152. write(`${pkg}/pages/detail.vue`, DETAIL_VUE)
  153. write(`${pkg}/pages/edit.vue`, EDIT_VUE)
  154. }
  155. for (const pkg of REPORT_PKGS) {
  156. write(`${pkg}/pages/list.vue`, REPORT_LIST_VUE)
  157. }
  158. // pages.json:为列表页开启下拉刷新
  159. const pagesJsonPath = path.join(root, 'src', 'pages.json')
  160. const pagesJson = JSON.parse(fs.readFileSync(pagesJsonPath, 'utf8'))
  161. for (const sub of pagesJson.subPackages) {
  162. for (const page of sub.pages) {
  163. if (page.path === 'pages/list') {
  164. page.style = { ...page.style, enablePullDownRefresh: true }
  165. }
  166. }
  167. }
  168. fs.writeFileSync(pagesJsonPath, JSON.stringify(pagesJson, null, 2) + '\n', 'utf8')
  169. console.log('pages.json: enablePullDownRefresh applied to list pages')