Skip to content

Commit

Permalink
网站logo 和标题可配置
Browse files Browse the repository at this point in the history
  • Loading branch information
wanglu committed Dec 20, 2021
1 parent 41a8a67 commit 31762eb
Show file tree
Hide file tree
Showing 29 changed files with 379 additions and 126 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
![后台登录](images/login.png)
![后台设置](images/admin-overview.png)
![增加工具](images/add-tool.png)
![修改配置](images/setting.png)


## 故事
Expand Down Expand Up @@ -37,11 +38,9 @@

## 参与项目
可以优化的点太多了,慢慢完善吧……
- [ ] 多平台构建流水线
- [ ] Docker 支持
- [ ] 多平台流水线
- [x] 多平台构建流水线
- [x] 定制化 logo 和标题
- [ ] 私有分类功能,对外隐藏
- [ ] 定制化 logo 和标题
- [ ] 主题切换
- [ ] 移动端优化

Expand Down
Binary file added images/setting.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
143 changes: 121 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ import (
// _ "github.com/mattn/go-sqlite3"
)
const INDEX = "index.html"

// 默认是 0
type Setting struct {
Id int `json:"id"`
Favicon string `json:"favicon"`
Title string `json:"title"`
}

type User struct {
Id int `json:"id"`
Name string `json:"name"`
Expand Down Expand Up @@ -72,7 +80,7 @@ type Catelog struct {

func checkErr(err error) {
if err != nil {
fmt.Println(err)
fmt.Println("捕获到错误:",err)
}
}

Expand All @@ -91,9 +99,9 @@ func updateCatelog(data updateCatelogDto, db *sql.DB) {
checkErr(err)
res, err := stmt.Exec(data.Name, data.Id)
checkErr(err)
affect, err := res.RowsAffected()
_, err = res.RowsAffected()
checkErr(err)
fmt.Println(affect)
// fmt.Println(affect)
}

func updateTool(data updateToolDto, db *sql.DB) {
Expand All @@ -106,11 +114,27 @@ func updateTool(data updateToolDto, db *sql.DB) {
checkErr(err)
res, err := stmt.Exec(data.Name, data.Url, data.Logo, data.Catelog, data.Desc, data.Id)
checkErr(err)
affect, err := res.RowsAffected()
_, err = res.RowsAffected()
checkErr(err)
// fmt.Println(affect)
}

func updateSetting(data Setting, db *sql.DB) {
sql_update_setting := `
UPDATE nav_setting
SET favicon = ?, title = ?
WHERE id = ?;
`
stmt, err := db.Prepare(sql_update_setting)
checkErr(err)
res, err := stmt.Exec(data.Favicon, data.Title, 0)
checkErr(err)
fmt.Println(affect)
_, err = res.RowsAffected()
checkErr(err)
// fmt.Println(affect)
}


func updateUser(data updateUserDto, db *sql.DB) {
sql_update_user := `
UPDATE nav_user
Expand All @@ -121,9 +145,9 @@ func updateUser(data updateUserDto, db *sql.DB) {
checkErr(err)
res, err := stmt.Exec(data.Name, data.Password, data.Id)
checkErr(err)
affect, err := res.RowsAffected()
_, err = res.RowsAffected()
checkErr(err)
fmt.Println(affect)
// fmt.Println(affect)
}

func addCatelog(data addCatelogDto, db *sql.DB) {
Expand All @@ -136,9 +160,9 @@ func addCatelog(data addCatelogDto, db *sql.DB) {
checkErr(err)
res, err := stmt.Exec(generateId(), data.Name)
checkErr(err)
id, err := res.LastInsertId()
_, err = res.LastInsertId()
checkErr(err)
fmt.Println(id)
// fmt.Println(id)
}

func addTool(data addToolDto, db *sql.DB) {
Expand All @@ -150,9 +174,9 @@ func addTool(data addToolDto, db *sql.DB) {
checkErr(err)
res, err := stmt.Exec(generateId(), data.Name, data.Url, data.Logo, data.Catelog, data.Desc)
checkErr(err)
id, err := res.LastInsertId()
_, err = res.LastInsertId()
checkErr(err)
fmt.Println(id)
// fmt.Println(id)
}

func getAllTool(db *sql.DB) []Tool {
Expand Down Expand Up @@ -199,7 +223,7 @@ var db *sql.DB
func initDB() {
// 创建数据库
db, _ = sql.Open("sqlite", "./nav.db")
// 创建表
// user 表
sql_create_table := `
CREATE TABLE IF NOT EXISTS nav_user (
id INTEGER PRIMARY KEY AUTOINCREMENT,
Expand All @@ -209,6 +233,17 @@ func initDB() {
`
_, err := db.Exec(sql_create_table)
checkErr(err)
// setting 表
sql_create_table = `
CREATE TABLE IF NOT EXISTS nav_setting (
id INTEGER PRIMARY KEY AUTOINCREMENT,
favicon TEXT,
title TEXT
);
`
_, err = db.Exec(sql_create_table)
checkErr(err)
// 默认 tools 用的 表
sql_create_table = `
CREATE TABLE IF NOT EXISTS nav_table (
id INTEGER PRIMARY KEY AUTOINCREMENT,
Expand All @@ -221,6 +256,7 @@ func initDB() {
`
_, err = db.Exec(sql_create_table)
checkErr(err)
// 分类表
sql_create_table = `
CREATE TABLE IF NOT EXISTS nav_catelog (
id INTEGER PRIMARY KEY AUTOINCREMENT,
Expand All @@ -244,11 +280,31 @@ func initDB() {
checkErr(err)
res, err := stmt.Exec(generateId(), "admin", "admin")
checkErr(err)
id, err := res.LastInsertId()
_, err = res.LastInsertId()
checkErr(err)
fmt.Println(id)
// fmt.Println(id)
}
defer rows.Close()
rows.Close()
// 如果不存在设置,就初始化
sql_get_setting := `
SELECT * FROM nav_setting;
`
rows, err = db.Query(sql_get_setting)
checkErr(err)
if !rows.Next() {
sql_add_setting := `
INSERT INTO nav_setting (id, favicon, title)
VALUES (?, ?, ?);
`
stmt, err := db.Prepare(sql_add_setting)
checkErr(err)
res, err := stmt.Exec(0, "https://pic.mereith.com/img/male.svg", "Van Nav")
checkErr(err)
_, err = res.LastInsertId()
checkErr(err)
// fmt.Println(id)
}
rows.Close()
fmt.Println("数据库初始化成功。。。")
}
//go:embed public
Expand Down Expand Up @@ -319,6 +375,8 @@ func main() {

admin.PUT("/user", UpdateUserHandler)

admin.PUT("/setting", UpdateSettingHandler)

admin.POST("/tool", AddToolHandler)
admin.DELETE("/tool/:id", DeleteToolHandler)
admin.PUT("/tool/:id", UpdateToolHandler)
Expand All @@ -328,11 +386,29 @@ func main() {
admin.PUT("/catelog/:id", UpdateCatelogHandler)
}
}

fmt.Println("应用启动成功,网址: http://127.0.0.1:8080")
router.Run(":8080")
}


func UpdateSettingHandler(c *gin.Context) {
var data Setting
if err := c.ShouldBindJSON(&data); err != nil {
checkErr(err)
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"errorMessage": err.Error(),
})
return
}
updateSetting(data, db)
c.JSON(200, gin.H{
"success": true,
"message": "更新配置成功",
})
}


func UpdateUserHandler(c *gin.Context) {
var data updateUserDto
if err := c.ShouldBindJSON(&data); err != nil {
Expand All @@ -350,15 +426,19 @@ func UpdateUserHandler(c *gin.Context) {
})
}



func GetAllHandler(c *gin.Context) {
// 获取全部数据
tools := getAllTool(db)
catelogs := getAllCatelog(db)
setting := getSetting(db)
c.JSON(200, gin.H{
"success": true,
"data": gin.H{
"tools": tools,
"catelogs": catelogs,
"setting": setting,
},
})
}
Expand All @@ -367,15 +447,21 @@ func GetAdminAllDataHandler(c *gin.Context) {
// 管理员获取全部数据,还有个用户名。
tools := getAllTool(db)
catelogs := getAllCatelog(db)
setting := getSetting(db)
userId,ok := c.Get("uid")
if !ok {
fmt.Println("不存在 uid!")
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"errorMessage": "不存在该用户!",
})
return
}
c.JSON(200,gin.H{
"success": true,
"data": gin.H{
"tools": tools,
"catelogs": catelogs,
"setting": setting,
"user": gin.H{
"name": c.GetString("username"),
"id": userId,
Expand All @@ -384,6 +470,19 @@ func GetAdminAllDataHandler(c *gin.Context) {
})
}



func getSetting(db *sql.DB) Setting {
sql_get_user := `
SELECT * FROM nav_setting WHERE id = ?;
`
var setting Setting
row := db.QueryRow(sql_get_user, 0)
err := row.Scan(&setting.Id,&setting.Favicon,&setting.Title)
checkErr(err)
return setting
}

func getUser(name string, db *sql.DB) User {
sql_get_user := `
SELECT * FROM nav_user WHERE name = ?;
Expand Down Expand Up @@ -437,7 +536,7 @@ func LoginHandler(c *gin.Context) {

// 退出登录
func LogoutHandler(c *gin.Context) {
fmt.Println("TODO: 销毁 JWT")
// fmt.Println("TODO: 销毁 JWT")
c.JSON(200, gin.H{
"success": true,
"message": "登出成功",
Expand Down Expand Up @@ -472,9 +571,9 @@ func DeleteToolHandler(c *gin.Context) {
checkErr(err)
res, err := stmt.Exec(id)
checkErr(err)
affect, err := res.RowsAffected()
_, err = res.RowsAffected()
checkErr(err)
fmt.Println(affect)
// fmt.Println(affect)
c.JSON(200, gin.H{
"success": true,
"message": "删除成功",
Expand Down Expand Up @@ -528,9 +627,9 @@ func DeleteCatelogHandler(c *gin.Context) {
checkErr(err)
res, err := stmt.Exec(id)
checkErr(err)
affect, err := res.RowsAffected()
_, err = res.RowsAffected()
checkErr(err)
fmt.Println(affect)
// fmt.Println(affect)
c.JSON(200, gin.H{
"success": true,
"message": "删除分类成功",
Expand Down
Binary file modified nav
Binary file not shown.
Binary file removed nav.db
Binary file not shown.
110 changes: 55 additions & 55 deletions public/admin/umi.js

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions public/asset-manifest.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{
"files": {
"main.css": "/static/css/main.8a2a4163.chunk.css",
"main.js": "/static/js/main.9395d624.chunk.js",
"main.js.map": "/static/js/main.9395d624.chunk.js.map",
"runtime-main.js": "/static/js/runtime-main.6ce670f5.js",
"runtime-main.js.map": "/static/js/runtime-main.6ce670f5.js.map",
"static/js/2.9f67e5ae.chunk.js": "/static/js/2.9f67e5ae.chunk.js",
"static/js/2.9f67e5ae.chunk.js.map": "/static/js/2.9f67e5ae.chunk.js.map",
"static/js/3.69f5443a.chunk.js": "/static/js/3.69f5443a.chunk.js",
"static/js/3.69f5443a.chunk.js.map": "/static/js/3.69f5443a.chunk.js.map",
"main.js": "/static/js/main.39bffd4f.chunk.js",
"main.js.map": "/static/js/main.39bffd4f.chunk.js.map",
"runtime-main.js": "/static/js/runtime-main.cae03145.js",
"runtime-main.js.map": "/static/js/runtime-main.cae03145.js.map",
"static/js/2.a6477d0c.chunk.js": "/static/js/2.a6477d0c.chunk.js",
"static/js/2.a6477d0c.chunk.js.map": "/static/js/2.a6477d0c.chunk.js.map",
"static/js/3.ac9f81be.chunk.js": "/static/js/3.ac9f81be.chunk.js",
"static/js/3.ac9f81be.chunk.js.map": "/static/js/3.ac9f81be.chunk.js.map",
"index.html": "/index.html",
"static/css/main.8a2a4163.chunk.css.map": "/static/css/main.8a2a4163.chunk.css.map",
"static/js/2.9f67e5ae.chunk.js.LICENSE.txt": "/static/js/2.9f67e5ae.chunk.js.LICENSE.txt"
"static/js/2.a6477d0c.chunk.js.LICENSE.txt": "/static/js/2.a6477d0c.chunk.js.LICENSE.txt"
},
"entrypoints": [
"static/js/runtime-main.6ce670f5.js",
"static/js/2.9f67e5ae.chunk.js",
"static/js/runtime-main.cae03145.js",
"static/js/2.a6477d0c.chunk.js",
"static/css/main.8a2a4163.chunk.css",
"static/js/main.9395d624.chunk.js"
"static/js/main.39bffd4f.chunk.js"
]
}
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!doctype html><html lang="cn"><head><meta charset="utf-8"/><link rel="icon" href="https://pic.mereith.com/img/Tools.png-slim"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Mereith 的工具站"/><link rel="manifest" href="/manifest.json"/><title>Mereith 的工具站</title><link href="/static/css/main.8a2a4163.chunk.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script>!function(e){function t(t){for(var n,a,i=t[0],c=t[1],l=t[2],s=0,p=[];s<i.length;s++)a=i[s],Object.prototype.hasOwnProperty.call(o,a)&&o[a]&&p.push(o[a][0]),o[a]=0;for(n in c)Object.prototype.hasOwnProperty.call(c,n)&&(e[n]=c[n]);for(f&&f(t);p.length;)p.shift()();return u.push.apply(u,l||[]),r()}function r(){for(var e,t=0;t<u.length;t++){for(var r=u[t],n=!0,i=1;i<r.length;i++){var c=r[i];0!==o[c]&&(n=!1)}n&&(u.splice(t--,1),e=a(a.s=r[0]))}return e}var n={},o={1:0},u=[];function a(t){if(n[t])return n[t].exports;var r=n[t]={i:t,l:!1,exports:{}};return e[t].call(r.exports,r,r.exports,a),r.l=!0,r.exports}a.e=function(e){var t=[],r=o[e];if(0!==r)if(r)t.push(r[2]);else{var n=new Promise((function(t,n){r=o[e]=[t,n]}));t.push(r[2]=n);var u,i=document.createElement("script");i.charset="utf-8",i.timeout=120,a.nc&&i.setAttribute("nonce",a.nc),i.src=function(e){return a.p+"static/js/"+({}[e]||e)+"."+{3:"69f5443a"}[e]+".chunk.js"}(e);var c=new Error;u=function(t){i.onerror=i.onload=null,clearTimeout(l);var r=o[e];if(0!==r){if(r){var n=t&&("load"===t.type?"missing":t.type),u=t&&t.target&&t.target.src;c.message="Loading chunk "+e+" failed.\n("+n+": "+u+")",c.name="ChunkLoadError",c.type=n,c.request=u,r[1](c)}o[e]=void 0}};var l=setTimeout((function(){u({type:"timeout",target:i})}),12e4);i.onerror=i.onload=u,document.head.appendChild(i)}return Promise.all(t)},a.m=e,a.c=n,a.d=function(e,t,r){a.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},a.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},a.t=function(e,t){if(1&t&&(e=a(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(a.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var n in e)a.d(r,n,function(t){return e[t]}.bind(null,n));return r},a.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return a.d(t,"a",t),t},a.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},a.p="/",a.oe=function(e){throw console.error(e),e};var i=this.webpackJsonptool=this.webpackJsonptool||[],c=i.push.bind(i);i.push=t,i=i.slice();for(var l=0;l<i.length;l++)t(i[l]);var f=c;r()}([])</script><script src="/static/js/2.9f67e5ae.chunk.js"></script><script src="/static/js/main.9395d624.chunk.js"></script></body></html>
<!doctype html><html lang="cn"><head><meta charset="utf-8"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Van Nav"/><link rel="manifest" href="/manifest.json"/><link href="/static/css/main.8a2a4163.chunk.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script>!function(e){function t(t){for(var n,a,i=t[0],c=t[1],l=t[2],s=0,p=[];s<i.length;s++)a=i[s],Object.prototype.hasOwnProperty.call(o,a)&&o[a]&&p.push(o[a][0]),o[a]=0;for(n in c)Object.prototype.hasOwnProperty.call(c,n)&&(e[n]=c[n]);for(f&&f(t);p.length;)p.shift()();return u.push.apply(u,l||[]),r()}function r(){for(var e,t=0;t<u.length;t++){for(var r=u[t],n=!0,i=1;i<r.length;i++){var c=r[i];0!==o[c]&&(n=!1)}n&&(u.splice(t--,1),e=a(a.s=r[0]))}return e}var n={},o={1:0},u=[];function a(t){if(n[t])return n[t].exports;var r=n[t]={i:t,l:!1,exports:{}};return e[t].call(r.exports,r,r.exports,a),r.l=!0,r.exports}a.e=function(e){var t=[],r=o[e];if(0!==r)if(r)t.push(r[2]);else{var n=new Promise((function(t,n){r=o[e]=[t,n]}));t.push(r[2]=n);var u,i=document.createElement("script");i.charset="utf-8",i.timeout=120,a.nc&&i.setAttribute("nonce",a.nc),i.src=function(e){return a.p+"static/js/"+({}[e]||e)+"."+{3:"ac9f81be"}[e]+".chunk.js"}(e);var c=new Error;u=function(t){i.onerror=i.onload=null,clearTimeout(l);var r=o[e];if(0!==r){if(r){var n=t&&("load"===t.type?"missing":t.type),u=t&&t.target&&t.target.src;c.message="Loading chunk "+e+" failed.\n("+n+": "+u+")",c.name="ChunkLoadError",c.type=n,c.request=u,r[1](c)}o[e]=void 0}};var l=setTimeout((function(){u({type:"timeout",target:i})}),12e4);i.onerror=i.onload=u,document.head.appendChild(i)}return Promise.all(t)},a.m=e,a.c=n,a.d=function(e,t,r){a.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},a.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},a.t=function(e,t){if(1&t&&(e=a(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(a.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var n in e)a.d(r,n,function(t){return e[t]}.bind(null,n));return r},a.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return a.d(t,"a",t),t},a.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},a.p="/",a.oe=function(e){throw console.error(e),e};var i=this.webpackJsonptool=this.webpackJsonptool||[],c=i.push.bind(i);i.push=t,i=i.slice();for(var l=0;l<i.length;l++)t(i[l]);var f=c;r()}([])</script><script src="/static/js/2.a6477d0c.chunk.js"></script><script src="/static/js/main.39bffd4f.chunk.js"></script></body></html>
3 changes: 0 additions & 3 deletions public/static/js/2.9f67e5ae.chunk.js

This file was deleted.

1 change: 0 additions & 1 deletion public/static/js/2.9f67e5ae.chunk.js.map

This file was deleted.

3 changes: 3 additions & 0 deletions public/static/js/2.a6477d0c.chunk.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions public/static/js/2.a6477d0c.chunk.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit 31762eb

Please sign in to comment.