user_login.go 3.6 KB

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