package login import ( base_ctl "SCRM/controllers" "SCRM/enums" "SCRM/service/admin_service" "SCRM/service/org_service" "SCRM/utils" "fmt" "net/url" "github.com/astaxie/beego" ) func LoginCtlRegistRouters() { beego.Router("/login", &LoginViewController{}, "get:Login") beego.Router("/logout", &LoginViewController{}, "get,post:Logout") beego.Router("/api/token/verify", &VerifyUserLoginAPIController{}, "post:VerifyToken") } type LoginViewController struct { base_ctl.BaseViewController } // /login [get] // @param token?:string // @param relogin?:bool func (this *LoginViewController) Login() { token := this.Ctx.Input.Query("token") if len(token) > 0 { // 带 token 参数的一般是从 SSO 回调回来的 utils.TraceLog("SSO Login 回调: token=%v", token) xtFrontEndDomain := beego.AppConfig.String("front_end_domain") + "?lt=" + token this.Redirect302(xtFrontEndDomain) } else { relogin, _ := this.GetBool("relogin", false) returnURL := url.QueryEscape(fmt.Sprintf("%v%v", beego.AppConfig.String("httpdomain"), this.Ctx.Request.RequestURI)) ssoDomain := beego.AppConfig.String("sso_domain") ssoLoginURL := fmt.Sprintf("%v/login?returnurl=%v&app_type=1&relogin=%v", ssoDomain, returnURL, relogin) this.Redirect302(ssoLoginURL) } } // /logout [get/post] func (this *LoginViewController) Logout() { if this.Ctx.Request.Method == "GET" { this.DelSession("admin_user_info") this.Redirect302(fmt.Sprintf("%v/logout", beego.AppConfig.String("sso_domain"))) } else if this.Ctx.Request.Method == "POST" { this.DelSession("admin_user_info") } } type VerifyUserLoginAPIController struct { base_ctl.BaseAPIController } // /api/token/verify [post] // @param token:string func (this *VerifyUserLoginAPIController) VerifyToken() { if this.Ctx.Request.Method == "OPTIONS" { this.Abort("200") } else { token := this.GetString("token") utils.TraceLog("token: %v", token) if len(token) == 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } ip := utils.GetIP(this.Ctx.Request) sessionID := this.Ctx.GetCookie("s") utils.TraceLog("cookie session id: %v %v", ip, sessionID) adminUserInfo, err, errCode := admin_service.VerifyToken(token, ip, sessionID) if err != nil { if errCode == 903 { // 未创建应用 this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNeverCreateTypeApp) } else if errCode == 904 { // 联系超管来开通 this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeContactSuperAdminCreateTypeApp) } else { utils.ErrorLog("令牌验证失败:%v", err) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInvalidToken) } return } else { this.SetSession("admin_user_info", adminUserInfo) adminUser := adminUserInfo.AdminUser appRole := adminUserInfo.AppRoles[adminUserInfo.CurrentAppId] userInfo := map[string]interface{}{ "id": adminUser.Id, "mobile": adminUser.Mobile, "user_name": appRole.UserName, "avatar": appRole.Avatar, "intro": appRole.Intro, "user_type": appRole.UserType, "user_title": appRole.UserTitle, } curOrg := adminUserInfo.Orgs[adminUserInfo.CurrentOrgId] org := map[string]interface{}{ "id": curOrg.Id, "org_name": curOrg.OrgName, "org_short_name": curOrg.OrgShortName, "org_intro": curOrg.OrgIntroduction, "org_logo": curOrg.OrgLogo, "province": curOrg.Province, "city": curOrg.City, "district": curOrg.District, "address": curOrg.Address, } curAppUrlfors := adminUserInfo.AppUrlfors[adminUserInfo.CurrentAppId] subscibe := adminUserInfo.Subscibes[adminUserInfo.CurrentOrgId] if err := org_service.GetOrgSubscibeState(subscibe); err != nil { this.ErrorLog("没有机构订阅信息,数据有误") this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } orgApps := adminUserInfo.OrgApps[curOrg.Id] didRegistedForXT := false didRegistedForMall := false didRegistedForCDM := false for _, app := range orgApps { if app.AppType == 3 && app.OpenStatus == 1 { didRegistedForXT = true } if app.AppType == 4 && app.OpenStatus == 1 { didRegistedForCDM = true } if app.AppType == 5 && app.OpenStatus == 1 { didRegistedForMall = true } } this.ServeSuccessJSON(map[string]interface{}{ "user": userInfo, "org": org, "urlfors": curAppUrlfors, "current_org_id": adminUserInfo.CurrentOrgId, "current_app_id": adminUserInfo.CurrentAppId, "subscibe": subscibe, "xt_role_exist": didRegistedForXT, "cdm_role_exist": didRegistedForCDM, "mall_role_exist": didRegistedForMall, }) return } } }