user_login.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/company"
  6. "eta/eta_api/models/system"
  7. "eta/eta_api/utils"
  8. "fmt"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. // SendAdminMobileVerifyCode 发送用户手机验证码
  14. func SendAdminMobileVerifyCode(source int, mobile string) (ok bool, err error) {
  15. verifyCode := utils.GetRandDigit(6)
  16. record := new(system.AdminVerifyCodeRecord)
  17. record.VerifyType = system.AdminVerifyCodeRecordTypeMobile
  18. record.Mobile = mobile
  19. record.Source = source
  20. record.Code = verifyCode
  21. record.ExpiredTime = time.Now().Add(utils.VerifyCodeExpireMinute * time.Minute)
  22. record.CreateTime = time.Now().Local()
  23. record.ModifyTime = time.Now().Local()
  24. if e := record.Create(); e != nil {
  25. err = fmt.Errorf("新增验证码记录失败, Err: %s", e.Error())
  26. return
  27. }
  28. tplId := utils.SmsNewLoginTplId
  29. ok = SendSmsCode(mobile, verifyCode, tplId)
  30. record.SendStatus = system.AdminVerifyCodeRecordStatusSuccess
  31. if !ok {
  32. record.SendStatus = system.AdminVerifyCodeRecordStatusFail
  33. }
  34. cols := []string{"SendStatus"}
  35. if e := record.Update(cols); e != nil {
  36. err = fmt.Errorf("更新验证码记录失败, Err: %s", e.Error())
  37. }
  38. return
  39. }
  40. // SendAdminEmailVerifyCode 发送用户邮箱验证码
  41. func SendAdminEmailVerifyCode(source int, email string) (ok bool, err error) {
  42. verifyCode := utils.GetRandDigit(6)
  43. record := new(system.AdminVerifyCodeRecord)
  44. record.VerifyType = system.AdminVerifyCodeRecordTypeEmail
  45. record.Email = email
  46. record.Source = source
  47. record.Code = verifyCode
  48. record.ExpiredTime = time.Now().Add(utils.VerifyCodeExpireMinute * time.Minute)
  49. record.CreateTime = time.Now().Local()
  50. record.ModifyTime = time.Now().Local()
  51. if e := record.Create(); e != nil {
  52. err = fmt.Errorf("新增验证码记录失败, Err: %s", e.Error())
  53. return
  54. }
  55. // 获取邮件配置
  56. authKey := "english_report_email_conf"
  57. emailConf, e := company.GetConfigDetailByCode(authKey)
  58. if e != nil {
  59. err = fmt.Errorf("获取群发邮件权限失败, Err: %s", e.Error())
  60. return
  61. }
  62. if emailConf.ConfigValue == "" {
  63. err = fmt.Errorf("邮件配置为空, 不可推送")
  64. return
  65. }
  66. conf := new(models.EnglishReportEmailConf)
  67. if e = json.Unmarshal([]byte(emailConf.ConfigValue), &conf); e != nil {
  68. err = fmt.Errorf("邮件配置有误, 不可推送")
  69. return
  70. }
  71. // 获取邮箱模板
  72. confKey := "admin_verify_code_email_tmp"
  73. confTmp, e := company.GetConfigDetailByCode(confKey)
  74. if e != nil {
  75. err = fmt.Errorf("获取邮件模板失败, Err: %s", e.Error())
  76. return
  77. }
  78. if confTmp.ConfigValue == `` {
  79. err = fmt.Errorf("邮件模板为空, 不可推送")
  80. return
  81. }
  82. req := new(EnglishReportSendEmailRequest)
  83. req.Subject = "弘则研究登录验证"
  84. req.Email = email
  85. req.FromAlias = conf.FromAlias
  86. // 填充模板
  87. t := time.Now().Format("2006年01月02日")
  88. ct := confTmp.ConfigValue
  89. ct = strings.Replace(ct, "{{VERIFY_CODE}}", verifyCode, 1)
  90. ct = strings.Replace(ct, "{{EXPIRED_MINUTE}}", strconv.Itoa(utils.VerifyCodeExpireMinute), 1)
  91. ct = strings.Replace(ct, "{{DATE_TIME}}", t, 1)
  92. req.HtmlBody = ct
  93. aliEmail := new(AliyunEmail)
  94. o, result, e := aliEmail.SendEmail(req)
  95. if e != nil {
  96. err = fmt.Errorf("邮箱推送失败, Err: %s", e.Error())
  97. return
  98. }
  99. ok = o
  100. record.SendStatus = system.AdminVerifyCodeRecordStatusSuccess
  101. if !ok {
  102. record.SendStatus = system.AdminVerifyCodeRecordStatusFail
  103. }
  104. record.SendResult = result
  105. cols := []string{"SendStatus", "SendResult"}
  106. if e = record.Update(cols); e != nil {
  107. err = fmt.Errorf("更新验证码记录失败, Err: %s", e.Error())
  108. }
  109. return
  110. }