rdluck 3 ani în urmă
părinte
comite
f1ea2bf359
2 a modificat fișierele cu 62 adăugiri și 42 ștergeri
  1. 48 41
      controllers/sms.go
  2. 14 1
      models/sms_record.go

+ 48 - 41
controllers/sms.go

@@ -1,6 +1,7 @@
 package controllers
 
 import (
+	"encoding/json"
 	"hongze/hongze_robot/cache"
 	"hongze/hongze_robot/models"
 	"hongze/hongze_robot/services"
@@ -30,56 +31,62 @@ func (this *SmsController) SmsSend() {
 		this.Data["json"] = br
 		this.ServeJSON()
 	}()
-	sign := this.GetString("Sign")
-	address := this.GetString("Address")
-	body := this.GetString("Body")
-	name := this.GetString("Name")
-	date := this.GetString("Date")
-
-	utils.FileLog.Info("send msg: %s; %s; %s; %s; %s", sign, address, body, name, date)
-	count, err := models.GetSmsRecordCount(address, date)
-	if err != nil && err.Error() != utils.ErrNoRow() {
-		br.Msg = "失败,请联系技术"
-		br.ErrMsg = "失败;Err:" + err.Error()
-		return
-	}
-
-	item, err := cache.GetSmsConfigByCache("mobile")
+	reqBody := this.Ctx.Input.RequestBody
+	utils.FileLog.Info("%s", string(reqBody))
+	req := make([]*models.SmsReq, 0)
+	err = json.Unmarshal(reqBody, &req)
 	if err != nil {
-		br.Msg = "失败,请联系技术"
-		br.ErrMsg = "获取配置信息失败;Err:" + err.Error()
-		return
-	}
-	if item.ConfigValue == "" {
-		br.Msg = "失败,请联系技术"
-		br.ErrMsg = "配置信息为空"
+		br.Msg = "参数解析异常!"
+		br.ErrMsg = "参数解析失败,Err:" + err.Error()
 		return
 	}
-	isAdd := false
-	//判断记录是否存在
-	if count <= 0 && strings.Contains(item.ConfigValue, address) {
-		var tm time.Time
-		if date != "" {
-			smsDateInt64, _ := strconv.ParseInt(date, 10, 64)
-			tm = time.Unix(smsDateInt64/1e3, 0)
+
+	addList := make([]*models.SmsRecord, 0)
+	for _, v := range req {
+		count, err := models.GetSmsRecordCount(v.Address, v.Date)
+		if err != nil && err.Error() != utils.ErrNoRow() {
+			br.Msg = "失败,请联系技术"
+			br.ErrMsg = "失败;Err:" + err.Error()
+			return
+		}
+
+		item, err := cache.GetSmsConfigByCache("mobile")
+		if err != nil {
+			br.Msg = "失败,请联系技术"
+			br.ErrMsg = "获取配置信息失败;Err:" + err.Error()
+			return
+		}
+		if item.ConfigValue == "" {
+			br.Msg = "失败,请联系技术"
+			br.ErrMsg = "配置信息为空"
+			return
+		}
+		//判断记录是否存在
+		if count <= 0 && strings.Contains(item.ConfigValue, v.Address) {
+			var tm time.Time
+			if v.Date != "" {
+				smsDateInt64, _ := strconv.ParseInt(v.Date, 10, 64)
+				tm = time.Unix(smsDateInt64/1e3, 0)
+			}
+			record := new(models.SmsRecord)
+			record.CreateTime = time.Now()
+			record.ModifyTime = time.Now()
+			record.IsSend = 0
+			record.SmsDateTime = tm
+			record.Mobile = v.Address
+			record.SmsDate = v.Date
+			record.Body = v.Body
+			addList = append(addList, record)
 		}
-		record := new(models.SmsRecord)
-		record.CreateTime = time.Now()
-		record.ModifyTime = time.Now()
-		record.IsSend = 0
-		record.SmsDateTime = tm
-		record.Mobile = address
-		record.SmsDate = date
-		record.Body = body
-		_, err = models.AddSmsRecord(record)
+	}
+	if len(addList) > 0 {
+		_, err = models.AddSmsRecordMulti(addList)
 		if err != nil {
 			br.Msg = "失败,请联系技术"
 			br.ErrMsg = "新增记录失败;Err:" + err.Error()
 			return
 		}
-		isAdd = true
-	}
-	if isAdd {
+		time.Sleep(1 * time.Second)
 		go services.SendSms()
 	}
 	br.Ret = 200

+ 14 - 1
models/sms_record.go

@@ -16,13 +16,20 @@ type SmsRecord struct {
 	SmsDateTime time.Time
 }
 
-//新增视频
+//新增记录
 func AddSmsRecord(item *SmsRecord) (newId int64, err error) {
 	o := orm.NewOrm()
 	newId, err = o.Insert(item)
 	return
 }
 
+//批量新增记录
+func AddSmsRecordMulti(items []*SmsRecord) (newId int64, err error) {
+	o := orm.NewOrm()
+	newId, err = o.InsertMulti(1, items)
+	return
+}
+
 func GetSmsRecordCount(mobile, smsDate string) (count int, err error) {
 	o := orm.NewOrm()
 	sql := `SELECT COUNT(1) AS count
@@ -45,3 +52,9 @@ func GetSmsRecord() (items []*SmsRecord, err error) {
 	_, err = o.Raw(sql).QueryRows(&items)
 	return
 }
+
+type SmsReq struct {
+	Body    string
+	Address string
+	Date    string
+}