package lib import ( "bytes" "encoding/json" "fmt" "io/ioutil" "math/rand" "mime/multipart" "net/http" "time" ) const ( API = "https://api-v4.mysubmail.com/sms/xsend" APPID = "100848***" APPKEY = "e9b9802ea6445b2a5ad27203322d733a" ) func Message_Sand_Verify(PhoneNumbers string) string { rand.Seed(time.Now().UnixNano()) // 生成4位数验证码 var code string for i := 0; i < 4; i++ { code += fmt.Sprintf("%d", rand.Intn(10)) // 生成0-9之间的随机数并转换为字符串拼接到验证码中 } vars := make(map[string]string) vars["code"] = code postdata := make(map[string]string) postdata["appid"] = APPID postdata["signature"] = APPKEY postdata["project"] = "7qCjA2" postdata["to"] = PhoneNumbers bs, _ := json.Marshal(vars) postdata["vars"] = string(bs) body := &bytes.Buffer{} writer := multipart.NewWriter(body) for key, val := range postdata { _ = writer.WriteField(key, val) } contentType := writer.FormDataContentType() writer.Close() resp, _ := http.Post(API, contentType, body) result, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(result)) return code } // println(lib.Message_Sand("18777951277","SMS_215350603","{\"code\":\"1111\"}"))