diff --git a/cmd/dashboard/controller/member_api.go b/cmd/dashboard/controller/member_api.go index 9e73755..03af657 100644 --- a/cmd/dashboard/controller/member_api.go +++ b/cmd/dashboard/controller/member_api.go @@ -57,18 +57,21 @@ func (ma *memberAPI) serve() { type apiResult struct { Token string `json:"token"` + Note string `json:"note"` } // getToken 获取 Token func (ma *memberAPI) getToken(c *gin.Context) { u := c.MustGet(model.CtxKeyAuthorizedUser).(*model.User) singleton.ApiLock.RLock() + defer singleton.ApiLock.RUnlock() + tokenList := singleton.UserIDToApiTokenList[u.ID] - singleton.ApiLock.RUnlock() res := make([]*apiResult, len(tokenList)) for i, token := range tokenList { res[i] = &apiResult{ Token: token, + Note: singleton.ApiTokenList[token].Note, } } c.JSON(http.StatusOK, gin.H{ @@ -78,12 +81,26 @@ func (ma *memberAPI) getToken(c *gin.Context) { }) } +type TokenForm struct { + Note string +} + // issueNewToken 生成新的 token func (ma *memberAPI) issueNewToken(c *gin.Context) { u := c.MustGet(model.CtxKeyAuthorizedUser).(*model.User) + tf := &TokenForm{} + err := c.ShouldBindJSON(tf) + if err != nil { + c.JSON(http.StatusOK, model.Response{ + Code: http.StatusBadRequest, + Message: fmt.Sprintf("请求错误:%s", err), + }) + return + } token := &model.ApiToken{ UserID: u.ID, Token: utils.MD5(fmt.Sprintf("%d%d%s", time.Now().UnixNano(), u.ID, u.Login)), + Note: tf.Note, } singleton.DB.Create(token) @@ -97,6 +114,7 @@ func (ma *memberAPI) issueNewToken(c *gin.Context) { Message: "success", Result: map[string]string{ "token": token.Token, + "note": token.Note, }, }) } diff --git a/cmd/dashboard/controller/member_page.go b/cmd/dashboard/controller/member_page.go index df0cf7d..7f18823 100644 --- a/cmd/dashboard/controller/member_page.go +++ b/cmd/dashboard/controller/member_page.go @@ -28,6 +28,16 @@ func (mp *memberPage) serve() { mr.GET("/cron", mp.cron) mr.GET("/notification", mp.notification) mr.GET("/setting", mp.setting) + mr.GET("/api", mp.api) +} + +func (mp *memberPage) api(c *gin.Context) { + singleton.ApiLock.RLock() + defer singleton.ApiLock.RUnlock() + c.HTML(http.StatusOK, "dashboard/api", mygin.CommonEnvironment(c, gin.H{ + "title": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "ApiManagement"}), + "Tokens": singleton.ApiTokenList, + })) } func (mp *memberPage) server(c *gin.Context) { diff --git a/model/api_token.go b/model/api_token.go index db10e26..1a777c9 100644 --- a/model/api_token.go +++ b/model/api_token.go @@ -4,4 +4,5 @@ type ApiToken struct { Common UserID uint64 `json:"user_id"` Token string `json:"token"` + Note string `json:"note"` } diff --git a/pkg/mygin/mygin.go b/pkg/mygin/mygin.go index d2afde0..47593cb 100644 --- a/pkg/mygin/mygin.go +++ b/pkg/mygin/mygin.go @@ -17,6 +17,7 @@ var adminPage = map[string]bool{ "/setting": true, "/notification": true, "/cron": true, + "/api": true, } func CommonEnvironment(c *gin.Context, data map[string]interface{}) gin.H { diff --git a/resource/l10n/en-US.toml b/resource/l10n/en-US.toml index 0a13711..f03dd87 100644 --- a/resource/l10n/en-US.toml +++ b/resource/l10n/en-US.toml @@ -469,6 +469,21 @@ other = "Services" [ScheduledTasks] other = "Scheduled Tasks" +[ApiManagement] +other="API" + +[IssueNewApiToken] +other="Create Token" + +[Token] +other="Token" + +[DeleteToken] +other="Delete Token" + +[ConfirmToDeleteThisToken] +other="Confirm Delete?" + [YouAreNotAuthorized] other = "You are not authorized" diff --git a/resource/l10n/es-ES.toml b/resource/l10n/es-ES.toml index 6966218..1744b3e 100644 --- a/resource/l10n/es-ES.toml +++ b/resource/l10n/es-ES.toml @@ -469,6 +469,21 @@ other = "Monitorización del servicio" [ScheduledTasks] other = "Tareas programadas" +[ApiManagement] +other="API" + +[IssueNewApiToken] +other="Create Token" + +[Token] +other="Token" + +[DeleteToken] +other="Delete Token" + +[ConfirmToDeleteThisToken] +other="Confirm Delete?" + [YouAreNotAuthorized] other = "Esta página requiere un acceso" diff --git a/resource/l10n/zh-CN.toml b/resource/l10n/zh-CN.toml index fb8fd3f..c135bd2 100644 --- a/resource/l10n/zh-CN.toml +++ b/resource/l10n/zh-CN.toml @@ -469,6 +469,21 @@ other = "服务监控" [ScheduledTasks] other = "计划任务" +[ApiManagement] +other="API" + +[IssueNewApiToken] +other="添加Token" + +[Token] +other="Token" + +[DeleteToken] +other="删除Token" + +[ConfirmToDeleteThisToken] +other="确认删除Token" + [YouAreNotAuthorized] other = "此页面需要登录" diff --git a/resource/static/main.js b/resource/static/main.js index 6b15125..d7da66e 100644 --- a/resource/static/main.js +++ b/resource/static/main.js @@ -202,6 +202,18 @@ function post(path, params, method = 'post') { document.body.removeChild(form); } +function issueNewApiToken(apiToken) { + const modal = $(".api.modal"); + modal.children(".header").text((apiToken ? LANG.Edit : LANG.Add) + ' ' + "API Token"); + modal + .find(".nezha-primary-btn.button") + .html( + apiToken ? LANG.Edit + '' : LANG.Add + '' + ); + modal.find("textarea[name=Note]").val(apiToken ? apiToken.Note : null); + showFormModal(".api.modal", "#apiForm", "/api/token"); +} + function addOrEditServer(server, conf) { const modal = $(".server.modal"); modal.children(".header").text((server ? LANG.Edit : LANG.Add) + ' ' + LANG.Server); diff --git a/resource/template/common/menu.html b/resource/template/common/menu.html index f909103..a48c7cc 100644 --- a/resource/template/common/menu.html +++ b/resource/template/common/menu.html @@ -9,6 +9,7 @@ {{tr "Services"}} {{tr "Task"}} {{tr "Notification"}} + API {{tr "Settings"}} diff --git a/resource/template/component/api.html b/resource/template/component/api.html new file mode 100644 index 0000000..02b6c6c --- /dev/null +++ b/resource/template/component/api.html @@ -0,0 +1,19 @@ +{{define "component/api"}} + +{{end}} \ No newline at end of file diff --git a/resource/template/dashboard/api.html b/resource/template/dashboard/api.html new file mode 100644 index 0000000..b9b7795 --- /dev/null +++ b/resource/template/dashboard/api.html @@ -0,0 +1,41 @@ +{{define "dashboard/api"}} +{{template "common/header" .}} +{{template "common/menu" .}} +
+
+
+
+ +
+
+ + + + + + + + + {{range $token := .Tokens}} + + + + + + {{end}} + +
{{tr "Token"}}{{tr "Note"}}
{{$token.Token}}{{$token.Note}} +
+ +
+
+
+
+{{template "component/api"}} +{{template "common/footer" .}} +{{end}} \ No newline at end of file