浏览代码

编辑修改代码

niujiuru 2 月之前
父节点
当前提交
4bf0e2c9d2
共有 4 个文件被更改,包括 19 次插入18 次删除
  1. 3 2
      web-ui.service/web/static/js/app.js
  2. 3 3
      web-ui.service/web_handlers.go
  3. 6 6
      web-ui.service/web_session.go
  4. 7 7
      web-ui.service/web_ui.go

+ 3 - 2
web-ui.service/web/static/js/app.js

@@ -1,3 +1,4 @@
+// DOM 加载完成后执行,启动入口:初始化菜单、默认页面加载
 document.addEventListener("DOMContentLoaded", () => {
   const items = document.querySelectorAll(".menu-item");
 
@@ -55,8 +56,8 @@ async function fetchIMEI() {
   }
 }
 
-function initIMEI() {
+function loadIMEI() {
     fetchIMEI();
 }
 
-initIMEI();
+loadIMEI();

+ 3 - 3
web-ui.service/web_handlers.go

@@ -32,8 +32,8 @@ func loginHandler(w http.ResponseWriter, r *http.Request) {
 	ok := req.Username == "admin" && req.Password == "admin123456"
 
 	if ok {
-		sessionID := WebCreateSession()
-		WebAddSession(sessionID)
+		sessionID := createSession()
+		addSession(sessionID)
 
 		http.SetCookie(w, &http.Cookie{
 			Name:     "session_id",
@@ -79,7 +79,7 @@ func (ui *WebUI) rootHandler(w http.ResponseWriter, r *http.Request) {
 	w.Header().Set("Expires", "0")
 
 	// 1, 未登录时, 只能看登录页
-	if !WebCheckSession(r) {
+	if !checkSession(r) {
 		data, err := fs.ReadFile(ui.fs, "login.html")
 		if err != nil {
 			http.NotFound(w, r)

+ 6 - 6
web-ui.service/web_session.go

@@ -22,19 +22,19 @@ const (
 	gcPeriod    = 1 * time.Hour
 )
 
-func WebCreateSession() string {
+func createSession() string {
 	buf := make([]byte, 32)
 	rand.Read(buf)
 	return hex.EncodeToString(buf)
 }
 
-func WebAddSession(id string) {
+func addSession(id string) {
 	sessionMu.Lock()
 	sessions[id] = &sessionInfo{last: time.Now()}
 	sessionMu.Unlock()
 }
 
-func WebHasSession(id string) bool {
+func hasSession(id string) bool {
 	now := time.Now()
 
 	sessionMu.Lock()
@@ -54,15 +54,15 @@ func WebHasSession(id string) bool {
 	return true
 }
 
-func WebCheckSession(r *http.Request) bool {
+func checkSession(r *http.Request) bool {
 	c, err := r.Cookie("session_id")
 	if err != nil {
 		return false
 	}
-	return WebHasSession(c.Value)
+	return hasSession(c.Value)
 }
 
-func StartSessionCleaner() {
+func startSessionCleaner() {
 	go func() {
 		ticker := time.NewTicker(gcPeriod)
 		defer ticker.Stop()

+ 7 - 7
web-ui.service/web_ui.go

@@ -24,18 +24,18 @@ func NewWebUI(addr string) (*WebUI, error) {
 	mux := http.NewServeMux()
 
 	fs := http.FileServer(http.FS(sub))
-	mux.Handle("/static/css/", fs) /////////////// 页面样式
-	mux.Handle("/static/js/", fs)  /////////////// 页面脚本
-
-	mux.HandleFunc("/login", loginHandler)   // 登录接口
-	mux.HandleFunc("/logout", logoutHandler) // 注销接口
-	mux.HandleFunc("/getIMEI", getIMEIHandler)
+	mux.Handle("/static/css/", fs) ////////////// 页面样式
+	mux.Handle("/static/js/", fs)  ////////////// 页面脚本
+	/////////////////////////////////////////////////////
+	mux.HandleFunc("/login", loginHandler)     // 登录接口
+	mux.HandleFunc("/logout", logoutHandler)   // 注销接口
+	mux.HandleFunc("/getIMEI", getIMEIHandler) // 设备的ID
 
 	return &WebUI{fs: sub, addr: addr, mux: mux}, nil
 }
 
 func (ui *WebUI) Start() error {
-	StartSessionCleaner() // session过期自动回收
+	startSessionCleaner() // session过期自动回收
 	ui.mux.HandleFunc("/", ui.rootHandler)
 	return http.ListenAndServe(ui.addr, ui.mux)
 }