sys_user.go 11 KB

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