phone.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package Utils
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io/ioutil"
  6. "log"
  7. "net/http"
  8. "regexp"
  9. )
  10. func MobileFormat(str string) string {
  11. re, _ := regexp.Compile("(\\d{3})(\\d{6})(\\d{2})")
  12. return re.ReplaceAllString(str, "$1******$3")
  13. }
  14. func SendMessage(form string, phone string, info string) bool {
  15. status := true
  16. if form == "" || phone == "" || info == "" {
  17. status = false
  18. return status
  19. }
  20. desc := ""
  21. if form == "express" {
  22. desc = "【GEEKROS】Hi," + info + " ,你在GEEKROS的订单已经发货,请留意快递信息,及时查收。"
  23. }
  24. if form == "account" {
  25. desc = "【GEEKROS】你的验证码为:" + info + " ,有效期10分钟,工作人员绝不会索取此验证码,切勿告知他人。"
  26. }
  27. apiUrl := "https://smssh1.253.com/msg/v1/send/json"
  28. params := make(map[string]interface{})
  29. params["account"] = ""
  30. params["password"] = ""
  31. params["phone"] = phone
  32. params["msg"] = desc
  33. params["report"] = "false"
  34. bytesData, err := json.Marshal(params)
  35. if err != nil {
  36. status = false
  37. return status
  38. }
  39. reader := bytes.NewReader(bytesData)
  40. request, err := http.NewRequest("POST", apiUrl, reader)
  41. if err != nil {
  42. status = false
  43. return status
  44. }
  45. request.Header.Set("Content-Type", "application/json;charset=UTF-8")
  46. client := http.Client{}
  47. resp, err := client.Do(request)
  48. if err != nil {
  49. status = false
  50. return status
  51. }
  52. respBytes, err := ioutil.ReadAll(resp.Body)
  53. if err != nil {
  54. status = false
  55. return status
  56. }
  57. log.Println("[PhoneMessage]", string(respBytes))
  58. return true
  59. }