volunteer.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <template>
  2. <CommonRoot class="main">
  3. <LoadingPage v-if="loading" />
  4. <FlexCol :padding="30">
  5. <DynamicForm
  6. ref="formRef"
  7. :model="formModel"
  8. :options="formDefine"
  9. :formGlobalParams="querys"
  10. />
  11. <Height :height="20" />
  12. <Button type="primary" @click="submit">提交</Button>
  13. </FlexCol>
  14. </CommonRoot>
  15. </template>
  16. <script setup lang="ts">
  17. import { computed, nextTick, ref } from 'vue';
  18. import { useLoadQuerys } from '@/common/composeabe/LoadQuerys';
  19. import { useAliOssUploadCo } from '@/common/components/upload/AliOssUploadCo';
  20. import { showError } from '@/common/composeabe/ErrorDisplay';
  21. import { alert, toast } from '@/components/dialog/CommonRoot';
  22. import { backAndCallOnPageBack } from '@/components/utils/PageAction';
  23. import { RequestApiError } from '@imengyu/imengyu-utils';
  24. import DynamicForm from '@/components/dynamic/DynamicForm.vue';
  25. import LoadingPage from '@/components/display/loading/LoadingPage.vue';
  26. import Button from '@/components/basic/Button.vue';
  27. import CommonRoot from '@/components/dialog/CommonRoot.vue';
  28. import FlexCol from '@/components/layout/FlexCol.vue';
  29. import Height from '@/components/layout/space/Height.vue';
  30. import VillageApi, { VolunteerInfo } from '@/api/inhert/VillageApi';
  31. import CommonContent from '@/api/CommonContent';
  32. import type { RuleItem } from 'async-validator';
  33. import type { IDynamicFormOptions, IDynamicFormRef, IDynamicFormItemCallbackAdditionalProps } from '@/components/dynamic';
  34. import type { UploaderFieldProps } from '@/components/form/UploaderField.vue';
  35. import type { FieldProps } from '@/components/form/Field.vue';
  36. import type { PickerIdFieldProps } from '@/components/dynamic/wrappers/PickerIdField';
  37. import type { RadioValueProps } from '@/components/dynamic/wrappers/RadioValue';
  38. import type { CheckBoxListProps } from '@/components/dynamic/wrappers/CheckBoxList.vue';
  39. import type { FormProps } from '@/components/form/Form.vue';
  40. const loading = ref(false);
  41. const formRef = ref<IDynamicFormRef>();
  42. const formModel = ref<VolunteerInfo>();
  43. const formDefine : IDynamicFormOptions = {
  44. formItems: [
  45. {
  46. label: '用户名', name: 'username', type: 'text',
  47. additionalProps: {
  48. placeholder: '请输入用户名',
  49. disabled: { callback: () => !isNew.value },
  50. },
  51. rules: [{ required: true, message: '请输入用户名' }],
  52. },
  53. {
  54. label: '密码',
  55. name: 'password',
  56. type: 'text',
  57. additionalProps: {
  58. placeholder: '请输入密码',
  59. type: 'password',
  60. } as FieldProps,
  61. rules: [{ required: true, message: '请输入密码' }],
  62. },
  63. {
  64. label: '确认密码',
  65. name: 'passwordRepeat',
  66. type: 'text',
  67. additionalProps: {
  68. placeholder: '请再输入一次密码',
  69. type: 'password',
  70. } as FieldProps,
  71. rules: [
  72. { required: true, message: '请再输入一次密码' },
  73. {
  74. async validator(rule, value) {
  75. if (value != formRef.value?.getValueByPath('password'))
  76. throw '两次输入密码不一致,请检查';
  77. },
  78. }
  79. ] as RuleItem[],
  80. },
  81. {
  82. label: '真实名称', name: 'name', type: 'text',
  83. additionalProps: { placeholder: '请输入真实名称' },
  84. rules: [{ required: true, message: '请输入真实名称' }],
  85. },
  86. {
  87. label: '手机号', name: 'mobile', type: 'text',
  88. additionalProps: { placeholder: '请输入手机号' },
  89. rules: [{ required: true, message: '请输入手机号' }],
  90. },
  91. {
  92. label: '区域', name: 'regionId', type: 'select-id',
  93. additionalProps: {
  94. placeholder: '请选择区域',
  95. disabled: { callback: () => !isNew.value },
  96. loadData: async () => (await CommonContent.getCategoryList(1)).map(p => ({ text: p.title, value: p.id, raw: p }))
  97. } as IDynamicFormItemCallbackAdditionalProps<PickerIdFieldProps>,
  98. rules: [{ required: true, message: '请选择区域' }],
  99. },
  100. {
  101. label: '所属村庄', name: 'villageId', type: 'select-id',
  102. additionalProps: {
  103. placeholder: '请选择所属村庄',
  104. disabled: { callback: () => !isNew.value },
  105. loadData: async () => (await VillageApi.getClaimedVallageList()).map(p => ({ text: p.title, value: p.id, raw: p })),
  106. } as IDynamicFormItemCallbackAdditionalProps<PickerIdFieldProps>,
  107. rules: [{ required: true, message: '请选择所属村庄' }],
  108. },
  109. {
  110. label: '性别', name: 'sex', type: 'radio-value',
  111. additionalProps: {
  112. options: [
  113. { text: '男', value: 1 },
  114. { text: '女', value: 2 }
  115. ]
  116. } as RadioValueProps,
  117. },
  118. {
  119. label: '头像', name: 'image', type: 'uploader',
  120. additionalProps: {
  121. single: true,
  122. maxFileSize: 1024 * 1024 * 10,
  123. upload: useAliOssUploadCo('xiangyuan/volunteer/images')
  124. } as UploaderFieldProps,
  125. },
  126. { label: '地址', name: 'address', type: 'text', additionalProps: { placeholder: '请输入地址' } },
  127. {
  128. label: '介绍',
  129. name: 'intro',
  130. type: 'textarea',
  131. additionalProps: {
  132. placeholder: '请输入介绍',
  133. showWordLimit: true,
  134. maxLength: 200,
  135. } as FieldProps,
  136. },
  137. {
  138. label: '采集版块', name: 'collectModule', type: 'check-box-list',
  139. additionalProps: {
  140. placeholder: '请选择采集版块',
  141. vertical: true,
  142. multiple: true,
  143. loadData: async () =>
  144. (await VillageApi.getCollectModuleList())
  145. .map((p) => ({
  146. text: p.label,
  147. value: p.value,
  148. raw: p
  149. }))
  150. ,
  151. } as CheckBoxListProps,
  152. },
  153. { label: '村落认领说明', name: 'claimReason', type: 'text', additionalProps: { placeholder: '请输入村落认领说明' } },
  154. ],
  155. formAdditionaProps: {
  156. labelWidth: '160rpx',
  157. labelAlign: 'right',
  158. innerStyle: {
  159. radius: '10rpx',
  160. },
  161. } as Omit<FormProps, 'model'>,
  162. }
  163. async function submit() {
  164. if (!formRef.value)
  165. return;
  166. try {
  167. await formRef.value.validate();
  168. } catch (e) {
  169. toast({ content: '有必填项未填写,请检查' });
  170. return;
  171. }
  172. try {
  173. loading.value = true;
  174. if (querys.value.id >= 0) {
  175. await VillageApi.updateVolunteer(formModel.value!);
  176. } else {
  177. await VillageApi.addVolunteer(formModel.value!);
  178. };
  179. alert({
  180. title: '操作成功',
  181. icon: 'success-filling',
  182. iconColor: 'success',
  183. content: isNew.value ? '新增志愿者信息成功' : '更新志愿者信息成功',
  184. onConfirm() {
  185. backPrev(true);
  186. },
  187. });
  188. } catch (e) {
  189. showError(e);
  190. } finally {
  191. loading.value = false;
  192. }
  193. }
  194. function backPrev(needRefresh: boolean) {
  195. backAndCallOnPageBack('list', {
  196. needRefresh,
  197. });
  198. }
  199. const { querys } = useLoadQuerys({
  200. villageId: 0,
  201. villageVolunteerId: 0,
  202. id: 0,
  203. }, async (querys) => {
  204. loading.value = true;
  205. if (!formRef.value)
  206. return;
  207. let formData = undefined;
  208. try {
  209. formModel.value = new VolunteerInfo();
  210. if (querys.id >= 0)
  211. formData = await VillageApi.getVolunteerInfoById(querys.id);
  212. } catch (e) {
  213. if (!(e instanceof RequestApiError && e.errorMessage.startsWith('请完成')))
  214. showError(e, undefined, () => backPrev(false));
  215. } finally {
  216. loading.value = false;
  217. }
  218. if (formData) {
  219. formModel.value = formData;
  220. }
  221. await nextTick();
  222. formRef.value.initDefaultValuesToModel();
  223. });
  224. const isNew = computed(() => querys.value.id < 0);
  225. </script>