user_login.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. package controllers
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/company"
  7. "eta/eta_api/models/system"
  8. "eta/eta_api/services"
  9. "eta/eta_api/utils"
  10. "fmt"
  11. "github.com/mojocn/base64Captcha"
  12. "image/color"
  13. "strings"
  14. "time"
  15. )
  16. // UserLoginController 登录-无需Token
  17. type UserLoginController struct {
  18. BaseCommonController
  19. }
  20. // UserLoginAuthController 登录-需Token
  21. type UserLoginAuthController struct {
  22. BaseAuthController
  23. }
  24. // GenerateCaptcha
  25. // @Title 生成图形验证码
  26. // @Description 生成图形验证码
  27. // @Success 200 Ret=200 获取成功
  28. // @router /get_captcha [get]
  29. func (this *UserLoginController) GenerateCaptcha() {
  30. br := new(models.BaseResponse).Init()
  31. defer func() {
  32. if br.ErrMsg == "" {
  33. br.IsSendEmail = false
  34. }
  35. this.Data["json"] = br
  36. this.ServeJSON()
  37. }()
  38. //driver := base64Captcha.DefaultDriverDigit
  39. // 自定义验证码样式
  40. var driver base64Captcha.Driver
  41. driverString := base64Captcha.DriverString{
  42. Height: 60, //高度
  43. Width: 120, //宽度
  44. NoiseCount: 0, //干扰数
  45. ShowLineOptions: 2 | 4, //展示个数
  46. Length: 4, //长度
  47. //Source: "1234567890qwertyuioplkjhgfdsazxcvbnm", //验证码随机字符串来源
  48. Source: "1234567890", //验证码随机字符串来源
  49. BgColor: &color.RGBA{ // 背景颜色
  50. R: 0,
  51. G: 0,
  52. B: 0,
  53. A: 0,
  54. },
  55. Fonts: []string{"wqy-microhei.ttc"}, // 字体
  56. }
  57. driver = driverString.ConvertFonts()
  58. // 生成验证码
  59. store := services.CaptchaRedis{}
  60. captcha := base64Captcha.NewCaptcha(driver, store)
  61. id, b64s, err := captcha.Generate()
  62. if err != nil {
  63. br.Msg = "生成失败"
  64. br.ErrMsg = "生成验证码失败, Err: " + err.Error()
  65. return
  66. }
  67. type CaptchaResult struct {
  68. Id string
  69. Base64Blob string
  70. }
  71. res := new(CaptchaResult)
  72. res.Id = id
  73. res.Base64Blob = b64s
  74. br.Ret = 200
  75. br.Success = true
  76. br.Msg = "获取成功"
  77. br.Data = res
  78. }
  79. // GetVerifyCode
  80. // @Title 获取短信/邮箱验证码
  81. // @Description 获取短信/邮箱验证码
  82. // @Param request body VerifyCodeReq true "type json string"
  83. // @Success 200 Ret=200 获取成功
  84. // @router /verify_code [post]
  85. func (this *UserLoginController) GetVerifyCode() {
  86. br := new(models.BaseResponse).Init()
  87. defer func() {
  88. if br.ErrMsg == "" {
  89. br.IsSendEmail = false
  90. }
  91. this.Data["json"] = br
  92. this.ServeJSON()
  93. }()
  94. type VerifyCodeReq struct {
  95. VerifyType int `description:"验证方式: 1-手机号; 2-邮箱"`
  96. CaptchaId string `description:"验证码ID"`
  97. CaptchaCode string `description:"图形验证码"`
  98. Mobile string `description:"手机号"`
  99. Email string `description:"邮箱"`
  100. Source int `description:"来源:1-登录;2-异常登录校验;3-忘记密码"`
  101. }
  102. var req VerifyCodeReq
  103. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  104. if err != nil {
  105. br.Msg = "参数解析异常!"
  106. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  107. return
  108. }
  109. if req.VerifyType != 1 && req.VerifyType != 2 {
  110. br.Msg = "验证方式有误"
  111. br.ErrMsg = "验证方式有误"
  112. return
  113. }
  114. if req.Source != 1 && req.Source != 2 && req.Source != 3 {
  115. br.Msg = "来源有误"
  116. br.ErrMsg = "来源有误"
  117. return
  118. }
  119. // 忘记密码图形校验在前一步, 这一步不再校验图形验证码
  120. if req.Source != 3 {
  121. if req.CaptchaId == "" || req.CaptchaCode == "" {
  122. br.Msg = "请输入图形验证码"
  123. return
  124. }
  125. }
  126. if req.VerifyType == 1 && req.Mobile == "" {
  127. br.Msg = "请输入手机号"
  128. return
  129. }
  130. if req.VerifyType == 2 && req.Email == "" {
  131. br.Msg = "请输入邮箱"
  132. return
  133. }
  134. if req.Source != 3 {
  135. store := services.CaptchaRedis{}
  136. ok := store.Verify(req.CaptchaId, req.CaptchaCode, true)
  137. if !ok {
  138. br.Msg = "图形验证码错误, 请重新输入"
  139. return
  140. }
  141. }
  142. // 限制最多60s获取一次
  143. var lockKey string
  144. if req.VerifyType == 1 {
  145. lockKey = fmt.Sprint(utils.CaptchaCachePrefix, req.Mobile)
  146. }
  147. if req.VerifyType == 2 {
  148. lockKey = fmt.Sprint(utils.CaptchaCachePrefix, req.Email)
  149. }
  150. locked := utils.Rc.SetNX(lockKey, 1, time.Minute)
  151. if !locked {
  152. br.Msg = "请勿频繁发送"
  153. return
  154. }
  155. // 发送验证码
  156. sendRes := false
  157. if req.VerifyType == 1 {
  158. r, e := services.SendAdminMobileVerifyCode(req.Source, req.Mobile)
  159. if e != nil {
  160. br.Msg = "发送失败"
  161. br.ErrMsg = "发送短信验证码失败, Err: " + e.Error()
  162. return
  163. }
  164. sendRes = r
  165. }
  166. if req.VerifyType == 2 {
  167. r, e := services.SendAdminEmailVerifyCode(req.Source, req.Email)
  168. if e != nil {
  169. br.Msg = "发送失败"
  170. br.ErrMsg = "发送邮箱验证码失败, Err: " + e.Error()
  171. return
  172. }
  173. sendRes = r
  174. }
  175. if !sendRes {
  176. br.Msg = "发送失败"
  177. br.ErrMsg = "发送短信验证码失败"
  178. return
  179. }
  180. br.Ret = 200
  181. br.Success = true
  182. br.Msg = "发送成功"
  183. }
  184. // Login
  185. // @Title 用户登录
  186. // @Description 用户登录
  187. // @Param request body UserLoginReq true "type json string"
  188. // @Success 200 {object} models.LoginResp
  189. // @router /login [post]
  190. func (this *UserLoginController) Login() {
  191. br := new(models.BaseResponse).Init()
  192. defer func() {
  193. if br.ErrMsg != "" {
  194. br.IsSendEmail = false
  195. }
  196. this.Data["json"] = br
  197. this.ServeJSON()
  198. }()
  199. // 入参
  200. type UserLoginReq struct {
  201. LoginType int `description:"登录方式: 1-账号; 2-手机号; 3-邮箱"`
  202. AbnormalCheckType int `description:"异常登陆校验方式: 1-手机号; 2-邮箱"`
  203. Username string `description:"账号"`
  204. Password string `description:"密码"`
  205. Mobile string `description:"手机号"`
  206. Email string `description:"邮箱"`
  207. VerifyCode string `description:"验证码"`
  208. }
  209. var req UserLoginReq
  210. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  211. if err != nil {
  212. br.Msg = "参数解析异常!"
  213. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  214. return
  215. }
  216. req.Username = strings.TrimSpace(req.Username)
  217. req.Mobile = strings.TrimSpace(req.Mobile)
  218. if req.Mobile != "" {
  219. if !utils.ValidateMobileFormatat(req.Mobile) {
  220. br.Msg = "您的手机号输入有误, 请检查"
  221. return
  222. }
  223. }
  224. req.Email = strings.TrimSpace(req.Email)
  225. if req.Email != "" {
  226. if !utils.ValidateEmailFormatat(req.Email) {
  227. br.Msg = "您的邮箱输入有误, 请检查"
  228. return
  229. }
  230. }
  231. req.VerifyCode = strings.TrimSpace(req.VerifyCode)
  232. if req.LoginType != 1 && req.LoginType != 2 && req.LoginType != 3 {
  233. br.Msg = "登录方式有误"
  234. br.ErrMsg = fmt.Sprintf("登录方式有误, Type: %d", req.LoginType)
  235. return
  236. }
  237. sysUser := new(system.Admin)
  238. // 账号密码登录
  239. if req.LoginType == 1 {
  240. if req.Username == "" {
  241. br.Msg = "请输入账号"
  242. return
  243. }
  244. if req.Password == "" {
  245. br.Msg = "请输入密码"
  246. return
  247. }
  248. // 判断账号是否为异常登录
  249. abnormalKey := fmt.Sprint(utils.CACHE_ABNORMAL_LOGIN, req.Username)
  250. isAbnormal, _ := utils.Rc.RedisString(abnormalKey)
  251. if isAbnormal != "" {
  252. if req.AbnormalCheckType != 1 && req.AbnormalCheckType != 2 {
  253. br.Msg = "账号异常, 请进行手机号/邮箱校验"
  254. br.ErrMsg = fmt.Sprintf("账号异常, 请进行手机号/邮箱校验, CheckType: %d", req.AbnormalCheckType)
  255. return
  256. }
  257. recordCond := ` AND source = ?`
  258. recordPars := make([]interface{}, 0)
  259. recordPars = append(recordPars, system.AdminVerifyCodeRecordSourceAbnormal)
  260. if req.AbnormalCheckType == 1 {
  261. if req.Mobile == "" {
  262. br.Msg = "请输入手机号"
  263. return
  264. }
  265. if !utils.ValidateMobileFormatat(req.Mobile) {
  266. br.Msg = "您的手机号输入有误, 请检查"
  267. return
  268. }
  269. recordCond += ` AND mobile = ?`
  270. recordPars = append(recordPars, req.Mobile)
  271. }
  272. if req.AbnormalCheckType == 2 {
  273. if req.Email == "" {
  274. br.Msg = "请输入邮箱"
  275. return
  276. }
  277. if !utils.ValidateEmailFormatat(req.Email) {
  278. br.Msg = "您的邮箱输入有误, 请检查"
  279. return
  280. }
  281. recordCond += ` AND email = ?`
  282. recordPars = append(recordPars, req.Email)
  283. }
  284. if req.VerifyCode == "" {
  285. br.Msg = "请输入验证码"
  286. return
  287. }
  288. expiredTime := time.Now().Add(utils.VerifyCodeExpireMinute * time.Minute).Format(utils.FormatDateTime)
  289. recordCond += ` AND code = ? AND expired_time <= ?`
  290. recordPars = append(recordPars, req.VerifyCode, expiredTime)
  291. // 校验验证码
  292. recordOb := new(system.AdminVerifyCodeRecord)
  293. _, e := recordOb.GetItemByCondition(recordCond, recordPars)
  294. if e != nil {
  295. if e.Error() == utils.ErrNoRow() {
  296. br.Msg = "验证码错误, 请重新输入"
  297. return
  298. }
  299. br.Msg = "验证码错误, 请重新输入"
  300. br.ErrMsg = "获取异常登陆验证码失败, Err: " + e.Error()
  301. return
  302. }
  303. }
  304. // 账号密码校验
  305. errPassKey := fmt.Sprint(utils.CACHE_LOGIN_ERR_PASS, req.Username)
  306. accountUser, e := system.CheckSysUser(req.Username, req.Password)
  307. if e != nil {
  308. br.Ret = models.BaseRespCodeLoginErr
  309. if e.Error() == utils.ErrNoRow() {
  310. br.Msg = "登录失败, 账号或密码错误"
  311. if isAbnormal != "" {
  312. return
  313. }
  314. // 错误密码计数, 超过6次标记异常
  315. if !utils.Rc.IsExist(errPassKey) {
  316. _ = utils.Rc.Put(errPassKey, 1, utils.GetTodayLastSecond())
  317. return
  318. }
  319. errNum, _ := utils.Rc.RedisInt(errPassKey)
  320. errNum += 1
  321. if errNum >= 6 {
  322. br.Ret = models.BaseRespCodeAbnormalLogin
  323. // 标记异常登录, 重置计数
  324. _ = utils.Rc.Put(abnormalKey, "true", utils.GetTodayLastSecond())
  325. _ = utils.Rc.Delete(errPassKey)
  326. return
  327. }
  328. _ = utils.Rc.Put(errPassKey, errNum, utils.GetTodayLastSecond())
  329. return
  330. }
  331. br.Msg = "登录失败, 账号或密码错误"
  332. br.ErrMsg = "登录失败, Err:" + e.Error()
  333. return
  334. }
  335. if accountUser.Enabled == 0 {
  336. br.Msg = "您的账号已被禁用, 如需登录, 请联系管理员"
  337. br.ErrMsg = fmt.Sprintf("账号已被禁用, 登录账号: %s, 账户名称: %s", accountUser.AdminName, accountUser.RealName)
  338. return
  339. }
  340. // 异常登录-是否登录间隔大于60天
  341. if isAbnormal == "" {
  342. abnormalTime := time.Now().AddDate(0, 0, -60)
  343. lastLogin, _ := time.ParseInLocation(utils.FormatDateTime, accountUser.LastLoginTime, time.Local)
  344. if !lastLogin.IsZero() && lastLogin.Before(abnormalTime) {
  345. br.Msg = "请进行异常登录校验"
  346. br.Ret = models.BaseRespCodeAbnormalLogin
  347. // 标记异常登录
  348. _ = utils.Rc.Put(abnormalKey, "true", utils.GetTodayLastSecond())
  349. return
  350. }
  351. }
  352. // 登录成功, 清除异常标记
  353. _ = utils.Rc.Delete(abnormalKey)
  354. _ = utils.Rc.Delete(errPassKey)
  355. sysUser = accountUser
  356. }
  357. // 手机号
  358. if req.LoginType == 2 {
  359. if req.Mobile == "" {
  360. br.Msg = "请输入手机号"
  361. return
  362. }
  363. if !utils.ValidateMobileFormatat(req.Mobile) {
  364. br.Msg = "您的手机号输入有误, 请检查"
  365. return
  366. }
  367. if req.VerifyCode == "" {
  368. br.Msg = "请输入验证码"
  369. return
  370. }
  371. expiredTime := time.Now().Add(utils.VerifyCodeExpireMinute * time.Minute).Format(utils.FormatDateTime)
  372. recordCond := ` AND source = ? AND mobile = ? AND code = ? AND expired_time <= ?`
  373. recordPars := make([]interface{}, 0)
  374. recordPars = append(recordPars, system.AdminVerifyCodeRecordSourceLogin, req.Mobile, req.VerifyCode, expiredTime)
  375. recordOb := new(system.AdminVerifyCodeRecord)
  376. _, e := recordOb.GetItemByCondition(recordCond, recordPars)
  377. if e != nil {
  378. if e.Error() == utils.ErrNoRow() {
  379. br.Msg = "验证码错误, 请重新输入"
  380. return
  381. }
  382. br.Msg = "验证码错误, 请重新输入"
  383. br.ErrMsg = "获取手机号登陆验证码失败, Err: " + e.Error()
  384. return
  385. }
  386. mobileUser, e := system.GetSysUserByMobile(req.Mobile)
  387. if e != nil {
  388. if e.Error() == utils.ErrNoRow() {
  389. br.Msg = "您的手机号未绑定账号, 请检查"
  390. return
  391. }
  392. br.Msg = "您的手机号未绑定账号, 请检查"
  393. br.ErrMsg = "手机号获取用户失败, Err:" + e.Error()
  394. return
  395. }
  396. if mobileUser.Enabled == 0 {
  397. br.Msg = "您的账号已被禁用, 如需登录, 请联系管理员"
  398. br.ErrMsg = fmt.Sprintf("账号已被禁用, 登录手机号: %s", req.Mobile)
  399. return
  400. }
  401. sysUser = mobileUser
  402. }
  403. // 邮箱登录
  404. if req.LoginType == 3 {
  405. if req.Email == "" {
  406. br.Msg = "请输入邮箱"
  407. return
  408. }
  409. if !utils.ValidateEmailFormatat(req.Email) {
  410. br.Msg = "您的邮箱输入有误, 请检查"
  411. return
  412. }
  413. if req.VerifyCode == "" {
  414. br.Msg = "请输入验证码"
  415. return
  416. }
  417. expiredTime := time.Now().Add(utils.VerifyCodeExpireMinute * time.Minute).Format(utils.FormatDateTime)
  418. recordCond := ` AND source = ? AND email = ? AND code = ? AND expired_time <= ?`
  419. recordPars := make([]interface{}, 0)
  420. recordPars = append(recordPars, system.AdminVerifyCodeRecordSourceLogin, req.Email, req.VerifyCode, expiredTime)
  421. recordOb := new(system.AdminVerifyCodeRecord)
  422. _, e := recordOb.GetItemByCondition(recordCond, recordPars)
  423. if e != nil {
  424. if e.Error() == utils.ErrNoRow() {
  425. br.Msg = "验证码错误, 请重新输入"
  426. return
  427. }
  428. br.Msg = "验证码错误, 请重新输入"
  429. br.ErrMsg = "获取手机号登陆验证码失败, Err: " + e.Error()
  430. return
  431. }
  432. emailUser, e := system.GetSysUserByEmail(req.Email)
  433. if e != nil {
  434. if e.Error() == utils.ErrNoRow() {
  435. br.Msg = "您的邮箱未绑定账号, 请检查"
  436. return
  437. }
  438. br.Msg = "您的邮箱未绑定账号, 请检查"
  439. br.ErrMsg = "邮箱获取用户失败, Err:" + e.Error()
  440. return
  441. }
  442. if emailUser.Enabled == 0 {
  443. br.Msg = "您的账号已被禁用, 如需登录, 请联系管理员"
  444. br.ErrMsg = fmt.Sprintf("账号已被禁用, 登录邮箱: %s", req.Email)
  445. return
  446. }
  447. sysUser = emailUser
  448. }
  449. account := utils.MD5(sysUser.AdminName)
  450. token := utils.GenToken(account)
  451. sysSession := new(system.SysSession)
  452. sysSession.UserName = req.Username
  453. sysSession.SysUserId = sysUser.AdminId
  454. sysSession.ExpiredTime = time.Now().AddDate(0, 0, 90)
  455. sysSession.IsRemember = 0 // 均需要做过期校验
  456. sysSession.CreatedTime = time.Now()
  457. sysSession.LastUpdatedTime = time.Now()
  458. sysSession.AccessToken = token
  459. if e := system.AddSysSession(sysSession); e != nil {
  460. br.Msg = "登录失败"
  461. br.ErrMsg = "新增session信息失败, Err:" + e.Error()
  462. return
  463. }
  464. // 修改最后登录时间
  465. {
  466. sysUser.LastLoginTime = time.Now().Format(utils.FormatDateTime)
  467. sysUser.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
  468. _ = sysUser.Update([]string{"LastLoginTime", "LastUpdatedTime"})
  469. }
  470. resp := new(system.LoginResp)
  471. resp.Authorization = token
  472. resp.Authorization = "authorization=" + token + "$account=" + account
  473. resp.RealName = sysUser.RealName
  474. resp.AdminName = sysUser.AdminName
  475. resp.RoleName = sysUser.RoleName
  476. resp.SysRoleTypeCode = sysUser.RoleTypeCode //系统角色编码
  477. resp.RoleTypeCode = sysUser.RoleTypeCode
  478. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP {
  479. resp.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  480. }
  481. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_TEAM {
  482. resp.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  483. }
  484. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_DEPARTMENT {
  485. resp.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  486. }
  487. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  488. resp.RoleTypeCode = utils.ROLE_TYPE_CODE_RAI_SELLER
  489. }
  490. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_DEPARTMENT {
  491. resp.RoleTypeCode = utils.ROLE_TYPE_CODE_RAI_SELLER
  492. }
  493. if sysUser.RoleName == utils.ROLE_NAME_FICC_DIRECTOR {
  494. resp.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  495. }
  496. resp.AdminId = sysUser.AdminId
  497. var productName string
  498. productId := services.GetProductId(sysUser.RoleTypeCode)
  499. if productId == 1 {
  500. productName = utils.COMPANY_PRODUCT_FICC_NAME
  501. } else if productId == 2 {
  502. productName = utils.COMPANY_PRODUCT_RAI_NAME
  503. } else {
  504. productName = "admin"
  505. }
  506. resp.ProductName = productName
  507. resp.Authority = sysUser.Authority
  508. // 设置redis缓存
  509. {
  510. // 获取不可信的登录态,并将该登录态重置掉,不允许多次登录
  511. noTrustLoginKey := fmt.Sprint(utils.CACHE_ACCESS_TOKEN_LOGIN_NO_TRUST, sysUser.AdminId)
  512. noTrustLoginId, _ := utils.Rc.RedisString(noTrustLoginKey)
  513. if noTrustLoginId != `` { // 如果存在不可信设备,那么将其下架
  514. oldNoTrustLoginKey := fmt.Sprint(utils.CACHE_ACCESS_TOKEN_LOGIN, noTrustLoginId)
  515. _ = utils.Rc.Put(oldNoTrustLoginKey, "0", 30*time.Minute)
  516. }
  517. // 如果当前是不可信设备,那么将其加入到不可信名单
  518. loginKey := fmt.Sprint(utils.CACHE_ACCESS_TOKEN_LOGIN, sysSession.Id)
  519. _ = utils.Rc.Put(loginKey, "1", 30*time.Minute)
  520. _ = utils.Rc.Put(noTrustLoginKey, sysSession.Id, 30*time.Minute)
  521. }
  522. // 新增登录记录
  523. go func() {
  524. record := new(system.SysUserLoginRecord)
  525. record.Uid = sysUser.AdminId
  526. record.UserName = req.Username
  527. record.Ip = this.Ctx.Input.IP()
  528. record.Stage = "login"
  529. record.CreateTime = time.Now()
  530. _ = system.AddSysUserLoginRecord(record)
  531. }()
  532. br.Data = resp
  533. br.Ret = 200
  534. br.Success = true
  535. br.Msg = "登录成功"
  536. }
  537. // ForgetAccountGet
  538. // @Title 忘记密码-账号校验
  539. // @Description 忘记密码-账号校验
  540. // @Param request body ForgetAccountGetReq true "type json string"
  541. // @Success 200 Ret=200 获取成功
  542. // @router /forget/account_get [post]
  543. func (this *UserLoginController) ForgetAccountGet() {
  544. br := new(models.BaseResponse).Init()
  545. defer func() {
  546. if br.ErrMsg == "" {
  547. br.IsSendEmail = false
  548. }
  549. this.Data["json"] = br
  550. this.ServeJSON()
  551. }()
  552. type ForgetAccountGetReq struct {
  553. CaptchaId string `description:"验证码ID"`
  554. CaptchaCode string `description:"图形验证码"`
  555. UserName string `description:"用户名"`
  556. }
  557. var req ForgetAccountGetReq
  558. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  559. if err != nil {
  560. br.Msg = "参数解析异常!"
  561. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  562. return
  563. }
  564. req.UserName = strings.TrimSpace(req.UserName)
  565. if req.UserName == "" {
  566. br.Msg = "请输入账号"
  567. return
  568. }
  569. if req.CaptchaId == "" || req.CaptchaCode == "" {
  570. br.Msg = "请输入图形验证码"
  571. return
  572. }
  573. store := services.CaptchaRedis{}
  574. ok := store.Verify(req.CaptchaId, req.CaptchaCode, true)
  575. if !ok {
  576. br.Msg = "验证码错误, 请重新输入"
  577. return
  578. }
  579. sysUser, e := system.GetSysUserByAdminName(req.UserName)
  580. if e != nil {
  581. if e.Error() == utils.ErrNoRow() {
  582. br.Msg = "用户不存在, 请检查"
  583. return
  584. }
  585. br.Msg = "账号错误, 请重新输入"
  586. br.ErrMsg = "用户名获取用户失败, Err: " + e.Error()
  587. return
  588. }
  589. type ForgetAccountCheckResp struct {
  590. Mobile string `description:"手机号"`
  591. Email string `description:"邮箱"`
  592. }
  593. resp := ForgetAccountCheckResp{
  594. Mobile: sysUser.Mobile,
  595. Email: sysUser.Email,
  596. }
  597. br.Data = resp
  598. br.Ret = 200
  599. br.Success = true
  600. br.Msg = "获取成功"
  601. }
  602. // ForgetCodeVerify
  603. // @Title 忘记密码-验证码校验
  604. // @Description 忘记密码-验证码校验
  605. // @Param request body ForgetCodeVerifyReq true "type json string"
  606. // @Success 200 Ret=200 获取成功
  607. // @router /forget/code_verify [post]
  608. func (this *UserLoginController) ForgetCodeVerify() {
  609. br := new(models.BaseResponse).Init()
  610. defer func() {
  611. if br.ErrMsg == "" {
  612. br.IsSendEmail = false
  613. }
  614. this.Data["json"] = br
  615. this.ServeJSON()
  616. }()
  617. type ForgetCodeVerifyReq struct {
  618. FindType int `description:"密码找回方式: 1-手机号; 2-邮箱"`
  619. VerifyCode string `description:"验证码"`
  620. UserName string `description:"用户名"`
  621. Mobile string `description:"手机号"`
  622. Email string `description:"邮箱"`
  623. }
  624. var req ForgetCodeVerifyReq
  625. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  626. if err != nil {
  627. br.Msg = "参数解析异常!"
  628. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  629. return
  630. }
  631. req.UserName = strings.TrimSpace(req.UserName)
  632. if req.UserName == "" {
  633. br.Msg = "请输入账号"
  634. return
  635. }
  636. if req.VerifyCode == "" {
  637. br.Msg = "请输入验证码"
  638. return
  639. }
  640. // 手机号找回
  641. var verifyRes bool
  642. if req.FindType == 1 {
  643. req.Mobile = strings.TrimSpace(req.Mobile)
  644. if req.Mobile == "" {
  645. br.Msg = "请输入手机号"
  646. return
  647. }
  648. if !utils.ValidateMobileFormatat(req.Mobile) {
  649. br.Msg = "您的手机号输入有误, 请检查"
  650. return
  651. }
  652. expiredTime := time.Now().Add(utils.VerifyCodeExpireMinute * time.Minute).Format(utils.FormatDateTime)
  653. recordCond := ` AND source = ? AND mobile = ? AND code = ? AND expired_time <= ?`
  654. recordPars := make([]interface{}, 0)
  655. recordPars = append(recordPars, system.AdminVerifyCodeRecordSourceForget, req.Mobile, req.VerifyCode, expiredTime)
  656. recordOb := new(system.AdminVerifyCodeRecord)
  657. _, e := recordOb.GetItemByCondition(recordCond, recordPars)
  658. if e != nil {
  659. if e.Error() == utils.ErrNoRow() {
  660. br.Msg = "验证码错误, 请重新输入"
  661. return
  662. }
  663. br.Msg = "验证码错误, 请重新输入"
  664. br.ErrMsg = "获取手机号登陆验证码失败, Err: " + e.Error()
  665. return
  666. }
  667. verifyRes = true
  668. }
  669. // 邮箱找回
  670. if req.FindType == 2 {
  671. req.Email = strings.TrimSpace(req.Email)
  672. if req.Email == "" {
  673. br.Msg = "请输入邮箱"
  674. return
  675. }
  676. if !utils.ValidateEmailFormatat(req.Email) {
  677. br.Msg = "您的邮箱输入有误, 请检查"
  678. return
  679. }
  680. expiredTime := time.Now().Add(utils.VerifyCodeExpireMinute * time.Minute).Format(utils.FormatDateTime)
  681. recordCond := ` AND source = ? AND email = ? AND code = ? AND expired_time <= ?`
  682. recordPars := make([]interface{}, 0)
  683. recordPars = append(recordPars, system.AdminVerifyCodeRecordSourceForget, req.Email, req.VerifyCode, expiredTime)
  684. recordOb := new(system.AdminVerifyCodeRecord)
  685. _, e := recordOb.GetItemByCondition(recordCond, recordPars)
  686. if e != nil {
  687. if e.Error() == utils.ErrNoRow() {
  688. br.Msg = "验证码错误, 请重新输入"
  689. return
  690. }
  691. br.Msg = "验证码错误, 请重新输入"
  692. br.ErrMsg = "获取手机号登陆验证码失败, Err: " + e.Error()
  693. return
  694. }
  695. verifyRes = true
  696. }
  697. if verifyRes {
  698. successKey := fmt.Sprint(utils.CACHE_FIND_PASS_VERIFY, req.UserName)
  699. _ = utils.Rc.Put(successKey, "true", utils.GetTodayLastSecond())
  700. }
  701. br.Data = verifyRes
  702. br.Ret = 200
  703. br.Success = true
  704. br.Msg = "操作成功"
  705. }
  706. // ForgetResetPass
  707. // @Title 忘记密码-重置密码
  708. // @Description 忘记密码-重置密码
  709. // @Param request body ForgetResetPassReq true "type json string"
  710. // @Success 200 Ret=200 获取成功
  711. // @router /forget/reset_pass [post]
  712. func (this *UserLoginController) ForgetResetPass() {
  713. br := new(models.BaseResponse).Init()
  714. defer func() {
  715. if br.ErrMsg == "" {
  716. br.IsSendEmail = false
  717. }
  718. this.Data["json"] = br
  719. this.ServeJSON()
  720. }()
  721. type ForgetResetPassReq struct {
  722. UserName string `description:"用户名"`
  723. Password string `description:"密码"`
  724. RePassword string `description:"重复密码"`
  725. }
  726. var req ForgetResetPassReq
  727. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  728. if err != nil {
  729. br.Msg = "参数解析异常!"
  730. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  731. return
  732. }
  733. req.UserName = strings.TrimSpace(req.UserName)
  734. if req.UserName == "" {
  735. br.Msg = "请输入账号"
  736. return
  737. }
  738. req.Password = strings.TrimSpace(req.Password)
  739. if req.Password == "" {
  740. br.Msg = "请输入新密码"
  741. return
  742. }
  743. req.RePassword = strings.TrimSpace(req.RePassword)
  744. if req.RePassword == "" {
  745. br.Msg = "请确认新密码"
  746. return
  747. }
  748. if req.Password != req.RePassword {
  749. br.Msg = "两次密码输入不一致, 请检查"
  750. return
  751. }
  752. // 接上一步-验证码校验成功后才允许修改
  753. successKey := fmt.Sprint(utils.CACHE_FIND_PASS_VERIFY, req.UserName)
  754. verifyOk, _ := utils.Rc.RedisString(successKey)
  755. if verifyOk != "true" {
  756. br.Msg = "验证码校验失效, 请重新验证"
  757. br.ErrMsg = "重置密码异常, 验证码校验已失效"
  758. return
  759. }
  760. sysUser, e := system.GetSysAdminByName(req.UserName)
  761. if e != nil {
  762. if e.Error() == utils.ErrNoRow() {
  763. br.Msg = "用户不存在, 请检查"
  764. return
  765. }
  766. br.Msg = "操作失败"
  767. br.ErrMsg = "密码找回获取用户失败, Err:" + e.Error()
  768. return
  769. }
  770. b, e := base64.StdEncoding.DecodeString(req.Password)
  771. if e != nil {
  772. br.Msg = "操作失败"
  773. br.ErrMsg = "解析密码失败, Err:" + e.Error()
  774. return
  775. }
  776. pwd := string(b)
  777. pwd = utils.MD5(pwd)
  778. sysUser.Password = pwd
  779. sysUser.LastUpdatedPasswordTime = time.Now().Format(utils.FormatDateTime)
  780. // 重置密码后更新一下登录时间, 不然账号密码登录如果超过60天还会被异常校验再校验一次, 影响体验
  781. sysUser.LastLoginTime = time.Now().Format(utils.FormatDateTime)
  782. sysUser.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
  783. if e = sysUser.Update([]string{"Password", "LastUpdatedPasswordTime", "LastLoginTime", "LastUpdatedTime"}); e != nil {
  784. br.Msg = "操作失败"
  785. br.ErrMsg = "更新密码失败, Err: " + e.Error()
  786. return
  787. }
  788. _ = utils.Rc.Delete(successKey)
  789. // 同步ETA用户缓存
  790. var syncData system.SyncAdminData
  791. syncData.Source = utils.SOURCE_CRM_FLAG
  792. syncData.AdminName = sysUser.AdminName
  793. _ = utils.Rc.LPush(utils.CACHE_SYNC_ADMIN, syncData)
  794. br.Data = true
  795. br.Ret = 200
  796. br.Success = true
  797. br.Msg = "操作成功"
  798. }
  799. // AreaCodeList
  800. // @Title 手机号区号列表
  801. // @Description 手机号区号列表
  802. // @Success 200 Ret=200 获取成功
  803. // @router /area_code/list [get]
  804. func (this *UserLoginController) AreaCodeList() {
  805. br := new(models.BaseResponse).Init()
  806. defer func() {
  807. if br.ErrMsg == "" {
  808. br.IsSendEmail = false
  809. }
  810. this.Data["json"] = br
  811. this.ServeJSON()
  812. }()
  813. type AreaCodeListResp struct {
  814. Name string `description:"地区"`
  815. Value string `description:"区号"`
  816. }
  817. resp := make([]AreaCodeListResp, 0)
  818. confAuth, e := company.GetConfigDetailByCode(company.ConfAreaCodeListKey)
  819. if e != nil {
  820. br.Msg = "获取失败"
  821. br.ErrMsg = "获取手机号区号配置失败, Err: " + e.Error()
  822. return
  823. }
  824. if confAuth.ConfigValue == "" {
  825. br.Msg = "获取失败"
  826. br.ErrMsg = "手机号区号配置为空"
  827. return
  828. }
  829. if e := json.Unmarshal([]byte(confAuth.ConfigValue), &resp); e != nil {
  830. br.Msg = "获取失败"
  831. br.ErrMsg = "手机号区号配置有误"
  832. return
  833. }
  834. br.Data = resp
  835. br.Ret = 200
  836. br.Success = true
  837. br.Msg = "获取成功"
  838. }