user_login.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // 获取配置好的短信模版
  77. cond := ` AND (conf_key = ? OR conf_key = ?)`
  78. pars := make([]interface{}, 0)
  79. pars = append(pars, "LoginEmailTemplateSubject", "LoginEmailTemplateContent")
  80. busiConf := new(models.BusinessConf)
  81. emailConfList, e := busiConf.GetItemsByCondition(cond, pars, []string{"conf_key, conf_val"}, "")
  82. if e != nil {
  83. if e.Error() == utils.ErrNoRow() {
  84. err = fmt.Errorf("请先配置邮件模版")
  85. return
  86. }
  87. err = fmt.Errorf("获取邮件模版失败, Err: %s", e.Error())
  88. return
  89. }
  90. var emaiContent, emailSubject string
  91. for _, v := range emailConfList {
  92. if v.ConfKey == "LoginEmailTemplateContent" {
  93. emaiContent = v.ConfVal
  94. } else if v.ConfKey == "LoginEmailTemplateSubject" {
  95. emailSubject = v.ConfVal
  96. }
  97. }
  98. if emailSubject == "" {
  99. err = fmt.Errorf("请先配置邮件模版主题")
  100. return
  101. }
  102. if emaiContent == "" {
  103. err = fmt.Errorf("请先配置邮件模版内容")
  104. return
  105. }
  106. req := new(EnglishReportSendEmailRequest)
  107. req.Subject = emailSubject
  108. req.Email = email
  109. // todo 发信人昵称
  110. req.FromAlias = conf.FromAlias
  111. // 填充模板
  112. t := time.Now().Format("2006年01月02日")
  113. ct := emaiContent
  114. ct = strings.Replace(ct, "{{VERIFY_CODE}}", verifyCode, 1)
  115. ct = strings.Replace(ct, "{{EXPIRED_MINUTE}}", strconv.Itoa(utils.VerifyCodeExpireMinute), 1)
  116. ct = strings.Replace(ct, "{{DATE_TIME}}", t, 1)
  117. req.HtmlBody = ct
  118. aliEmail := new(AliyunEmail)
  119. o, result, e := aliEmail.SendEmail(req)
  120. if e != nil {
  121. err = fmt.Errorf("邮箱推送失败, Err: %s", e.Error())
  122. return
  123. }
  124. ok = o
  125. record.SendStatus = system.AdminVerifyCodeRecordStatusSuccess
  126. if !ok {
  127. record.SendStatus = system.AdminVerifyCodeRecordStatusFail
  128. }
  129. record.SendResult = result
  130. cols := []string{"SendStatus", "SendResult"}
  131. if e = record.Update(cols); e != nil {
  132. err = fmt.Errorf("更新验证码记录失败, Err: %s", e.Error())
  133. }
  134. return
  135. }