login.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <FlexCol gap="gap.xl" padding="space.lg">
  3. <FlexCol center>
  4. <Text fontConfig="subText">欢迎使用闽南文化数字资源采集</Text>
  5. <Text fontConfig="subText">技术支持:18649931391</Text>
  6. </FlexCol>
  7. <FlexCol position="relative" radius="radius.md" backgroundColor="white" overflow="hidden">
  8. <LoadingPage :loading="loading" loadingText="登录中..." />
  9. <DynamicForm
  10. ref="formRef"
  11. :model="formModel"
  12. :options="formDefine"
  13. />
  14. </FlexCol>
  15. <FlexCol gap="gap.md">
  16. <Button type="primary" block :loading="loading" @click="handleSubmit">
  17. 登录
  18. </Button>
  19. <Button block @click="back()">
  20. 返回
  21. </Button>
  22. </FlexCol>
  23. <XBarSpace />
  24. </FlexCol>
  25. </template>
  26. <script setup lang="ts">
  27. import { onMounted, ref } from 'vue';
  28. import { useAuthStore } from '@/store/auth';
  29. import { showError } from '@/common/composeabe/ErrorDisplay';
  30. import { toast } from '@/components/utils/DialogAction';
  31. import Button from '@/components/basic/Button.vue';
  32. import XBarSpace from '@/components/layout/space/XBarSpace.vue';
  33. import DynamicForm from '@/components/dynamic/DynamicForm.vue';
  34. import type { IDynamicFormOptions, IDynamicFormRef } from '@/components/dynamic';
  35. import type { FormProps } from '@/components/form/Form.vue';
  36. import type { RadioValueProps } from '@/components/dynamic/wrappers/RadioValue';
  37. import { back, redirectTo } from '@/components/utils/PageAction';
  38. import FlexCol from '@/components/layout/FlexCol.vue';
  39. import FlexRow from '@/components/layout/FlexRow.vue';
  40. import Text from '@/components/basic/Text.vue';
  41. import LoadingPage from '@/components/display/loading/LoadingPage.vue';
  42. import { waitTimeOut } from '@imengyu/imengyu-utils';
  43. const authStore = useAuthStore();
  44. const formRef = ref<IDynamicFormRef>();
  45. const loading = ref(true);
  46. const formModel = ref({
  47. mobile: '',
  48. account: '',
  49. password: '',
  50. type: 0,
  51. });
  52. const formDefine: IDynamicFormOptions = {
  53. formAdditionaProps: {
  54. labelFlex: 4,
  55. inputFlex: 8,
  56. } as FormProps,
  57. formRules: {
  58. mobile: [{ required: true, message: '请输入账号' }],
  59. account: [{ required: true, message: '请输入账号' }],
  60. password: [{ required: true, message: '请输入密码' }],
  61. },
  62. formItems: [
  63. {
  64. label: '账号',
  65. name: 'mobile',
  66. type: 'text',
  67. defaultValue: '',
  68. show: { callback: (_, m) => (m as { type: number }).type === 0 },
  69. additionalProps: {
  70. placeholder: '请输入账号',
  71. },
  72. },
  73. {
  74. label: '账号',
  75. name: 'account',
  76. type: 'text',
  77. defaultValue: '',
  78. show: { callback: (_, m) => (m as { type: number }).type === 1 },
  79. additionalProps: {
  80. placeholder: '请输入账号',
  81. },
  82. },
  83. {
  84. label: '密码',
  85. name: 'password',
  86. type: 'text',
  87. defaultValue: '',
  88. formProps: {
  89. type: 'password',
  90. },
  91. additionalProps: {
  92. placeholder: '请输入密码',
  93. },
  94. },
  95. {
  96. label: '登录类型',
  97. name: 'type',
  98. type: 'radio-value',
  99. defaultValue: 0,
  100. additionalProps: {
  101. options: [
  102. { text: '传承人', value: 0 },
  103. { text: '管理员', value: 1 },
  104. ],
  105. } as RadioValueProps,
  106. },
  107. ],
  108. };
  109. async function handleSubmit() {
  110. if (!formRef.value)
  111. return;
  112. try {
  113. await formRef.value.validate();
  114. } catch {
  115. uni.showToast({
  116. title: '有必填项未填写,请检查',
  117. icon: 'none',
  118. });
  119. return;
  120. }
  121. const loginType = Number(formModel.value.type);
  122. const account =
  123. loginType === 1
  124. ? formModel.value.account.trim()
  125. : formModel.value.mobile.trim();
  126. if (!account) {
  127. uni.showToast({
  128. title: '请输入账号',
  129. icon: 'none',
  130. });
  131. return;
  132. }
  133. loading.value = true;
  134. try {
  135. await authStore.loginCollect(account, formModel.value.password, loginType);
  136. toast('您已成功登录');
  137. await new Promise((r) => setTimeout(r, 200));
  138. if (authStore.loginType === 0) {
  139. uni.redirectTo({ url: '/pages/collect/inheritor' });
  140. } else if (authStore.loginType === 1) {
  141. uni.redirectTo({ url: '/pages/collect/admin' });
  142. } else {
  143. uni.switchTab({ url: '/pages/user/index' });
  144. }
  145. } catch (e) {
  146. showError(e);
  147. } finally {
  148. loading.value = false;
  149. }
  150. }
  151. onMounted(async () => {
  152. await waitTimeOut(1000);
  153. if (authStore.isLogged) {
  154. if (authStore.isCollect) {
  155. redirectTo('/pages/collect/inheritor');
  156. } else if (authStore.isCollectReviewer || authStore.isCollectAdmin) {
  157. redirectTo('/pages/collect/admin');
  158. }
  159. }
  160. loading.value = false;
  161. });
  162. </script>
  163. <style lang="scss">
  164. </style>