sys_user.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hz_crm_api/models"
  6. "hongze/hz_crm_api/models/system"
  7. "hongze/hz_crm_api/services"
  8. "hongze/hz_crm_api/services/eta_forum"
  9. "hongze/hz_crm_api/utils"
  10. "time"
  11. )
  12. // 系统相关
  13. type SysUserController struct {
  14. BaseCommonController
  15. }
  16. // @Title 系统用户登录接口
  17. // @Description 系统用户登录
  18. // @Param request body models.LoginReq true "type json string"
  19. // @Success 200 {object} models.LoginResp
  20. // @router /login [post]
  21. func (this *SysUserController) Login() {
  22. br := new(models.BaseResponse).Init()
  23. defer func() {
  24. this.Data["json"] = br
  25. this.ServeJSON()
  26. }()
  27. var req system.LoginReq
  28. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  29. if err != nil {
  30. br.Msg = "参数解析异常!"
  31. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  32. return
  33. }
  34. if req.Username == "" {
  35. br.Msg = "请输入账号"
  36. return
  37. }
  38. if req.Password == "" {
  39. br.Msg = "请输入密码"
  40. return
  41. }
  42. sysUser, err := system.CheckSysUser(req.Username, req.Password)
  43. if err != nil {
  44. if err.Error() == utils.ErrNoRow() {
  45. br.Msg = "登录失败,账号或密码错误"
  46. br.ErrMsg = "登录失败,账号或密码错误,Err:" + err.Error()
  47. br.IsSendEmail = false
  48. return
  49. }
  50. br.IsSendEmail = false
  51. br.Msg = "登录失败,账号或密码错误"
  52. br.ErrMsg = "登录失败,Err:" + err.Error()
  53. return
  54. }
  55. if sysUser == nil {
  56. br.IsSendEmail = false
  57. br.Msg = "登录失败,账号或密码错误"
  58. br.ErrMsg = "登录失败,sysUser is nil "
  59. return
  60. }
  61. if sysUser.Enabled == 0 {
  62. br.Msg = "您的账号已被禁用,如需登录,请联系管理员"
  63. br.ErrMsg = fmt.Sprintf("您的账号已被禁用,登录账号:%s,账户名称:%s,如需登录,请联系管理员", sysUser.AdminName, sysUser.RealName)
  64. return
  65. }
  66. // 是否记住密码
  67. var isRemember int
  68. if req.IsRemember {
  69. isRemember = 1
  70. }
  71. account := utils.MD5(req.Username)
  72. token := utils.GenToken(account)
  73. sysSession := new(system.SysSession)
  74. sysSession.UserName = req.Username
  75. sysSession.SysUserId = sysUser.AdminId
  76. sysSession.ExpiredTime = time.Now().AddDate(0, 0, 90)
  77. sysSession.IsRemember = isRemember
  78. sysSession.CreatedTime = time.Now()
  79. sysSession.LastUpdatedTime = time.Now()
  80. sysSession.AccessToken = token
  81. err = system.AddSysSession(sysSession)
  82. fmt.Println("id:", sysSession.Id)
  83. if err != nil {
  84. br.ErrMsg = "新增session信息失败,Err:" + err.Error()
  85. br.Msg = "登录失败"
  86. return
  87. }
  88. // 修改最后登录时间
  89. {
  90. sysUser.LastLoginTime = time.Now().Format(utils.FormatDateTime)
  91. sysUser.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
  92. sysUser.Update([]string{"LastLoginTime", "LastUpdatedTime"})
  93. }
  94. resp := new(system.LoginResp)
  95. resp.Authorization = token
  96. resp.Authorization = "authorization=" + token + "$account=" + account
  97. resp.RealName = sysUser.RealName
  98. resp.AdminName = sysUser.AdminName
  99. resp.RoleName = sysUser.RoleName
  100. resp.SysRoleTypeCode = sysUser.RoleTypeCode //系统角色编码
  101. resp.RoleTypeCode = sysUser.RoleTypeCode
  102. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP {
  103. resp.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  104. }
  105. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_TEAM {
  106. resp.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  107. }
  108. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_DEPARTMENT {
  109. resp.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  110. }
  111. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  112. resp.RoleTypeCode = utils.ROLE_TYPE_CODE_RAI_SELLER
  113. }
  114. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_DEPARTMENT {
  115. resp.RoleTypeCode = utils.ROLE_TYPE_CODE_RAI_SELLER
  116. }
  117. if sysUser.RoleName == utils.ROLE_NAME_FICC_DIRECTOR {
  118. resp.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  119. }
  120. resp.AdminId = sysUser.AdminId
  121. var productName string
  122. productId := services.GetProductId(sysUser.RoleTypeCode)
  123. //if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_SELLER || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  124. // productName = utils.COMPANY_PRODUCT_FICC_NAME
  125. //} else if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  126. // productName = utils.COMPANY_PRODUCT_RAI_NAME
  127. //} else {
  128. // productName = "admin"
  129. //}
  130. if productId == 1 {
  131. productName = utils.COMPANY_PRODUCT_FICC_NAME
  132. } else if productId == 2 {
  133. productName = utils.COMPANY_PRODUCT_RAI_NAME
  134. } else {
  135. productName = "admin"
  136. }
  137. resp.ProductName = productName
  138. resp.Authority = sysUser.Authority
  139. br.Ret = 200
  140. br.Success = true
  141. br.Msg = "登录成功"
  142. br.Data = resp
  143. //新增登录记录
  144. {
  145. record := new(system.SysUserLoginRecord)
  146. record.Uid = sysUser.AdminId
  147. record.UserName = req.Username
  148. record.Ip = this.Ctx.Input.IP()
  149. record.Stage = "login"
  150. record.CreateTime = time.Now()
  151. go system.AddSysUserLoginRecord(record)
  152. }
  153. // 设置redis缓存
  154. {
  155. // 获取不可信的登录态,并将该登录态重置掉,不允许多次登录
  156. noTrustLoginKey := fmt.Sprint(utils.CACHE_ACCESS_TOKEN_LOGIN_NO_TRUST, sysUser.AdminId)
  157. noTrustLoginId, _ := utils.Rc.RedisString(noTrustLoginKey)
  158. fmt.Println("noTrustLoginId:", noTrustLoginId)
  159. if noTrustLoginId != `` { // 如果存在不可信设备,那么将其下架
  160. oldNoTrustLoginKey := fmt.Sprint(utils.CACHE_ACCESS_TOKEN_LOGIN, noTrustLoginId)
  161. utils.Rc.Put(oldNoTrustLoginKey, "0", 30*time.Minute)
  162. }
  163. if !req.IsRemember { // 如果当前是不可信设备,那么将其加入到不可信名单
  164. loginKey := fmt.Sprint(utils.CACHE_ACCESS_TOKEN_LOGIN, sysSession.Id)
  165. utils.Rc.Put(loginKey, "1", 30*time.Minute)
  166. noTrustLoginKey := fmt.Sprint(utils.CACHE_ACCESS_TOKEN_LOGIN_NO_TRUST, sysUser.AdminId)
  167. utils.Rc.Put(noTrustLoginKey, sysSession.Id, 30*time.Minute)
  168. }
  169. }
  170. }
  171. type SysUserAuthController struct {
  172. BaseAuthController
  173. }
  174. // @Title 修改密码
  175. // @Description 修改密码
  176. // @Param request body models.ModifyPwdReq true "type json string"
  177. // @Success 200 {object} models.LoginResp
  178. // @router /modify/pwd [post]
  179. func (this *SysUserAuthController) ModifyPwd() {
  180. br := new(models.BaseResponse).Init()
  181. defer func() {
  182. this.Data["json"] = br
  183. this.ServeJSON()
  184. }()
  185. var req system.ModifyPwdReq
  186. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  187. if err != nil {
  188. br.Msg = "参数解析异常!"
  189. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  190. return
  191. }
  192. sysUser := this.SysUser
  193. if sysUser == nil {
  194. br.Msg = "登录失败,账号或密码错误"
  195. br.ErrMsg = "登录失败,sysUser is nil "
  196. return
  197. }
  198. if req.OldPwd == "" {
  199. br.Msg = "原始密码不能为空"
  200. return
  201. }
  202. if req.NewPwd == "" {
  203. br.Msg = "新密码不能为空"
  204. return
  205. }
  206. if sysUser.Password != req.OldPwd {
  207. br.Msg = "旧密码错误,请重新输入"
  208. return
  209. }
  210. err = system.ModifyPwd(sysUser.AdminId, req.NewPwd)
  211. if err != nil {
  212. br.Msg = "修改失败"
  213. br.ErrMsg = "修改失败,Err:" + err.Error()
  214. return
  215. }
  216. br.Ret = 200
  217. br.Success = true
  218. br.Msg = "修改成功"
  219. }
  220. // CheckPwd
  221. // @Title 校验是否初始密码
  222. // @Description 校验是否初始密码
  223. // @Success 200 {bool} true "是否初始密码;false代表不是初始密码,直接进去;true代表是初始密码,需要强制修改密码"
  224. // @router /check_pwd [get]
  225. func (this *SysUserAuthController) CheckPwd() {
  226. br := new(models.BaseResponse).Init()
  227. defer func() {
  228. this.Data["json"] = br
  229. this.ServeJSON()
  230. }()
  231. sysUser := this.SysUser
  232. if sysUser == nil {
  233. br.Msg = "登录失败,账号或密码错误"
  234. br.ErrMsg = "登录失败,sysUser is nil "
  235. return
  236. }
  237. br.Data = false
  238. if sysUser.Password == utils.MD5("123456a") {
  239. br.Data = true
  240. }
  241. br.Ret = 200
  242. br.Success = true
  243. br.Msg = "校验成功"
  244. }
  245. // @Title 获取系统用户UUID
  246. // @Description 获取系统用户UUID
  247. // @Success 200 {object} system.SysuserListResp
  248. // @router /uuid [get]
  249. func (this *SysUserController) Uuid() {
  250. br := new(models.BaseResponse).Init()
  251. defer func() {
  252. this.Data["json"] = br
  253. this.ServeJSON()
  254. }()
  255. nowTime := time.Now().Format(utils.FormatDateTimeUnSpace)
  256. randBigint := utils.GetRandDigit(4)
  257. randStr := utils.GetRandStringNoSpecialChar(4)
  258. uuid := nowTime + randBigint + randStr
  259. br.Ret = 200
  260. br.Success = true
  261. br.Msg = "获取成功"
  262. br.Data = uuid
  263. }
  264. // GetEtaAuthCode
  265. // @Title 免密登录-获取登录ETA系统的编码
  266. // @Description 免密登录-获取登录ETA系统的编码
  267. // @Success 200 Ret=200 获取成功
  268. // @router /get_eta_auth_code [get]
  269. func (this *SysUserAuthController) GetEtaAuthCode() {
  270. br := new(models.BaseResponse).Init()
  271. defer func() {
  272. if br.ErrMsg == "" {
  273. br.IsSendEmail = false
  274. }
  275. this.Data["json"] = br
  276. this.ServeJSON()
  277. }()
  278. sysUser := this.SysUser
  279. if sysUser == nil {
  280. br.Msg = "请登录"
  281. br.ErrMsg = "请登录,SysUser Is Empty"
  282. br.Ret = 408
  283. return
  284. }
  285. code, e := services.GetAuthCodeFromMiddleServer(sysUser.AdminName)
  286. if e != nil {
  287. br.Msg = "获取失败"
  288. br.ErrMsg = "获取登录编码失败, Err: " + e.Error()
  289. return
  290. }
  291. br.Data = code
  292. br.Ret = 200
  293. br.Success = true
  294. br.Msg = "获取成功"
  295. }
  296. // AuthCodeLogin
  297. // @Title 编码登录
  298. // @Description 编码登录
  299. // @Success 200 Ret=200 操作成功
  300. // @router /auth_code_login [get]
  301. func (this *SysUserController) AuthCodeLogin() {
  302. br := new(models.BaseResponse).Init()
  303. defer func() {
  304. if br.ErrMsg == "" {
  305. br.IsSendEmail = false
  306. }
  307. this.Data["json"] = br
  308. this.ServeJSON()
  309. }()
  310. authCode := this.GetString("AuthCode", "")
  311. if authCode == "" {
  312. br.Msg = "参数有误"
  313. br.ErrMsg = "参数缺失, AuthCode"
  314. return
  315. }
  316. data, e := services.CodeLoginFromMiddleServer(authCode)
  317. if e != nil {
  318. br.Msg = "获取失败"
  319. br.ErrMsg = "获取登录Token失败, Err: " + e.Error()
  320. return
  321. }
  322. br.Data = data
  323. br.Ret = 200
  324. br.Success = true
  325. br.Msg = "获取成功"
  326. }
  327. // GetEtaForumAdminAuthCode
  328. // @Title 免密登录-获取登录ETA社区管理后台系统的编码
  329. // @Description 免密登录-获取登录ETA社区管理后台系统的编码
  330. // @Success 200 Ret=200 获取成功
  331. // @router /forum_admin/auth_code [get]
  332. func (this *SysUserAuthController) GetEtaForumAdminAuthCode() {
  333. br := new(models.BaseResponse).Init()
  334. defer func() {
  335. if br.ErrMsg == "" {
  336. br.IsSendEmail = false
  337. }
  338. this.Data["json"] = br
  339. this.ServeJSON()
  340. }()
  341. sysUser := this.SysUser
  342. if sysUser == nil {
  343. br.Msg = "请登录"
  344. br.ErrMsg = "请登录,SysUser Is Empty"
  345. br.Ret = 408
  346. return
  347. }
  348. code, e := eta_forum.GetForumAdminAuthCode(sysUser.AdminName)
  349. if e != nil {
  350. br.Msg = "获取失败"
  351. br.ErrMsg = "获取登录编码失败, Err: " + e.Error()
  352. return
  353. }
  354. br.Data = code
  355. br.Ret = 200
  356. br.Success = true
  357. br.Msg = "获取成功"
  358. }