sys_user.go 9.2 KB

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