forms.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import { CommonInfoModel } from "@/api/inhert/VillageInfoApi";
  2. import type { IDynamicFormOptions, IDynamicFormRef } from "@/components/dynamic";
  3. import type { FieldProps } from "@/components/form/Field.vue";
  4. import type { NewDataModel } from "@imengyu/js-request-transform";
  5. import type { Ref } from "vue";
  6. import { villageCommonContent } from "./data/common";
  7. import { villageInfoBuildingForm, villageInfoDistributionForm } from "./data/building";
  8. import { villageInfoCulture, villageInfoFolkCultureForm } from "./data/cultural";
  9. import { villageInfoFoodProductsForm } from "./data/food";
  10. import { villageInfoOverviewForm } from "./data/overview";
  11. import { villageInfoEnvironmentForm } from "./data/environment";
  12. import { villageInfoRelicForm } from "./data/relic";
  13. import { vilElementForm } from "./data/element";
  14. import { villageInfoFigureFormItems, villageInfoStoryFormItems } from "./data/history";
  15. import { villageInfoRouteForm, villageInfoTravelGuideForm } from "./data/travel";
  16. import { villageInfoSpeakerForm } from "./data/specker";
  17. import { ichFormItems } from "./data/ich";
  18. import type { PickerIdFieldProps } from "@/components/dynamic/wrappers/PickerIdField";
  19. import { villageInfoSpotsFormItems } from "./data/spots";
  20. export type SingleForm = [NewDataModel, (formRef: Ref<IDynamicFormRef>) => IDynamicFormOptions, {
  21. title: string,
  22. typeName: string,
  23. mergeSubs?: boolean,
  24. order?: number,
  25. id?: number,
  26. }]
  27. export type GroupForm = Record<number, SingleForm>;
  28. export const CollectableModulesIdMap : Record<string, number> = {
  29. 'overview': 11,
  30. 'cultural': 2,
  31. 'story': 14,
  32. 'spots': 13,
  33. 'speaker': 17,
  34. 'figure': 7,
  35. 'element': 5,
  36. 'environment': 6,
  37. 'building': 16,
  38. 'distribution': 4,
  39. 'relic': 3,
  40. 'folk_culture': 8,
  41. 'food_product': 9,
  42. 'route': 12,
  43. 'travel_guide': 15,
  44. 'ich': 10,
  45. 'collect': 18,
  46. 'glory': 23,
  47. };
  48. /**
  49. * 采集模块ID到表单定义的映射
  50. */
  51. const villageInfoForm : Record<number, GroupForm> = {
  52. 11: villageInfoOverviewForm,//村落概况
  53. 2: villageInfoCulture,//历史文化
  54. 14: {//掌故轶事
  55. [0]: villageInfoStoryFormItems
  56. },
  57. 13: {//风景名胜
  58. [0]: villageInfoSpotsFormItems,
  59. },
  60. 17: {//口述者
  61. [1]: villageInfoSpeakerForm,
  62. },
  63. 7: {//历史人物
  64. [0]: villageInfoFigureFormItems,
  65. },
  66. 5: {//环境要素
  67. [0]: vilElementForm,
  68. },
  69. 6: {//环境格局
  70. [0]: villageInfoEnvironmentForm
  71. },
  72. 16: {//传统建筑
  73. [1]: villageInfoBuildingForm('文物建筑'),
  74. [2]: villageInfoBuildingForm('历史建筑'),
  75. [3]: villageInfoBuildingForm('重要传统建筑'),
  76. },
  77. 4: { [0]: villageInfoDistributionForm },//建筑分布
  78. 3: { [0]: villageInfoRelicForm },//文物古迹
  79. 8: {//民俗文化
  80. [1]: villageInfoFolkCultureForm('节庆活动'),
  81. [2]: villageInfoFolkCultureForm('祭祀崇礼'),
  82. [3]: villageInfoFolkCultureForm('婚丧嫁娶'),
  83. [4]: villageInfoFolkCultureForm('地方方言'),
  84. [5]: villageInfoFolkCultureForm('特色文化'),
  85. },
  86. 10: { [0]: ichFormItems },//非遗
  87. 15: { [0]: villageInfoTravelGuideForm },//旅游导览
  88. 12: { [0]: villageInfoRouteForm },//旅游路线
  89. 9: {//美食物产
  90. [1]: villageInfoFoodProductsForm('农副产品'),
  91. [2]: villageInfoFoodProductsForm('食品产品'),
  92. [3]: villageInfoFoodProductsForm('特色美食'),
  93. [4]: villageInfoFoodProductsForm('商业集市'),
  94. [5]: villageInfoFoodProductsForm('服装服饰'),
  95. [6]: villageInfoFoodProductsForm('运输工具'),
  96. },
  97. 18: {//18
  98. [1]: [CommonInfoModel, (r) => ({
  99. formItems: [
  100. {
  101. label: '标题',
  102. name: 'name',
  103. type: 'text',
  104. defaultValue: '',
  105. additionalProps: {
  106. placeholder: '请输入标题',
  107. } as FieldProps,
  108. rules: [{
  109. required: true,
  110. message: '请输入标题',
  111. }]
  112. },
  113. {
  114. label: '描述',
  115. name: 'desc',
  116. type: 'textarea',
  117. defaultValue: '',
  118. additionalProps: {
  119. placeholder: '请输入描述',
  120. maxLength: 5000,
  121. showWordLimit: true,
  122. } as FieldProps,
  123. rules: []
  124. },
  125. ...villageCommonContent(r, {
  126. title: '记录',
  127. showTitle: false,
  128. }).formItems
  129. ]
  130. }), { title: '随手记', typeName: '', }]
  131. },
  132. 23: {//乡源荣光
  133. [1]: [CommonInfoModel, (r) => ({
  134. formItems: [
  135. {
  136. label: '标题',
  137. name: 'name',
  138. type: 'text',
  139. defaultValue: '',
  140. additionalProps: {
  141. placeholder: '请输入标题',
  142. } as FieldProps,
  143. rules: [{
  144. required: true,
  145. message: '请输入标题',
  146. }]
  147. },
  148. {
  149. label: '描述',
  150. name: 'desc',
  151. type: 'textarea',
  152. defaultValue: '',
  153. additionalProps: {
  154. placeholder: '请输入描述',
  155. } as FieldProps,
  156. },
  157. ...villageCommonContent(r, {
  158. title: '记录',
  159. showTitle: false,
  160. }).formItems
  161. ]
  162. }), { title: '乡源荣光', typeName: '', }]
  163. },
  164. }
  165. export function getVillageInfoForm(collectModuleId: number, subId: number) {
  166. const group = villageInfoForm[collectModuleId];
  167. if (!group)
  168. throw new Error('未定义的表单类型: ' + collectModuleId);
  169. if (subId === -1)
  170. return mergeFormItems(group);
  171. return group[subId];
  172. }
  173. export function getVillageInfoFormIds(collectModuleId: number) {
  174. return Object.keys(villageInfoForm[collectModuleId]).map((k) => Number(k));
  175. }
  176. export function mergeFormItems(group: GroupForm) {
  177. const groupArray = Object.keys(group)
  178. .map((k) => {
  179. const id = Number(k);
  180. const it = group[id];
  181. it[2].id = id;
  182. return it;
  183. })
  184. .sort((a, b) =>
  185. (a[2].order || 0) - (b[2].order || 0)
  186. );
  187. const [model, firstForm, info] = groupArray[0];
  188. const isOverview = group === villageInfoOverviewForm;
  189. const needType = !isOverview && info.typeName;
  190. const result : SingleForm = [model, (formRef: Ref<IDynamicFormRef>) => {
  191. const options : IDynamicFormOptions = {
  192. formItems: [],
  193. };
  194. if (needType) {
  195. options.formItems.push({
  196. label: '类型',
  197. name: info.typeName,
  198. type: 'select-id',
  199. additionalProps: {
  200. disabled: groupArray.length <= 1,
  201. loadData: async () => groupArray.map((it) => ({
  202. value: it[2].id,
  203. text: it[2].title,
  204. })),
  205. } as PickerIdFieldProps,
  206. defaultValue: groupArray[0][2].id,
  207. formProps: { showRightArrow: true } as FieldProps,
  208. rules: [{
  209. required: true,
  210. type: 'number',
  211. message: '请选择类型',
  212. }],
  213. });
  214. }
  215. if (needType || isOverview) {
  216. let i = 0;
  217. for (const it of groupArray) {
  218. const op = it[1](formRef);
  219. const ex = it[2];
  220. options.formItems.push({
  221. label: ex.title,
  222. name: 'group' + i,
  223. type: 'flat-group',
  224. childrenColProps: { span: 24 },
  225. children: op.formItems,
  226. show: isOverview ? undefined : { callback: (_, m) => m[info.typeName] == ex.id }
  227. });
  228. i++;
  229. }
  230. } else {
  231. options.formItems.push(...firstForm(formRef).formItems)
  232. }
  233. return options;
  234. }, info ?? { title: '合并', typeName: '' }]
  235. return result;
  236. }