package controllers import ( "encoding/json" "hongze/hongze_mobile_admin/models/request/admin" admin3 "hongze/hongze_mobile_admin/models/response/admin" "hongze/hongze_mobile_admin/models/tables/h5_admin_session" "hongze/hongze_mobile_admin/services" "hongze/hongze_mobile_admin/utils" "time" ) type AdminCommon struct { BaseAuth } // Login // @Title 用户账号、密码登录接口 // @Description 用户账号、密码登录接口 // @Param request body admin.LoginReq true "type json string" // @Success 200 {object} admin.LoginResp // @router /login [post] func (c *AdminCommon) Login() { var req admin.LoginReq err := json.Unmarshal(c.Ctx.Input.RequestBody, &req) if err != nil { c.FailWithMessage("参数解析异常!", "参数解析失败,Err:"+err.Error()) return } if req.Username == "" { c.FailWithMessage("请输入账号", "请输入账号") return } if req.Password == "" { c.FailWithMessage("请输入密码", "请输入密码") return } adminWx, err, errMsg := services.BindWxUser(c.Session.OpenId, req.Username, req.Password, utils.WxPlatform) if err != nil { if err.Error() == utils.ErrNoRow() { c.FailWithMessage("登录失败,账号或密码错误", "登录失败,账号或密码错误") return } msg := `` if errMsg != `` { msg = errMsg } c.FailWithMessage(msg, "登录失败,Err:"+err.Error()) return } if adminWx.Enabled != 1 { c.FailWithMessage("账户信息异常!", "账户状态异常,不允许登录") return } err = h5_admin_session.UpdateSession(c.Session.SessionId, adminWx.AdminId, time.Now().AddDate(0, 0, 90)) if err != nil { c.FailWithMessage("登录失败", "变更session信息失败,Err:"+err.Error()) return } resp := admin3.LoginResp{ RealName: adminWx.RealName, AdminName: adminWx.AdminName, RoleName: adminWx.RoleName, RoleTypeCode: adminWx.RoleTypeCode, AdminId: adminWx.AdminId, DepartmentName: adminWx.DepartmentName, GroupName: adminWx.GroupName, } resp.AdminId = adminWx.AdminId var productName string productId := services.GetProductId(adminWx.RoleTypeCode) if productId == 1 { productName = utils.COMPANY_PRODUCT_FICC_NAME } else if productId == 2 { productName = utils.COMPANY_PRODUCT_RAI_NAME } else { productName = "admin" } resp.ProductName = productName resp.Authority = adminWx.Authority resp.Headimgurl = adminWx.Headimgurl if adminWx.AdminId == utils.ApproveUserId { resp.IsBusinessTrip = true } if adminWx.RoleTypeCode == "rai_seller" || adminWx.RoleTypeCode == "rai_group" || adminWx.RoleTypeCode == "rai_admin" { resp.IsRai = true } c.OkDetailed(resp, "登录成功") } // Logout // @Title 用户退出登录接口 // @Description 用户退出登录接口 // @Success Ret=200 退出成功 // @router /logout [post] func (c *AdminCommon) Logout() { adminWx := c.AdminWx err := services.UnbindWxUser(adminWx.OpenId) if err != nil { c.FailWithMessage("退出失败", "退出失败,Err:"+err.Error()) return } err = h5_admin_session.UpdateSession(c.Session.SessionId, 0, time.Now().AddDate(0, 0, 10)) if err != nil { c.FailWithMessage("退出失败", "变更session信息失败,Err:"+err.Error()) return } c.OkWithMessage("退出成功") }