user_login.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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, 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. // 获取配置好的短信模版
  29. smsCond := ` AND conf_key in (?,?) `
  30. smsPars := make([]interface{}, 0)
  31. smsPars = append(smsPars, "LoginSmsTpId", "LoginSmsGjTpId")
  32. conf := new(models.BusinessConf)
  33. confList, e := conf.GetItemsByCondition(smsCond, smsPars, []string{"conf_key", "conf_val"}, "")
  34. if e != nil {
  35. if e.Error() == utils.ErrNoRow() {
  36. err = fmt.Errorf("请先配置短信模版")
  37. return
  38. }
  39. err = fmt.Errorf("获取短信模版失败, Err: %s", e.Error())
  40. return
  41. }
  42. tplId := ""
  43. gjTplId := ""
  44. for _, v := range confList {
  45. if v.ConfKey == "LoginSmsTpId" {
  46. tplId = v.ConfVal
  47. } else if v.ConfKey == "LoginSmsGjTpId" {
  48. gjTplId = v.ConfVal
  49. }
  50. }
  51. if tplId == "" {
  52. err = fmt.Errorf("请先配置短信模版")
  53. return
  54. }
  55. if areaCode == "86" {
  56. ok = SendSmsCode(mobile, verifyCode, tplId)
  57. } else {
  58. if gjTplId == "" {
  59. err = fmt.Errorf("请先配置国际短信模版")
  60. return
  61. }
  62. ok = SendSmsCodeGj(mobile, verifyCode, areaCode, gjTplId)
  63. }
  64. record.SendStatus = system.AdminVerifyCodeRecordStatusSuccess
  65. if !ok {
  66. record.SendStatus = system.AdminVerifyCodeRecordStatusFail
  67. }
  68. cols := []string{"SendStatus"}
  69. if e := record.Update(cols); e != nil {
  70. err = fmt.Errorf("更新验证码记录失败, Err: %s", e.Error())
  71. }
  72. return
  73. }
  74. // SendAdminEmailVerifyCode 发送用户邮箱验证码
  75. func SendAdminEmailVerifyCode(source int, email string) (ok bool, err error) {
  76. verifyCode := utils.GetRandDigit(6)
  77. record := new(system.AdminVerifyCodeRecord)
  78. record.VerifyType = system.AdminVerifyCodeRecordTypeEmail
  79. record.Email = email
  80. record.Source = source
  81. record.Code = verifyCode
  82. record.ExpiredTime = time.Now().Add(utils.VerifyCodeExpireMinute * time.Minute)
  83. record.CreateTime = time.Now().Local()
  84. record.ModifyTime = time.Now().Local()
  85. if e := record.Create(); e != nil {
  86. err = fmt.Errorf("新增验证码记录失败, Err: %s", e.Error())
  87. return
  88. }
  89. // 获取邮件配置
  90. authKey := "english_report_email_conf"
  91. emailConf, e := company.GetConfigDetailByCode(authKey)
  92. if e != nil {
  93. err = fmt.Errorf("获取群发邮件权限失败, Err: %s", e.Error())
  94. return
  95. }
  96. if emailConf.ConfigValue == "" {
  97. err = fmt.Errorf("邮件配置为空, 不可推送")
  98. return
  99. }
  100. conf := new(models.EnglishReportEmailConf)
  101. if e = json.Unmarshal([]byte(emailConf.ConfigValue), &conf); e != nil {
  102. err = fmt.Errorf("邮件配置有误, 不可推送")
  103. return
  104. }
  105. // 获取邮箱模板
  106. // 获取配置好的短信模版
  107. cond := ` AND (conf_key = ? OR conf_key = ?)`
  108. pars := make([]interface{}, 0)
  109. pars = append(pars, "LoginEmailTemplateSubject", "LoginEmailTemplateContent")
  110. busiConf := new(models.BusinessConf)
  111. emailConfList, e := busiConf.GetItemsByCondition(cond, pars, []string{"conf_key, conf_val"}, "")
  112. if e != nil {
  113. if e.Error() == utils.ErrNoRow() {
  114. err = fmt.Errorf("请先配置邮件模版")
  115. return
  116. }
  117. err = fmt.Errorf("获取邮件模版失败, Err: %s", e.Error())
  118. return
  119. }
  120. var emaiContent, emailSubject string
  121. for _, v := range emailConfList {
  122. if v.ConfKey == "LoginEmailTemplateContent" {
  123. emaiContent = v.ConfVal
  124. } else if v.ConfKey == "LoginEmailTemplateSubject" {
  125. emailSubject = v.ConfVal
  126. }
  127. }
  128. if emailSubject == "" {
  129. err = fmt.Errorf("请先配置邮件模版主题")
  130. return
  131. }
  132. if emaiContent == "" {
  133. err = fmt.Errorf("请先配置邮件模版内容")
  134. return
  135. }
  136. req := new(EnglishReportSendEmailRequest)
  137. req.Subject = emailSubject
  138. req.Email = email
  139. // todo 发信人昵称
  140. req.FromAlias = conf.FromAlias
  141. // 填充模板
  142. t := time.Now().Format("2006年01月02日")
  143. ct := emaiContent
  144. ct = strings.Replace(ct, "{{VERIFY_CODE}}", verifyCode, 1)
  145. ct = strings.Replace(ct, "{{EXPIRED_MINUTE}}", strconv.Itoa(utils.VerifyCodeExpireMinute), 1)
  146. ct = strings.Replace(ct, "{{DATE_TIME}}", t, 1)
  147. req.HtmlBody = ct
  148. aliEmail := new(AliyunEmail)
  149. o, result, e := aliEmail.SendEmail(req)
  150. if e != nil {
  151. err = fmt.Errorf("邮箱推送失败, Err: %s", e.Error())
  152. return
  153. }
  154. ok = o
  155. record.SendStatus = system.AdminVerifyCodeRecordStatusSuccess
  156. if !ok {
  157. record.SendStatus = system.AdminVerifyCodeRecordStatusFail
  158. }
  159. record.SendResult = result
  160. cols := []string{"SendStatus", "SendResult"}
  161. if e = record.Update(cols); e != nil {
  162. err = fmt.Errorf("更新验证码记录失败, Err: %s", e.Error())
  163. }
  164. return
  165. }