Bläddra i källkod

fix(@six/smart-pharmacy) - 698: 智慧药事系统第一版用户密码无法修改

cmj 4 veckor sedan
förälder
incheckning
0dd3461347

+ 28 - 11
apps/smart-pharmacy/src/views/system/user/data.ts

@@ -124,7 +124,15 @@ export function useUserTableColumns<T = SystemModel.User>(
   ];
 }
 
-export function useUserFormSchema(): VbenFormSchema[] {
+const passwordRule = z.string().refine(
+  (value) =>
+    /^(?=.*[A-Z])(?=.*[a-z]|.*\d)|(?=.*[a-z])(?=.*[A-Z]|.*\d)|(?=.*\d)(?=.*[A-Z]|.*[a-z]).{8,18}$/.test(
+      value,
+    ),
+  { message: '请输入8-18位数字、大写字母、小写字母最少两种组合的密码' },
+);
+
+export function useUserFormSchema(mode: 'add' | 'edit' = 'add'): VbenFormSchema[] {
   return [
     {
       component: 'ApiTreeSelect',
@@ -150,19 +158,28 @@ export function useUserFormSchema(): VbenFormSchema[] {
     {
       component: 'InputPassword',
       componentProps: {
-        placeholder: '请输入密码',
+        placeholder:
+          mode === 'edit' ? '留空则不修改密码' : '请输入密码',
       },
       fieldName: 'password',
       label: '密码',
-      rules: z
-        .string()
-        .refine(
-          (value) =>
-            /^(?=.*[A-Z])(?=.*[a-z]|.*\d)|(?=.*[a-z])(?=.*[A-Z]|.*\d)|(?=.*\d)(?=.*[A-Z]|.*[a-z]).{8,18}$/.test(
-              value,
-            ),
-          { message: '请输入8-18位数字、大写字母、小写字母最少两种组合的密码' },
-        ),
+      rules:
+        mode === 'edit'
+          ? z
+              .string()
+              .optional()
+              .refine(
+                (value) =>
+                  !value ||
+                  /^(?=.*[A-Z])(?=.*[a-z]|.*\d)|(?=.*[a-z])(?=.*[A-Z]|.*\d)|(?=.*\d)(?=.*[A-Z]|.*[a-z]).{8,18}$/.test(
+                    value,
+                  ),
+                {
+                  message:
+                    '请输入8-18位数字、大写字母、小写字母最少两种组合的密码',
+                },
+              )
+          : passwordRule,
     },
     {
       component: 'Input',

+ 7 - 10
apps/smart-pharmacy/src/views/system/user/modules/form.vue

@@ -28,7 +28,7 @@ const getTitle = computed(() => {
 });
 
 const [Form, formApi] = useVbenForm({
-  schema: useUserFormSchema(),
+  schema: useUserFormSchema('add'),
   showDefaultActions: false,
 });
 
@@ -43,6 +43,9 @@ const [Modal, modalApi] = useVbenModal({
     if (valid) {
       modalApi.lock();
       const data = await formApi.getValues();
+      if (formData.value?.id && !data.password) {
+        delete data.password;
+      }
       try {
         await edit.send({ ...formData.value, ...data });
         await modalApi.close();
@@ -55,15 +58,9 @@ const [Modal, modalApi] = useVbenModal({
     if (isOpen) {
       const data = modalApi.getData<SystemModel.User>();
       if (data) {
-        if (data.id) {
-          formApi.setState((prev) => {
-            const schema =
-              prev.schema?.filter(
-                (schema) => schema.fieldName !== 'password',
-              ) ?? [];
-            return { schema };
-          });
-        }
+        formApi.setState({
+          schema: useUserFormSchema(data.id ? 'edit' : 'add'),
+        });
         formData.value = data;
         formData.value.roles = data.roles?.map((role) =>
           typeof role === 'string' ? role : role.id,