diff --git a/config/config.go b/config/config.go index a168fac..6fb7a9d 100644 --- a/config/config.go +++ b/config/config.go @@ -2,8 +2,10 @@ package config import ( "encoding/json" + "fmt" "github.com/taoshihan1991/imaptool/tools" "io/ioutil" + "os" ) const Dir = "config/" @@ -23,3 +25,32 @@ func GetAccount()map[string]string{ err=json.Unmarshal(info,&account) return account } +func GetUserInfo(uid string)map[string]string{ + var userInfo map[string]string + userFile:=Dir+"sess_"+uid+".json" + isExist,_:=tools.IsFileExist(userFile) + if !isExist{ + return userInfo + } + info,err:=ioutil.ReadFile(userFile) + if err!=nil{ + return userInfo + } + + err=json.Unmarshal(info,&userInfo) + return userInfo +} +func SetUserInfo(uid string,info map[string]string){ + userFile:=Dir+"sess_"+uid+".json" + isExist,_:=tools.IsFileExist(Dir) + if !isExist{ + os.Mkdir(Dir,os.ModePerm) + } + file, _ := os.OpenFile(userFile, os.O_RDWR|os.O_CREATE, os.ModePerm) + str:="{\r\n" + for k,v:=range info{ + str+=fmt.Sprintf(`"%s":"%s",`,k,v) + } + str+=fmt.Sprintf(`"session_id":"%s"%s}`,uid,"\r\n") + file.WriteString(str) +} \ No newline at end of file diff --git a/controller/auth.go b/controller/auth.go index afc0d01..09d4595 100644 --- a/controller/auth.go +++ b/controller/auth.go @@ -1,12 +1,24 @@ package controller -import "github.com/taoshihan1991/imaptool/config" - -func AuthLocal(username string,password string)bool{ +import ( + "github.com/taoshihan1991/imaptool/config" + "github.com/taoshihan1991/imaptool/tools" +) +func AuthLocal(username string,password string)string{ account:=config.GetAccount() if username==account["Username"] && password==account["Password"]{ - return true + + sessionId:=tools.Md5(username) + info:=make(map[string]string) + info["username"]=username + config.SetUserInfo(sessionId,info) + return sessionId } - return false + return "" +} +//验证是否已经登录 +func AuthCheck(uid string)map[string]string{ + info:=config.GetUserInfo(uid) + return info } diff --git a/controller/chat.go b/controller/chat.go index 7c6f4d7..7c2f2f8 100644 --- a/controller/chat.go +++ b/controller/chat.go @@ -23,6 +23,7 @@ func ChatUsers(w http.ResponseWriter, r *http.Request) { for uid, _ := range clientList { userInfo := make(map[string]string) userInfo["uid"] = uid + userInfo["username"]=clientNameList[uid] result = append(result, userInfo) } msg, _ := json.Marshal(tools.JsonListResult{ @@ -86,6 +87,7 @@ func ChatServer(w *websocket.Conn) { //用户id对应的连接 clientList[userMsg.From_id] = w clientNameList[userMsg.From_id]=userMsg.From_name + SendUserAllNotice(userMsg.From_id) //客服上线 case "kfOnline": json.Unmarshal(msgData,&kfMsg) @@ -99,7 +101,7 @@ func ChatServer(w *websocket.Conn) { SendKefuOnline(kfMsg,conn) } //发送给客服通知 - SendOnekfuAllNotice(w) + //SendOnekfuAllNotice(w) //客服接手 case "kfConnect": json.Unmarshal(msgData,&kfMsg) @@ -141,17 +143,15 @@ func ChatServer(w *websocket.Conn) { } } //发送给所有客服客户上线 -func SendUserAllNotice(){ +func SendUserAllNotice(uid string){ if len(kefuList)!=0{ //发送给客服通知 for _, conn := range kefuList { result := make([]map[string]string, 0) - for uid, _ := range clientList { - userInfo := make(map[string]string) - userInfo["uid"] = uid - userInfo["username"]=clientNameList[uid] - result = append(result, userInfo) - } + userInfo := make(map[string]string) + userInfo["uid"] = uid + userInfo["username"]=clientNameList[uid] + result = append(result, userInfo) msg:=NoticeMessage{ Type: "notice", Data:result, diff --git a/controller/login.go b/controller/login.go index dce9f95..30d784c 100644 --- a/controller/login.go +++ b/controller/login.go @@ -22,7 +22,9 @@ func LoginCheck(w http.ResponseWriter, r *http.Request) { switch authType { case "local": username := r.PostFormValue("username") - if AuthLocal(username,password){ + sessionId:=AuthLocal(username,password) + if sessionId!=""{ + tools.SetCookie("session_id", sessionId, &w) msg, _ = json.Marshal(tools.JsonResult{Code: 200, Msg: "验证成功,正在跳转..."}) w.Write(msg) return diff --git a/controller/main.go b/controller/main.go index 1ed26ce..e6203df 100644 --- a/controller/main.go +++ b/controller/main.go @@ -2,9 +2,16 @@ package controller import ( "github.com/taoshihan1991/imaptool/tmpl" + "github.com/taoshihan1991/imaptool/tools" "net/http" ) func ActionMain(w http.ResponseWriter, r *http.Request){ + sessionId:=tools.GetCookie(r,"session_id") + info:=AuthCheck(sessionId) + if len(info)==0{ + http.Redirect(w, r, "/login", 302) + return + } render:=tmpl.NewRender(w) render.Display("main",render) } diff --git a/controller/setting.go b/controller/setting.go index 0f061a8..8c25dc2 100644 --- a/controller/setting.go +++ b/controller/setting.go @@ -22,13 +22,6 @@ func ActionSetting(w http.ResponseWriter, r *http.Request){ } func SettingAccount(w http.ResponseWriter, r *http.Request){ w.Header().Set("content-type", "text/json;charset=utf-8;") - mailServer := tools.GetMailServerFromCookie(r) - - if mailServer == nil { - msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: "验证失败"}) - w.Write(msg) - return - } username:=r.PostFormValue("username") password:=r.PostFormValue("password") diff --git a/static/html/chat_main.html b/static/html/chat_main.html index 8eb859d..d322313 100644 --- a/static/html/chat_main.html +++ b/static/html/chat_main.html @@ -116,12 +116,12 @@ }, getOnlineUsers() { let _this = this; - // $.get('/chat_users',function (rs) { - // _this.users=rs.result - // _this.fullscreenLoading=false; - // }).then(()=>{ - // _this.fullscreenLoading=false; - // }); + $.get('/chat_users',function (rs) { + _this.users=rs.result + _this.fullscreenLoading=false; + }).then(()=>{ + _this.fullscreenLoading=false; + }); }, //初始化websocket initConn() { @@ -145,7 +145,7 @@ type: 'success', duration: 0, }); - this.users = redata.data; + //this.users = redata.data; this.currentGuest = redata.data[0].uid; for(i=0;i