Files
nbi_alarm/handle/api/req_login_alarm.go
2023-08-23 17:22:22 +08:00

83 lines
2.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package api
import (
"omc/core"
"omc/core/manage"
"omc/core/parse"
"omc/handle/service"
"github.com/aceld/zinx/ziface"
"github.com/aceld/zinx/zlog"
"github.com/aceld/zinx/znet"
)
var ReqLoginAlarmMsgID uint32 = 1
var ReqLoginAlarmMsgType uint32 = 2
var ReqLoginAlarmMsgName string = "ackLoginAlarm"
// reqLoginAlarm 登录
type ReqLoginAlarm struct {
znet.BaseRouter
}
// reqLoginAlarm;user=omc;key=omc;type=msg
func (s *ReqLoginAlarm) Handle(request ziface.IRequest) {
// 登录消息处理
body, err := parse.RequestBodyDecode(request, []string{"user", "key", "type"})
// 账户密码
username := body.Data["user"]
key := body.Data["key"]
tp := body.Data["type"]
if err != nil || username == "" || key == "" {
zlog.Ins().ErrorF("inlaid message body %s", err.Error())
request.GetConnection().SendMsg(ReqLoginAlarmMsgType, core.ResultError(ReqLoginAlarmMsgName, "inlaid message body", ""))
return
}
// 获取当前请求的通道
m := manage.GetManager(request.GetConnection().GetName())
if m == nil {
zlog.Ins().ErrorF("server internal error")
request.GetConnection().SendMsg(ReqLoginAlarmMsgType, core.ResultError(ReqLoginAlarmMsgName, "server internal error", ""))
return
}
uid, err := request.GetConnection().GetProperty("UID")
if err != nil {
zlog.Ins().ErrorF("GetProperty UID error %s", err)
request.GetConnection().Stop()
return
}
// 登录信息
err = service.UserLogin(username, key)
if err != nil {
zlog.Ins().ErrorF("LoginFail %s", err)
request.GetConnection().SendMsg(ReqLoginAlarmMsgType, core.Result(ReqLoginAlarmMsgName, map[string]string{
"result": "autherror",
"resDesc": err.Error(),
}))
// 已登录的登录错误超过3次,断开连接
if uid != nil || uid != "" {
isClose, _ := m.LoginFail(uid.(string))
if isClose {
request.GetConnection().Stop()
return
}
}
return
}
// manager 用户登录更新
err = m.LoginSuccess(uid.(string), username, tp)
if err != nil {
zlog.Ins().ErrorF("manager:%s", err)
request.GetConnection().SendMsg(ReqLoginAlarmMsgType, core.Result(ReqLoginAlarmMsgName, map[string]string{
"result": "autherror",
"resDesc": err.Error(),
}))
return
}
zlog.Ins().InfoF("user login loginSuccess,username:%s, type:%s, channel:%s", username, tp, request.GetConnection().GetName())
request.GetConnection().SendMsg(ReqLoginAlarmMsgType, core.ResultSuccess(ReqLoginAlarmMsgName, "ok", ""))
}