github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/static_source/admin/src/views/Users/new.vue (about)

     1  <script setup lang="ts">
     2  import {ref, unref} from 'vue'
     3  import {useI18n} from '@/hooks/web/useI18n'
     4  import {ElButton} from 'element-plus'
     5  import {useForm} from '@/hooks/web/useForm'
     6  import {useCache} from '@/hooks/web/useCache'
     7  import {useAppStore} from '@/store/modules/app'
     8  import {usePermissionStore} from '@/store/modules/permission'
     9  import {useRoute, useRouter} from 'vue-router'
    10  import {useValidator} from '@/hooks/web/useValidator'
    11  import api from "@/api/api";
    12  import Form from './components/Form.vue'
    13  import {ApiNewtUserRequest} from "@/api/stub";
    14  import {ContentWrap} from "@/components/ContentWrap";
    15  import {User} from "@/views/Users/components/Types";
    16  
    17  const {register, elFormRef, methods} = useForm()
    18  const {required} = useValidator()
    19  const appStore = useAppStore()
    20  const permissionStore = usePermissionStore()
    21  const {currentRoute, addRoute, push} = useRouter()
    22  const route = useRoute();
    23  const {wsCache} = useCache()
    24  const {t} = useI18n()
    25  
    26  const writeRef = ref<ComponentRef<typeof Form>>()
    27  const loading = ref(false)
    28  const currentRow = ref<Nullable<ApiNewtUserRequest>>(null)
    29  
    30  const save = async () => {
    31    const write = unref(writeRef)
    32    const validate = await write?.elFormRef?.validate()?.catch(() => {
    33    })
    34    if (validate) {
    35      loading.value = true
    36      const data = (await write?.getFormData()) as User
    37      const body = {
    38        nickname: data.nickname,
    39        firstName: data.firstName,
    40        lastName: data.lastName,
    41        password: data.password,
    42        passwordRepeat: data.passwordRepeat,
    43        email: data.email,
    44        status: data.status,
    45        lang: data.lang,
    46        imageId: data.image?.id,
    47        roleName: data.role?.name || null,
    48      } as ApiNewtUserRequest
    49      const res = await api.v1.userServiceAddUser(body)
    50          .catch(() => {
    51          })
    52          .finally(() => {
    53            loading.value = false
    54          })
    55      if (res) {
    56        cancel()
    57      }
    58    }
    59  }
    60  
    61  const cancel = () => {
    62    push('/etc/users')
    63  }
    64  
    65  </script>
    66  
    67  <template>
    68    <ContentWrap>
    69      <Form ref="writeRef" :current-row="currentRow"/>
    70  
    71      <div style="text-align: right">
    72  
    73        <ElButton type="primary" @click="save()">
    74          {{ t('main.save') }}
    75        </ElButton>
    76  
    77        <ElButton type="default" @click="cancel()">
    78          {{ t('main.cancel') }}
    79        </ElButton>
    80  
    81      </div>
    82    </ContentWrap>
    83  
    84  </template>
    85  
    86  <style lang="less" scoped>
    87  
    88  </style>