package ws2 import ( "encoding/json" "github.com/gogf/gf/v2/frame/g" "github.com/gorilla/websocket" "github.com/tiger1103/gfast/v3/api/constant" "golang.org/x/net/context" "log" "time" ) // 根据 Token 去登录 WS 请求 func Login(conn *websocket.Conn) { loginMessage := map[string]string{ "act": "ma_login", "user_name": constant.Username, "access_token": constant.Token, } conn.WriteJSON(loginMessage) // 休眠两秒钟,以便第三方服务端记录连接的信息 time.Sleep(time.Second * 3) } // 定义管理员登录响应结构体 type AdminLoginResponse struct { Cmd string `json:"cmd"` Status bool `json:"status"` Msg string `json:"msg"` AdminInfo AdminInfo `json:"admin_info"` } // 定义管理员信息结构体 type AdminInfo struct { AdminID string `json:"admin_id"` UserName string `json:"user_name"` Mobile string `json:"mobile"` CTime string `json:"c_time"` Pwd string `json:"pwd"` PID string `json:"p_id"` Department string `json:"department"` Role string `json:"role"` SIPInfo SIPInfo `json:"sip_info"` } // 定义 SIP 信息结构体 type SIPInfo struct { SIPID string `json:"sip_id" dc:"SIPID"` SIPPwd string `json:"sip_pwd" dc:"SIP密码"` SIPHost string `json:"sip_host" dc:"SIP服务器URL"` WSSURL string `json:"wss_url" dc:"WSS地址"` STUNHost string `json:"stun_host" dc:"stun服务器"` TurnHost string `json:"turn_host" dc:"turnserver地址"` TurnUser string `json:"turn_user" dc:"turnserver账号"` TurnPwd string `json:"turn_pwd" dc:"turnserver密码"` } // 处理登录 func HandleLogin(jsonString string) { var response AdminLoginResponse err := json.Unmarshal([]byte(jsonString), &response) if err != nil { log.Fatal("Error decoding JSON to struct:", err) } sipInfo := response.AdminInfo.SIPInfo // 查询是否存在该数据 IfExists, _ := g.Model("sip_info").Where("id = ?", 1).Count() // 如果不存在,则插入数据 if IfExists == 0 { sipinfo := g.Map{ "sip_id": sipInfo.SIPID, "sip_pwd": sipInfo.SIPPwd, "sip_host": sipInfo.SIPHost, "wss_url": sipInfo.WSSURL, "stun_host": sipInfo.STUNHost, "turn_host": sipInfo.TurnHost, "turn_user": sipInfo.TurnUser, "turn_pwd": sipInfo.TurnPwd, } g.Model("sip_info").Data(sipinfo).Insert() } else { // 存在则更新数据 sipinfo := g.Map{ "sip_id": sipInfo.SIPID, "sip_pwd": sipInfo.SIPPwd, "sip_host": sipInfo.SIPHost, "wss_url": sipInfo.WSSURL, "stun_host": sipInfo.STUNHost, "turn_host": sipInfo.TurnHost, "turn_user": sipInfo.TurnUser, "turn_pwd": sipInfo.TurnPwd, } g.Model("sip_info").Data(sipinfo).Where("id =?", 1).Update() } } // 管理员详情sip type GetAdminLoginInfoReq struct { g.Meta `path:"/device/admininfo" method:"get" tags:"视频安全帽相关" summary:"管理员详情sip"` } type GetAdminLoginInfoRes struct { List []SIPInfo `json:"list"` } func (w WsRouter) GetAdminLoginInfo(ctx context.Context, req *GetAdminLoginInfoReq) (res *GetAdminLoginInfoRes, err error) { res = new(GetAdminLoginInfoRes) var list []SIPInfo g.Model("sip_info").WithAll().Scan(&list) res.List = list return res, nil }