Skip to content

Commit

Permalink
Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
sunquakes committed May 30, 2024
1 parent dbac95c commit b0f5c13
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 46 deletions.
3 changes: 2 additions & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
sonar.organization=sunquakes
sonar.projectKey=sunquakes_electron-kits

sonar.tests=src
sonar.tests=__test__
sonar.sources=src
sonar.exclusions=src/lang/**.ts,coverage/**
sonar.javascript.lcov.reportPaths=coverage/lcov.info
2 changes: 1 addition & 1 deletion src/api/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { datetime } from '../utils/date'

const TABLE_NAME = 'user'

export async function pageList(current: number, pageSize: number, where: string[][], orderBy: string): Promise<any> {
export async function pageList(current: number, pageSize: number, where?: string[][], orderBy?: string): Promise<any> {
return page(TABLE_NAME, current, pageSize, where, orderBy)
}

Expand Down
6 changes: 3 additions & 3 deletions src/components/Layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ const store = useStore()
options.before = (resolve, reject) => {
const user = store.getters.user
const currentRoute = router.currentRoute.value
if (!user && !['Login'].includes(currentRoute.name)) {
if (!user && !['Login'].includes(currentRoute.name as string)) {
// Route to the login page.
// router.push({ name: 'Login' })
// reject(new Error('No permission.'))
router.push({ name: 'Login' })
reject(new Error('No permission.'))
}
}
</script>
Expand Down
20 changes: 13 additions & 7 deletions src/components/User.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@

<script lang="ts" setup>
import { ref, onMounted, reactive } from 'vue'
import type { TableColumnsType, FormInstance } from 'ant-design-vue'
import type { TableColumnsType, FormInstance, TablePaginationConfig } from 'ant-design-vue'
import { pageList, del } from '../api/user'
import { useI18n } from 'vue-i18n'
import Form from './user/Form.vue'
const formRef = ref<FormInstance>()
const formState = reactive({
const formState = reactive<any>({
username: undefined,
nickname: undefined
})
Expand All @@ -79,7 +79,7 @@ const initPagination = {
pageSize: 5
}
const pagination = reactive({ ...initPagination })
const pagination = reactive<Pagination>({ ...initPagination })
const { t } = useI18n({ useScope: 'global' })
Expand All @@ -97,6 +97,12 @@ const columns: TableColumnsType = [
}
]
interface Pagination {
total?: number
pageSize: number
current: number
}
interface DataItem {
id: number
username: string
Expand Down Expand Up @@ -125,7 +131,7 @@ const parseWhere = (): string[][] => {
return where
}
const handlePage = async (p) => {
const handlePage = async (p?: TablePaginationConfig) => {
if (p) {
Object.assign(pagination, p)
} else {
Expand All @@ -139,13 +145,13 @@ const handlePage = async (p) => {
} catch (e) {}
}
const handleDel = async (record) => {
const handleDel = async (record: any) => {
await del([['id', record.id]])
handlePage()
}
const open = ref<boolean>(false)
const item = ref(null)
const item = ref()
const formTitle = ref<string>('')
Expand All @@ -155,7 +161,7 @@ const handleAdd = () => {
item.value = null
}
const handleEdit = (record: []) => {
const handleEdit = (record: Record<string, any>) => {
formTitle.value = t('title.edit')
open.value = true
item.value = record
Expand Down
14 changes: 9 additions & 5 deletions src/components/layout/BreadCrumb.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import { watch, defineModel, ref } from 'vue'
import router from '../../router'
import { useI18n } from 'vue-i18n'
import { RouteRecordRaw } from 'vue-router'
const { t, locale } = useI18n({ useScope: 'global' })
interface Route {
Expand All @@ -22,20 +23,23 @@ interface Route {
}>
}
const list = router.options.routes
const routes = router.options.routes
const list = routes.map((item) => {
return { ...item }
})
const getMenu = (list: RouteRecord[], name: string): Route[] | undefined => {
const getMenu = (list: RouteRecordRaw[], name: string): Route[] | undefined => {
let breadcrumb = []
for (let item of list) {
if (item.meta != undefined && item.meta.isMenu) {
breadcrumb.push({ path: item.path, breadcrumbName: t(item.meta.title) })
breadcrumb.push({ path: item.path, breadcrumbName: t(item.meta.title as string) })
if (item.name == name) {
return breadcrumb
}
}
if (item.children != undefined && item.children.length > 0) {
const children = getMenu(item.children, name)
if (children.length > 0) {
if (children != undefined && children.length > 0) {
breadcrumb = breadcrumb.concat(children)
return breadcrumb
}
Expand All @@ -44,7 +48,7 @@ const getMenu = (list: RouteRecord[], name: string): Route[] | undefined => {
}
}
const model = defineModel({ required: true })
const model = defineModel({ required: true, type: String })
const items = ref<Route[]>()
items.value = getMenu(list, model.value)
Expand Down
39 changes: 24 additions & 15 deletions src/components/layout/Menu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@
</template>

<script lang="ts" setup>
import { reactive, watch, ref, h, defineModel } from 'vue'
import { reactive, watch, ref, h, defineModel, VueElement } from 'vue'
import router from '../../router'
import { useI18n } from 'vue-i18n'
import { RouteRecord } from 'vue-router'
import { RouteRecord, RouteRecordName } from 'vue-router'
import { ItemType } from 'ant-design-vue'
import { Key } from 'ant-design-vue/es/_util/type'
const { t, locale } = useI18n({ useScope: 'global' })
const model = defineModel({ required: true })
const state = reactive({
selectedKeys: [model.value]
selectedKeys: [model.value as Key]
})
const list = router.options.routes
const routes = router.options.routes
const list = routes.map((item) => {
return { ...item }
})
function getItem(
label: VueElement | string,
Expand All @@ -39,7 +44,7 @@ function getItem(
} as ItemType
}
const getMenu = (list: RouteRecord[]) => {
const getMenu = (list: RouteRecord[]): any => {
let menu = []
for (let item of list) {
if (item.meta != undefined && item.meta.isMenu) {
Expand All @@ -60,13 +65,17 @@ const getMenuItem = (item: RouteRecord) => {
let menuItem
if (item.children != undefined && item.children.length > 0) {
menuItem = getItem(
t(item.meta.title),
item.name,
t(item.meta.title as string),
item.name as string,
item?.meta?.icon ? h(item.meta.icon) : null,
getMenu(item.children)
getMenu(item.children as RouteRecord[])
)
} else {
menuItem = getItem(t(item.meta.title), item.name, item?.meta?.icon ? h(item.meta.icon) : null)
menuItem = getItem(
t(item.meta.title as string),
item.name as string,
item?.meta?.icon ? h(item.meta.icon) : null
)
}
return menuItem
Expand All @@ -76,7 +85,7 @@ const getMenuChildren = (list: RouteRecord[]) => {
let menu = []
for (let item of list) {
if (item.children != undefined && item.children.length > 0) {
menu = getMenu(item.children)
menu = getMenu(item.children as RouteRecord[])
}
}
return menu
Expand All @@ -90,19 +99,19 @@ watch(
)
const items = ref<ItemType[]>()
items.value = getMenu(list)
items.value = getMenu(list as RouteRecord[])
watch(
() => locale.value,
(_val) => {
items.value = getMenu(list)
items.value = getMenu(list as RouteRecord[])
}
)
router.push({ name: model.value })
router.push({ name: model.value as RouteRecordName })
const click = (menu) => {
const click = (menu: any) => {
model.value = menu.key
router.push({ name: menu.key })
router.push({ name: menu.key as RouteRecordName })
}
</script>
17 changes: 10 additions & 7 deletions src/components/user/Form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,23 @@ const validateMessages = {
}
interface User {
id: number | undefined
username: string
nickname: string
password: string
}
const user: User = {
username: undefined,
nickname: undefined,
password: undefined
id: undefined,
username: '',
nickname: '',
password: ''
}
let formState = ref<User>({ ...user })
const formRef = ref(null)
const formRef = ref()
const handleSubmit = async (values: any) => {
const valid = await formRef.value.validate()
if (valid) {
Expand All @@ -95,7 +98,7 @@ const handleAdd = async () => {
const handleEdit = async () => {
try {
await edit(formState.value.id, formState.value)
await edit(formState.value.id as number, formState.value)
message.success(t('message.edit.success'))
onClose()
emit('refreshList')
Expand All @@ -108,7 +111,7 @@ const onClose = () => {
emit('close', false)
}
const resetFormState = (data) => {
const resetFormState = (data: User) => {
if (data == null) {
formState.value = { ...user }
} else {
Expand All @@ -120,7 +123,7 @@ const resetFormState = (data) => {
watch(
() => model.value,
(_val) => {
resetFormState(model.value)
resetFormState(model.value as User)
}
)
</script>
14 changes: 7 additions & 7 deletions src/db/sqlite3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ export async function updateById(tableName: string, id: any, data: any): Promise

export async function list(
tableName: string,
where: string[][],
orderBy: string,
offset: number,
limit: number
where?: string[][],
orderBy?: string,
offset?: number,
limit?: number
): Promise<any> {
let sql = `SELECT * FROM ${tableName}`
if (where) {
Expand All @@ -120,7 +120,7 @@ export async function list(
})
}

export async function count(tableName: string, where: string[][]): Promise<any> {
export async function count(tableName: string, where?: string[][]): Promise<any> {
let sql = `SELECT COUNT(*) AS count FROM ${tableName}`
if (where) {
sql = parseWhere(sql, where)
Expand Down Expand Up @@ -203,8 +203,8 @@ export async function page(
tableName: string,
current: number,
pageSize: number,
where: string[][],
orderBy: string
where?: string[][],
orderBy?: string
): Promise<any> {
const offset = (current - 1) * pageSize
const total = await count(tableName, where)
Expand Down

0 comments on commit b0f5c13

Please sign in to comment.