sys_user.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mobile/models"
  5. "eta/eta_mobile/models/system"
  6. "eta/eta_mobile/services"
  7. "eta/eta_mobile/services/eta_trial"
  8. "eta/eta_mobile/utils"
  9. "fmt"
  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 = sysUser.AdminName
  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
  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. // ETA试用平台-请求中间服务更新用户最后登录时间和次数
  171. if utils.BusinessCode == utils.BusinessCodeSandbox {
  172. go func() {
  173. var r eta_trial.EtaTrialUserReq
  174. r.Mobile = sysUser.Mobile
  175. _, _ = eta_trial.UpdateEtaTrialUserLogin(r)
  176. }()
  177. }
  178. }
  179. type SysUserAuthController struct {
  180. BaseAuthController
  181. }
  182. // @Title 修改密码
  183. // @Description 修改密码
  184. // @Param request body models.ModifyPwdReq true "type json string"
  185. // @Success 200 {object} models.LoginResp
  186. // @router /modify/pwd [post]
  187. func (this *SysUserAuthController) ModifyPwd() {
  188. br := new(models.BaseResponse).Init()
  189. defer func() {
  190. this.Data["json"] = br
  191. this.ServeJSON()
  192. }()
  193. var req system.ModifyPwdReq
  194. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  195. if err != nil {
  196. br.Msg = "参数解析异常!"
  197. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  198. return
  199. }
  200. sysUser := this.SysUser
  201. if sysUser == nil {
  202. br.Msg = "登录失败,账号或密码错误"
  203. br.ErrMsg = "登录失败,sysUser is nil "
  204. return
  205. }
  206. if req.OldPwd == "" {
  207. br.Msg = "原始密码不能为空"
  208. return
  209. }
  210. if req.NewPwd == "" {
  211. br.Msg = "新密码不能为空"
  212. return
  213. }
  214. if sysUser.Password != req.OldPwd {
  215. br.Msg = "旧密码错误,请重新输入"
  216. return
  217. }
  218. err = system.ModifyPwd(sysUser.AdminId, req.NewPwd)
  219. if err != nil {
  220. br.Msg = "修改失败"
  221. br.ErrMsg = "修改失败,Err:" + err.Error()
  222. return
  223. }
  224. br.Ret = 200
  225. br.Success = true
  226. br.Msg = "修改成功"
  227. }
  228. // CheckPwd
  229. // @Title 校验是否初始密码
  230. // @Description 校验是否初始密码
  231. // @Success 200 {bool} true "是否初始密码;false代表不是初始密码,直接进去;true代表是初始密码,需要强制修改密码"
  232. // @router /check_pwd [get]
  233. func (this *SysUserAuthController) CheckPwd() {
  234. br := new(models.BaseResponse).Init()
  235. defer func() {
  236. this.Data["json"] = br
  237. this.ServeJSON()
  238. }()
  239. sysUser := this.SysUser
  240. if sysUser == nil {
  241. br.Msg = "登录失败,账号或密码错误"
  242. br.ErrMsg = "登录失败,sysUser is nil "
  243. return
  244. }
  245. br.Data = false
  246. if sysUser.Password == utils.MD5("123456a") {
  247. br.Data = true
  248. }
  249. br.Ret = 200
  250. br.Success = true
  251. br.Msg = "校验成功"
  252. }
  253. // @Title 获取系统用户UUID
  254. // @Description 获取系统用户UUID
  255. // @Success 200 {object} system.SysuserListResp
  256. // @router /uuid [get]
  257. func (this *SysUserController) Uuid() {
  258. br := new(models.BaseResponse).Init()
  259. defer func() {
  260. this.Data["json"] = br
  261. this.ServeJSON()
  262. }()
  263. nowTime := time.Now().Format(utils.FormatDateTimeUnSpace)
  264. randBigint := utils.GetRandDigit(4)
  265. randStr := utils.GetRandStringNoSpecialChar(4)
  266. uuid := nowTime + randBigint + randStr
  267. br.Ret = 200
  268. br.Success = true
  269. br.Msg = "获取成功"
  270. br.Data = uuid
  271. }
  272. // GetCrmAuthCode
  273. // @Title 免密登录-获取登录CRM系统的编码
  274. // @Description 免密登录-获取登录CRM系统的编码
  275. // @Success 200 Ret=200 获取成功
  276. // @router /get_crm_auth_code [get]
  277. func (this *SysUserAuthController) GetCrmAuthCode() {
  278. br := new(models.BaseResponse).Init()
  279. defer func() {
  280. if br.ErrMsg == "" {
  281. br.IsSendEmail = false
  282. }
  283. this.Data["json"] = br
  284. this.ServeJSON()
  285. }()
  286. sysUser := this.SysUser
  287. if sysUser == nil {
  288. br.Msg = "请登录"
  289. br.ErrMsg = "请登录,SysUser Is Empty"
  290. br.Ret = 408
  291. return
  292. }
  293. code, e := services.GetAuthCodeFromMiddleServer(sysUser.AdminName)
  294. if e != nil {
  295. br.Msg = "获取失败"
  296. br.ErrMsg = "获取登录编码失败, Err: " + e.Error()
  297. return
  298. }
  299. br.Data = code
  300. br.Ret = 200
  301. br.Success = true
  302. br.Msg = "获取成功"
  303. }
  304. // AuthCodeLogin
  305. // @Title 编码登录
  306. // @Description 编码登录
  307. // @Success 200 Ret=200 操作成功
  308. // @router /auth_code_login [get]
  309. func (this *SysUserController) AuthCodeLogin() {
  310. br := new(models.BaseResponse).Init()
  311. defer func() {
  312. if br.ErrMsg == "" {
  313. br.IsSendEmail = false
  314. }
  315. this.Data["json"] = br
  316. this.ServeJSON()
  317. }()
  318. //authCode := this.GetString("AuthCode", "")
  319. //if authCode == "" {
  320. // br.Msg = "参数有误"
  321. // br.ErrMsg = "参数缺失, AuthCode"
  322. // return
  323. //}
  324. req := make(map[string]interface{})
  325. query := this.Ctx.Request.URL.Query()
  326. for key, value := range query {
  327. req[key] = value[0]
  328. }
  329. fmt.Println(req)
  330. data, e, errMsg := services.ThirdLogin(req)
  331. if e != nil {
  332. br.Msg = "获取失败"
  333. if errMsg != `` {
  334. br.Msg = errMsg
  335. }
  336. br.ErrMsg = "获取登录Token失败, Err: " + e.Error()
  337. return
  338. }
  339. br.Data = data
  340. br.Ret = 200
  341. br.Success = true
  342. br.Msg = "获取成功"
  343. }
  344. // SystemConfig
  345. // @Title 系统配置列表
  346. // @Description 系统配置列表
  347. // @Success 200 {object} []system.BusinessConf
  348. // @router /public_config [get]
  349. func (this *SysUserController) SystemConfig() {
  350. br := new(models.BaseResponse).Init()
  351. defer func() {
  352. if br.ErrMsg == "" {
  353. br.IsSendEmail = false
  354. }
  355. this.Data["json"] = br
  356. this.ServeJSON()
  357. }()
  358. list := make([]system.BusinessConf, 0)
  359. // 获取基础配置, 若未配置则直接返回
  360. conf, e := models.GetBusinessConf()
  361. if e != nil {
  362. br.Msg = "获取失败"
  363. br.ErrMsg = "获取基础配置失败, Err: " + e.Error()
  364. return
  365. }
  366. list = append(list, system.BusinessConf{
  367. ConfKey: "LoginUrl",
  368. ConfVal: conf["LoginUrl"],
  369. }, system.BusinessConf{
  370. ConfKey: "LogoutUrl",
  371. ConfVal: conf["LogoutUrl"],
  372. })
  373. br.Data = list
  374. br.Ret = 200
  375. br.Success = true
  376. br.Msg = "获取成功"
  377. }
  378. // Logout
  379. // @Title 登出接口
  380. // @Description 登出接口
  381. // @Success 200 Ret=200 获取成功
  382. // @router /logout [post]
  383. func (this *SysUserAuthController) Logout() {
  384. br := new(models.BaseResponse).Init()
  385. defer func() {
  386. if br.ErrMsg == "" {
  387. br.IsSendEmail = false
  388. }
  389. this.Data["json"] = br
  390. this.ServeJSON()
  391. }()
  392. sysUser := this.SysUser
  393. if sysUser == nil {
  394. br.Msg = "请登录"
  395. br.ErrMsg = "请登录,SysUser Is Empty"
  396. br.Ret = 408
  397. return
  398. }
  399. services.ThirdLogout(this.Session.AccessToken)
  400. //e := services.ThirdLogout(this.Session.AccessToken)
  401. //if e != nil {
  402. // br.Msg = "登出失败"
  403. // br.ErrMsg = "登出失败, Err: " + e.Error()
  404. // return
  405. //}
  406. br.Ret = 200
  407. br.Success = true
  408. br.Msg = "登出成功"
  409. }