submit.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <template>
  2. <CommonTopBanner title="点亮村社">
  3. <ProvideVar :vars="{
  4. FieldBackgroundColor: 'transparent',
  5. }">
  6. <FlexCol :gap="20" :padding="30">
  7. <!--注册-->
  8. <FlexCol v-if="step === 'register'">
  9. <BackgroundBox
  10. backgroundImage="https://xy.wenlvti.net/app_static/images/village/BoxMid.png"
  11. :backgroundCutBorder="32"
  12. :backgroundCutBorderSize="36"
  13. :padding="10"
  14. >
  15. <DynamicForm
  16. ref="registerFormRef"
  17. :model="registerFormModel"
  18. :options="registerFormDefine"
  19. />
  20. </BackgroundBox>
  21. <Height :height="20" />
  22. <PrimaryButton text="提交" @click="registerSubmit" :loading="registerFormLoading" />
  23. </FlexCol>
  24. <!--认领-->
  25. <FlexCol v-else-if="step === 'add'">
  26. <Alert
  27. type="info"
  28. message="您已经是志愿者,请完善以下信息认领当前村社"
  29. />
  30. <BackgroundBox
  31. backgroundImage="https://xy.wenlvti.net/app_static/images/village/BoxMid.png"
  32. :backgroundCutBorder="32"
  33. :backgroundCutBorderSize="36"
  34. :padding="10"
  35. >
  36. <DynamicForm
  37. ref="addFormRef"
  38. :model="addFormModel"
  39. :options="addFormDefine"
  40. />
  41. </BackgroundBox>
  42. <Height :height="20" />
  43. <PrimaryButton block text="提交" @click="addSubmit" :loading="addFormLoading" />
  44. </FlexCol>
  45. <!--注册完成-->
  46. <Result
  47. v-else-if="step === 'finished'"
  48. status="success"
  49. :title="finishedMode === 'claim' ? '认领申请已提交' : '注册志愿者成功'"
  50. :description="finishedMode === 'claim'
  51. ? '请等待管理员审核认领申请,审核通过后即可采编该村社文化资源'
  52. : '请等待管理员审核,在此期间,可以在社区中先逛逛,学习如何采编村社文化资源信息吧'"
  53. >
  54. <Height :size="20" />
  55. <PrimaryButton block text="进入首页" @click="back()" />
  56. </Result>
  57. <!--错误-->
  58. <Result
  59. v-else-if="step === 'error'"
  60. status="error"
  61. title="分享链接参数有误"
  62. description="请联系管理员,或稍后重试"
  63. />
  64. <!--登录-->
  65. <FlexCol v-else center :height="400">
  66. <Icon icon="smile-filling" color="primary" :size="156" />
  67. <Height :height="20" />
  68. <Text :fontSize="26" color="primary" text="欢迎注册,加入志愿者队伍,点亮村社" />
  69. <Height :size="40" />
  70. <!-- #ifdef MP-WEIXIN -->
  71. <PrimaryButton block text="微信登录" @click="loginWechat" />
  72. <Height :size="20" />
  73. <!-- #endif -->
  74. <!-- #ifndef MP-WEIXIN -->
  75. <Result status="warning" title="提示" desc="当前环境不支持微信登录" />
  76. <!-- #endif -->
  77. </FlexCol>
  78. </FlexCol>
  79. </ProvideVar>
  80. </CommonTopBanner>
  81. </template>
  82. <script setup lang="ts">
  83. import { onMounted, ref } from 'vue';
  84. import { useAppInit } from '@/common/composeabe/AppInit';
  85. import { useAuthStore } from '@/store/auth';
  86. import { useUserTools } from '@/common/composeabe/UserTools';
  87. import { useLoadQuerys } from '@/components/composeabe/LoadQuerys';
  88. import { UserApi } from '@/api/auth/UserApi';
  89. import { waitTimeOut } from '@imengyu/imengyu-utils';
  90. import { fillClaimFromVolunteer, getClaimVillageForm } from './form/claim';
  91. import { getVolunteerForm } from './form/volunteer';
  92. import { back } from '@/components/utils/PageAction';
  93. import { closeToast, toast } from '@/components/dialog/CommonRoot';
  94. import { showError } from '@/common/composeabe/ErrorDisplay';
  95. import type { IDynamicFormOptions, IDynamicFormRef } from '@/components/dynamic';
  96. import Button from '@/components/basic/Button.vue';
  97. import Result from '@/components/feedback/Result.vue';
  98. import FlexCol from '@/components/layout/FlexCol.vue';
  99. import Height from '@/components/layout/space/Height.vue';
  100. import Icon from '@/components/basic/Icon.vue';
  101. import Text from '@/components/basic/Text.vue';
  102. import CommonRoot from '@/components/dialog/CommonRoot.vue';
  103. import DynamicForm from '@/components/dynamic/DynamicForm.vue';
  104. import Alert from '@/components/feedback/Alert.vue';
  105. import VillageApi, { VillageClaimInfo, VolunteerInfo } from '@/api/inhert/VillageApi';
  106. import CommonTopBanner from '@/common/components/CommonTopBanner.vue';
  107. import BackgroundBox from '@/components/display/block/BackgroundBox.vue';
  108. import PrimaryButton from '@/common/components/PrimaryButton.vue';
  109. import ProvideVar from '@/components/theme/ProvideVar.vue';
  110. /**
  111. * 点亮村社页面
  112. */
  113. const authStore = useAuthStore();
  114. const { init } = useAppInit();
  115. const { getIsVolunteer } = useUserTools();
  116. const { querys } = useLoadQuerys({
  117. villageId: 0,
  118. regionId: 0,
  119. unit: '',
  120. });
  121. const step = ref<'register' |'add'|'finished'|'error'>('register');
  122. const finishedMode = ref<'register' | 'claim'>('register');
  123. onMounted(async () => {
  124. if (!querys.value.villageId) {
  125. step.value = 'error';
  126. return;
  127. }
  128. //如果已登录,则尝试获取志愿者信息,有注册志愿者,则显示认领当前村社表单
  129. if (authStore.isLogged) {
  130. try {
  131. const volunteerInfo = await VillageApi.getVolunteerInfo();
  132. if (volunteerInfo) {
  133. step.value = 'add';
  134. addFormDefine.value = getClaimVillageForm({ formRef: addFormRef });
  135. const model = new VillageClaimInfo();
  136. model.villageId = querys.value.villageId;
  137. fillClaimFromVolunteer(model, volunteerInfo);
  138. addFormModel.value = model;
  139. return;
  140. }
  141. } catch {
  142. }
  143. }
  144. await waitTimeOut(1000);
  145. registerFormDefine.value = getVolunteerForm({
  146. canSetCatalog: false,
  147. villageId: querys.value.villageId,
  148. onlyPassword: false,
  149. isNew: ref(true),
  150. formRef: registerFormRef,
  151. });
  152. registerFormModel.value.regionId = querys.value.regionId;
  153. registerFormModel.value.unit = querys.value.unit;
  154. });
  155. async function loginWechat() {
  156. toast({
  157. type: 'loading',
  158. content: '登录中...',
  159. })
  160. try {
  161. const res = await Promise.all([
  162. uni.login({ provider: 'weixin' }),
  163. uni.getUserProfile({ desc: '用于完善会员资料' }),
  164. ]);
  165. await authStore.loginWechart(res[0].code, res[1]);
  166. toast({
  167. type: 'success',
  168. content: '登录成功',
  169. });
  170. step.value = 'register';
  171. } catch(e) {
  172. showError(e);
  173. } finally {
  174. closeToast();
  175. }
  176. }
  177. const registerFormLoading = ref(false);
  178. const registerFormRef = ref<IDynamicFormRef>();
  179. const registerFormModel = ref<VolunteerInfo>(new VolunteerInfo());
  180. const registerFormDefine = ref<IDynamicFormOptions>();
  181. async function registerSubmit() {
  182. if (!registerFormRef.value || !registerFormModel.value)
  183. return;
  184. try {
  185. await registerFormRef.value.validate();
  186. } catch (e) {
  187. toast({ content: '有必填项未填写,请检查' });
  188. return;
  189. }
  190. try {
  191. registerFormLoading.value = true;
  192. registerFormModel.value!.villageId = querys.value.villageId;
  193. const loginRes = await VillageApi.shareAddVolunteer(registerFormModel.value as VolunteerInfo);
  194. await authStore.loginResultHandle(loginRes, UserApi.LOGIN_TYPE_USER);
  195. await suscribePassMessage();
  196. toast({ content: '注册成功' });
  197. finishedMode.value = 'register';
  198. step.value = 'finished';
  199. } catch (e) {
  200. showError(e);
  201. } finally {
  202. registerFormLoading.value = false;
  203. }
  204. await init();
  205. }
  206. const addFormLoading = ref(false);
  207. const addFormRef = ref<IDynamicFormRef>();
  208. const addFormModel = ref<VillageClaimInfo>(new VillageClaimInfo());
  209. const addFormDefine = ref<IDynamicFormOptions>();
  210. async function addSubmit() {
  211. if (!addFormRef.value || !addFormModel.value)
  212. return;
  213. try {
  214. await addFormRef.value.validate();
  215. } catch (e) {
  216. toast({ content: '有必填项未填写,请检查' });
  217. return;
  218. }
  219. try {
  220. addFormLoading.value = true;
  221. addFormModel.value.villageId = querys.value.villageId;
  222. await VillageApi.claimVallage(addFormModel.value as VillageClaimInfo);
  223. await suscribePassMessage();
  224. toast({ content: '提交成功' });
  225. finishedMode.value = 'claim';
  226. step.value = 'finished';
  227. } catch (e) {
  228. showError(e);
  229. } finally {
  230. addFormLoading.value = false;
  231. }
  232. }
  233. async function suscribePassMessage() {
  234. const TEMPLATE_ID = 'iNdqAKNyltLso9nFMvzrlcgCMGMALveIfZWfI2HYAQQ';
  235. return new Promise<boolean>((resolve, reject) => {
  236. uni.requestSubscribeMessage({
  237. tmplIds: [TEMPLATE_ID],
  238. success(res) {
  239. if ((res as any)[TEMPLATE_ID] === 'accept')
  240. resolve(true);
  241. else
  242. resolve(false);
  243. },
  244. fail(err) {
  245. console.error('[suscribePassMessage] fail', err);
  246. reject(err);
  247. }
  248. });
  249. });
  250. }
  251. </script>