From c54d517f6e7ecb3bc00d7db2adcbba780e3ada30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E7=94=B0=E5=BC=98?= <1240092443@qq.com> Date: Fri, 17 Jan 2025 16:18:00 +0800 Subject: [PATCH] =?UTF-8?q?add=EF=BC=9A=E9=94=81=E5=B1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/desktop/LockDesktop.vue | 66 ++- .../src/components/setting/SetAccount.vue | 500 +++++++++++------- frontend/src/system/index.ts | 29 + godo/cmd/main.go | 8 +- godo/cmd/serve.go | 2 +- godo/user/user.go | 27 +- 6 files changed, 443 insertions(+), 189 deletions(-) diff --git a/frontend/src/components/desktop/LockDesktop.vue b/frontend/src/components/desktop/LockDesktop.vue index a1c1182..d22c48d 100644 --- a/frontend/src/components/desktop/LockDesktop.vue +++ b/frontend/src/components/desktop/LockDesktop.vue @@ -1,9 +1,11 @@ @@ -330,8 +360,9 @@ import { useSystem } from "@/system"; import { getSystemConfig, setSystemConfig } from "@/system/config"; import router from "@/system/router"; + import { SystemStateEnum } from "@/system/type/enum"; import { RestartApp } from "@/util/goutil"; - import { notifyError } from "@/util/msg"; + import { notifyError, notifySuccess } from "@/util/msg"; import { computed, onMounted, ref, watchEffect } from "vue"; import { useRoute } from "vue-router"; const route = useRoute(); @@ -346,6 +377,12 @@ localStorage.removeItem("ThirdPartyPlatform"); }; + // /screen/unlock + const unlockForm = ref({ + username: "123", + password: "123", + }); + const phoneForm = ref({ phone: "", code: "", @@ -356,6 +393,20 @@ code: "", }); + const lockScreen = async () => { + const res = await fetch(config.apiUrl + "/user/screen/unlock", { + method: "POST", + body: JSON.stringify(unlockForm.value), + }); + const data = await res.json(); + if (data.code == 0) { + sys._rootState.state = SystemStateEnum.open; + notifySuccess("解锁成功"); + } else { + notifyError("解锁失败"); + } + }; + const thirdpartyCode = ref(""); const validateAndLoginByThirdparty = () => { @@ -1210,4 +1261,17 @@ } } } + + .lock-card { + border-radius: 10px; + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + width: 300px; + height: 300px; + background-color: #ffffff; + box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); + padding: 20px; + } diff --git a/frontend/src/components/setting/SetAccount.vue b/frontend/src/components/setting/SetAccount.vue index 39b6838..5395993 100644 --- a/frontend/src/components/setting/SetAccount.vue +++ b/frontend/src/components/setting/SetAccount.vue @@ -1,199 +1,341 @@ diff --git a/frontend/src/system/index.ts b/frontend/src/system/index.ts index 55b225e..713c6dc 100644 --- a/frontend/src/system/index.ts +++ b/frontend/src/system/index.ts @@ -103,6 +103,28 @@ export class System { initBuiltinApp(this); initBuiltinFileOpener(this); // 注册内建文件打开器 await this.initSavedConfig(); // 初始化保存的配置 + const config = getSystemConfig(); + const syslock = localStorage.getItem("syslock"); + const lockData = JSON.parse(syslock || "{}"); + + // 判断是解锁 + if (lockData.lock) { + const sysuser = lockData.sysuser; + const res = await fetch(config.apiUrl + "/user/screen/status", { + headers: { + "sysuser": sysuser, + }, + }); + const data = await res.json(); + // data.data 为 true 表示锁屏 + // 如果没锁屏,则删除 sysuser + if (!data.data) { + lockData.lock = false; + lockData.sysuser = ""; + localStorage.setItem("syslock", JSON.stringify(lockData)); + } + } + // 判断是否为钉钉工作台登录 const login_type = new URLSearchParams(window.location.search).get("login_type"); @@ -177,6 +199,13 @@ export class System { } this._rootState.state = SystemStateEnum.lock; + + const syslock = localStorage.getItem("syslock"); + const lockData = JSON.parse(syslock || "{}"); + if (!lockData.lock) { + this._rootState.state = SystemStateEnum.open; + } + const tempCallBack = this._options.loginCallback; if (!tempCallBack) { throw new Error('没有设置登录回调函数'); diff --git a/godo/cmd/main.go b/godo/cmd/main.go index b727854..daa4d4a 100644 --- a/godo/cmd/main.go +++ b/godo/cmd/main.go @@ -186,10 +186,10 @@ func OsStart() { proxyRouter.HandleFunc("/frpc/status", proxy.StatusFrpcHandler).Methods(http.MethodGet) userRouter := router.PathPrefix("/user").Subrouter() - userRouter.HandleFunc("/register", user.RegisterSysUserHandler).Methods(http.MethodPost) - userRouter.HandleFunc("/screen/lock", user.LockedScreenHandler).Methods(http.MethodPost) - userRouter.HandleFunc("/screen/unlock", user.UnLockScreenHandler).Methods(http.MethodPost) - userRouter.HandleFunc("/screen/status", user.CheckLockedScreenHandler).Methods(http.MethodGet) + userRouter.HandleFunc("/register", user.RegisterSysUserHandler).Methods(http.MethodPost) // 注册系统用户 + userRouter.HandleFunc("/screen/lock", user.LockedScreenHandler).Methods(http.MethodPost) // 锁屏 + userRouter.HandleFunc("/screen/unlock", user.UnLockScreenHandler).Methods(http.MethodPost) // 解锁 + userRouter.HandleFunc("/screen/status", user.CheckLockedScreenHandler).Methods(http.MethodGet) // 检查锁屏状态 // 注册根路径的处理函数 distFS, _ := fs.Sub(deps.Frontendassets, "dist") diff --git a/godo/cmd/serve.go b/godo/cmd/serve.go index c07a650..113c56c 100644 --- a/godo/cmd/serve.go +++ b/godo/cmd/serve.go @@ -115,7 +115,7 @@ func recoverMiddleware(next http.Handler) http.Handler { // CORS 中间件 func corsMiddleware() mux.MiddlewareFunc { - allowHeaders := "Content-Type, Accept, Authorization, Origin, Pwd" + allowHeaders := "Content-Type, Accept, Authorization, Origin, Pwd, sysuser" allowMethods := "GET, POST, PUT, DELETE, OPTIONS" return func(next http.Handler) http.Handler { diff --git a/godo/user/user.go b/godo/user/user.go index 4d421c0..debe8a6 100644 --- a/godo/user/user.go +++ b/godo/user/user.go @@ -4,6 +4,8 @@ import ( "encoding/json" "godo/libs" "godo/model" + "io" + "log" "net/http" "strconv" "sync" @@ -21,20 +23,35 @@ func RegisterSysUserHandler(w http.ResponseWriter, r *http.Request) { } var user model.SysUser - if err := json.NewDecoder(r.Body).Decode(&user); err != nil { + + // 获取请求体 + body, err := io.ReadAll(r.Body) + if err != nil { + log.Println("读取请求体错误:", err) libs.ErrorMsg(w, "invalid input") return } + + // 解析请求体 + if err := json.Unmarshal(body, &user); err != nil { + log.Println("解析请求体错误:", err) + libs.ErrorMsg(w, "invalid input") + return + } + + // 检查用户名和密码是否为空 if user.Username == "" || user.Password == "" { libs.ErrorMsg(w, "username or password is empty") return } + // 创建用户 if err := model.Db.Create(&user).Error; err != nil { libs.ErrorMsg(w, "failed to create user") return } + // 返回成功消息 libs.SuccessMsg(w, user.ID, "") } @@ -63,7 +80,7 @@ func LockedScreenHandler(w http.ResponseWriter, r *http.Request) { return } - w.Header().Set("sysuser", userEncodeStr) + // w.Header().Set("sysuser", userEncodeStr) // 登录成功,锁屏状态设置为 true mu.Lock() @@ -73,7 +90,7 @@ func LockedScreenHandler(w http.ResponseWriter, r *http.Request) { userLockedScreen[user.ID] = true mu.Unlock() - libs.SuccessMsg(w, nil, "system locked") + libs.SuccessMsg(w, userEncodeStr, "system locked") } // 系统用户登录(解锁) @@ -115,9 +132,11 @@ func CheckLockedScreenHandler(w http.ResponseWriter, r *http.Request) { decoderUid := r.Header.Get("sysuser") if decoderUid == "" { - libs.ErrorMsg(w, "invalid uid") + //获取锁屏状态 + libs.SuccessMsg(w, false, "") return } + uidStr, err := libs.DecodeFile("godoos", decoderUid) if err != nil { libs.ErrorMsg(w, "invalid uid")