verify_login_controller.go 12KB

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