|
@@ -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))
|
|
|
-}
|