123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- package login
-
- import (
- base_ctl "SCRM/controllers"
- "SCRM/enums"
- "SCRM/models"
- "SCRM/service/admin_service"
- "SCRM/utils"
- "fmt"
- "net/url"
- "strconv"
- "strings"
-
- "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, _ := 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,
- }
-
- var pruviews []*models.Purview
- var curAppUrlfors []string
-
- if len(curAppUrlfors) == 0 {
- if adminUser.Id == curOrg.Creator { //超级管理员
- urlfors, _, _ := admin_service.GetSuperAdminUsersPurviewTreeAndUrlfors(6)
- curAppUrlfors = urlfors
- }
-
- } else {
- appRole, _ := admin_service.FindAdminUserIDA(appRole.Id)
- if appRole.Id > 0 && len(appRole.RoleIds) > 0 {
- role_arr := strings.Split(appRole.RoleIds, ",")
- var ids string
- for _, role_id := range role_arr {
- id, _ := strconv.ParseInt(role_id, 10, 64)
- purview_ids, _ := admin_service.GetRolePurviewIds(id)
- if len(ids) == 0 {
- ids = purview_ids
- } else {
- ids = ids + "," + purview_ids
- }
- }
- if len(ids) != 0 {
- pruviews, _ = admin_service.GetPurviewById(ids)
- for _, item := range pruviews {
- if item.Module == 3 && item.Parentid > 0 {
- fmt.Println(item.Urlfor)
- curAppUrlfors = append(curAppUrlfors, item.Urlfor)
- }
- }
- } else {
- curAppUrlfors = append(curAppUrlfors, "")
- }
- } else {
- curAppUrlfors = append(curAppUrlfors, "")
- }
- }
-
- subscibe, _ := admin_service.GetOrgSubscibe(adminUserInfo.CurrentOrgId)
- this.ServeSuccessJSON(map[string]interface{}{
- "user": userInfo,
- "org": org,
- "urlfors": curAppUrlfors,
- "current_org_id": adminUserInfo.CurrentOrgId,
- "current_app_id": adminUserInfo.CurrentAppId,
- "subscibe": subscibe,
- })
- return
- }
- }
- }
|