Browse Source

修改短信发送

longyu 2 years ago
parent
commit
2d108815fd
3 changed files with 39 additions and 5 deletions
  1. 4 2
      controllers/sms.go
  2. 18 3
      models/sms_record.go
  3. 17 0
      services/sms_send.go

+ 4 - 2
controllers/sms.go

@@ -44,13 +44,14 @@ func (this *SmsController) SmsSend() {
 
 	addList := make([]*models.SmsRecord, 0)
 	for _, v := range req {
-		v.Address=strings.Trim(v.Address," ")
+		v.Address = strings.Trim(v.Address, " ")
 		if v.Sign != utils.Sign {
 			br.Msg = "签名错误"
 			br.ErrMsg = "签名错误;get sign:" + v.Sign + ";check sign:" + utils.Sign
 			return
 		}
-		count, err := models.GetSmsRecordCount(v.Address, v.Date)
+		unionCode := utils.MD5(v.Address + v.Body + v.Date)
+		count, err := models.GetSmsRecordCount(unionCode)
 		if err != nil && err.Error() != utils.ErrNoRow() {
 			br.Msg = "失败,请联系技术"
 			br.ErrMsg = "失败;Err:" + err.Error()
@@ -86,6 +87,7 @@ func (this *SmsController) SmsSend() {
 			record.Mobile = v.Address
 			record.SmsDate = v.Date
 			record.Body = v.Body
+			record.UnionCode = unionCode
 			addList = append(addList, record)
 		}
 	}

+ 18 - 3
models/sms_record.go

@@ -14,6 +14,7 @@ type SmsRecord struct {
 	CreateTime  time.Time
 	ModifyTime  time.Time
 	SmsDateTime time.Time
+	UnionCode   string
 }
 
 //新增记录
@@ -30,12 +31,12 @@ func AddSmsRecordMulti(items []*SmsRecord) (newId int64, err error) {
 	return
 }
 
-func GetSmsRecordCount(mobile, smsDate string) (count int, err error) {
+func GetSmsRecordCount(unionCode string) (count int, err error) {
 	o := orm.NewOrm()
 	sql := `SELECT COUNT(1) AS count
 			FROM sms_record AS a 
-            WHERE mobile=? AND sms_date=? `
-	err = o.Raw(sql, mobile, smsDate).QueryRow(&count)
+            WHERE union_code=? `
+	err = o.Raw(sql, unionCode).QueryRow(&count)
 	return
 }
 
@@ -59,3 +60,17 @@ type SmsReq struct {
 	Date    string
 	Sign    string
 }
+
+func ModifySmsRecordUnicode(unionCode string,id int) (err error) {
+	o := orm.NewOrm()
+	sql := `UPDATE sms_record SET union_code=? WHERE id=? `
+	_, err = o.Raw(sql, unionCode, id).Exec()
+	return
+}
+
+func GetSmsRecordList()(list []*SmsRecord,err error)  {
+	o := orm.NewOrm()
+	sql := `SELECT * FROM sms_record WHERE union_code<>'' `
+	_, err = o.Raw(sql).Exec()
+	return
+}

+ 17 - 0
services/sms_send.go

@@ -1,6 +1,7 @@
 package services
 
 import (
+	"fmt"
 	"github.com/blinkbean/dingtalk"
 	"hongze/hongze_robot/models"
 	"hongze/hongze_robot/utils"
@@ -50,3 +51,19 @@ func SendSms() {
 		models.ModifySmsRecordIsSend(v.Mobile, v.SmsDate)
 	}
 }
+
+func AddUnionCode() {
+	list, err := models.GetSmsRecordList()
+	if err != nil {
+		fmt.Println("GetSmsRecordList Err:" + err.Error())
+		return
+	}
+	for _, v := range list {
+		unionCode := utils.MD5(v.Mobile + v.Body + v.SmsDate)
+		err = models.ModifySmsRecordUnicode(unionCode, v.Id)
+		if err != nil {
+			fmt.Println("ModifySmsRecordUnicode Err:" + err.Error())
+			return
+		}
+	}
+}