verify_login_controller.go 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. //产寻该机构是否有收缩压和舒张压
  139. pressure, err := service.GetDefaultSystolicPressure(curOrg.Id)
  140. fmt.Println(err)
  141. if len(pressure) == 0 {
  142. err = service.BathInsertQualityControlTwo(curOrg.Id)
  143. } else {
  144. utils.ErrorLog("字段批量插入失败:%v", err)
  145. }
  146. major, err := service.GetInspectionMajor(curOrg.Id)
  147. if len(major) == 0 {
  148. QualityeList, err := service.FindQualityByOrgId(curOrg.Id)
  149. if len(QualityeList) == 0 {
  150. err = service.BatchInsertQualityControl(curOrg.Id)
  151. } else {
  152. utils.ErrorLog("字段批量插入失败:%v", err)
  153. }
  154. InspectionList, err := service.FindeInspectionByOrgId(curOrg.Id)
  155. if len(InspectionList) == 0 {
  156. err = service.BatchInspectionConfiguration(curOrg.Id)
  157. } else {
  158. utils.ErrorLog("字段批量插入失败:%v", err)
  159. }
  160. } else {
  161. utils.ErrorLog("字段批量插入失败:%v", err)
  162. }
  163. var pruviews []*models.Purview
  164. var curAppUrlfors []string
  165. if len(curAppUrlfors) == 0 {
  166. if adminUser.Id == curOrg.Creator { //超级管理员
  167. urlfors, _, _ := service.GetSuperAdminUsersPurviewTreeAndUrlfors(3)
  168. didRegistedForSCRM = true
  169. didRegistedForCDM = true
  170. didRegistedForMall = true
  171. //urlfors, _, _ := service.GetSuperAdminUsersPurviewTreeAndUrlfors(4)
  172. //urlfors, _, _ := service.GetSuperAdminUsersPurviewTreeAndUrlfors(5)
  173. //urlfors, _, _ := service.GetSuperAdminUsersPurviewTreeAndUrlfors(6)
  174. curAppUrlfors = urlfors
  175. } else {
  176. appRole, _ := service.FindAdminUserIDA(appRole.Id)
  177. if appRole.Id > 0 && len(appRole.RoleIds) > 0 {
  178. role_arr := strings.Split(appRole.RoleIds, ",")
  179. var ids string
  180. for _, role_id := range role_arr {
  181. id, _ := strconv.ParseInt(role_id, 10, 64)
  182. role, _ := service.GetRoleByRoleID(id)
  183. var system_ids = ""
  184. if role.RoleName == "子管理员" && role.IsSystem > 0 {
  185. purviews, _ := service.GetSystemPurview()
  186. for _, purview := range purviews {
  187. if len(system_ids) == 0 {
  188. system_ids = strconv.FormatInt(purview.Id, 10)
  189. } else {
  190. system_ids = system_ids + "," + strconv.FormatInt(purview.Id, 10)
  191. }
  192. }
  193. }
  194. purview_ids, _ := service.GetRolePurviewIds(id)
  195. if len(ids) == 0 {
  196. ids = purview_ids
  197. } else {
  198. ids = ids + "," + purview_ids
  199. }
  200. if len(system_ids) > 0 {
  201. ids = ids + "," + system_ids
  202. }
  203. }
  204. if len(ids) != 0 {
  205. pruviews, _ = service.GetPurviewById(CompressStr(ids))
  206. for _, item := range pruviews {
  207. if item.Module == 3 && item.Parentid > 0 {
  208. fmt.Println(item.Urlfor)
  209. curAppUrlfors = append(curAppUrlfors, item.Urlfor)
  210. }
  211. }
  212. } else {
  213. curAppUrlfors = append(curAppUrlfors, "")
  214. }
  215. } else {
  216. curAppUrlfors = append(curAppUrlfors, "")
  217. }
  218. }
  219. }
  220. for _, item := range pruviews {
  221. if item.Module == 6 {
  222. didRegistedForSCRM = true
  223. }
  224. if item.Module == 4 {
  225. didRegistedForCDM = true
  226. }
  227. if item.Module == 7 {
  228. didRegistedForMall = true
  229. }
  230. }
  231. if adminUser.Id == curOrg.Creator { //超级管理员
  232. didRegistedForSCRM = true
  233. didRegistedForCDM = true
  234. didRegistedForMall = true
  235. }
  236. subscibe, _ := service.GetOrgSubscibe(adminUserInfo.CurrentOrgId)
  237. this.SetSession("admin_user_info", adminUserInfo)
  238. this.ServeSuccessJSON(map[string]interface{}{
  239. "user": userInfo,
  240. "org": org,
  241. "urlfors": curAppUrlfors,
  242. "current_org_id": adminUserInfo.CurrentOrgId,
  243. "current_app_id": adminUserInfo.CurrentAppId,
  244. "subscibe": subscibe,
  245. "scrm_role_exist": didRegistedForSCRM,
  246. "cdm_role_exist": didRegistedForCDM,
  247. "mall_role_exist": didRegistedForMall,
  248. "template_info": template_info,
  249. "fileds": FiledList,
  250. })
  251. return
  252. }
  253. }
  254. }
  255. // /api/admin/edit_info [post]
  256. // @param avatar:string
  257. // @param name:string
  258. // @param opwd?:string 没有原始密码的时候,认为不修改密码
  259. // @param npwd?:string
  260. func (this *VerifyUserLoginAPIController) EditAdminUserInfo() {
  261. adminUserInfo := this.GetAdminUserInfo()
  262. avatar := this.GetString("avatar")
  263. name := this.GetString("name")
  264. if len(name) == 0 {
  265. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMissingUserName)
  266. return
  267. }
  268. // oldPwd := this.GetString("opwd")
  269. // newPwd := this.GetString("npwd")
  270. // modifyPwd := len(oldPwd) != 0
  271. // if modifyPwd {
  272. // if len(newPwd) == 0 {
  273. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodePasswordEmpty)
  274. // this.ServeJSON()
  275. // return
  276. // }
  277. // pwdRight, err := service.IsPasswordRight(adminUserInfo.AdminUser.Id, oldPwd)
  278. // if err != nil {
  279. // utils.ErrorLog("判断旧密码是否错误失败:%v", err)
  280. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  281. // this.ServeJSON()
  282. // return
  283. // }
  284. // if !pwdRight {
  285. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeOldPasswordWrong)
  286. // this.ServeJSON()
  287. // return
  288. // }
  289. // } else {
  290. // newPwd = ""
  291. // }
  292. modifyErr := service.ModifyAdminUserInfo(adminUserInfo.AdminUser.Id, adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, name, avatar, "")
  293. if modifyErr != nil {
  294. this.ErrorLog("修改个人信息失败:%v", modifyErr)
  295. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  296. } else {
  297. appRole := adminUserInfo.AppRoles[adminUserInfo.CurrentAppId]
  298. appRole.UserName = name
  299. appRole.Avatar = avatar
  300. this.ServeSuccessJSON(nil)
  301. }
  302. }
  303. type PersonAPIController struct {
  304. BaseAuthAPIController
  305. }
  306. // /api/password/code [post]
  307. func (this *PersonAPIController) CodeOfModifyPwd() {
  308. adminUserInfo := this.GetAdminUserInfo()
  309. mobile := adminUserInfo.AdminUser.Mobile
  310. if err := service.SMSSendVerificationCode(mobile); err != nil {
  311. utils.ErrorLog("修改密码发送验证码失败:%v", err)
  312. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  313. return
  314. } else {
  315. this.ServeSuccessJSON(map[string]interface{}{
  316. "msg": "短信发送成功,有效期为10分钟",
  317. })
  318. }
  319. }
  320. // /api/password/modify [post]
  321. // @param password:string
  322. // @param code:string
  323. func (this *PersonAPIController) ModifyPwd() {
  324. new_pwd := this.GetString("password")
  325. code := this.GetString("code")
  326. if len(new_pwd) == 0 || len(code) == 0 {
  327. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  328. return
  329. }
  330. adminUserInfo := this.GetAdminUserInfo()
  331. mobile := adminUserInfo.AdminUser.Mobile
  332. redisClient := service.RedisClient()
  333. defer redisClient.Close()
  334. cachedCode, err := redisClient.Get("xt_modify_pwd_" + mobile).Result()
  335. if err != nil {
  336. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrVerCodeWrong)
  337. return
  338. }
  339. if code != cachedCode {
  340. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrVerCodeWrong)
  341. return
  342. }
  343. if modifyErr := service.ModifyPassword(adminUserInfo.AdminUser.Id, new_pwd); modifyErr != nil {
  344. this.ErrorLog("修改密码失败:%v", modifyErr)
  345. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  346. return
  347. }
  348. // 清除验证码
  349. redisClient.Del("xt_modify_pwd_" + mobile)
  350. this.ServeSuccessJSON(map[string]interface{}{
  351. "msg": "密码已修改",
  352. })
  353. }
  354. func CompressStr(str string) string {
  355. if str == "" {
  356. return ""
  357. }
  358. //匹配一个或多个空白符的正则表达式
  359. reg := regexp.MustCompile("\\s+")
  360. return reg.ReplaceAllString(str, "")
  361. }