新增权限判断

pull/30/head
taoshihan1991 4 years ago
parent 59c75cb77d
commit 4395defbaa

@ -2,8 +2,10 @@ package config
import ( import (
"encoding/json" "encoding/json"
"fmt"
"github.com/taoshihan1991/imaptool/tools" "github.com/taoshihan1991/imaptool/tools"
"io/ioutil" "io/ioutil"
"os"
) )
const Dir = "config/" const Dir = "config/"
@ -23,3 +25,32 @@ func GetAccount()map[string]string{
err=json.Unmarshal(info,&account) err=json.Unmarshal(info,&account)
return 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)
}

@ -1,12 +1,24 @@
package controller package controller
import "github.com/taoshihan1991/imaptool/config" import (
"github.com/taoshihan1991/imaptool/config"
func AuthLocal(username string,password string)bool{ "github.com/taoshihan1991/imaptool/tools"
)
func AuthLocal(username string,password string)string{
account:=config.GetAccount() account:=config.GetAccount()
if username==account["Username"] && password==account["Password"]{ 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
} }

@ -23,6 +23,7 @@ func ChatUsers(w http.ResponseWriter, r *http.Request) {
for uid, _ := range clientList { for uid, _ := range clientList {
userInfo := make(map[string]string) userInfo := make(map[string]string)
userInfo["uid"] = uid userInfo["uid"] = uid
userInfo["username"]=clientNameList[uid]
result = append(result, userInfo) result = append(result, userInfo)
} }
msg, _ := json.Marshal(tools.JsonListResult{ msg, _ := json.Marshal(tools.JsonListResult{
@ -86,6 +87,7 @@ func ChatServer(w *websocket.Conn) {
//用户id对应的连接 //用户id对应的连接
clientList[userMsg.From_id] = w clientList[userMsg.From_id] = w
clientNameList[userMsg.From_id]=userMsg.From_name clientNameList[userMsg.From_id]=userMsg.From_name
SendUserAllNotice(userMsg.From_id)
//客服上线 //客服上线
case "kfOnline": case "kfOnline":
json.Unmarshal(msgData,&kfMsg) json.Unmarshal(msgData,&kfMsg)
@ -99,7 +101,7 @@ func ChatServer(w *websocket.Conn) {
SendKefuOnline(kfMsg,conn) SendKefuOnline(kfMsg,conn)
} }
//发送给客服通知 //发送给客服通知
SendOnekfuAllNotice(w) //SendOnekfuAllNotice(w)
//客服接手 //客服接手
case "kfConnect": case "kfConnect":
json.Unmarshal(msgData,&kfMsg) json.Unmarshal(msgData,&kfMsg)
@ -141,17 +143,15 @@ func ChatServer(w *websocket.Conn) {
} }
} }
//发送给所有客服客户上线 //发送给所有客服客户上线
func SendUserAllNotice(){ func SendUserAllNotice(uid string){
if len(kefuList)!=0{ if len(kefuList)!=0{
//发送给客服通知 //发送给客服通知
for _, conn := range kefuList { for _, conn := range kefuList {
result := make([]map[string]string, 0) result := make([]map[string]string, 0)
for uid, _ := range clientList { userInfo := make(map[string]string)
userInfo := make(map[string]string) userInfo["uid"] = uid
userInfo["uid"] = uid userInfo["username"]=clientNameList[uid]
userInfo["username"]=clientNameList[uid] result = append(result, userInfo)
result = append(result, userInfo)
}
msg:=NoticeMessage{ msg:=NoticeMessage{
Type: "notice", Type: "notice",
Data:result, Data:result,

@ -22,7 +22,9 @@ func LoginCheck(w http.ResponseWriter, r *http.Request) {
switch authType { switch authType {
case "local": case "local":
username := r.PostFormValue("username") 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: "验证成功,正在跳转..."}) msg, _ = json.Marshal(tools.JsonResult{Code: 200, Msg: "验证成功,正在跳转..."})
w.Write(msg) w.Write(msg)
return return

@ -2,9 +2,16 @@ package controller
import ( import (
"github.com/taoshihan1991/imaptool/tmpl" "github.com/taoshihan1991/imaptool/tmpl"
"github.com/taoshihan1991/imaptool/tools"
"net/http" "net/http"
) )
func ActionMain(w http.ResponseWriter, r *http.Request){ 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:=tmpl.NewRender(w)
render.Display("main",render) render.Display("main",render)
} }

@ -22,13 +22,6 @@ func ActionSetting(w http.ResponseWriter, r *http.Request){
} }
func SettingAccount(w http.ResponseWriter, r *http.Request){ func SettingAccount(w http.ResponseWriter, r *http.Request){
w.Header().Set("content-type", "text/json;charset=utf-8;") 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") username:=r.PostFormValue("username")
password:=r.PostFormValue("password") password:=r.PostFormValue("password")

@ -116,12 +116,12 @@
}, },
getOnlineUsers() { getOnlineUsers() {
let _this = this; let _this = this;
// $.get('/chat_users',function (rs) { $.get('/chat_users',function (rs) {
// _this.users=rs.result _this.users=rs.result
// _this.fullscreenLoading=false; _this.fullscreenLoading=false;
// }).then(()=>{ }).then(()=>{
// _this.fullscreenLoading=false; _this.fullscreenLoading=false;
// }); });
}, },
//初始化websocket //初始化websocket
initConn() { initConn() {
@ -145,7 +145,7 @@
type: 'success', type: 'success',
duration: 0, duration: 0,
}); });
this.users = redata.data; //this.users = redata.data;
this.currentGuest = redata.data[0].uid; this.currentGuest = redata.data[0].uid;
for(i=0;i<redata.data.length;i++){ for(i=0;i<redata.data.length;i++){
if (typeof (this.msgListUser[redata.data[i].uid]) == "undefined") { if (typeof (this.msgListUser[redata.data[i].uid]) == "undefined") {
@ -226,6 +226,7 @@
}, },
created: function () { created: function () {
this.initConn(); this.initConn();
this.getOnlineUsers();
} }
}) })

@ -264,7 +264,7 @@
message: data.msg, message: data.msg,
type: 'success' type: 'success'
}); });
window.location.href="/"; window.location.href="/main";
}else{ }else{
_this.$message({ _this.$message({
message: data.msg, message: data.msg,

@ -2,6 +2,8 @@
package tools package tools
import ( import (
"crypto/md5"
"encoding/hex"
"github.com/gobuffalo/packr" "github.com/gobuffalo/packr"
"golang.org/x/net/html/charset" "golang.org/x/net/html/charset"
"golang.org/x/text/encoding" "golang.org/x/text/encoding"
@ -55,3 +57,10 @@ func FileGetContent(file string) string {
} }
return content return content
} }
//md5加密
func Md5(src string)string{
m:=md5.New()
m.Write([]byte(src))
res:=hex.EncodeToString(m.Sum(nil))
return res
}

Loading…
Cancel
Save