Browse Source

邮件发送模板

ziwen 1 year ago
parent
commit
50965cb9de
1 changed files with 29 additions and 8 deletions
  1. 29 8
      services/user_login.go

+ 29 - 8
services/user_login.go

@@ -79,24 +79,45 @@ func SendAdminEmailVerifyCode(source int, email string) (ok bool, err error) {
 	}
 
 	// 获取邮箱模板
-	confKey := "admin_verify_code_email_tmp"
-	confTmp, e := company.GetConfigDetailByCode(confKey)
+	// 获取配置好的短信模版
+	cond := ` AND (conf_key = ? OR conf_key = ?)`
+	pars := make([]interface{}, 0)
+	pars = append(pars, "LoginEmailTemplateSubject", "LoginEmailTemplateContent")
+	busiConf := new(models.BusinessConf)
+	emailConfList, e := busiConf.GetItemsByCondition(cond, pars, []string{"conf_key, conf_val"}, "")
 	if e != nil {
-		err = fmt.Errorf("获取邮件模板失败, Err: %s", e.Error())
+		if e.Error() == utils.ErrNoRow() {
+			err = fmt.Errorf("请先配置邮件模版")
+			return
+		}
+		err = fmt.Errorf("获取邮件模版失败, Err: %s", e.Error())
 		return
 	}
-	if confTmp.ConfigValue == `` {
-		err = fmt.Errorf("邮件模板为空, 不可推送")
+	var emaiContent, emailSubject string
+	for _, v := range emailConfList {
+		if v.ConfKey == "LoginEmailTemplateContent" {
+			emaiContent = v.ConfVal
+		} else if v.ConfKey == "LoginEmailTemplateSubject" {
+			emailSubject = v.ConfVal
+		}
+	}
+	if emailSubject == "" {
+		err = fmt.Errorf("请先配置邮件模版主题")
+		return
+	}
+	if emaiContent == "" {
+		err = fmt.Errorf("请先配置邮件模版内容")
 		return
 	}
 
 	req := new(EnglishReportSendEmailRequest)
-	req.Subject = "弘则研究登录验证"
+	req.Subject = emailSubject
 	req.Email = email
+	// todo 发信人昵称
 	req.FromAlias = conf.FromAlias
 	// 填充模板
 	t := time.Now().Format("2006年01月02日")
-	ct := confTmp.ConfigValue
+	ct := emaiContent
 	ct = strings.Replace(ct, "{{VERIFY_CODE}}", verifyCode, 1)
 	ct = strings.Replace(ct, "{{EXPIRED_MINUTE}}", strconv.Itoa(utils.VerifyCodeExpireMinute), 1)
 	ct = strings.Replace(ct, "{{DATE_TIME}}", t, 1)
@@ -120,4 +141,4 @@ func SendAdminEmailVerifyCode(source int, email string) (ok bool, err error) {
 		err = fmt.Errorf("更新验证码记录失败, Err: %s", e.Error())
 	}
 	return
-}
+}