verify_login_controller.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. var didRegistedForSCRM bool = false
  126. var didRegistedForCDM bool = false
  127. var didRegistedForMall bool = false
  128. tempInfo, _ := service.GetOrgInfoTemplate(curOrg.Id)
  129. fmt.Println("teimpInfo", tempInfo)
  130. template_info := map[string]interface{}{
  131. "id": tempInfo.ID,
  132. "org_id": tempInfo.OrgId,
  133. "template_id": tempInfo.TemplateId,
  134. }
  135. var FiledList []*models.FiledConfig
  136. FiledList, _ = service.FindFiledByOrgId(curOrg.Id)
  137. if len(FiledList) == 0 {
  138. err := service.BatchInsertFiledConfig(curOrg.Id)
  139. if err == nil {
  140. FiledList, _ = service.FindFiledByOrgId(curOrg.Id)
  141. } else {
  142. utils.ErrorLog("字段批量插入失败:%v", err)
  143. }
  144. }
  145. var pruviews []*models.Purview
  146. var curAppUrlfors []string
  147. if len(curAppUrlfors) == 0 {
  148. if adminUser.Id == curOrg.Creator { //超级管理员
  149. urlfors, _, _ := service.GetSuperAdminUsersPurviewTreeAndUrlfors(3)
  150. didRegistedForSCRM = true
  151. didRegistedForCDM = true
  152. didRegistedForMall = true
  153. //urlfors, _, _ := service.GetSuperAdminUsersPurviewTreeAndUrlfors(4)
  154. //urlfors, _, _ := service.GetSuperAdminUsersPurviewTreeAndUrlfors(5)
  155. //urlfors, _, _ := service.GetSuperAdminUsersPurviewTreeAndUrlfors(6)
  156. curAppUrlfors = urlfors
  157. } else {
  158. appRole, _ := service.FindAdminUserIDA(appRole.Id)
  159. if appRole.Id > 0 && len(appRole.RoleIds) > 0 {
  160. role_arr := strings.Split(appRole.RoleIds, ",")
  161. var ids string
  162. for _, role_id := range role_arr {
  163. id, _ := strconv.ParseInt(role_id, 10, 64)
  164. purview_ids, _ := service.GetRolePurviewIds(id)
  165. if len(ids) == 0 {
  166. ids = purview_ids
  167. } else {
  168. ids = ids + "," + purview_ids
  169. }
  170. }
  171. if len(ids) != 0 {
  172. pruviews, _ = service.GetPurviewById(ids)
  173. for _, item := range pruviews {
  174. if item.Module == 3 && item.Parentid > 0 {
  175. fmt.Println(item.Urlfor)
  176. curAppUrlfors = append(curAppUrlfors, item.Urlfor)
  177. }
  178. }
  179. } else {
  180. curAppUrlfors = append(curAppUrlfors, "")
  181. }
  182. } else {
  183. curAppUrlfors = append(curAppUrlfors, "")
  184. }
  185. }
  186. }
  187. for _, item := range pruviews {
  188. if item.Module == 6 {
  189. didRegistedForSCRM = true
  190. }
  191. if item.Module == 4 {
  192. didRegistedForCDM = true
  193. }
  194. if item.Module == 7 {
  195. didRegistedForMall = true
  196. }
  197. }
  198. if adminUser.Id == curOrg.Creator { //超级管理员
  199. didRegistedForSCRM = true
  200. didRegistedForCDM = true
  201. didRegistedForMall = true
  202. }
  203. subscibe, _ := service.GetOrgSubscibe(adminUserInfo.CurrentOrgId)
  204. this.SetSession("admin_user_info", adminUserInfo)
  205. this.ServeSuccessJSON(map[string]interface{}{
  206. "user": userInfo,
  207. "org": org,
  208. "urlfors": curAppUrlfors,
  209. "current_org_id": adminUserInfo.CurrentOrgId,
  210. "current_app_id": adminUserInfo.CurrentAppId,
  211. "subscibe": subscibe,
  212. "scrm_role_exist": didRegistedForSCRM,
  213. "cdm_role_exist": didRegistedForCDM,
  214. "mall_role_exist": didRegistedForMall,
  215. "template_info": template_info,
  216. "fileds": FiledList,
  217. })
  218. return
  219. }
  220. }
  221. }
  222. // /api/admin/edit_info [post]
  223. // @param avatar:string
  224. // @param name:string
  225. // @param opwd?:string 没有原始密码的时候,认为不修改密码
  226. // @param npwd?:string
  227. func (this *VerifyUserLoginAPIController) EditAdminUserInfo() {
  228. adminUserInfo := this.GetAdminUserInfo()
  229. avatar := this.GetString("avatar")
  230. name := this.GetString("name")
  231. if len(name) == 0 {
  232. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMissingUserName)
  233. return
  234. }
  235. // oldPwd := this.GetString("opwd")
  236. // newPwd := this.GetString("npwd")
  237. // modifyPwd := len(oldPwd) != 0
  238. // if modifyPwd {
  239. // if len(newPwd) == 0 {
  240. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodePasswordEmpty)
  241. // this.ServeJSON()
  242. // return
  243. // }
  244. // pwdRight, err := service.IsPasswordRight(adminUserInfo.AdminUser.Id, oldPwd)
  245. // if err != nil {
  246. // utils.ErrorLog("判断旧密码是否错误失败:%v", err)
  247. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  248. // this.ServeJSON()
  249. // return
  250. // }
  251. // if !pwdRight {
  252. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeOldPasswordWrong)
  253. // this.ServeJSON()
  254. // return
  255. // }
  256. // } else {
  257. // newPwd = ""
  258. // }
  259. modifyErr := service.ModifyAdminUserInfo(adminUserInfo.AdminUser.Id, adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, name, avatar, "")
  260. if modifyErr != nil {
  261. this.ErrorLog("修改个人信息失败:%v", modifyErr)
  262. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  263. } else {
  264. appRole := adminUserInfo.AppRoles[adminUserInfo.CurrentAppId]
  265. appRole.UserName = name
  266. appRole.Avatar = avatar
  267. this.ServeSuccessJSON(nil)
  268. }
  269. }
  270. type PersonAPIController struct {
  271. BaseAuthAPIController
  272. }
  273. // /api/password/code [post]
  274. func (this *PersonAPIController) CodeOfModifyPwd() {
  275. adminUserInfo := this.GetAdminUserInfo()
  276. mobile := adminUserInfo.AdminUser.Mobile
  277. if err := service.SMSSendVerificationCode(mobile); err != nil {
  278. utils.ErrorLog("修改密码发送验证码失败:%v", err)
  279. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  280. return
  281. } else {
  282. this.ServeSuccessJSON(map[string]interface{}{
  283. "msg": "短信发送成功,有效期为10分钟",
  284. })
  285. }
  286. }
  287. // /api/password/modify [post]
  288. // @param password:string
  289. // @param code:string
  290. func (this *PersonAPIController) ModifyPwd() {
  291. new_pwd := this.GetString("password")
  292. code := this.GetString("code")
  293. if len(new_pwd) == 0 || len(code) == 0 {
  294. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  295. return
  296. }
  297. adminUserInfo := this.GetAdminUserInfo()
  298. mobile := adminUserInfo.AdminUser.Mobile
  299. redisClient := service.RedisClient()
  300. defer redisClient.Close()
  301. cachedCode, err := redisClient.Get("xt_modify_pwd_" + mobile).Result()
  302. if err != nil {
  303. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrVerCodeWrong)
  304. return
  305. }
  306. if code != cachedCode {
  307. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrVerCodeWrong)
  308. return
  309. }
  310. if modifyErr := service.ModifyPassword(adminUserInfo.AdminUser.Id, new_pwd); modifyErr != nil {
  311. this.ErrorLog("修改密码失败:%v", modifyErr)
  312. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  313. return
  314. }
  315. // 清除验证码
  316. redisClient.Del("xt_modify_pwd_" + mobile)
  317. this.ServeSuccessJSON(map[string]interface{}{
  318. "msg": "密码已修改",
  319. })
  320. }