verify_login_controller.go 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package controllers
  2. import (
  3. "XT_New/models"
  4. "fmt"
  5. "net/url"
  6. "XT_New/enums"
  7. "XT_New/service"
  8. "XT_New/utils"
  9. "github.com/astaxie/beego"
  10. )
  11. func VerifyUserLoginControllerRegistRouters() {
  12. beego.Router("/login", &VerifyUserLoginController{}, "get:Login")
  13. beego.Router("/logout", &VerifyUserLoginController{}, "get,post:Logout")
  14. beego.Router("/handle_error", &VerifyUserLoginController{}, "get:HandleError")
  15. beego.Router("/api/token/verify", &VerifyUserLoginAPIController{}, "post:VerifyToken")
  16. beego.Router("/api/admin/edit_info", &VerifyUserLoginAPIController{}, "post:EditAdminUserInfo")
  17. beego.Router("/api/password/code", &PersonAPIController{}, "post:CodeOfModifyPwd")
  18. beego.Router("/api/password/modify", &PersonAPIController{}, "post:ModifyPwd")
  19. }
  20. type VerifyUserLoginController struct {
  21. BaseViewController
  22. }
  23. // /login [get]
  24. // @param token?:string
  25. // @param relogin?:bool
  26. func (this *VerifyUserLoginController) Login() {
  27. token := this.Ctx.Input.Query("token")
  28. if len(token) > 0 { // 带 token 参数的一般是从 SSO 回调回来的
  29. utils.TraceLog("SSO Login 回调: token=%v", token)
  30. xtFrontEndDomain := beego.AppConfig.String("front_end_domain") + "?lt=" + token
  31. this.Redirect302(xtFrontEndDomain)
  32. } else {
  33. relogin, _ := this.GetBool("relogin", false)
  34. returnURL := url.QueryEscape(fmt.Sprintf("%v%v", beego.AppConfig.String("httpdomain"), this.Ctx.Request.RequestURI))
  35. ssoDomain := beego.AppConfig.String("sso_domain")
  36. ssoLoginURL := fmt.Sprintf("%v/login?returnurl=%v&app_type=3&relogin=%v", ssoDomain, returnURL, relogin)
  37. this.Redirect302(ssoLoginURL)
  38. }
  39. }
  40. // /logout [get/post]
  41. func (this *VerifyUserLoginController) Logout() {
  42. if this.Ctx.Request.Method == "GET" {
  43. this.DelSession("admin_user_info")
  44. this.Redirect302(fmt.Sprintf("%v/logout", beego.AppConfig.String("sso_domain")))
  45. } else if this.Ctx.Request.Method == "POST" {
  46. this.DelSession("admin_user_info")
  47. }
  48. }
  49. // /handle_error [get]
  50. // @param code:int
  51. func (this *VerifyUserLoginController) HandleError() {
  52. code, _ := this.GetInt("code")
  53. if code == enums.ErrorCodeNeverCreateTypeApp {
  54. ssoDomain := beego.AppConfig.String("sso_domain")
  55. createAppURL := fmt.Sprintf("%v/org/app/create", ssoDomain)
  56. this.Redirect302(createAppURL)
  57. } else if code == enums.ErrorCodeContactSuperAdminCreateTypeApp {
  58. ssoDomain := beego.AppConfig.String("sso_domain")
  59. hitURL := fmt.Sprintf("%v/create_app_hint", ssoDomain)
  60. this.Redirect302(hitURL)
  61. } else {
  62. this.Abort404()
  63. }
  64. }
  65. type VerifyUserLoginAPIController struct {
  66. BaseAPIController
  67. }
  68. // /api/token/verify [post]
  69. // @param token:string
  70. func (this *VerifyUserLoginAPIController) VerifyToken() {
  71. if this.Ctx.Request.Method == "OPTIONS" {
  72. this.Abort("200")
  73. } else {
  74. token := this.GetString("token")
  75. utils.TraceLog("token: %v", token)
  76. if len(token) == 0 {
  77. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  78. return
  79. }
  80. ip := utils.GetIP(this.Ctx.Request)
  81. fmt.Println("ip是什么", ip)
  82. sessionID := this.Ctx.GetCookie("s")
  83. fmt.Println("sessionID", sessionID)
  84. utils.TraceLog("Request: %v", this.Ctx.Request)
  85. utils.TraceLog("cookie session id: %v", sessionID)
  86. adminUserInfo, err, errCode := service.VerifyToken(token, ip, sessionID)
  87. fmt.Println("错误是什么", err)
  88. fmt.Println("errCode是什么", errCode)
  89. if err != nil {
  90. if errCode == 903 { // 未创建应用
  91. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNeverCreateTypeApp)
  92. } else if errCode == 904 { // 联系超管来开通
  93. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeContactSuperAdminCreateTypeApp)
  94. } else {
  95. utils.ErrorLog("令牌验证失败:%v", err)
  96. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInvalidToken)
  97. }
  98. return
  99. } else {
  100. adminUser := adminUserInfo.AdminUser
  101. fmt.Println("adminUser", adminUser)
  102. appRole := adminUserInfo.AppRoles[adminUserInfo.CurrentAppId]
  103. userInfo := map[string]interface{}{
  104. "id": adminUser.Id,
  105. "mobile": adminUser.Mobile,
  106. "user_name": appRole.UserName,
  107. "avatar": appRole.Avatar,
  108. "intro": appRole.Intro,
  109. "user_type": appRole.UserType,
  110. "user_title": appRole.UserTitle,
  111. }
  112. curOrg := adminUserInfo.Orgs[adminUserInfo.CurrentOrgId]
  113. org := map[string]interface{}{
  114. "id": curOrg.Id,
  115. "org_name": curOrg.OrgName,
  116. "org_short_name": curOrg.OrgShortName,
  117. "org_intro": curOrg.OrgIntroduction,
  118. "org_logo": curOrg.OrgLogo,
  119. "province": curOrg.Province,
  120. "city": curOrg.City,
  121. "district": curOrg.District,
  122. "address": curOrg.Address,
  123. }
  124. tempInfo, _ := service.GetOrgInfoTemplate(curOrg.Id)
  125. fmt.Println("teimpInfo", tempInfo)
  126. template_info := map[string]interface{}{
  127. "id": tempInfo.ID,
  128. "org_id": tempInfo.OrgId,
  129. "template_id": tempInfo.TemplateId,
  130. }
  131. var FiledList []*models.FiledConfig
  132. FiledList, _ = service.FindFiledByOrgId(curOrg.Id)
  133. if len(FiledList) == 0 {
  134. err := service.BatchInsertFiledConfig(curOrg.Id)
  135. if err == nil {
  136. FiledList, _ = service.FindFiledByOrgId(curOrg.Id)
  137. } else {
  138. utils.ErrorLog("字段批量插入失败:%v", err)
  139. }
  140. }
  141. curApp := adminUserInfo.OrgApps[adminUserInfo.CurrentOrgId][adminUserInfo.CurrentAppId]
  142. if curApp.OpenStatus != 1 {
  143. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNeverCreateTypeApp)
  144. return
  145. }
  146. curAppUrlfors := adminUserInfo.AppUrlfors[adminUserInfo.CurrentAppId]
  147. subscibe := adminUserInfo.Subscibes[adminUserInfo.CurrentOrgId]
  148. fmt.Println("subscibe", subscibe)
  149. if err := service.GetOrgSubscibeState(subscibe); err != nil {
  150. this.ErrorLog("没有机构订阅信息,数据有误")
  151. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  152. return
  153. }
  154. orgApps := adminUserInfo.OrgApps[curOrg.Id]
  155. didRegistedForSCRM := false
  156. didRegistedForMall := false
  157. didRegistedForCDM := false
  158. for _, app := range orgApps {
  159. if app.AppType == 1 && app.OpenStatus == 1 {
  160. didRegistedForSCRM = true
  161. }
  162. if app.AppType == 4 && app.OpenStatus == 1 {
  163. didRegistedForCDM = true
  164. }
  165. if app.AppType == 5 && app.OpenStatus == 1 {
  166. didRegistedForMall = true
  167. }
  168. }
  169. this.SetSession("admin_user_info", adminUserInfo)
  170. this.ServeSuccessJSON(map[string]interface{}{
  171. "user": userInfo,
  172. "org": org,
  173. "urlfors": curAppUrlfors,
  174. "current_org_id": adminUserInfo.CurrentOrgId,
  175. "current_app_id": adminUserInfo.CurrentAppId,
  176. "subscibe": subscibe,
  177. "scrm_role_exist": didRegistedForSCRM,
  178. "cdm_role_exist": didRegistedForCDM,
  179. "mall_role_exist": didRegistedForMall,
  180. "template_info": template_info,
  181. "fileds": FiledList,
  182. })
  183. return
  184. }
  185. }
  186. }
  187. // /api/admin/edit_info [post]
  188. // @param avatar:string
  189. // @param name:string
  190. // @param opwd?:string 没有原始密码的时候,认为不修改密码
  191. // @param npwd?:string
  192. func (this *VerifyUserLoginAPIController) EditAdminUserInfo() {
  193. adminUserInfo := this.GetAdminUserInfo()
  194. avatar := this.GetString("avatar")
  195. name := this.GetString("name")
  196. if len(name) == 0 {
  197. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMissingUserName)
  198. return
  199. }
  200. // oldPwd := this.GetString("opwd")
  201. // newPwd := this.GetString("npwd")
  202. // modifyPwd := len(oldPwd) != 0
  203. // if modifyPwd {
  204. // if len(newPwd) == 0 {
  205. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodePasswordEmpty)
  206. // this.ServeJSON()
  207. // return
  208. // }
  209. // pwdRight, err := service.IsPasswordRight(adminUserInfo.AdminUser.Id, oldPwd)
  210. // if err != nil {
  211. // utils.ErrorLog("判断旧密码是否错误失败:%v", err)
  212. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  213. // this.ServeJSON()
  214. // return
  215. // }
  216. // if !pwdRight {
  217. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeOldPasswordWrong)
  218. // this.ServeJSON()
  219. // return
  220. // }
  221. // } else {
  222. // newPwd = ""
  223. // }
  224. modifyErr := service.ModifyAdminUserInfo(adminUserInfo.AdminUser.Id, adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, name, avatar, "")
  225. if modifyErr != nil {
  226. this.ErrorLog("修改个人信息失败:%v", modifyErr)
  227. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  228. } else {
  229. appRole := adminUserInfo.AppRoles[adminUserInfo.CurrentAppId]
  230. appRole.UserName = name
  231. appRole.Avatar = avatar
  232. this.ServeSuccessJSON(nil)
  233. }
  234. }
  235. type PersonAPIController struct {
  236. BaseAuthAPIController
  237. }
  238. // /api/password/code [post]
  239. func (this *PersonAPIController) CodeOfModifyPwd() {
  240. adminUserInfo := this.GetAdminUserInfo()
  241. mobile := adminUserInfo.AdminUser.Mobile
  242. if err := service.SMSSendVerificationCode(mobile); err != nil {
  243. utils.ErrorLog("修改密码发送验证码失败:%v", err)
  244. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  245. return
  246. } else {
  247. this.ServeSuccessJSON(map[string]interface{}{
  248. "msg": "短信发送成功,有效期为10分钟",
  249. })
  250. }
  251. }
  252. // /api/password/modify [post]
  253. // @param password:string
  254. // @param code:string
  255. func (this *PersonAPIController) ModifyPwd() {
  256. new_pwd := this.GetString("password")
  257. code := this.GetString("code")
  258. if len(new_pwd) == 0 || len(code) == 0 {
  259. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  260. return
  261. }
  262. adminUserInfo := this.GetAdminUserInfo()
  263. mobile := adminUserInfo.AdminUser.Mobile
  264. redisClient := service.RedisClient()
  265. defer redisClient.Close()
  266. cachedCode, err := redisClient.Get("xt_modify_pwd_" + mobile).Result()
  267. if err != nil {
  268. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrVerCodeWrong)
  269. return
  270. }
  271. if code != cachedCode {
  272. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrVerCodeWrong)
  273. return
  274. }
  275. if modifyErr := service.ModifyPassword(adminUserInfo.AdminUser.Id, new_pwd); modifyErr != nil {
  276. this.ErrorLog("修改密码失败:%v", modifyErr)
  277. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  278. return
  279. }
  280. // 清除验证码
  281. redisClient.Del("xt_modify_pwd_" + mobile)
  282. this.ServeSuccessJSON(map[string]interface{}{
  283. "msg": "密码已修改",
  284. })
  285. }