新增权限判断

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

@ -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)
}

@ -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
}

@ -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,

@ -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

@ -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)
}

@ -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")

@ -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<redata.data.length;i++){
if (typeof (this.msgListUser[redata.data[i].uid]) == "undefined") {
@ -226,6 +226,7 @@
},
created: function () {
this.initConn();
this.getOnlineUsers();
}
})

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

@ -2,6 +2,8 @@
package tools
import (
"crypto/md5"
"encoding/hex"
"github.com/gobuffalo/packr"
"golang.org/x/net/html/charset"
"golang.org/x/text/encoding"
@ -55,3 +57,10 @@ func FileGetContent(file string) string {
}
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