From 595f3fb2bb5d5e648e0704b6d2cf4e4f11da2eb4 Mon Sep 17 00:00:00 2001 From: Shing Rui Date: Fri, 10 May 2024 22:53:28 +0800 Subject: [PATCH 01/20] Use i18n in router. --- .sonarlint/connectedMode.json | 4 +++ .vscode/extensions.json | 2 +- src/components/User.vue | 50 ++++++++++++++++++++++++--- src/components/layout/EBreadCrumb.vue | 6 ++-- src/components/layout/EMenu.vue | 6 ++-- src/lang/en.js | 7 +++- src/lang/zh.js | 5 +++ src/router/index.js | 6 ++-- 8 files changed, 72 insertions(+), 14 deletions(-) create mode 100644 .sonarlint/connectedMode.json diff --git a/.sonarlint/connectedMode.json b/.sonarlint/connectedMode.json new file mode 100644 index 0000000..b01b014 --- /dev/null +++ b/.sonarlint/connectedMode.json @@ -0,0 +1,4 @@ +{ + "sonarCloudOrganization": "sunquakes", + "projectKey": "sunquakes_electron-kits" +} \ No newline at end of file diff --git a/.vscode/extensions.json b/.vscode/extensions.json index c0a6e5a..427af50 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -1,3 +1,3 @@ { - "recommendations": ["Vue.volar", "Vue.vscode-typescript-vue-plugin"] + "recommendations": ["Vue.vscode-typescript-vue-plugin"] } diff --git a/src/components/User.vue b/src/components/User.vue index cf20f7f..b178dc9 100644 --- a/src/components/User.vue +++ b/src/components/User.vue @@ -1,9 +1,49 @@ - + diff --git a/src/components/layout/EBreadCrumb.vue b/src/components/layout/EBreadCrumb.vue index 546c26d..c5e3ad4 100644 --- a/src/components/layout/EBreadCrumb.vue +++ b/src/components/layout/EBreadCrumb.vue @@ -10,6 +10,8 @@ diff --git a/src/components/layout/EBreadCrumb.vue b/src/components/layout/BreadCrumb.vue similarity index 100% rename from src/components/layout/EBreadCrumb.vue rename to src/components/layout/BreadCrumb.vue diff --git a/src/components/layout/EFooter.vue b/src/components/layout/Footer.vue similarity index 100% rename from src/components/layout/EFooter.vue rename to src/components/layout/Footer.vue diff --git a/src/components/layout/EHeader.vue b/src/components/layout/Header.vue similarity index 100% rename from src/components/layout/EHeader.vue rename to src/components/layout/Header.vue diff --git a/src/components/layout/ELogo.vue b/src/components/layout/Logo.vue similarity index 100% rename from src/components/layout/ELogo.vue rename to src/components/layout/Logo.vue diff --git a/src/components/layout/EMenu.vue b/src/components/layout/Menu.vue similarity index 100% rename from src/components/layout/EMenu.vue rename to src/components/layout/Menu.vue diff --git a/src/components/user/Form.vue b/src/components/user/Form.vue new file mode 100644 index 0000000..8fd8f74 --- /dev/null +++ b/src/components/user/Form.vue @@ -0,0 +1,127 @@ + + + diff --git a/src/db/init.js b/src/db/init.js index 46f0305..903bd7e 100644 --- a/src/db/init.js +++ b/src/db/init.js @@ -5,6 +5,7 @@ export function init() { id Integer PRIMARY KEY AUTOINCREMENT, username VARCHAR(255) NOT NULL DEFAULT "", password VARCHAR(255) NOT NULL DEFAULT "", + nickname VARCHAR(255) NOT NULL DEFAULT "", state Integer NOT NULL DEFAULT 0, create_time DATETIME, update_time DATETIME @@ -15,6 +16,6 @@ export function init() { execute(createUniqueIndexSql) // Insert a user record, the username is `admin`, the password is `admin`. - const insertSql = `INSERT INTO user (username, password, state, create_time, update_time) VALUES ('admin', '21232f297a57a5a743894a0e4a801fc3', 1, CURRENT_TIMESTAMP, null);` + const insertSql = `INSERT INTO user (username, password, nickname, state, create_time, update_time) VALUES ('admin', '21232f297a57a5a743894a0e4a801fc3', 'Administrator', 1, CURRENT_TIMESTAMP, null);` execute(insertSql) } diff --git a/src/db/sqlite3.js b/src/db/sqlite3.js index 4580596..8c1b0a8 100755 --- a/src/db/sqlite3.js +++ b/src/db/sqlite3.js @@ -86,7 +86,7 @@ export async function updateById(tableName, id, data) { if (err != null) { reject(new Error(err)) } else { - resolve(this.lastID) + resolve(id) } }) }) @@ -115,6 +115,23 @@ export async function list(tableName, where, orderBy, offset, limit) { }) } +export async function count(tableName, where) { + let sql = `SELECT COUNT(*) AS count FROM ${tableName}` + if (where) { + sql = parseWhere(sql, where) + } + const db = await getDb() + return await new Promise((resolve, reject) => { + db.get(sql, (err, row) => { + if (err != null) { + reject(new Error(err)) + } else { + resolve(row.count) + } + }) + }) +} + function parseWhere(sql, where) { let whereArray = [] for (let item of where) { diff --git a/src/lang/en.js b/src/lang/en.js index e655842..aaa3acc 100644 --- a/src/lang/en.js +++ b/src/lang/en.js @@ -1,16 +1,36 @@ // sonar-ignore-start export default { + title: { + add: 'Add', + edit: 'Edit' + }, action: { delete: 'Delete', - edit: 'Edit' + add: 'Add', + edit: 'Edit', + search: 'Search', + reset: 'Reset' }, button: { ok: 'Ok', - no: 'No', + no: 'No' }, confirm: { delete: 'Are you sure delete this record?' }, + message: { + add: { + success: 'Add successful!', + fail: 'Add failed!' + }, + edit: { + success: 'Edit successful!', + fail: 'Edit failed!' + } + }, + form: { + required: 'Please input your {0}!' + }, menu: { dashboard: 'Dashboard', system: 'System', @@ -24,6 +44,11 @@ export default { username_not_exist: 'Username does not exist!', wrong_password: 'Wrong password!' // NOSONAR }, + user: { + username: 'Username', + nickname: 'Nickname', + password: 'Password' // NOSONAR + }, logout: { confirm: 'Do you want to logout?' }, diff --git a/src/lang/zh.js b/src/lang/zh.js index 8e01b98..5d6db70 100644 --- a/src/lang/zh.js +++ b/src/lang/zh.js @@ -1,15 +1,35 @@ export default { + title: { + add: '新增', + edit: '编辑' + }, action: { delete: '删除', - edit: '编辑' + add: '新增', + edit: '编辑', + search: '搜索', + reset: '重置' }, button: { ok: '是', - no: '否', + no: '否' }, confirm: { delete: '确定删除这条记录?' }, + message: { + add: { + success: '新增成功!', + fail: '新增失败!' + }, + edit: { + success: '编辑成功!', + fail: '编辑失败!' + } + }, + form: { + required: '请输入{0}!' + }, menu: { dashboard: '首页', system: '系统管理', @@ -23,6 +43,11 @@ export default { username_not_exist: '用户名不存在!', wrong_password: '密码错误!' // NOSONAR }, + user: { + username: '用户名', + nickname: '昵称', + password: '密码' // NOSONAR + }, logout: { confirm: '确定退出登录?' }, diff --git a/src/router/index.js b/src/router/index.js index 91729d1..21d9373 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -2,9 +2,9 @@ import { h } from 'vue' import { createMemoryHistory, createRouter } from 'vue-router' import StrayBirds from '../components/StrayBirds.vue' -import ELayout from '../components/ELayout.vue' +import Layout from '../components/Layout.vue' import User from '../components/User.vue' -import ELogin from '../components/ELogin.vue' +import Login from '../components/Login.vue' import { DashboardOutlined, @@ -12,10 +12,10 @@ import { } from '@ant-design/icons-vue' const routes = [ - { name: 'Login', path: '/login', component: ELogin }, + { name: 'Login', path: '/login', component: Login }, { path: '/', - component: ELayout, + component: Layout, children: [ { name: 'Dashboard', diff --git a/src/utils/date.js b/src/utils/date.js new file mode 100644 index 0000000..c5a4009 --- /dev/null +++ b/src/utils/date.js @@ -0,0 +1,3 @@ +export function datetime(date) { + return date.toISOString().replace('T', ' ').split('.')[0] +} From bf9df43f78fa871dc95c21a04c458b2c351ecec1 Mon Sep 17 00:00:00 2001 From: Shing Rui Date: Tue, 21 May 2024 20:53:18 +0800 Subject: [PATCH 13/20] Get page list. --- __test__/sqlite3.test.js | 10 ++++-- src/api/user.js | 6 ++-- src/components/Login.vue | 2 +- src/components/User.vue | 63 ++++++++++++++++++++++++++++++------ src/components/user/Form.vue | 9 +++--- src/db/sqlite3.js | 7 ++++ 6 files changed, 76 insertions(+), 21 deletions(-) diff --git a/__test__/sqlite3.test.js b/__test__/sqlite3.test.js index 17c7503..bfae72d 100644 --- a/__test__/sqlite3.test.js +++ b/__test__/sqlite3.test.js @@ -1,4 +1,4 @@ -import { execute, save, list, count, getOne, updateById, remove } from '../src/db/sqlite3' +import { execute, save, list, page, count, getOne, updateById, remove } from '../src/db/sqlite3' test('Create Table', () => { const sql = @@ -31,8 +31,12 @@ test('CRUD', async () => { expect(row.content).toBe('Hello China!') // Test records count. - const c = await count('test') - expect(c).toBeGreaterThan(0) + const total = await count('test') + expect(total).toBeGreaterThan(0) + + // Test page. + const pageList = await page('test') + expect(pageList.total).toBeGreaterThan(0) // Test delete records. remove('test', ['id', insertId]) diff --git a/src/api/user.js b/src/api/user.js index 2f14799..81db1c3 100644 --- a/src/api/user.js +++ b/src/api/user.js @@ -1,11 +1,11 @@ -import { list, getOne, remove, save, updateById } from '../db/sqlite3' +import { page, getOne, remove, save, updateById } from '../db/sqlite3' import CryptoJS from 'crypto-js' import { datetime } from '../utils/date' const TABLE_NAME = 'user' -export async function page() { - return list(TABLE_NAME) +export async function pageList(current, pageSize, where, orderBy) { + return page(TABLE_NAME, current, pageSize, where, orderBy) } export async function login(username, password) { diff --git a/src/components/Login.vue b/src/components/Login.vue index ae86ee2..13b156a 100644 --- a/src/components/Login.vue +++ b/src/components/Login.vue @@ -47,7 +47,7 @@