login_controller.go 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package login
  2. import (
  3. base_ctl "SCRM/controllers"
  4. "SCRM/enums"
  5. "SCRM/models"
  6. "SCRM/service/admin_service"
  7. "SCRM/utils"
  8. "fmt"
  9. "net/url"
  10. "strconv"
  11. "strings"
  12. "github.com/astaxie/beego"
  13. )
  14. func LoginCtlRegistRouters() {
  15. beego.Router("/login", &LoginViewController{}, "get:Login")
  16. beego.Router("/logout", &LoginViewController{}, "get,post:Logout")
  17. beego.Router("/api/token/verify", &VerifyUserLoginAPIController{}, "post:VerifyToken")
  18. }
  19. type LoginViewController struct {
  20. base_ctl.BaseViewController
  21. }
  22. // /login [get]
  23. // @param token?:string
  24. // @param relogin?:bool
  25. func (this *LoginViewController) Login() {
  26. token := this.Ctx.Input.Query("token")
  27. if len(token) > 0 { // 带 token 参数的一般是从 SSO 回调回来的
  28. utils.TraceLog("SSO Login 回调: token=%v", token)
  29. xtFrontEndDomain := beego.AppConfig.String("front_end_domain") + "?lt=" + token
  30. this.Redirect302(xtFrontEndDomain)
  31. } else {
  32. relogin, _ := this.GetBool("relogin", false)
  33. returnURL := url.QueryEscape(fmt.Sprintf("%v%v", beego.AppConfig.String("httpdomain"), this.Ctx.Request.RequestURI))
  34. ssoDomain := beego.AppConfig.String("sso_domain")
  35. ssoLoginURL := fmt.Sprintf("%v/login?returnurl=%v&app_type=1&relogin=%v", ssoDomain, returnURL, relogin)
  36. this.Redirect302(ssoLoginURL)
  37. }
  38. }
  39. // /logout [get/post]
  40. func (this *LoginViewController) Logout() {
  41. if this.Ctx.Request.Method == "GET" {
  42. this.DelSession("admin_user_info")
  43. this.Redirect302(fmt.Sprintf("%v/logout", beego.AppConfig.String("sso_domain")))
  44. } else if this.Ctx.Request.Method == "POST" {
  45. this.DelSession("admin_user_info")
  46. }
  47. }
  48. type VerifyUserLoginAPIController struct {
  49. base_ctl.BaseAPIController
  50. }
  51. // /api/token/verify [post]
  52. // @param token:string
  53. func (this *VerifyUserLoginAPIController) VerifyToken() {
  54. if this.Ctx.Request.Method == "OPTIONS" {
  55. this.Abort("200")
  56. } else {
  57. token := this.GetString("token")
  58. utils.TraceLog("token: %v", token)
  59. if len(token) == 0 {
  60. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  61. return
  62. }
  63. ip := utils.GetIP(this.Ctx.Request)
  64. sessionID := this.Ctx.GetCookie("s")
  65. utils.TraceLog("cookie session id: %v %v", ip, sessionID)
  66. adminUserInfo, err, errCode := admin_service.VerifyToken(token, ip, sessionID)
  67. if err != nil {
  68. if errCode == 903 { // 未创建应用
  69. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNeverCreateTypeApp)
  70. } else if errCode == 904 { // 联系超管来开通
  71. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeContactSuperAdminCreateTypeApp)
  72. } else {
  73. utils.ErrorLog("令牌验证失败:%v", err)
  74. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInvalidToken)
  75. }
  76. return
  77. } else {
  78. this.SetSession("admin_user_info", adminUserInfo)
  79. adminUser := adminUserInfo.AdminUser
  80. appRole := adminUserInfo.AppRoles[adminUserInfo.CurrentAppId]
  81. userInfo := map[string]interface{}{
  82. "id": adminUser.Id,
  83. "mobile": adminUser.Mobile,
  84. "user_name": appRole.UserName,
  85. "avatar": appRole.Avatar,
  86. "intro": appRole.Intro,
  87. "user_type": appRole.UserType,
  88. "user_title": appRole.UserTitle,
  89. }
  90. curOrg := adminUserInfo.Orgs[adminUserInfo.CurrentOrgId]
  91. org := map[string]interface{}{
  92. "id": curOrg.Id,
  93. "org_name": curOrg.OrgName,
  94. "org_short_name": curOrg.OrgShortName,
  95. "org_intro": curOrg.OrgIntroduction,
  96. "org_logo": curOrg.OrgLogo,
  97. "province": curOrg.Province,
  98. "city": curOrg.City,
  99. "district": curOrg.District,
  100. "address": curOrg.Address,
  101. }
  102. var pruviews []*models.Purview
  103. var curAppUrlfors []string
  104. if len(curAppUrlfors) == 0 {
  105. if adminUser.Id == curOrg.Creator { //超级管理员
  106. urlfors, _, _ := admin_service.GetSuperAdminUsersPurviewTreeAndUrlfors(6)
  107. curAppUrlfors = urlfors
  108. }
  109. } else {
  110. appRole, _ := admin_service.FindAdminUserIDA(appRole.Id)
  111. if appRole.Id > 0 && len(appRole.RoleIds) > 0 {
  112. role_arr := strings.Split(appRole.RoleIds, ",")
  113. var ids string
  114. for _, role_id := range role_arr {
  115. id, _ := strconv.ParseInt(role_id, 10, 64)
  116. purview_ids, _ := admin_service.GetRolePurviewIds(id)
  117. if len(ids) == 0 {
  118. ids = purview_ids
  119. } else {
  120. ids = ids + "," + purview_ids
  121. }
  122. }
  123. if len(ids) != 0 {
  124. pruviews, _ = admin_service.GetPurviewById(ids)
  125. for _, item := range pruviews {
  126. if item.Module == 3 && item.Parentid > 0 {
  127. fmt.Println(item.Urlfor)
  128. curAppUrlfors = append(curAppUrlfors, item.Urlfor)
  129. }
  130. }
  131. } else {
  132. curAppUrlfors = append(curAppUrlfors, "")
  133. }
  134. } else {
  135. curAppUrlfors = append(curAppUrlfors, "")
  136. }
  137. }
  138. subscibe, _ := admin_service.GetOrgSubscibe(adminUserInfo.CurrentOrgId)
  139. this.ServeSuccessJSON(map[string]interface{}{
  140. "user": userInfo,
  141. "org": org,
  142. "urlfors": curAppUrlfors,
  143. "current_org_id": adminUserInfo.CurrentOrgId,
  144. "current_app_id": adminUserInfo.CurrentAppId,
  145. "subscibe": subscibe,
  146. })
  147. return
  148. }
  149. }
  150. }