package api import ( "encoding/hex" "github.com/aceld/zinx/ziface" "github.com/aceld/zinx/zlog" "github.com/aceld/zinx/znet" "github.com/google/uuid" "omc/core" "omc/omc" "omc/service" "strings" ) // LoginApi 登录API type LoginApi struct { znet.BaseRouter } // Handle Login reqLoginAlarm;user=yiy;key=qw#$@;type=msg func (*LoginApi) Handle(request ziface.IRequest) { // 登录消息处理 msgBody := omc.MsgBody{ RawData: request.GetData(), Msg: make(map[string]string, 0), } if err := msgBody.Decode(); err != nil { zlog.Ins().ErrorF("inlaid message body %s", err.Error()) request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.ErrorMsg("ackLoginAlarm", "", "inlaid message body")) return } user, userOK := msgBody.Msg["user"] pw, pwOK := msgBody.Msg["key"] tp, tpOK := msgBody.Msg["type"] if !userOK || !pwOK || !tpOK { zlog.Ins().ErrorF("missing parameter of message body") request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.ErrorMsg("ackLoginAlarm", "", "missing parameter of message body")) return } m := core.GetManager(request.GetConnection().GetName()) if m == nil { zlog.Ins().ErrorF("server internal error") request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.ErrorMsg("ackLoginAlarm", "", "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 } //登录信息check if err := service.UserLogin(user, pw); err != nil { zlog.Ins().ErrorF("LoginFail %s", err) request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.ErrorMsg("ackLoginAlarm", "", "Incorrect username and password")) isClose, _ := m.LoginFail(uID.(string)) //登录错误超过3次,断开连接 if isClose { request.GetConnection().Stop() return } return } //manager 更新 if err := m.LoginSuccess(uID.(string), user, tp); err != nil { zlog.Ins().ErrorF("manager:%s", err) request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.ErrorMsg("ackLoginAlarm", "", err.Error())) return } zlog.Ins().InfoF("user login loginSuccess,username:%s, type:%s, channel:%s", user, tp, request.GetConnection().GetName()) request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.SuccessMsg("ackLoginAlarm", "", "")) } // CMCALoginSeq 登录API type CMCALoginSeq struct { znet.BaseRouter } //reqCMCALoginSeq func (*CMCALoginSeq) Handle(request ziface.IRequest) { uid := uuid.New() seqNo := hex.EncodeToString(uid[0:]) seqNo = strings.ToUpper(seqNo) //发送文件同步信息 ackBody := omc.MsgBody{ MsgName: "ackCMCALoginSeq", Msg: make(map[string]string, 0), } ackBody.Msg["seqNo"] = seqNo ackBody.Pack() m := core.GetManager(request.GetConnection().GetName()) uID, err := request.GetConnection().GetProperty("UID") if err != nil { zlog.Ins().ErrorF("GetProperty UID error %s", err) request.GetConnection().Stop() return } if m == nil { zlog.Ins().ErrorF("server internal error") request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.ErrorMsg("ackLoginAlarm", "", "server internal error")) return } m.SetSeqNo(uID.(string), seqNo) request.GetConnection().SendMsg(omc.AckCMCALoginSeq, ackBody.RawData) } // type CMCALoginAlarm struct { znet.BaseRouter } //reqCMCALoginSeq //reqCMCALoginAlarm;user=yiy;key=12313121213123;cert=AAAAAAAAAA;type=msg func (*CMCALoginAlarm) Handle(request ziface.IRequest) { // 登录消息处理 msgBody := omc.MsgBody{ RawData: request.GetData(), Msg: make(map[string]string, 0), } if err := msgBody.Decode(); err != nil { zlog.Ins().ErrorF("inlaid message body %s", err.Error()) request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.ErrorMsg("ackLoginAlarm", "", "inlaid message body")) return } user, userOK := msgBody.Msg["user"] key, keyOK := msgBody.Msg["key"] cert, certOK := msgBody.Msg["cert"] tp, tpOK := msgBody.Msg["type"] if !userOK || !keyOK || certOK || !tpOK { zlog.Ins().ErrorF("missing parameter of message body") request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.ErrorMsg("ackLoginAlarm", "", "missing parameter of message body")) return } m := core.GetManager(request.GetConnection().GetName()) if m == nil { zlog.Ins().ErrorF("server internal error") request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.ErrorMsg("ackLoginAlarm", "", "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 } //登录信息check seqNo := m.GetUserByPID(uID.(string)).SeqNo if ok, err := service.CMCALogin(seqNo, key, cert); !ok || err != nil { zlog.Ins().ErrorF("LoginFail %s", err) request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.ErrorMsg("ackLoginAlarm", "", "Incorrect username and password")) isClose, _ := m.LoginFail(uID.(string)) //登录错误超过3次,断开连接 if isClose { request.GetConnection().Stop() return } return } //manager 更新 if err := m.LoginSuccess(uID.(string), user, tp); err != nil { zlog.Ins().ErrorF("manager:%s", err) request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.ErrorMsg("ackLoginAlarm", "", err.Error())) return } zlog.Ins().InfoF("user login loginSuccess,username:%s, type:%s, channel:%s", user, tp, request.GetConnection().GetName()) request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.SuccessMsg("ackLoginAlarm", "", "")) }