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, _ := admin_service.VerifyToken(token, ip, sessionID)
  67. if err != nil {
  68. //if errCode == 903 { // 未创建应用
  69. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNeverCreateTypeApp)
  70. //
  71. //} else if errCode == 904 { // 联系超管来开通
  72. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeContactSuperAdminCreateTypeApp)
  73. //
  74. //} else {
  75. utils.ErrorLog("令牌验证失败:%v", err)
  76. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInvalidToken)
  77. //}
  78. return
  79. } else {
  80. this.SetSession("admin_user_info", adminUserInfo)
  81. adminUser := adminUserInfo.AdminUser
  82. appRole := adminUserInfo.AppRoles[adminUserInfo.CurrentAppId]
  83. userInfo := map[string]interface{}{
  84. "id": adminUser.Id,
  85. "mobile": adminUser.Mobile,
  86. "user_name": appRole.UserName,
  87. "avatar": appRole.Avatar,
  88. "intro": appRole.Intro,
  89. "user_type": appRole.UserType,
  90. "user_title": appRole.UserTitle,
  91. }
  92. curOrg := adminUserInfo.Orgs[adminUserInfo.CurrentOrgId]
  93. org := map[string]interface{}{
  94. "id": curOrg.Id,
  95. "org_name": curOrg.OrgName,
  96. "org_short_name": curOrg.OrgShortName,
  97. "org_intro": curOrg.OrgIntroduction,
  98. "org_logo": curOrg.OrgLogo,
  99. "province": curOrg.Province,
  100. "city": curOrg.City,
  101. "district": curOrg.District,
  102. "address": curOrg.Address,
  103. }
  104. var pruviews []*models.Purview
  105. var curAppUrlfors []string
  106. if len(curAppUrlfors) == 0 {
  107. if adminUser.Id == curOrg.Creator { //超级管理员
  108. urlfors, _, _ := admin_service.GetSuperAdminUsersPurviewTreeAndUrlfors(6)
  109. curAppUrlfors = urlfors
  110. }
  111. } else {
  112. appRole, _ := admin_service.FindAdminUserIDA(appRole.Id)
  113. if appRole.Id > 0 && len(appRole.RoleIds) > 0 {
  114. role_arr := strings.Split(appRole.RoleIds, ",")
  115. var ids string
  116. for _, role_id := range role_arr {
  117. id, _ := strconv.ParseInt(role_id, 10, 64)
  118. purview_ids, _ := admin_service.GetRolePurviewIds(id)
  119. if len(ids) == 0 {
  120. ids = purview_ids
  121. } else {
  122. ids = ids + "," + purview_ids
  123. }
  124. }
  125. if len(ids) != 0 {
  126. pruviews, _ = admin_service.GetPurviewById(ids)
  127. for _, item := range pruviews {
  128. if item.Module == 3 && item.Parentid > 0 {
  129. fmt.Println(item.Urlfor)
  130. curAppUrlfors = append(curAppUrlfors, item.Urlfor)
  131. }
  132. }
  133. } else {
  134. curAppUrlfors = append(curAppUrlfors, "")
  135. }
  136. } else {
  137. curAppUrlfors = append(curAppUrlfors, "")
  138. }
  139. }
  140. subscibe, _ := admin_service.GetOrgSubscibe(adminUserInfo.CurrentOrgId)
  141. this.ServeSuccessJSON(map[string]interface{}{
  142. "user": userInfo,
  143. "org": org,
  144. "urlfors": curAppUrlfors,
  145. "current_org_id": adminUserInfo.CurrentOrgId,
  146. "current_app_id": adminUserInfo.CurrentAppId,
  147. "subscibe": subscibe,
  148. })
  149. return
  150. }
  151. }
  152. }