package notice import ( "bytes" "encoding/json" "errors" "fmt" "io/ioutil" "net/http" ) // SendChineseSMS 发送国内手机验证码 func SendChineseSMS(user, pass, mobile, content string) error { params := make(map[string]interface{}) params["account"] = user params["password"] = pass params["phone"] = mobile params["msg"] = content params["report"] = true bytesData, err := json.Marshal(params) if err != nil { return err } reader := bytes.NewReader(bytesData) url := "http://smssh1.253.com/msg/send/json" request, err := http.NewRequest("POST", url, reader) if err != nil { return err } request.Header.Set("Content-Type", "application/json;charset=UTF-8") client := http.Client{} resp, err := client.Do(request) if err != nil { return err } defer resp.Body.Close() respBytes, err := ioutil.ReadAll(resp.Body) if err != nil { return err } result := struct { Code string `json:"code"` MsgID string `json:"msgId"` Time string `json:"time"` ErrorMsg string `json:"errorMsg"` }{} err = json.Unmarshal(respBytes, &result) if err != nil { return err } if result.Code != "0" { switch result.Code { case "103": return fmt.Errorf("one verification code per minute per phone") case "107": return fmt.Errorf("phone number format is not correct") case "135": return fmt.Errorf("up to 5 verification codes a day") default: return fmt.Errorf(result.ErrorMsg) } } return nil } // SendForeignSMS 发送国外手机验证码 func SendForeignSMS(user, pass, mobile, content string) error { params := make(map[string]interface{}) params["account"] = user params["password"] = pass params["mobile"] = mobile params["msg"] = content params["report"] = true bytesData, err := json.Marshal(params) if err != nil { return err } reader := bytes.NewReader(bytesData) url := "http://intapi.253.com/send/json" request, err := http.NewRequest("POST", url, reader) if err != nil { return err } request.Header.Set("Content-Type", "application/json;charset=UTF-8") client := http.Client{} resp, err := client.Do(request) if err != nil { return err } defer resp.Body.Close() respBytes, err := ioutil.ReadAll(resp.Body) if err != nil { return err } result := struct { Code string `json:"code"` Error string `json:"error"` MsgID string `json:"msgid"` }{} err = json.Unmarshal(respBytes, &result) if err != nil { return err } if result.Code != "0" { return errors.New(result.Error) } return nil }