user_login.go 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. package controllers
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "eta/eta_mobile/models"
  6. "eta/eta_mobile/models/company"
  7. "eta/eta_mobile/models/system"
  8. "eta/eta_mobile/services"
  9. "eta/eta_mobile/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. TelAreaCode string `description:"手机区号"`
  100. Email string `description:"邮箱"`
  101. Source int `description:"来源:1-登录;2-异常登录校验;3-忘记密码"`
  102. }
  103. var req VerifyCodeReq
  104. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  105. if err != nil {
  106. br.Msg = "参数解析异常!"
  107. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  108. return
  109. }
  110. if req.VerifyType != 1 && req.VerifyType != 2 {
  111. br.Msg = "验证方式有误"
  112. br.ErrMsg = "验证方式有误"
  113. return
  114. }
  115. if req.Source != 1 && req.Source != 2 && req.Source != 3 {
  116. br.Msg = "来源有误"
  117. br.ErrMsg = "来源有误"
  118. return
  119. }
  120. // 忘记密码图形校验在前一步, 这一步不再校验图形验证码
  121. if req.Source != 3 {
  122. if req.CaptchaId == "" || req.CaptchaCode == "" {
  123. br.Msg = "请输入图形验证码"
  124. return
  125. }
  126. }
  127. if req.VerifyType == 1 {
  128. if req.TelAreaCode == "" {
  129. br.Msg = "请选择区号"
  130. return
  131. }
  132. if req.Mobile == "" {
  133. br.Msg = "请输入手机号"
  134. return
  135. }
  136. if req.TelAreaCode == utils.TelAreaCodeHome && !utils.ValidateMobileFormatat(req.Mobile) {
  137. br.Msg = "您的手机号输入有误, 请检查"
  138. return
  139. }
  140. _, e := system.GetSysUserByMobile(req.Mobile)
  141. if e != nil {
  142. if e.Error() == utils.ErrNoRow() {
  143. br.Msg = "您的手机号未绑定账号, 请检查"
  144. return
  145. }
  146. br.Msg = "您的手机号未绑定账号, 请检查"
  147. br.ErrMsg = "手机号获取用户失败, Err:" + e.Error()
  148. return
  149. }
  150. }
  151. if req.VerifyType == 2 {
  152. if req.Email == "" {
  153. br.Msg = "请输入邮箱"
  154. return
  155. }
  156. if !utils.ValidateEmailFormatat(req.Email) {
  157. br.Msg = "您的邮箱输入有误, 请检查"
  158. return
  159. }
  160. _, e := system.GetSysUserByEmail(req.Email)
  161. if e != nil {
  162. if e.Error() == utils.ErrNoRow() {
  163. br.Msg = "您的邮箱未绑定账号, 请检查"
  164. return
  165. }
  166. br.Msg = "您的邮箱未绑定账号, 请检查"
  167. br.ErrMsg = "邮箱获取用户失败, Err:" + e.Error()
  168. return
  169. }
  170. }
  171. if req.Source != 3 {
  172. store := services.CaptchaRedis{}
  173. ok := store.Verify(req.CaptchaId, req.CaptchaCode, true)
  174. if !ok {
  175. br.Msg = "图形验证码错误, 请重新输入"
  176. return
  177. }
  178. }
  179. // 限制最多60s获取一次
  180. var lockKey string
  181. if req.VerifyType == 1 {
  182. lockKey = fmt.Sprint(utils.CaptchaCachePrefix, req.Mobile)
  183. }
  184. if req.VerifyType == 2 {
  185. lockKey = fmt.Sprint(utils.CaptchaCachePrefix, req.Email)
  186. }
  187. locked := utils.Rc.SetNX(lockKey, 1, time.Minute)
  188. if !locked {
  189. br.Msg = "请勿频繁发送"
  190. return
  191. }
  192. // 发送验证码
  193. sendRes := false
  194. if req.VerifyType == 1 {
  195. r, e := services.SendAdminMobileVerifyCode(req.Source, req.Mobile, req.TelAreaCode)
  196. if e != nil {
  197. br.Msg = "发送失败"
  198. br.ErrMsg = "发送短信验证码失败, Err: " + e.Error()
  199. return
  200. }
  201. sendRes = r
  202. }
  203. if req.VerifyType == 2 {
  204. r, e := services.SendAdminEmailVerifyCode(req.Source, req.Email)
  205. if e != nil {
  206. br.Msg = "发送失败"
  207. br.ErrMsg = "发送邮箱验证码失败, Err: " + e.Error()
  208. return
  209. }
  210. sendRes = r
  211. }
  212. if !sendRes {
  213. br.Msg = "发送失败"
  214. br.ErrMsg = "发送短信验证码失败"
  215. return
  216. }
  217. br.Ret = 200
  218. br.Success = true
  219. br.Msg = "发送成功"
  220. }
  221. // Login
  222. // @Title 用户登录
  223. // @Description 用户登录
  224. // @Param request body UserLoginReq true "type json string"
  225. // @Success 200 {object} models.LoginResp
  226. // @router /login [post]
  227. func (this *UserLoginController) Login() {
  228. br := new(models.BaseResponse).Init()
  229. defer func() {
  230. if br.ErrMsg == "" {
  231. br.IsSendEmail = false
  232. }
  233. this.Data["json"] = br
  234. this.ServeJSON()
  235. }()
  236. // 入参
  237. type UserLoginReq struct {
  238. LoginType int `description:"登录方式: 1-账号; 2-手机号; 3-邮箱"`
  239. Username string `description:"账号"`
  240. Password string `description:"密码"`
  241. Mobile string `description:"手机号"`
  242. Email string `description:"邮箱"`
  243. VerifyCode string `description:"验证码"`
  244. ReqTime string `description:"登录时间戳"`
  245. TelAreaCode string `description:"区号"`
  246. }
  247. var req UserLoginReq
  248. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  249. if err != nil {
  250. br.Msg = "参数解析异常!"
  251. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  252. return
  253. }
  254. req.Username = strings.TrimSpace(req.Username)
  255. req.Mobile = strings.TrimSpace(req.Mobile)
  256. req.Email = strings.TrimSpace(req.Email)
  257. req.VerifyCode = strings.TrimSpace(req.VerifyCode)
  258. if req.LoginType != 1 && req.LoginType != 2 && req.LoginType != 3 {
  259. br.Msg = "登录方式有误"
  260. br.ErrMsg = fmt.Sprintf("登录方式有误, Type: %d", req.LoginType)
  261. return
  262. }
  263. sysUser := new(system.Admin)
  264. // 账号密码登录
  265. if req.LoginType == 1 {
  266. if req.Username == "" {
  267. br.Msg = "请输入账号"
  268. return
  269. }
  270. if req.Password == "" {
  271. br.Msg = "请输入密码"
  272. return
  273. }
  274. // 判断账号是否为异常登录, 算作异常的话就需要换另外的方式去登录了
  275. abnormalKey := fmt.Sprint(utils.CACHE_ABNORMAL_LOGIN, req.Username)
  276. isAbnormal, _ := utils.Rc.RedisString(abnormalKey)
  277. if isAbnormal != "" {
  278. br.Ret = models.BaseRespCodeAbnormalLogin
  279. br.Msg = "账号异常, 请进行手机号/邮箱校验"
  280. return
  281. }
  282. // 查询账号信息
  283. errPassKey := fmt.Sprint(utils.CACHE_LOGIN_ERR_PASS, req.Username)
  284. accountUser, e := system.GetSysUserByAdminName(req.Username)
  285. if e != nil {
  286. br.Ret = models.BaseRespCodeLoginErr
  287. br.Msg = "登录失败, 账号或密码错误"
  288. // 账号查询异常均进行标记, 避免一直尝试进行登录
  289. if !utils.Rc.IsExist(errPassKey) {
  290. _ = utils.Rc.Put(errPassKey, 1, utils.GetTodayLastSecond())
  291. return
  292. }
  293. errNum, _ := utils.Rc.RedisInt(errPassKey)
  294. errNum += 1
  295. if errNum < 6 {
  296. _ = utils.Rc.Put(errPassKey, errNum, utils.GetTodayLastSecond())
  297. return
  298. }
  299. // 标记异常登录, 重置计数
  300. br.Ret = models.BaseRespCodeAbnormalLogin
  301. br.Msg = "账号异常, 请进行手机号/邮箱校验"
  302. _ = utils.Rc.Put(abnormalKey, "true", utils.GetTodayLastSecond())
  303. _ = utils.Rc.Delete(errPassKey)
  304. return
  305. }
  306. // 系统用户-账号密码校验
  307. if accountUser.IsLdap == 0 {
  308. dbPass := utils.MD5(fmt.Sprintf("%s%s%s", accountUser.Password, utils.UserLoginSalt, req.ReqTime))
  309. if req.Password != dbPass {
  310. br.Ret = models.BaseRespCodeLoginErr
  311. br.Msg = "登录失败, 账号或密码错误"
  312. // 错误密码计数, 超过6次标记异常
  313. if !utils.Rc.IsExist(errPassKey) {
  314. _ = utils.Rc.Put(errPassKey, 1, utils.GetTodayLastSecond())
  315. return
  316. }
  317. errNum, _ := utils.Rc.RedisInt(errPassKey)
  318. errNum += 1
  319. if errNum < 6 {
  320. _ = utils.Rc.Put(errPassKey, errNum, utils.GetTodayLastSecond())
  321. return
  322. }
  323. // 标记异常登录, 重置计数
  324. br.Ret = models.BaseRespCodeAbnormalLogin
  325. br.Msg = "账号异常, 请进行手机号/邮箱校验"
  326. _ = utils.Rc.Put(abnormalKey, "true", utils.GetTodayLastSecond())
  327. _ = utils.Rc.Delete(errPassKey)
  328. return
  329. }
  330. if accountUser.Enabled == 0 {
  331. br.Msg = "您的账号已被禁用, 如需登录, 请联系管理员"
  332. br.ErrMsg = fmt.Sprintf("账号已被禁用, 登录账号: %s, 账户名称: %s", accountUser.AdminName, accountUser.RealName)
  333. return
  334. }
  335. // 异常登录-是否登录间隔大于60天
  336. if isAbnormal == "" {
  337. abnormalTime := time.Now().AddDate(0, 0, -60)
  338. lastLogin, _ := time.ParseInLocation(utils.FormatDateTime, accountUser.LastLoginTime, time.Local)
  339. if !lastLogin.IsZero() && lastLogin.Before(abnormalTime) {
  340. br.Msg = "请进行异常登录校验"
  341. br.Ret = models.BaseRespCodeAbnormalLogin
  342. // 标记异常登录
  343. _ = utils.Rc.Put(abnormalKey, "true", utils.GetTodayLastSecond())
  344. return
  345. }
  346. }
  347. }
  348. // 如果是域账号, 那么取出原始密码走AD域校验
  349. if accountUser.IsLdap == 1 {
  350. passDecode, e := base64.StdEncoding.DecodeString(req.Password)
  351. if e != nil {
  352. br.Ret = models.BaseRespCodeLoginErr
  353. br.Msg = "登录失败, 账号或密码错误"
  354. br.ErrMsg = "Pass Decode err: " + e.Error()
  355. return
  356. }
  357. originPass := strings.Replace(string(passDecode), utils.UserLoginSalt, "", 1)
  358. pass, e := services.LdapUserCheck(req.Username, originPass)
  359. if e != nil {
  360. br.Ret = models.BaseRespCodeLoginErr
  361. br.Msg = "登录失败, 账号或密码错误"
  362. br.ErrMsg = "LdapLogin err: " + e.Error()
  363. return
  364. }
  365. if !pass {
  366. br.Ret = models.BaseRespCodeLoginErr
  367. br.Msg = "登录失败, 账号或密码错误"
  368. return
  369. }
  370. }
  371. sysUser = accountUser
  372. }
  373. // 手机号
  374. if req.LoginType == 2 {
  375. if req.Mobile == "" {
  376. br.Msg = "请输入手机号"
  377. return
  378. }
  379. // 大陆校验区号
  380. if req.TelAreaCode == utils.TelAreaCodeHome && !utils.ValidateMobileFormatat(req.Mobile) {
  381. br.Msg = "您的手机号输入有误, 请检查"
  382. return
  383. }
  384. if req.VerifyCode == "" {
  385. br.Msg = "请输入验证码"
  386. return
  387. }
  388. expiredTime := time.Now().Add(utils.VerifyCodeExpireMinute * time.Minute).Format(utils.FormatDateTime)
  389. recordCond := ` AND source = ? AND mobile = ? AND code = ? AND expired_time <= ?`
  390. recordPars := make([]interface{}, 0)
  391. recordPars = append(recordPars, system.AdminVerifyCodeRecordSourceLogin, req.Mobile, req.VerifyCode, expiredTime)
  392. recordOb := new(system.AdminVerifyCodeRecord)
  393. _, e := recordOb.GetItemByCondition(recordCond, recordPars)
  394. if e != nil {
  395. if e.Error() == utils.ErrNoRow() {
  396. br.Msg = "验证码错误, 请重新输入"
  397. return
  398. }
  399. br.Msg = "验证码错误, 请重新输入"
  400. br.ErrMsg = "获取手机号登陆验证码失败, Err: " + e.Error()
  401. return
  402. }
  403. mobileUser, e := system.GetSysUserByMobile(req.Mobile)
  404. if e != nil {
  405. if e.Error() == utils.ErrNoRow() {
  406. br.Msg = "您的手机号未绑定账号, 请检查"
  407. return
  408. }
  409. br.Msg = "您的手机号未绑定账号, 请检查"
  410. br.ErrMsg = "手机号获取用户失败, Err:" + e.Error()
  411. return
  412. }
  413. if mobileUser.Enabled == 0 {
  414. br.Msg = "您的账号已被禁用, 如需登录, 请联系管理员"
  415. br.ErrMsg = fmt.Sprintf("账号已被禁用, 登录手机号: %s", req.Mobile)
  416. return
  417. }
  418. sysUser = mobileUser
  419. }
  420. // 邮箱登录
  421. if req.LoginType == 3 {
  422. if req.Email == "" {
  423. br.Msg = "请输入邮箱"
  424. return
  425. }
  426. if !utils.ValidateEmailFormatat(req.Email) {
  427. br.Msg = "您的邮箱输入有误, 请检查"
  428. return
  429. }
  430. if req.VerifyCode == "" {
  431. br.Msg = "请输入验证码"
  432. return
  433. }
  434. expiredTime := time.Now().Add(utils.VerifyCodeExpireMinute * time.Minute).Format(utils.FormatDateTime)
  435. recordCond := ` AND source = ? AND email = ? AND code = ? AND expired_time <= ?`
  436. recordPars := make([]interface{}, 0)
  437. recordPars = append(recordPars, system.AdminVerifyCodeRecordSourceLogin, req.Email, req.VerifyCode, expiredTime)
  438. recordOb := new(system.AdminVerifyCodeRecord)
  439. _, e := recordOb.GetItemByCondition(recordCond, recordPars)
  440. if e != nil {
  441. if e.Error() == utils.ErrNoRow() {
  442. br.Msg = "验证码错误, 请重新输入"
  443. return
  444. }
  445. br.Msg = "验证码错误, 请重新输入"
  446. br.ErrMsg = "获取手机号登陆验证码失败, Err: " + e.Error()
  447. return
  448. }
  449. emailUser, e := system.GetSysUserByEmail(req.Email)
  450. if e != nil {
  451. if e.Error() == utils.ErrNoRow() {
  452. br.Msg = "您的邮箱未绑定账号, 请检查"
  453. return
  454. }
  455. br.Msg = "您的邮箱未绑定账号, 请检查"
  456. br.ErrMsg = "邮箱获取用户失败, Err:" + e.Error()
  457. return
  458. }
  459. if emailUser.Enabled == 0 {
  460. br.Msg = "您的账号已被禁用, 如需登录, 请联系管理员"
  461. br.ErrMsg = fmt.Sprintf("账号已被禁用, 登录邮箱: %s", req.Email)
  462. return
  463. }
  464. sysUser = emailUser
  465. }
  466. // 登录成功(无论哪种方式登录), 清除异常标记
  467. abnormalCache := fmt.Sprint(utils.CACHE_ABNORMAL_LOGIN, sysUser.AdminName)
  468. errPassCache := fmt.Sprint(utils.CACHE_LOGIN_ERR_PASS, sysUser.AdminName)
  469. _ = utils.Rc.Delete(abnormalCache)
  470. _ = utils.Rc.Delete(errPassCache)
  471. account := utils.MD5(sysUser.AdminName)
  472. token := utils.GenToken(account)
  473. sysSession := new(system.SysSession)
  474. sysSession.UserName = sysUser.AdminName
  475. sysSession.SysUserId = sysUser.AdminId
  476. sysSession.ExpiredTime = time.Now().AddDate(0, 0, 90)
  477. sysSession.IsRemember = 0 // 均需要做过期校验
  478. sysSession.CreatedTime = time.Now()
  479. sysSession.LastUpdatedTime = time.Now()
  480. sysSession.AccessToken = token
  481. if e := system.AddSysSession(sysSession); e != nil {
  482. br.Msg = "登录失败"
  483. br.ErrMsg = "新增session信息失败, Err:" + e.Error()
  484. return
  485. }
  486. // 修改最后登录时间
  487. {
  488. sysUser.LastLoginTime = time.Now().Format(utils.FormatDateTime)
  489. sysUser.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
  490. _ = sysUser.Update([]string{"LastLoginTime", "LastUpdatedTime"})
  491. }
  492. resp := new(system.LoginResp)
  493. resp.Authorization = token
  494. resp.Authorization = "authorization=" + token + "$account=" + account
  495. resp.RealName = sysUser.RealName
  496. resp.AdminName = sysUser.AdminName
  497. resp.RoleName = sysUser.RoleName
  498. resp.SysRoleTypeCode = sysUser.RoleTypeCode //系统角色编码
  499. resp.RoleTypeCode = sysUser.RoleTypeCode
  500. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP {
  501. resp.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  502. }
  503. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_TEAM {
  504. resp.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  505. }
  506. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_DEPARTMENT {
  507. resp.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  508. }
  509. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  510. resp.RoleTypeCode = utils.ROLE_TYPE_CODE_RAI_SELLER
  511. }
  512. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_DEPARTMENT {
  513. resp.RoleTypeCode = utils.ROLE_TYPE_CODE_RAI_SELLER
  514. }
  515. if sysUser.RoleName == utils.ROLE_NAME_FICC_DIRECTOR {
  516. resp.RoleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  517. }
  518. resp.AdminId = sysUser.AdminId
  519. var productName string
  520. productId := services.GetProductId(sysUser.RoleTypeCode)
  521. if productId == 1 {
  522. productName = utils.COMPANY_PRODUCT_FICC_NAME
  523. } else if productId == 2 {
  524. productName = utils.COMPANY_PRODUCT_RAI_NAME
  525. } else {
  526. productName = "admin"
  527. }
  528. resp.ProductName = productName
  529. resp.Authority = sysUser.Authority
  530. resp.DepartmentName = sysUser.DepartmentName
  531. resp.GroupName = sysUser.GroupName
  532. // 设置redis缓存
  533. {
  534. // 获取不可信的登录态,并将该登录态重置掉,不允许多次登录
  535. noTrustLoginKey := fmt.Sprint(utils.CACHE_ACCESS_TOKEN_LOGIN_NO_TRUST, sysUser.AdminId)
  536. noTrustLoginId, _ := utils.Rc.RedisString(noTrustLoginKey)
  537. if noTrustLoginId != `` { // 如果存在不可信设备,那么将其下架
  538. oldNoTrustLoginKey := fmt.Sprint(utils.CACHE_ACCESS_TOKEN_LOGIN, noTrustLoginId)
  539. _ = utils.Rc.Put(oldNoTrustLoginKey, "0", utils.LoginCacheTime*time.Minute)
  540. }
  541. // 如果当前是不可信设备,那么将其加入到不可信名单
  542. loginKey := fmt.Sprint(utils.CACHE_ACCESS_TOKEN_LOGIN, sysSession.Id)
  543. _ = utils.Rc.Put(loginKey, "1", utils.LoginCacheTime*time.Minute)
  544. _ = utils.Rc.Put(noTrustLoginKey, sysSession.Id, utils.LoginCacheTime*time.Minute)
  545. }
  546. // 新增登录记录
  547. go func() {
  548. record := new(system.SysUserLoginRecord)
  549. record.Uid = sysUser.AdminId
  550. record.UserName = req.Username
  551. record.Ip = this.Ctx.Input.IP()
  552. record.Stage = "login"
  553. record.CreateTime = time.Now()
  554. _ = system.AddSysUserLoginRecord(record)
  555. }()
  556. br.Data = resp
  557. br.Ret = 200
  558. br.Success = true
  559. br.Msg = "登录成功"
  560. }
  561. // ForgetAccountGet
  562. // @Title 忘记密码-账号校验
  563. // @Description 忘记密码-账号校验
  564. // @Param request body ForgetAccountGetReq true "type json string"
  565. // @Success 200 Ret=200 获取成功
  566. // @router /forget/account_get [post]
  567. func (this *UserLoginController) ForgetAccountGet() {
  568. br := new(models.BaseResponse).Init()
  569. defer func() {
  570. if br.ErrMsg == "" {
  571. br.IsSendEmail = false
  572. }
  573. this.Data["json"] = br
  574. this.ServeJSON()
  575. }()
  576. type ForgetAccountGetReq struct {
  577. CaptchaId string `description:"验证码ID"`
  578. CaptchaCode string `description:"图形验证码"`
  579. UserName string `description:"用户名"`
  580. }
  581. var req ForgetAccountGetReq
  582. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  583. if err != nil {
  584. br.Msg = "参数解析异常!"
  585. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  586. return
  587. }
  588. req.UserName = strings.TrimSpace(req.UserName)
  589. if req.UserName == "" {
  590. br.Msg = "请输入账号"
  591. return
  592. }
  593. if req.CaptchaId == "" || req.CaptchaCode == "" {
  594. br.Msg = "请输入图形验证码"
  595. return
  596. }
  597. store := services.CaptchaRedis{}
  598. ok := store.Verify(req.CaptchaId, req.CaptchaCode, true)
  599. if !ok {
  600. br.Msg = "验证码错误, 请重新输入"
  601. return
  602. }
  603. sysUser, e := system.GetSysUserByAdminName(req.UserName)
  604. if e != nil {
  605. if e.Error() == utils.ErrNoRow() {
  606. br.Msg = "用户不存在, 请检查"
  607. return
  608. }
  609. br.Msg = "账号错误, 请重新输入"
  610. br.ErrMsg = "用户名获取用户失败, Err: " + e.Error()
  611. return
  612. }
  613. type ForgetAccountCheckResp struct {
  614. Mobile string `description:"手机号"`
  615. Email string `description:"邮箱"`
  616. TelAreaCode string `description:"手机区号"`
  617. }
  618. resp := ForgetAccountCheckResp{
  619. Mobile: sysUser.Mobile,
  620. Email: sysUser.Email,
  621. TelAreaCode: sysUser.TelAreaCode,
  622. }
  623. br.Data = resp
  624. br.Ret = 200
  625. br.Success = true
  626. br.Msg = "获取成功"
  627. }
  628. // ForgetCodeVerify
  629. // @Title 忘记密码-验证码校验
  630. // @Description 忘记密码-验证码校验
  631. // @Param request body ForgetCodeVerifyReq true "type json string"
  632. // @Success 200 Ret=200 获取成功
  633. // @router /forget/code_verify [post]
  634. func (this *UserLoginController) ForgetCodeVerify() {
  635. br := new(models.BaseResponse).Init()
  636. defer func() {
  637. if br.ErrMsg == "" {
  638. br.IsSendEmail = false
  639. }
  640. this.Data["json"] = br
  641. this.ServeJSON()
  642. }()
  643. type ForgetCodeVerifyReq struct {
  644. FindType int `description:"密码找回方式: 1-手机号; 2-邮箱"`
  645. VerifyCode string `description:"验证码"`
  646. UserName string `description:"用户名"`
  647. Mobile string `description:"手机号"`
  648. Email string `description:"邮箱"`
  649. TelAreaCode string `description:"区号"`
  650. }
  651. var req ForgetCodeVerifyReq
  652. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  653. if err != nil {
  654. br.Msg = "参数解析异常!"
  655. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  656. return
  657. }
  658. req.UserName = strings.TrimSpace(req.UserName)
  659. if req.UserName == "" {
  660. br.Msg = "请输入账号"
  661. return
  662. }
  663. if req.VerifyCode == "" {
  664. br.Msg = "请输入验证码"
  665. return
  666. }
  667. // 手机号找回
  668. var verifyRes bool
  669. if req.FindType == 1 {
  670. req.Mobile = strings.TrimSpace(req.Mobile)
  671. if req.Mobile == "" {
  672. br.Msg = "请输入手机号"
  673. return
  674. }
  675. if req.TelAreaCode == utils.TelAreaCodeHome && !utils.ValidateMobileFormatat(req.Mobile) {
  676. br.Msg = "您的手机号输入有误, 请检查"
  677. return
  678. }
  679. expiredTime := time.Now().Add(utils.VerifyCodeExpireMinute * time.Minute).Format(utils.FormatDateTime)
  680. recordCond := ` AND source = ? AND mobile = ? AND code = ? AND expired_time <= ?`
  681. recordPars := make([]interface{}, 0)
  682. recordPars = append(recordPars, system.AdminVerifyCodeRecordSourceForget, req.Mobile, req.VerifyCode, expiredTime)
  683. recordOb := new(system.AdminVerifyCodeRecord)
  684. _, e := recordOb.GetItemByCondition(recordCond, recordPars)
  685. if e != nil {
  686. if e.Error() == utils.ErrNoRow() {
  687. br.Msg = "验证码错误, 请重新输入"
  688. return
  689. }
  690. br.Msg = "验证码错误, 请重新输入"
  691. br.ErrMsg = "获取手机号登陆验证码失败, Err: " + e.Error()
  692. return
  693. }
  694. verifyRes = true
  695. }
  696. // 邮箱找回
  697. if req.FindType == 2 {
  698. req.Email = strings.TrimSpace(req.Email)
  699. if req.Email == "" {
  700. br.Msg = "请输入邮箱"
  701. return
  702. }
  703. if !utils.ValidateEmailFormatat(req.Email) {
  704. br.Msg = "您的邮箱输入有误, 请检查"
  705. return
  706. }
  707. expiredTime := time.Now().Add(utils.VerifyCodeExpireMinute * time.Minute).Format(utils.FormatDateTime)
  708. recordCond := ` AND source = ? AND email = ? AND code = ? AND expired_time <= ?`
  709. recordPars := make([]interface{}, 0)
  710. recordPars = append(recordPars, system.AdminVerifyCodeRecordSourceForget, req.Email, req.VerifyCode, expiredTime)
  711. recordOb := new(system.AdminVerifyCodeRecord)
  712. _, e := recordOb.GetItemByCondition(recordCond, recordPars)
  713. if e != nil {
  714. if e.Error() == utils.ErrNoRow() {
  715. br.Msg = "验证码错误, 请重新输入"
  716. return
  717. }
  718. br.Msg = "验证码错误, 请重新输入"
  719. br.ErrMsg = "获取手机号登陆验证码失败, Err: " + e.Error()
  720. return
  721. }
  722. verifyRes = true
  723. }
  724. if verifyRes {
  725. successKey := fmt.Sprint(utils.CACHE_FIND_PASS_VERIFY, req.UserName)
  726. _ = utils.Rc.Put(successKey, "true", utils.GetTodayLastSecond())
  727. }
  728. br.Data = verifyRes
  729. br.Ret = 200
  730. br.Success = true
  731. br.Msg = "操作成功"
  732. }
  733. // ForgetResetPass
  734. // @Title 忘记密码-重置密码
  735. // @Description 忘记密码-重置密码
  736. // @Param request body ForgetResetPassReq true "type json string"
  737. // @Success 200 Ret=200 获取成功
  738. // @router /forget/reset_pass [post]
  739. func (this *UserLoginController) ForgetResetPass() {
  740. br := new(models.BaseResponse).Init()
  741. defer func() {
  742. if br.ErrMsg == "" {
  743. br.IsSendEmail = false
  744. }
  745. this.Data["json"] = br
  746. this.ServeJSON()
  747. }()
  748. type ForgetResetPassReq struct {
  749. UserName string `description:"用户名"`
  750. Password string `description:"密码"`
  751. RePassword string `description:"重复密码"`
  752. }
  753. var req ForgetResetPassReq
  754. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  755. if err != nil {
  756. br.Msg = "参数解析异常!"
  757. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  758. return
  759. }
  760. req.UserName = strings.TrimSpace(req.UserName)
  761. if req.UserName == "" {
  762. br.Msg = "请输入账号"
  763. return
  764. }
  765. req.Password = strings.TrimSpace(req.Password)
  766. if req.Password == "" {
  767. br.Msg = "请输入新密码"
  768. return
  769. }
  770. req.RePassword = strings.TrimSpace(req.RePassword)
  771. if req.RePassword == "" {
  772. br.Msg = "请确认新密码"
  773. return
  774. }
  775. if req.Password != req.RePassword {
  776. br.Msg = "两次密码输入不一致, 请检查"
  777. return
  778. }
  779. // 接上一步-验证码校验成功后才允许修改
  780. successKey := fmt.Sprint(utils.CACHE_FIND_PASS_VERIFY, req.UserName)
  781. verifyOk, _ := utils.Rc.RedisString(successKey)
  782. if verifyOk != "true" {
  783. br.Msg = "验证码校验失效, 请重新验证"
  784. br.ErrMsg = "重置密码异常, 验证码校验已失效"
  785. return
  786. }
  787. sysUser, e := system.GetSysAdminByName(req.UserName)
  788. if e != nil {
  789. if e.Error() == utils.ErrNoRow() {
  790. br.Msg = "用户不存在, 请检查"
  791. return
  792. }
  793. br.Msg = "操作失败"
  794. br.ErrMsg = "密码找回获取用户失败, Err:" + e.Error()
  795. return
  796. }
  797. b, e := base64.StdEncoding.DecodeString(req.Password)
  798. if e != nil {
  799. br.Msg = "操作失败"
  800. br.ErrMsg = "解析密码失败, Err:" + e.Error()
  801. return
  802. }
  803. pwd := string(b)
  804. pwd = utils.MD5(pwd)
  805. sysUser.Password = pwd
  806. sysUser.LastUpdatedPasswordTime = time.Now().Format(utils.FormatDateTime)
  807. // 重置密码后更新一下登录时间, 不然账号密码登录如果超过60天还会被异常校验再校验一次, 影响体验
  808. sysUser.LastLoginTime = time.Now().Format(utils.FormatDateTime)
  809. sysUser.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
  810. if e = sysUser.Update([]string{"Password", "LastUpdatedPasswordTime", "LastLoginTime", "LastUpdatedTime"}); e != nil {
  811. br.Msg = "操作失败"
  812. br.ErrMsg = "更新密码失败, Err: " + e.Error()
  813. return
  814. }
  815. // 如果有异常标记也清除掉
  816. abnormalKey := fmt.Sprint(utils.CACHE_ABNORMAL_LOGIN, req.UserName)
  817. _ = utils.Rc.Delete(abnormalKey)
  818. _ = utils.Rc.Delete(successKey)
  819. br.Data = true
  820. br.Ret = 200
  821. br.Success = true
  822. br.Msg = "操作成功"
  823. }
  824. // AreaCodeList
  825. // @Title 手机号区号列表
  826. // @Description 手机号区号列表
  827. // @Success 200 Ret=200 获取成功
  828. // @router /area_code/list [get]
  829. func (this *UserLoginController) AreaCodeList() {
  830. br := new(models.BaseResponse).Init()
  831. defer func() {
  832. if br.ErrMsg == "" {
  833. br.IsSendEmail = false
  834. }
  835. this.Data["json"] = br
  836. this.ServeJSON()
  837. }()
  838. type AreaCodeListResp struct {
  839. Name string `description:"地区"`
  840. Value string `description:"区号"`
  841. }
  842. resp := make([]AreaCodeListResp, 0)
  843. confAuth, e := company.GetConfigDetailByCode(company.ConfAreaCodeListKey)
  844. if e != nil {
  845. br.Msg = "获取失败"
  846. br.ErrMsg = "获取手机号区号配置失败, Err: " + e.Error()
  847. return
  848. }
  849. if confAuth.ConfigValue == "" {
  850. br.Msg = "获取失败"
  851. br.ErrMsg = "手机号区号配置为空"
  852. return
  853. }
  854. if e := json.Unmarshal([]byte(confAuth.ConfigValue), &resp); e != nil {
  855. br.Msg = "获取失败"
  856. br.ErrMsg = "手机号区号配置有误"
  857. return
  858. }
  859. br.Data = resp
  860. br.Ret = 200
  861. br.Success = true
  862. br.Msg = "获取成功"
  863. }
  864. // CheckUserLdap
  865. // @Title 校验用户是否为域用户
  866. // @Description 校验用户是否为域用户
  867. // @Param request body CheckUserLdapReq true "type json string"
  868. // @Success 200 Ret=200 获取成功
  869. // @router /ldap/user_check [post]
  870. func (this *UserLoginController) CheckUserLdap() {
  871. br := new(models.BaseResponse).Init()
  872. defer func() {
  873. if br.ErrMsg == "" {
  874. br.IsSendEmail = false
  875. }
  876. this.Data["json"] = br
  877. this.ServeJSON()
  878. }()
  879. type CheckUserLdapReq struct {
  880. UserName string `description:"用户名"`
  881. }
  882. var req CheckUserLdapReq
  883. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  884. if err != nil {
  885. br.Msg = "参数解析异常!"
  886. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  887. return
  888. }
  889. req.UserName = strings.TrimSpace(req.UserName)
  890. if req.UserName == "" {
  891. br.Msg = "请输入账号"
  892. return
  893. }
  894. isLdap := false
  895. accountUser, e := system.GetSysUserByAdminName(req.UserName)
  896. if e != nil {
  897. // 无该用户视作普通用户
  898. if e.Error() == utils.ErrNoRow() {
  899. br.Data = false
  900. br.Success = true
  901. br.Msg = "操作成功"
  902. return
  903. }
  904. br.Msg = "操作失败"
  905. br.ErrMsg = "获取用户信息失败, Err: " + e.Error()
  906. return
  907. }
  908. if accountUser.IsLdap == 1 {
  909. isLdap = true
  910. }
  911. br.Data = isLdap
  912. br.Ret = 200
  913. br.Success = true
  914. br.Msg = "操作成功"
  915. }
  916. // BaseInfo
  917. // @Title 基础信息
  918. // @Description 基础信息
  919. // @Success 200 Ret=200 获取成功
  920. // @router /base_info [get]
  921. func (this *UserLoginController) BaseInfo() {
  922. br := new(models.BaseResponse).Init()
  923. defer func() {
  924. if br.ErrMsg == "" {
  925. br.IsSendEmail = false
  926. }
  927. this.Data["json"] = br
  928. this.ServeJSON()
  929. }()
  930. icp, e := models.GetBusinessConfByKey("ICPLicense")
  931. if e != nil {
  932. br.Msg = "获取失败"
  933. br.ErrMsg = "获取商家配置失败, Err: " + e.Error()
  934. return
  935. }
  936. title, e := models.GetBusinessConfByKey("ETATitle")
  937. if e != nil {
  938. br.Msg = "获取失败"
  939. br.ErrMsg = "获取商家配置失败, Err: " + e.Error()
  940. return
  941. }
  942. tabNameMobile, e := models.GetBusinessConfByKey("TabNameMobile")
  943. if e != nil {
  944. br.Msg = "获取失败"
  945. br.ErrMsg = "获取商家配置失败, Err: " + e.Error()
  946. return
  947. }
  948. logoCn, e := models.GetBusinessConfByKey("LogoCN")
  949. if e != nil {
  950. br.Msg = "获取失败"
  951. br.ErrMsg = "获取商家配置失败, Err: " + e.Error()
  952. return
  953. }
  954. logoEn, e := models.GetBusinessConfByKey("LogoEN")
  955. if e != nil {
  956. br.Msg = "获取失败"
  957. br.ErrMsg = "获取商家配置失败, Err: " + e.Error()
  958. return
  959. }
  960. logoMobile, e := models.GetBusinessConfByKey("LogoMobile")
  961. if e != nil {
  962. br.Msg = "获取失败"
  963. br.ErrMsg = "获取商家配置失败, Err: " + e.Error()
  964. return
  965. }
  966. companyName, e := models.GetBusinessConfByKey("CompanyName")
  967. if e != nil {
  968. br.Msg = "获取失败"
  969. br.ErrMsg = "获取商家配置失败, Err: " + e.Error()
  970. return
  971. }
  972. type BaseInfoResp struct {
  973. Icp *models.BusinessConf `description:"Icp信息"`
  974. ETATitle *models.BusinessConf `description:"eta系统名称"`
  975. TabNameMobile *models.BusinessConf `description:"tab页名称"`
  976. LogoCn *models.BusinessConf `description:"中文logo"`
  977. LogoEn *models.BusinessConf `description:"英文logo"`
  978. LogoMobile *models.BusinessConf `description:"移动端logo"`
  979. CompanyName *models.BusinessConf `description:"公司名称"`
  980. }
  981. resp := BaseInfoResp{
  982. Icp: icp,
  983. ETATitle: title,
  984. TabNameMobile: tabNameMobile,
  985. LogoCn: logoCn,
  986. LogoEn: logoEn,
  987. LogoMobile: logoMobile,
  988. CompanyName: companyName,
  989. }
  990. br.Data = resp
  991. br.Ret = 200
  992. br.Success = true
  993. br.Msg = "获取成功"
  994. }