소스 검색

feat:集成云片

wangchuanjin 3 년 전
부모
커밋
e507286f24
11개의 변경된 파일287개의 추가작업 그리고 179개의 파일을 삭제
  1. BIN
      src/Sms_Service
  2. 21 9
      src/config.json
  3. 33 0
      src/config/config.go
  4. 8 0
      src/logs/sms.log
  5. 16 93
      src/main.go
  6. 3 6
      src/main_test.go
  7. 41 0
      src/service/service.go
  8. 93 0
      src/service/weiwangtonglian.go
  9. 72 0
      src/service/yunpian.go
  10. BIN
      src/test/ddd.exe
  11. 0 71
      src/test/main.go

BIN
src/Sms_Service


+ 21 - 9
src/config.json

@@ -1,15 +1,27 @@
 {
 	"port":"932",
-	"api":"https://api.51welink.com/EncryptionSubmit/SendTemplateSms.ashx",
-	"accountId":"dljianyu0",
-	"password":"Jianyubx20220315",
-	"productId":1012888,
-	"fixEncryptKey":"SMmsEncryptKey",
-	"tempCode":"134220",
-	"poolSize":20,
+	"model":1,
 	"reTry":3,
-	"idToCode":{
-		"01":134220
+	"poolSize":10,
+	"weiwangtonglian":{
+		"url":"https://api.51welink.com/EncryptionSubmit/SendTemplateSms.ashx",
+		"accountId":"dljianyu0",
+		"password":"Jianyubx20220315",
+		"productId":1012888,
+		"fixEncryptKey":"SMmsEncryptKey",
+		"idToTpl":{
+			"01":134220
+		}
+	},
+	"yunpian":{
+		"url":"https://sms.yunpian.com/v2/sms/tpl_batch_send.json",
+		"key":"950090a3dfefb4f821ed8f0221482fcd",
+		"idToTpl":{
+			"01":{
+				"id":2828060,
+				"variable":["code"]
+			}
+		}
 	},
 	"warn":"http://172.17.4.195:19281/_send/_mail?program=Sms_Service&to=wangchuanjin@topnet.net.cn,renzheng@topnet.net.cn,zhaoyujian@topnet.net.cn&title=短信服务程序报警&body=%s"
 }

+ 33 - 0
src/config/config.go

@@ -0,0 +1,33 @@
+package config
+
+import "qfw/util"
+
+var Config *config
+
+type config struct {
+	Port            string
+	Model           int
+	ReTry           int
+	PoolSize        int
+	Weiwangtonglian struct {
+		Url           string
+		AccountId     string
+		Password      string
+		ProductId     int
+		FixEncryptKey string
+		IdToTpl       map[string]int
+	}
+	Yunpian struct {
+		Url     string
+		Key     string
+		IdToTpl map[string]struct {
+			Id       int
+			Variable []string
+		}
+	}
+	Warn string
+}
+
+func init() {
+	util.ReadConfig(&Config)
+}

+ 8 - 0
src/logs/sms.log

@@ -21,3 +21,11 @@
 2022/03/16 13:20:21 main.go:84: info  req data: &{Id:01 Phones:15037870765 Params:[5]}
 2022/03/16 13:20:21 main.go:114: info  返回值: {"Result":"succ","Reason":"提交成功","MsgId":2203161320217202597,"SplitCount":1}
 2022/03/16 13:20:21 main.go:122: info  succ 提交成功  1
+2022/03/17 13:29:21 main.go:64: info  req data: &{Id:01 Phones:15037870765 Params:[4]}
+2022/03/17 13:29:21 service.go:37: info  微网通联 返回值: {"Result":"succ","Reason":"提交成功","MsgId":2203171329210092797,"SplitCount":1}
+2022/03/17 13:33:14 main.go:64: info  req data: &{Id:01 Phones:15037870765 Params:[133314]}
+2022/03/17 13:33:14 yunpian.go:24: error  无效的参数id,没有找到对应的tpl
+2022/03/17 13:37:26 main.go:64: info  req data: &{Id:01 Phones:15037870765 Params:[133726]}
+2022/03/17 13:37:27 service.go:37: info  云片 返回值: {"total_count":1,"total_fee":"0.0480","unit":"RMB","data":[{"code":0,"count":1,"fee":0.048,"mobile":"15037870765","msg":"发送成功","sid":71170988266,"unit":"RMB"}]}
+2022/03/17 13:39:47 main.go:64: info  req data: &{Id:01 Phones:1503787076 Params:[133947]}
+2022/03/17 13:39:48 service.go:37: info  云片 返回值: {"total_count":0,"total_fee":"0.0000","unit":"RMB","data":[{"http_status_code":400,"code":2,"msg":"请求参数格式错误","count":"0","fee":"0","unit":"RMB","mobile":"1503787076","sid":"0"}]}

+ 16 - 93
src/main.go

@@ -1,56 +1,34 @@
 package main
 
 import (
-	"bytes"
-	"crypto/md5"
-	"crypto/sha256"
+	. "config"
 	"encoding/json"
 	"fmt"
-	"io/ioutil"
 	"log"
-	"math/rand"
 	"net"
 	"net/http"
 	"net/rpc"
-	"net/url"
 	"qfw/util"
-	"strings"
-	"time"
+	. "service"
 
 	"github.com/donnie4w/go-logger/logger"
 )
 
-type Config struct {
-	Port          string
-	Api           string
-	AccountId     string
-	Password      string
-	ProductId     int
-	FixEncryptKey string
-	TempCode      string
-	PoolSize      int
-	ReTry         int
-	IdToCode      map[string]int
-	Warn          string
-}
-
 //
 type Sms struct {
 }
 
-//
-type ReqData struct {
-	Id     string
-	Phones string
-	Params []string
-}
-
-var config *Config
-var pool chan bool
+var pool = make(chan bool, Config.PoolSize)
+var service Service
 
 func init() {
-	util.ReadConfig(&config)
-	pool = make(chan bool, config.PoolSize)
+	if Config.Model == 1 {
+		service = Weiwangtonglian
+	} else if Config.Model == 2 {
+		service = Yunpian
+	} else {
+		log.Fatalln("无效的model配置项", Config.Model)
+	}
 }
 
 //
@@ -61,86 +39,31 @@ func main() {
 	rpc.Register(frpc)
 	rpc.HandleHTTP()
 	//处理链接
-	listen, err := net.Listen("tcp", ":"+config.Port)
+	listen, err := net.Listen("tcp", ":"+Config.Port)
 	if err != nil {
 		log.Println(err)
 	} else {
-		log.Println("rpc server is listening", config.Port)
+		log.Println("rpc server is listening", Config.Port)
 	}
 	http.Serve(listen, nil)
 }
 
-//
+//提交模板短信
 func (s *Sms) Execute(param *[]byte, ret *string) error {
 	go func() {
-		defer util.Catch()
 		pool <- true
 		defer func() {
 			<-pool
 		}()
+		defer util.Catch()
 		var reqData *ReqData
 		if err := json.Unmarshal(*param, &reqData); err != nil {
 			logger.Error(err)
 			return
 		}
 		logger.Info("req data:", fmt.Sprintf("%+v", reqData))
-		TempCode := config.IdToCode[reqData.Id]
-		if TempCode == 0 {
-			logger.Error("无效的参数id,没有找到TempCode")
-			return
-		}
-		r := rand.New(rand.NewSource(time.Now().UnixNano()))
-		randInt := r.Intn(999999) + 5
-		Timestamp := time.Now().Unix()
-		m5 := strings.ToUpper(Mmd5(config.Password + config.FixEncryptKey))
-		m256 := Msha256(fmt.Sprintf(`AccountId=%s&PhoneNos=%s&Password=%s&Random=%d&TempCode=%d&Timestamp=%d`, config.AccountId, strings.Split(reqData.Phones, ",")[0], m5, randInt, TempCode, Timestamp))
-		pb, _ := json.Marshal(map[string]interface{}{
-			"AccountId":  config.AccountId,
-			"AccessKey":  m256,
-			"Timestamp":  Timestamp,
-			"Random":     randInt,
-			"ProductId":  config.ProductId,
-			"PhoneNos":   reqData.Phones,
-			"TempCode":   TempCode,
-			"TempParams": reqData.Params,
-		})
-		for i := 1; i <= config.ReTry; i++ {
-			resp, err := http.Post(config.Api, "application/json", bytes.NewReader(pb))
-			if err != nil {
-				logger.Error("第", i, "次请求出错", err)
-				if i == config.ReTry {
-					go http.Get(fmt.Sprintf(config.Warn, reqData.Phones+",调用接口超时"))
-				}
-				continue
-			}
-			defer resp.Body.Close()
-			b, _ := ioutil.ReadAll(resp.Body)
-			logger.Info("返回值:", string(b))
-			var result struct {
-				Result     string
-				Reason     string
-				MsgId      string
-				SplitCount int
-			}
-			json.Unmarshal(b, &result)
-			if result.Result != "succ" {
-				go http.Get(fmt.Sprintf(config.Warn, url.QueryEscape(result.Reason)))
-			}
-			return
-		}
+		service.Execute(reqData)
 	}()
 	*ret = "y"
 	return nil
 }
-
-func Msha256(val string) string {
-	sh := sha256.New()
-	sh.Write([]byte(val))
-	return fmt.Sprintf("%x", sh.Sum(nil))
-}
-
-func Mmd5(val string) string {
-	m5 := md5.New()
-	m5.Write([]byte(val))
-	return fmt.Sprintf("%x", m5.Sum(nil))
-}

+ 3 - 6
src/main_test.go

@@ -2,12 +2,10 @@ package main
 
 import (
 	"encoding/json"
-	"fmt"
 	"log"
-	"math/rand"
 	"net/rpc"
+	"qfw/util"
 	"testing"
-	"time"
 )
 
 func Test_Main(t *testing.T) {
@@ -18,11 +16,10 @@ func Test_Main(t *testing.T) {
 		return
 	}
 	defer client.Close()
-	r := rand.New(rand.NewSource(time.Now().UnixNano()))
 	b, _ := json.Marshal(map[string]interface{}{
 		"id":     "01",
-		"phones": "15037870765",
-		"params": []string{fmt.Sprint(r.Intn(6))},
+		"phones": "1503787076",
+		"params": []string{util.NowFormat("150405")},
 	})
 	err = client.Call("Sms.Execute", b, &repl)
 	if err != nil {

+ 41 - 0
src/service/service.go

@@ -0,0 +1,41 @@
+package service
+
+import (
+	. "config"
+	"fmt"
+	"io"
+	"io/ioutil"
+	"net/http"
+
+	"github.com/donnie4w/go-logger/logger"
+)
+
+type Service interface {
+	Execute(reqData *ReqData)
+}
+
+//
+type ReqData struct {
+	Id     string
+	Phones string
+	Params []string
+}
+
+//
+func Post(url, contentType string, body io.Reader, t string) ([]byte, error) {
+	for i := 1; i <= Config.ReTry; i++ {
+		resp, err := http.Post(url, contentType, body)
+		if err != nil {
+			logger.Error(t, "第", i, "次请求出错", err)
+			if i == Config.ReTry {
+				go http.Get(fmt.Sprintf(Config.Warn, "调用"+t+"接口超时"))
+			}
+			continue
+		}
+		defer resp.Body.Close()
+		b, _ := ioutil.ReadAll(resp.Body)
+		logger.Info(t, "返回值:", string(b))
+		return b, nil
+	}
+	return nil, nil
+}

+ 93 - 0
src/service/weiwangtonglian.go

@@ -0,0 +1,93 @@
+package service
+
+import (
+	"bytes"
+	. "config"
+	"crypto/md5"
+	"crypto/sha256"
+	"encoding/json"
+	"fmt"
+	"math/rand"
+	"net/http"
+	"net/url"
+	"strings"
+	"time"
+
+	"github.com/donnie4w/go-logger/logger"
+)
+
+//微网通联短信平台
+var Weiwangtonglian = &weiwangtonglian{
+	t: "微网通联",
+}
+
+type weiwangtonglian struct {
+	t string
+}
+
+//提交模板短信
+func (w *weiwangtonglian) Execute(reqData *ReqData) {
+	TempCode := Config.Weiwangtonglian.IdToTpl[reqData.Id]
+	if TempCode == 0 {
+		logger.Error("无效的参数id,没有找到对应的tpl")
+		return
+	}
+	r := rand.New(rand.NewSource(time.Now().UnixNano()))
+	randInt := r.Intn(999999) + 5
+	Timestamp := time.Now().Unix()
+	m5 := strings.ToUpper(w.Mmd5(Config.Weiwangtonglian.Password + Config.Weiwangtonglian.FixEncryptKey))
+	m256 := w.Msha256(fmt.Sprintf(`AccountId=%s&PhoneNos=%s&Password=%s&Random=%d&TempCode=%d&Timestamp=%d`, Config.Weiwangtonglian.AccountId, strings.Split(reqData.Phones, ",")[0], m5, randInt, TempCode, Timestamp))
+	pb, _ := json.Marshal(map[string]interface{}{
+		"AccountId":  Config.Weiwangtonglian.AccountId,
+		"AccessKey":  m256,
+		"Timestamp":  Timestamp,
+		"Random":     randInt,
+		"ProductId":  Config.Weiwangtonglian.ProductId,
+		"PhoneNos":   reqData.Phones,
+		"TempCode":   TempCode,
+		"TempParams": reqData.Params,
+	})
+	b, err := Post(Config.Weiwangtonglian.Url, "application/json", bytes.NewReader(pb), w.t)
+	if err != nil {
+		return
+	}
+	var result struct {
+		Result     string
+		Reason     string
+		MsgId      string
+		SplitCount int
+	}
+	json.Unmarshal(b, &result)
+	if result.Result != "succ" {
+		go http.Get(fmt.Sprintf(Config.Warn, url.QueryEscape(reqData.Phones+":"+result.Reason)))
+	}
+}
+
+func (w *weiwangtonglian) Msha256(val string) string {
+	sh := sha256.New()
+	sh.Write([]byte(val))
+	return fmt.Sprintf("%x", sh.Sum(nil))
+}
+
+func (w *weiwangtonglian) Mmd5(val string) string {
+	m5 := md5.New()
+	m5.Write([]byte(val))
+	return fmt.Sprintf("%x", m5.Sum(nil))
+}
+
+//删除模板(短信/视频短信)
+func (w *weiwangtonglian) delTemplate() {
+	r := rand.New(rand.NewSource(time.Now().UnixNano()))
+	randInt := r.Intn(999999) + 5
+	Timestamp := time.Now().Unix()
+	m5 := strings.ToUpper(w.Mmd5(Config.Weiwangtonglian.Password + Config.Weiwangtonglian.FixEncryptKey))
+	m256 := w.Msha256(fmt.Sprintf(`AccountId=%s&Password=%s&Random=%d&Timestamp=%d`, Config.Weiwangtonglian.AccountId, m5, randInt, Timestamp))
+	pb, _ := json.Marshal(map[string]interface{}{
+		"AccountId": Config.Weiwangtonglian.AccountId,
+		"AccessKey": m256,
+		"Timestamp": Timestamp,
+		"Random":    randInt,
+		"TempCode":  134219,
+	})
+	Post("https://api.51welink.com/EncryptionQuery/DelTemplate.ashx", "application/json", bytes.NewReader(pb), w.t)
+}

+ 72 - 0
src/service/yunpian.go

@@ -0,0 +1,72 @@
+package service
+
+import (
+	. "config"
+	"encoding/json"
+	"fmt"
+	"net/http"
+	"net/url"
+	"strings"
+
+	"github.com/donnie4w/go-logger/logger"
+)
+
+//云片短信平台
+var Yunpian = &yunpian{
+	t: "云片",
+}
+
+type yunpian struct {
+	t string
+}
+
+func (y *yunpian) Execute(reqData *ReqData) {
+	idToTpl := Config.Yunpian.IdToTpl[reqData.Id]
+	if idToTpl.Id == 0 {
+		logger.Error("无效的参数id,没有找到对应的tpl")
+		return
+	}
+	array := []string{}
+	for k, v := range reqData.Params {
+		if k >= len(idToTpl.Variable) {
+			break
+		}
+		array = append(array, "#"+idToTpl.Variable[k]+"#="+v)
+	}
+	content, _ := url.Parse(strings.Join(array, "&"))
+	param := url.Values{}
+	param.Add("apikey", Config.Yunpian.Key)
+	param.Add("mobile", reqData.Phones)
+	param.Add("tpl_id", fmt.Sprint(idToTpl.Id))
+	param.Add("tpl_value", content.String())
+	b, err := Post(Config.Yunpian.Url, "application/x-www-form-urlencoded", strings.NewReader(param.Encode()), y.t)
+	if err != nil {
+		return
+	}
+	var result struct {
+		Total_count int
+		Total_fee   string
+		Unit        string
+		Data        []struct {
+			Code   int
+			Msg    string
+			Count  int
+			Fee    float64
+			Unit   string
+			Mobile string
+			Sid    int
+		}
+	}
+	json.Unmarshal(b, &result)
+	if result.Total_count != len(strings.Split(reqData.Phones, ",")) {
+		msg := []string{}
+		for _, v := range result.Data {
+			if v.Code == 0 {
+				continue
+			}
+			msg = append(msg, v.Mobile+":"+v.Msg)
+		}
+		go http.Get(fmt.Sprintf(Config.Warn, url.QueryEscape(strings.Join(msg, ","))))
+	}
+
+}

BIN
src/test/ddd.exe


+ 0 - 71
src/test/main.go

@@ -1,71 +0,0 @@
-package main
-
-import (
-	"bytes"
-	"context"
-	"crypto/md5"
-	"crypto/sha256"
-	"encoding/json"
-	"fmt"
-	"io/ioutil"
-	"log"
-	"math/rand"
-	"net/http"
-	"strings"
-	"time"
-)
-
-var (
-	url       = `https://api.51welink.com/EncryptionSubmit/SendTemplateSms.ashx`
-	AccountId = "dljianyu0"
-	pwd       = "Jianyubx20220315"
-	ProductId = 1012888
-	ctx       = context.Background()
-	fix       = "SMmsEncryptKey"
-)
-
-func main() {
-	content := "您的验证码:2598。请勿泄露。【数字本草】"
-	Send("15037870765", content)
-
-}
-
-func Send(phone, content string) {
-	r := rand.New(rand.NewSource(time.Now().UnixNano()))
-	randInt := r.Intn(999999) + 5
-	Timestamp := time.Now().Unix()
-	m5 := strings.ToUpper(Mmd5(pwd + fix))
-	TempCode := "134220"
-	m256 := Msha256(fmt.Sprintf(`AccountId=%s&PhoneNos=%s&Password=%s&Random=%d&TempCode=%s&Timestamp=%d`, AccountId, phone, m5, randInt, TempCode, Timestamp))
-	log.Println(m5, randInt, Timestamp, m256, fmt.Sprintf(`AccountId=%s&PhoneNos=%s&Password=%s&Random=%d&TempCode=%s&Timestamp=%d`, AccountId, phone, m5, randInt, TempCode, Timestamp))
-	pb, _ := json.Marshal(map[string]interface{}{
-		"AccountId":  AccountId,
-		"AccessKey":  m256,
-		"Timestamp":  Timestamp,
-		"Random":     randInt,
-		"ProductId":  ProductId,
-		"PhoneNos":   phone,
-		"TempCode":   134220,
-		"TempParams": []string{fmt.Sprint(r.Intn(6))},
-	})
-	resp, err := http.Post(url, "application/json", bytes.NewReader(pb))
-	if err != nil {
-		log.Println(err)
-		return
-	}
-	defer resp.Body.Close()
-	b, _ := ioutil.ReadAll(resp.Body)
-	log.Println("返回值:", string(b))
-}
-
-func Msha256(val string) string {
-	sh := sha256.New()
-	sh.Write([]byte(val))
-	return fmt.Sprintf("%x", sh.Sum(nil))
-}
-
-func Mmd5(val string) string {
-	m5 := md5.New()
-	m5.Write([]byte(val))
-	return fmt.Sprintf("%x", m5.Sum(nil))
-}