Message_Sand.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package lib
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "math/rand"
  8. "mime/multipart"
  9. "net/http"
  10. "time"
  11. )
  12. const (
  13. API = "https://api-v4.mysubmail.com/sms/xsend"
  14. APPID = "100848***"
  15. APPKEY = "e9b9802ea6445b2a5ad27203322d733a"
  16. )
  17. func Message_Sand_Verify(PhoneNumbers string) string {
  18. rand.Seed(time.Now().UnixNano())
  19. // 生成4位数验证码
  20. var code string
  21. for i := 0; i < 4; i++ {
  22. code += fmt.Sprintf("%d", rand.Intn(10)) // 生成0-9之间的随机数并转换为字符串拼接到验证码中
  23. }
  24. vars := make(map[string]string)
  25. vars["code"] = code
  26. postdata := make(map[string]string)
  27. postdata["appid"] = APPID
  28. postdata["signature"] = APPKEY
  29. postdata["project"] = "7qCjA2"
  30. postdata["to"] = PhoneNumbers
  31. bs, _ := json.Marshal(vars)
  32. postdata["vars"] = string(bs)
  33. body := &bytes.Buffer{}
  34. writer := multipart.NewWriter(body)
  35. for key, val := range postdata {
  36. _ = writer.WriteField(key, val)
  37. }
  38. contentType := writer.FormDataContentType()
  39. writer.Close()
  40. resp, _ := http.Post(API, contentType, body)
  41. result, _ := ioutil.ReadAll(resp.Body)
  42. fmt.Println(string(result))
  43. return code
  44. }
  45. // println(lib.Message_Sand("18777951277","SMS_215350603","{\"code\":\"1111\"}"))