Browse Source

change sms port

Paul 4 years ago
parent
commit
3e77a10fef
1 changed files with 71 additions and 26 deletions
  1. 71
    26
      notice/sms.go

+ 71
- 26
notice/sms.go View File

@@ -1,83 +1,128 @@
1 1
 package notice
2 2
 
3 3
 import (
4
-	"encoding/xml"
4
+	"bytes"
5
+	"encoding/json"
6
+	"errors"
5 7
 	"fmt"
6 8
 	"io/ioutil"
7 9
 	"net/http"
8 10
 	"net/url"
9
-	"strings"
10 11
 )
11 12
 
12
-func SendChineseSMS(host, user, pass, mobile, content string) error {
13
-	client := &http.Client{}
14
-	req, err := http.NewRequest("POST", host, strings.NewReader("account="+user+"&password="+pass+"&mobile="+mobile+"&content="+content))
13
+// SendChineseSMS 发送国内手机验证码
14
+func SendChineseSMS(user, pass, mobile, content string) error {
15
+	params := make(map[string]interface{})
16
+	params["account"] = user
17
+	params["password"] = pass
18
+	params["phone"] = mobile
19
+	params["msg"] = url.QueryEscape(content)
20
+	params["report"] = true
21
+
22
+	bytesData, err := json.Marshal(params)
23
+	if err != nil {
24
+		return err
25
+	}
26
+
27
+	reader := bytes.NewReader(bytesData)
28
+	url := "http://smssh1.253.com/msg/send/json"
29
+	request, err := http.NewRequest("POST", url, reader)
15 30
 	if err != nil {
16 31
 		return err
17 32
 	}
18 33
 
19
-	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
34
+	request.Header.Set("Content-Type", "application/json;charset=UTF-8")
20 35
 
21
-	resp, err := client.Do(req)
36
+	client := http.Client{}
37
+	resp, err := client.Do(request)
22 38
 	if err != nil {
23 39
 		return err
24 40
 	}
25 41
 
26 42
 	defer resp.Body.Close()
27 43
 
28
-	body, err := ioutil.ReadAll(resp.Body)
44
+	respBytes, err := ioutil.ReadAll(resp.Body)
29 45
 	if err != nil {
30 46
 		return err
31 47
 	}
32 48
 
33 49
 	result := struct {
34
-		Code int    `xml:"code"`
35
-		Msg  string `xml:"msg"`
50
+		Code     string `json:"code"`
51
+		MsgID    string `json:"msgId"`
52
+		Time     string `json:"time"`
53
+		ErrorMsg string `json:"errorMsg"`
36 54
 	}{}
37 55
 
38
-	err = xml.Unmarshal(body, &result)
56
+	err = json.Unmarshal(respBytes, &result)
39 57
 	if err != nil {
40 58
 		return err
41 59
 	}
42 60
 
43
-	if result.Code != 2 {
61
+	if result.Code != "0" {
44 62
 		switch result.Code {
45
-		case 406, 4030:
46
-			return fmt.Errorf("phone number format is not correct")
47
-		case 4072:
48
-			return fmt.Errorf("your message must match the templat")
49
-		case 4081:
63
+		case "103":
50 64
 			return fmt.Errorf("one verification code per minute per phone")
51
-		case 4085:
65
+		case "107":
66
+			return fmt.Errorf("phone number format is not correct")
67
+		case "135":
52 68
 			return fmt.Errorf("up to 5 verification codes a day")
53 69
 		default:
54
-			return fmt.Errorf("send sms faild")
70
+			return fmt.Errorf(result.ErrorMsg)
55 71
 		}
56 72
 	}
57 73
 
58 74
 	return nil
59 75
 }
60 76
 
77
+// SendForeignSMS 发送国外手机验证码
61 78
 func SendForeignSMS(user, pass, mobile, content string) error {
62
-	content = url.QueryEscape(content)
63
-	url := "http://222.73.117.140:8044/mt?dc=15&tf=3&rf=1&un=" + user + "&pw=" + pass + "&da=" + mobile + "&sm=" + content
79
+	params := make(map[string]interface{})
80
+	params["account"] = user
81
+	params["password"] = pass
82
+	params["mobile"] = mobile
83
+	params["msg"] = url.QueryEscape(content)
84
+	params["report"] = true
85
+
86
+	bytesData, err := json.Marshal(params)
87
+	if err != nil {
88
+		return err
89
+	}
64 90
 
65
-	resp, err := http.Get(url)
91
+	reader := bytes.NewReader(bytesData)
92
+	url := "http://intapi.253.com/send/json"
93
+	request, err := http.NewRequest("POST", url, reader)
94
+	if err != nil {
95
+		return err
96
+	}
97
+
98
+	request.Header.Set("Content-Type", "application/json;charset=UTF-8")
99
+
100
+	client := http.Client{}
101
+	resp, err := client.Do(request)
66 102
 	if err != nil {
67 103
 		return err
68 104
 	}
69 105
 
70 106
 	defer resp.Body.Close()
71 107
 
72
-	body, err := ioutil.ReadAll(resp.Body)
108
+	respBytes, err := ioutil.ReadAll(resp.Body)
73 109
 	if err != nil {
74 110
 		return err
75 111
 	}
76 112
 
77
-	result := string(body)
113
+	result := struct {
114
+		Code  string `json:"code"`
115
+		Error string `json:"error"`
116
+		MsgID string `json:"msgid"`
117
+	}{}
118
+
119
+	err = json.Unmarshal(respBytes, &result)
120
+	if err != nil {
121
+		return err
122
+	}
78 123
 
79
-	if !strings.Contains(result, "id") {
80
-		return fmt.Errorf("phone number format is not correct")
124
+	if result.Code != "0" {
125
+		return errors.New(result.Error)
81 126
 	}
82 127
 
83 128
 	return nil