ny
昨天 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<script lang="ts" setup>
import { reactive, toRefs } from 'vue';
 
import { useMessage } from '@jnpf/hooks';
import { BasicForm, useForm } from '@jnpf/ui/form';
import { BasicModal, useModalInner } from '@jnpf/ui/modal';
import { formValidate } from '@jnpf/utils';
 
import { updateUserInfo } from '#/api/permission/userSetting';
 
interface State {
  type: string;
  schemas: any[];
  title: string;
}
 
const props = defineProps({
  user: { type: Object, default: () => ({}) },
});
 
defineEmits(['register']);
const [registerForm, { setFieldsValue, resetFields, validate }] = useForm({
  labelWidth: 80,
});
const [registerModal, { closeModal, changeOkLoading }] = useModalInner(init);
const state = reactive<State>({
  type: '',
  schemas: [],
  title: '',
});
const { title, schemas } = toRefs(state);
const { createMessage } = useMessage();
 
function init(data) {
  state.type = data.type;
  const dataForm = { [data.type]: props.user[data.type] || '' };
  if (data.type === 'mobilePhone') {
    state.title = '修改手机';
    state.schemas = [
      {
        field: 'mobilePhone',
        label: `手机号码`,
        component: 'Input',
        componentProps: { maxLength: 11 },
        rules: [{ validator: formValidate('iphone'), trigger: 'blur' }],
      },
    ];
  }
  if (data.type === 'email') {
    state.title = '修改邮箱';
    state.schemas = [{ field: 'email', label: `电子邮箱`, component: 'Input', rules: [{ validator: formValidate('email'), trigger: 'blur' }] }];
  }
  resetFields();
  setFieldsValue(dataForm);
}
async function handleSubmit() {
  const values = await validate();
  if (!values) return;
  changeOkLoading(true);
  updateUserInfo(values)
    .then((res) => {
      props.user[state.type] = values[state.type];
      createMessage.success(res.msg);
      changeOkLoading(false);
      closeModal();
    })
    .catch(() => {
      changeOkLoading(false);
    });
}
</script>
<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="title" @ok="handleSubmit">
    <BasicForm @register="registerForm" :schemas="schemas" />
  </BasicModal>
</template>