sso

mobile_login_controller.go 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package controllers
  2. import (
  3. "SSO/enums"
  4. "SSO/models"
  5. "SSO/service"
  6. "SSO/utils"
  7. "time"
  8. )
  9. type MobileLoginController struct {
  10. BaseController
  11. }
  12. func (this *MobileLoginController) Prepare() {
  13. this.BaseController.Prepare()
  14. this.EnableXSRF = false
  15. }
  16. // /m/login/pwd [post]
  17. // @param mobile:string
  18. // @param password:string
  19. // @param app_type:int
  20. // @param ip:string
  21. func (this *MobileLoginController) LoginByPwd() {
  22. appType, _ := this.GetInt("app_type")
  23. if appType != 3 { // 暂时只支持血透系统
  24. appType = 3
  25. }
  26. mobile := this.GetString("mobile")
  27. password := this.GetString("password")
  28. if len(mobile) == 0 || len(password) == 0 {
  29. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeAccountOrPasswordWrong)
  30. this.ServeJSON()
  31. return
  32. }
  33. if service.IsPasswordRight(mobile, password) {
  34. // 只取最近被创建的 admin_role
  35. adminUser, getAdminErr := service.GetValidAdminUserByMobileReturnErr(mobile)
  36. if getAdminErr != nil {
  37. utils.ErrorLog("获取管理员失败:%v", getAdminErr)
  38. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  39. this.ServeJSON()
  40. return
  41. } else if adminUser == nil {
  42. utils.ErrorLog("查找不到 mobile = %v 的用户", mobile)
  43. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeAccountOrPasswordWrong)
  44. this.ServeJSON()
  45. return
  46. } else {
  47. appRole, getAppRoleErr := service.GetLastXTAdminRole(adminUser.Id, appType)
  48. if getAppRoleErr != nil {
  49. utils.ErrorLog("获取 app_role 失败:%v", getAppRoleErr)
  50. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  51. this.ServeJSON()
  52. return
  53. } else if appRole == nil {
  54. utils.ErrorLog("该管理员没有开通或被授权 app_type = %v 的应用", appType)
  55. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  56. this.ServeJSON()
  57. return
  58. }
  59. org, getOrgErr := service.GetOrgById(appRole.OrgId)
  60. if getOrgErr != nil {
  61. utils.ErrorLog("获取机构失败:%v", getOrgErr)
  62. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  63. this.ServeJSON()
  64. return
  65. } else if org == nil {
  66. utils.ErrorLog("该机构不存在或被删除")
  67. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  68. this.ServeJSON()
  69. return
  70. }
  71. app, getAppErr := service.GetAppById(appRole.AppId)
  72. if getAppErr != nil {
  73. utils.ErrorLog("获取应用失败:%v", getAppErr)
  74. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  75. this.ServeJSON()
  76. return
  77. } else if app == nil {
  78. utils.ErrorLog("该应用不存在或被删除")
  79. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  80. this.ServeJSON()
  81. return
  82. }
  83. subscibe, getSubscibeErr := service.GetOrgServeSubscibe(org.Id)
  84. if getSubscibeErr != nil {
  85. utils.ErrorLog("获取机构订阅信息失败:%v", getSubscibeErr)
  86. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  87. this.ServeJSON()
  88. return
  89. } else if subscibe == nil {
  90. now := time.Now()
  91. nextMonthDate := now.AddDate(0, 0, 30)
  92. subscibe = &models.ServeSubscibe{
  93. OrgId: int64(org.Id),
  94. PeriodStart: now.Unix(),
  95. PeriodEnd: nextMonthDate.Unix(),
  96. Status: 1,
  97. CreatedTime: now.Unix(),
  98. UpdatedTime: now.Unix(),
  99. State: 2,
  100. }
  101. createErr := service.CreateOrgServeSubscibe(subscibe)
  102. if createErr != nil {
  103. utils.ErrorLog("创建机构订阅信息失败:%v", createErr)
  104. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  105. this.ServeJSON()
  106. return
  107. }
  108. }
  109. // 插入一条登录记录
  110. ip := this.GetString("ip")
  111. loginLog := &models.AdminUserLoginLog{
  112. AdminUserId: adminUser.Id,
  113. OrgId: org.Id,
  114. AppId: app.Id,
  115. IP: ip,
  116. OperateType: 1,
  117. AppType: int8(appType),
  118. CreateTime: time.Now().Unix(),
  119. }
  120. if insertErr := service.InsertLoginLog(loginLog); insertErr != nil {
  121. utils.ErrorLog("为手机号为%v的用户插入一条登录记录失败:%v", mobile, insertErr)
  122. }
  123. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{
  124. "admin": adminUser,
  125. "org": org,
  126. "app": app,
  127. "app_role": appRole,
  128. "subscibe": subscibe,
  129. })
  130. this.ServeJSON()
  131. }
  132. } else {
  133. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeAccountOrPasswordWrong)
  134. this.ServeJSON()
  135. }
  136. }