scrm-go

login_controller.go 4.7KB

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