verify_login_controller.go 11KB

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