refactor: oauth2 vxe

This commit is contained in:
xingyu4j 2022-11-13 14:40:51 +08:00
parent bdc988e5b7
commit 7250e23df3
9 changed files with 348 additions and 436 deletions

View File

@ -1,5 +1,24 @@
import request from '@/config/axios'
import { OAuth2ClientVo } from './client.types'
export interface OAuth2ClientVO {
id: number
clientId: string
secret: string
name: string
logo: string
description: string
status: number
accessTokenValiditySeconds: number
refreshTokenValiditySeconds: number
redirectUris: string[]
autoApprove: boolean
authorizedGrantTypes: string[]
scopes: string[]
authorities: string[]
resourceIds: string[]
additionalInformation: string
isAdditionalInformationJson: boolean
createTime: string
}
// 查询 OAuth2列表
export const getOAuth2ClientPageApi = (params) => {
@ -12,12 +31,12 @@ export const getOAuth2ClientApi = (id: number) => {
}
// 新增 OAuth2
export const createOAuth2ClientApi = (data: OAuth2ClientVo) => {
export const createOAuth2ClientApi = (data: OAuth2ClientVO) => {
return request.post({ url: '/system/oauth2-client/create', data })
}
// 修改 OAuth2
export const updateOAuth2ClientApi = (data: OAuth2ClientVo) => {
export const updateOAuth2ClientApi = (data: OAuth2ClientVO) => {
return request.put({ url: '/system/oauth2-client/update', data })
}

View File

@ -1,20 +0,0 @@
export type OAuth2ClientVo = {
id: number
clientId: string
secret: string
name: string
logo: string
description: string
status: number
accessTokenValiditySeconds: number
refreshTokenValiditySeconds: number
redirectUris: string[]
autoApprove: boolean
authorizedGrantTypes: string[]
scopes: string[]
authorities: string[]
resourceIds: string[]
additionalInformation: string
isAdditionalInformationJson: boolean
createTime: string
}

View File

@ -1,7 +1,24 @@
import request from '@/config/axios'
export interface OAuth2TokenVO {
id: number
accessToken: string
refreshToken: string
userId: number
userType: number
clientId: string
createTime: string
expiresTime: string
}
export interface OAuth2TokenPageReqVO extends BasePage {
code?: string
name?: string
status?: number
}
// 查询 token列表
export const getAccessTokenPageApi = (params) => {
export const getAccessTokenPageApi = (params: OAuth2TokenPageReqVO) => {
return request.get({ url: '/system/oauth2-token/page', params })
}

View File

@ -1,10 +0,0 @@
export type OAuth2TokenVo = {
id: number
accessToken: string
refreshToken: string
userId: number
userType: number
clientId: string
createTime: string
expiresTime: string
}

View File

@ -45,13 +45,13 @@ export const useVxeGrid = <T = any>(config?: UseVxeGridConfig<T>) => {
isHover: true // 当鼠标移到行时,是否要高亮当前行
},
toolbarConfig: {
custom: true,
slots: { buttons: 'toolbar_buttons' }
},
printConfig: {
columns: config?.allSchemas.printSchema
},
formConfig: {
enabled: true,
titleWidth: 100,
titleAlign: 'right',
items: config?.allSchemas.searchSchema
@ -120,6 +120,7 @@ export const useVxeGrid = <T = any>(config?: UseVxeGridConfig<T>) => {
})
})
}
return {
gridOptions,
delList

View File

@ -1,10 +1,9 @@
import { reactive } from 'vue'
import { useI18n } from '@/hooks/web/useI18n'
import { required } from '@/utils/formRules'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { DICT_TYPE } from '@/utils/dict'
const { t } = useI18n()
// 国际化
import { VxeCrudSchema, useVxeCrudSchemas } from '@/hooks/web/useVxeCrudSchemas'
const { t } = useI18n() // 国际化
// 表单校验
export const rules = reactive({
@ -20,94 +19,75 @@ export const rules = reactive({
})
// CrudSchema
const crudSchemas = reactive<CrudSchema[]>([
const crudSchemas = reactive<VxeCrudSchema>({
primaryKey: 'clientId',
primaryType: 'seq',
action: true,
columns: [
{
label: '客户端编号',
field: 'clientId',
form: {
show: false
},
detail: {
show: false
}
},
{
label: '客户端密钥',
title: '客户端密钥',
field: 'secret'
},
{
label: '应用名',
title: '应用名',
field: 'name',
search: {
show: true
}
isSearch: true
},
{
label: '应用图标',
title: '应用图标',
field: 'logo'
},
{
label: t('common.status'),
title: t('common.status'),
field: 'status',
dictType: DICT_TYPE.COMMON_STATUS,
search: {
show: true
}
isSearch: true
},
{
label: '访问令牌的有效期',
title: '访问令牌的有效期',
field: 'accessTokenValiditySeconds'
},
{
label: '刷新令牌的有效期',
title: '刷新令牌的有效期',
field: 'refreshTokenValiditySeconds'
},
{
label: '授权类型',
title: '授权类型',
field: 'authorizedGrantTypes',
dictType: DICT_TYPE.SYSTEM_OAUTH2_GRANT_TYPE
dictType: DICT_TYPE.SYSTEM_OAUTH2_GRANT_TYPE,
form: {
component: 'Select'
}
},
{
label: '授权范围',
title: '授权范围',
field: 'scopes',
table: {
show: false
}
isTable: false
},
{
label: '自动授权范围',
title: '自动授权范围',
field: 'autoApproveScopes',
table: {
show: false
}
isTable: false
},
{
label: '可重定向的 URI 地址',
title: '可重定向的 URI 地址',
field: 'redirectUris',
table: {
show: false
}
isTable: false
},
{
label: '权限',
title: '权限',
field: 'authorities',
table: {
show: false
}
isTable: false
},
{
label: '资源',
title: '资源',
field: 'resourceIds',
table: {
show: false
}
isTable: false
},
{
label: '附加信息',
title: '附加信息',
field: 'additionalInformation',
table: {
show: false
},
isTable: false,
form: {
component: 'Input',
componentProps: {
@ -120,22 +100,11 @@ const crudSchemas = reactive<CrudSchema[]>([
}
},
{
label: t('common.createTime'),
title: t('common.createTime'),
field: 'createTime',
form: {
show: false
formatter: 'formatDate',
isForm: false
}
},
{
label: t('table.action'),
field: 'action',
width: '240px',
form: {
show: false
},
detail: {
show: false
}
}
])
export const { allSchemas } = useCrudSchemas(crudSchemas)
]
})
export const { allSchemas } = useVxeCrudSchemas(crudSchemas)

View File

@ -1,29 +1,112 @@
<template>
<ContentWrap>
<!-- 列表 -->
<vxe-grid ref="xGrid" v-bind="gridOptions" class="xtable-scrollbar">
<!-- 操作新增 -->
<template #toolbar_buttons>
<XButton
type="primary"
preIcon="ep:zoom-in"
:title="t('action.add')"
v-hasPermi="['system:oauth2-client:create']"
@click="handleCreate()"
/>
</template>
<template #authorizedGrantTypes="{ row }">
<el-tag
:disable-transitions="true"
:key="index"
v-for="(authorizedGrantType, index) in row.authorizedGrantTypes"
:index="index"
>
{{ authorizedGrantType }}
</el-tag>
</template>
<template #actionbtns_default="{ row }">
<!-- 操作修改 -->
<XTextButton
preIcon="ep:edit"
:title="t('action.edit')"
v-hasPermi="['system:oauth2-client:update']"
@click="handleUpdate(row.id)"
/>
<!-- 操作详情 -->
<XTextButton
preIcon="ep:view"
:title="t('action.detail')"
v-hasPermi="['system:oauth2-client:update']"
@click="handleDetail(row.id)"
/>
<!-- 操作删除 -->
<XTextButton
preIcon="ep:delete"
:title="t('action.del')"
v-hasPermi="['system:oauth2-client:delete']"
@click="handleDelete(row.id)"
/>
</template>
</vxe-grid>
</ContentWrap>
<!-- 弹窗 -->
<XModal id="postModel" v-model="dialogVisible" :title="dialogTitle">
<template #default>
<!-- 表单添加/修改 -->
<Form
ref="formRef"
v-if="['create', 'update'].includes(actionType)"
:schema="allSchemas.formSchema"
:rules="rules"
/>
<!-- 表单详情 -->
<Descriptions
v-if="actionType === 'detail'"
:schema="allSchemas.detailSchema"
:data="detailRef"
/>
</template>
<template #footer>
<!-- 按钮保存 -->
<XButton
v-if="['create', 'update'].includes(actionType)"
type="primary"
:title="t('action.save')"
:loading="actionLoading"
@click="submitForm"
/>
<!-- 按钮关闭 -->
<XButton :loading="actionLoading" :title="t('dialog.close')" @click="dialogVisible = false" />
</template>
</XModal>
</template>
<script setup lang="ts">
// import
import { ref, unref } from 'vue'
import dayjs from 'dayjs'
import { ElMessage, ElImage, ElTag } from 'element-plus'
import { DICT_TYPE } from '@/utils/dict'
import { useTable } from '@/hooks/web/useTable'
import { ElTag } from 'element-plus'
import { useI18n } from '@/hooks/web/useI18n'
import { useMessage } from '@/hooks/web/useMessage'
import { useVxeGrid } from '@/hooks/web/useVxeGrid'
import { VxeGridInstance } from 'vxe-table'
import { FormExpose } from '@/components/Form'
import type { OAuth2ClientVo } from '@/api/system/oauth2/client.types'
import { rules, allSchemas } from './client.data'
// import
import * as ClientApi from '@/api/system/oauth2/client'
import { rules, allSchemas } from './client.data'
const { t } = useI18n() //
const message = useMessage() //
// ========== ==========
const { register, tableObject, methods } = useTable<OAuth2ClientVo>({
getListApi: ClientApi.getOAuth2ClientPageApi,
delListApi: ClientApi.deleteOAuth2ClientApi
//
const xGrid = ref<VxeGridInstance>() // Grid Ref
const { gridOptions } = useVxeGrid<ClientApi.OAuth2ClientVO>({
allSchemas: allSchemas,
getListApi: ClientApi.getOAuth2ClientPageApi
})
const { getList, setSearchParams, delList } = methods
// ========== CRUD ==========
const actionLoading = ref(false) //
const actionType = ref('') //
//
const dialogVisible = ref(false) //
const dialogTitle = ref('edit') //
const actionType = ref('') //
const actionLoading = ref(false) // Loading
const formRef = ref<FormExpose>() // Ref
const detailRef = ref() // Ref
//
const setDialogTile = (type: string) => {
@ -40,14 +123,35 @@ const handleCreate = () => {
}
//
const handleUpdate = async (row: OAuth2ClientVo) => {
const handleUpdate = async (rowId: number) => {
setDialogTile('update')
//
const res = await ClientApi.getOAuth2ClientApi(row.id)
const res = await ClientApi.getOAuth2ClientApi(rowId)
unref(formRef)?.setValues(res)
}
//
//
const handleDetail = async (rowId: number) => {
setDialogTile('detail')
const res = await ClientApi.getOAuth2ClientApi(rowId)
detailRef.value = res
}
//
const handleDelete = async (rowId: number) => {
message
.delConfirm()
.then(async () => {
await ClientApi.deleteOAuth2ClientApi(rowId)
message.success(t('common.delSuccess'))
})
.finally(() => {
//
xGrid.value?.commitProxy('query')
})
}
// /
const submitForm = async () => {
const elForm = unref(formRef)?.getElFormRef()
if (!elForm) return
@ -56,143 +160,21 @@ const submitForm = async () => {
actionLoading.value = true
//
try {
const data = unref(formRef)?.formModel as OAuth2ClientVo
const data = unref(formRef)?.formModel as ClientApi.OAuth2ClientVO
if (actionType.value === 'create') {
await ClientApi.createOAuth2ClientApi(data)
ElMessage.success(t('common.createSuccess'))
message.success(t('common.createSuccess'))
} else {
await ClientApi.updateOAuth2ClientApi(data)
ElMessage.success(t('common.updateSuccess'))
message.success(t('common.updateSuccess'))
}
//
dialogVisible.value = false
await getList()
} finally {
actionLoading.value = false
//
xGrid.value?.commitProxy('query')
}
}
})
}
// ========== ==========
const detailRef = ref() // Ref
//
const handleDetail = async (row: OAuth2ClientVo) => {
//
detailRef.value = row
setDialogTile('detail')
}
// ========== ==========
getList()
</script>
<template>
<!-- 搜索工作区 -->
<ContentWrap>
<Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
</ContentWrap>
<ContentWrap>
<!-- 操作工具栏 -->
<div class="mb-10px">
<el-button type="primary" v-hasPermi="['system:oauth2-client:create']" @click="handleCreate">
<Icon icon="ep:zoom-in" class="mr-5px" /> {{ t('action.add') }}
</el-button>
</div>
<!-- 列表 -->
<Table
:columns="allSchemas.tableColumns"
:selection="false"
:data="tableObject.tableList"
:loading="tableObject.loading"
:pagination="{
total: tableObject.total
}"
v-model:pageSize="tableObject.pageSize"
v-model:currentPage="tableObject.currentPage"
@register="register"
>
<template #logo="{ row }">
<el-image :src="row.logo" :preview-src-list="[row.logo]" />
</template>
<template #status="{ row }">
<DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
</template>
<template #authorizedGrantTypes="{ row }">
<el-tag
:disable-transitions="true"
:key="index"
v-for="(authorizedGrantType, index) in row.authorizedGrantTypes"
:index="index"
>
{{ authorizedGrantType }}
</el-tag>
</template>
<template #createTime="{ row }">
<span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
</template>
<template #action="{ row }">
<el-button
link
type="primary"
v-hasPermi="['system:oauth2-client:update']"
@click="handleUpdate(row)"
>
<Icon icon="ep:edit" class="mr-1px" /> {{ t('action.edit') }}
</el-button>
<el-button
link
type="primary"
v-hasPermi="['system:oauth2-client:update']"
@click="handleDetail(row)"
>
<Icon icon="ep:view" class="mr-1px" /> {{ t('action.detail') }}
</el-button>
<el-button
link
type="primary"
v-hasPermi="['system:oauth2-client:delete']"
@click="delList(row.id, false)"
>
<Icon icon="ep:delete" class="mr-1px" /> {{ t('action.del') }}
</el-button>
</template>
</Table>
</ContentWrap>
<Dialog v-model="dialogVisible" :title="dialogTitle" width="60%" maxHeight="420px">
<!-- 对话框(添加 / 修改) -->
<Form
v-if="['create', 'update'].includes(actionType)"
:schema="allSchemas.formSchema"
:rules="rules"
ref="formRef"
/>
<!-- 对话框(详情) -->
<Descriptions
v-if="actionType === 'detail'"
:schema="allSchemas.detailSchema"
:data="detailRef"
>
<template #status="{ row }">
<DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
</template>
<template #createTime="{ row }">
<span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
</template>
</Descriptions>
<!-- 操作按钮 -->
<template #footer>
<el-button
v-if="['create', 'update'].includes(actionType)"
type="primary"
:loading="actionLoading"
@click="submitForm"
>
{{ t('action.save') }}
</el-button>
<el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
</template>
</Dialog>
</template>

View File

@ -1,92 +1,69 @@
<script setup lang="ts">
import dayjs from 'dayjs'
import { useTable } from '@/hooks/web/useTable'
import { allSchemas } from './token.data'
import { DICT_TYPE } from '@/utils/dict'
import { useI18n } from '@/hooks/web/useI18n'
import type { OAuth2TokenVo } from '@/api/system/oauth2/token.types'
import * as TokenApi from '@/api/system/oauth2/token'
import { ref } from 'vue'
const { t } = useI18n() //
// ========== ==========
const { register, tableObject, methods } = useTable<OAuth2TokenVo>({
getListApi: TokenApi.getAccessTokenPageApi,
delListApi: TokenApi.deleteAccessTokenApi
})
// ========== ==========
const detailRef = ref() // Ref
const dialogVisible = ref(false) //
const dialogTitle = ref(t('action.detail')) //
const { getList, setSearchParams, delList } = methods
//
const handleDetail = (row: OAuth2TokenVo) => {
//
detailRef.value = row
dialogVisible.value = true
}
// 退
const handleForceLogout = (row: OAuth2TokenVo) => {
delList(row.id, false)
}
getList()
</script>
<template>
<ContentWrap>
<Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
</ContentWrap>
<ContentWrap>
<Table
:columns="allSchemas.tableColumns"
:selection="false"
:data="tableObject.tableList"
:loading="tableObject.loading"
:pagination="{
total: tableObject.total
}"
v-model:pageSize="tableObject.pageSize"
v-model:currentPage="tableObject.currentPage"
@register="register"
>
<template #userType="{ row }">
<DictTag :type="DICT_TYPE.USER_TYPE" :value="row.userType" />
</template>
<template #createTime="{ row }">
<span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
</template>
<template #expiresTime="{ row }">
<span>{{ dayjs(row.expiresTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
</template>
<template #action="{ row }">
<el-button link type="primary" @click="handleDetail(row)">
<Icon icon="ep:view" class="mr-1px" /> {{ t('action.detail') }}
</el-button>
<el-button
link
type="primary"
<!-- 列表 -->
<vxe-grid ref="xGrid" v-bind="gridOptions" class="xtable-scrollbar">
<template #actionbtns_default="{ row }">
<!-- 操作详情 -->
<XTextButton preIcon="ep:view" :title="t('action.detail')" @click="handleDetail(row)" />
<!-- 操作删除 -->
<XTextButton
preIcon="ep:delete"
:title="t('action.logout')"
v-hasPermi="['system:oauth2-token:delete']"
@click="handleForceLogout(row)"
>
<Icon icon="ep:delete" class="mr-1px" /> {{ t('action.logout') }}
</el-button>
@click="handleForceLogout(row.id)"
/>
</template>
</Table>
</vxe-grid>
</ContentWrap>
<Dialog v-model="dialogVisible" :title="dialogTitle">
<!-- 对话框(详情) -->
<Descriptions :schema="allSchemas.detailSchema" :data="detailRef">
<template #userType="{ row }">
<DictTag :type="DICT_TYPE.USER_TYPE" :value="row.userType" />
</template>
<template #createTime="{ row }">
<span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
</template>
<template #expiresTime="{ row }">
<span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
</template>
</Descriptions>
<Descriptions :schema="allSchemas.detailSchema" :data="detailRef" />
<!-- 操作按钮 -->
<template #footer>
<el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
</template>
</Dialog>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { useI18n } from '@/hooks/web/useI18n'
import { useMessage } from '@/hooks/web/useMessage'
import { useVxeGrid } from '@/hooks/web/useVxeGrid'
import { VxeGridInstance } from 'vxe-table'
import { allSchemas } from './token.data'
import * as TokenApi from '@/api/system/oauth2/token'
const { t } = useI18n() //
const message = useMessage() //
//
const xGrid = ref<VxeGridInstance>() // Grid Ref
const { gridOptions } = useVxeGrid<TokenApi.OAuth2TokenVO>({
allSchemas: allSchemas,
getListApi: TokenApi.getAccessTokenPageApi
})
// ========== ==========
const detailRef = ref() // Ref
const dialogVisible = ref(false) //
const dialogTitle = ref(t('action.detail')) //
//
const handleDetail = async (row: TokenApi.OAuth2TokenVO) => {
//
detailRef.value = row
dialogVisible.value = true
}
// 退
const handleForceLogout = (rowId: number) => {
message
.delConfirm()
.then(async () => {
await TokenApi.deleteAccessTokenApi(rowId)
message.success(t('common.delSuccess'))
})
.finally(() => {
//
xGrid.value?.commitProxy('query')
})
}
</script>

View File

@ -1,68 +1,45 @@
import { reactive } from 'vue'
import { useI18n } from '@/hooks/web/useI18n'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { DICT_TYPE } from '@/utils/dict'
import { VxeCrudSchema, useVxeCrudSchemas } from '@/hooks/web/useVxeCrudSchemas'
const { t } = useI18n() // 国际化
// CrudSchema
const crudSchemas = reactive<CrudSchema[]>([
const crudSchemas = reactive<VxeCrudSchema>({
primaryKey: 'id',
primaryType: 'seq',
action: true,
columns: [
{
label: t('common.index'),
field: 'id',
type: 'index',
form: {
show: false
},
detail: {
show: false
}
},
{
label: '用户编号',
title: '用户编号',
field: 'userId',
search: {
show: true
}
isSearch: true
},
{
label: '访问令牌',
title: '访问令牌',
field: 'accessToken'
},
{
label: '刷新令牌',
title: '刷新令牌',
field: 'refreshToken'
},
{
label: '用户类型',
title: '用户类型',
field: 'userType',
dictType: DICT_TYPE.USER_TYPE,
search: {
show: true
}
isSearch: true
},
{
label: t('common.createTime'),
title: t('common.createTime'),
field: 'createTime',
form: {
show: false
}
formatter: 'formatDate',
isForm: false
},
{
label: '过期时间',
title: '过期时间',
field: 'expiresTime',
form: {
show: false
formatter: 'formatDate',
isForm: false
}
},
{
label: t('table.action'),
field: 'action',
width: '200px',
form: {
show: false
},
detail: {
show: false
}
}
])
export const { allSchemas } = useCrudSchemas(crudSchemas)
]
})
export const { allSchemas } = useVxeCrudSchemas(crudSchemas)