Browse Source

feat: 添加认证页面的错误提示信息;优化表单验证逻辑以提升用户体验

EvilDragon 1 month ago
parent
commit
45e5b923c5

+ 14 - 1
packages/app/src/core/libs/messages.ts

@@ -1 +1,14 @@
-export const messages = { home: { shareTitle: '筑巢荟' }, moment: { imageNotExist: '请上传图片' } }
+export const messages = {
+  home: { shareTitle: '筑巢荟' },
+  moment: { imageNotExist: '请上传图片' },
+  mine: {
+    authentication: {
+      channelSourceErrorText: '请选择来源',
+      referrerErrorText: '请输入推荐人编号',
+      designerNameErrorText: '请输入姓名',
+      employerErrorText: '请输入公司',
+      spatialExpertiseTypeErrorText: '请选择擅长空间类型',
+      attachmentErrorText: '请上传附件',
+    },
+  },
+}

+ 47 - 26
packages/app/src/pages/mine/authentication/index.vue

@@ -28,6 +28,7 @@ import { requestToast } from '../../../core/utils/common'
 import { omit, pick } from 'radash'
 import UploadEvo from '@/components/upload-evo.vue'
 import { DataFormSchema } from '../../../components/data-form'
+import { messages } from '../../../core/libs/messages'
 
 const { alert } = useMessage()
 const router = useRouter()
@@ -108,25 +109,57 @@ const setReferrerExisting = (value: string) => {
 }
 const handleSubmit = async () => {
   console.log(formData.value)
-  if (!userAuthInfo.value) {
-    if (formData.value.channelSource !== '4') {
-      const { data, code: status } = await requestToast(() =>
-        validateReferrerCode({
-          code: formData.value.referrer,
-          channelSource: formData.value.channelSource,
-        }),
-      )
-      if (data === false || status !== 0) {
-        uni.showToast({ title: '推荐人编号不正确', icon: 'none' })
-        return
-      }
+  if (formData.value.channelSource && formData.value.channelSource !== '4') {
+    if ((formData.value.referrer ?? '') === '') {
+      uni.showToast({ title: messages.mine.authentication.referrerErrorText, icon: 'none' })
+      return
+    }
+    const { data, code: status } = await requestToast(() =>
+      validateReferrerCode({
+        code: formData.value.referrer,
+        channelSource: formData.value.channelSource,
+      }),
+    )
+    if (data === false || status !== 0) {
+      uni.showToast({ title: '推荐人编号不正确', icon: 'none' })
+      return
     }
+  }
+  const body = {
+    ...formData.value,
+    spatialExpertiseType: formData.value.spatialExpertiseType?.join(','),
+    attachment: attachment.value,
+  }
+  const rules = {
+    channelSource: { required: true, message: messages.mine.authentication.channelSourceErrorText },
+    // referrer: { required: true, message: messages.mine.authentication.referrerErrorText },
+    designerName: { required: true, message: messages.mine.authentication.designerNameErrorText },
+    employer: { required: true, message: messages.mine.authentication.employerErrorText },
+    spatialExpertiseType: {
+      required: true,
+      message: messages.mine.authentication.spatialExpertiseTypeErrorText,
+    },
+    attachment: { required: true, message: messages.mine.authentication.attachmentErrorText },
+  }
+  // 校验上述字段
+  const errors = Object.entries(rules)
+    .filter(([key, value]) => (body[key] ?? '') === '')
+    .map(([key, value]) => value.message)
+  console.log(errors)
+  if (errors.length > 0) {
+    uni.showToast({ title: errors[0], icon: 'none' })
+    return
+  }
+
+  if (!userAuthInfo.value) {
+    console.log(3333)
+
     const { code, msg } = await createUserAuthInfo({
       gender: userInfo.value.sex,
       attachment: attachment.value,
       userId: userInfo.value.userId,
       ...formData.value,
-      spatialExpertiseType: formData.value.spatialExpertiseType.join(','),
+      spatialExpertiseType: formData.value.spatialExpertiseType?.join(','),
     })
     if (code === 0) {
       router.replace(`/pages/mine/authentication/submit/success/index`)
@@ -134,21 +167,9 @@ const handleSubmit = async () => {
       error(msg)
     }
   } else {
-    if (formData.value.channelSource !== '4') {
-      const { data, code: status } = await requestToast(() =>
-        validateReferrerCode({
-          code: formData.value.referrer,
-          channelSource: formData.value.channelSource,
-        }),
-      )
-      if (data === false || status !== 0) {
-        uni.showToast({ title: '推荐人编号不正确', icon: 'none' })
-        return
-      }
-    }
     const toBeUpdate = {
       ...omit(formData.value, ['spatialExpertiseType']),
-      spatialExpertiseType: formData.value.spatialExpertiseType.join(','),
+      spatialExpertiseType: formData.value.spatialExpertiseType?.join(','),
       attachment: attachment.value,
       id: userAuthInfo.value.id,
       gender: userInfo.value.sex,