sms.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package services
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "eta/eta_mini_api/utils"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "net/http"
  10. "net/url"
  11. )
  12. func SendSmsCode(mobile, verifyCode string) (sendOk bool, result string, err error) {
  13. defer func() {
  14. if err != nil {
  15. tips := fmt.Sprintf("SendSmsCode err: %v, result: %s", err, result)
  16. utils.FileLog.Info(tips)
  17. }
  18. }()
  19. tplId := utils.SMS_TPLID
  20. if tplId == "" {
  21. tplId = "262642"
  22. }
  23. expiresTime := "15"
  24. postResult, e := sendSms(mobile, tplId, verifyCode, expiresTime)
  25. if e != nil {
  26. err = fmt.Errorf("post sms err: %v", e)
  27. return
  28. }
  29. result = string(postResult)
  30. var netReturn map[string]interface{}
  31. if e = json.Unmarshal(postResult, &netReturn); e != nil {
  32. err = fmt.Errorf("result unmarshal err: %v", e)
  33. return
  34. }
  35. errCode, ok := netReturn["error_code"].(float64)
  36. if !ok {
  37. err = fmt.Errorf("err code err")
  38. return
  39. }
  40. if errCode != 0 {
  41. err = fmt.Errorf("短信验证码发送失败")
  42. return
  43. }
  44. sendOk = true
  45. return
  46. }
  47. func sendSms(mobile, tplId, code, expirdTime string) (rs []byte, err error) {
  48. var Url *url.URL
  49. apiURL := "http://v.juhe.cn/sms/send"
  50. //初始化参数
  51. param := url.Values{}
  52. //配置请求参数,方法内部已处理urlencode问题,中文参数可以直接传参
  53. param.Set("mobile", mobile) //接受短信的用户手机号码
  54. param.Set("tpl_id", tplId) //您申请的短信模板ID,根据实际情况修改
  55. param.Set("tpl_value", "#code#="+code+"&"+"#m#="+expirdTime) //您设置的模板变量,根据实际情况
  56. param.Set("key", utils.JhGnAppKey) //应用APPKEY(应用详细页查询)
  57. Url, err = url.Parse(apiURL)
  58. if err != nil {
  59. fmt.Printf("解析url错误:\r\n%v", err)
  60. return nil, err
  61. }
  62. //如果参数中有中文参数,这个方法会进行URLEncode
  63. Url.RawQuery = param.Encode()
  64. resp, err := http.Get(Url.String())
  65. if err != nil {
  66. fmt.Println("err:", err)
  67. return nil, err
  68. }
  69. defer resp.Body.Close()
  70. return ioutil.ReadAll(resp.Body)
  71. }
  72. const (
  73. SmsApiServiceProviderDefault = "1" // 默认短信服务商-金慧盈通
  74. )
  75. type SmsApiPars struct {
  76. PhoneList []string `json:"phonelist" description:"收信手机号码,最多50个一次"`
  77. ServiceProvider string `json:"serviceprovider" description:"短信服务提供商:1-金慧盈通;2-高斯通"`
  78. SmsContent string `json:"smscontent" description:"短信内容"`
  79. Sender string `json:"sender" description:"发送人"`
  80. Department string `json:"department" description:"发送人部门"`
  81. Purpose string `json:"purpose" description:"用途"`
  82. }
  83. func PostSmsApi(params SmsApiPars) (result string, err error) {
  84. requestUrl := utils.SmsApiUrl
  85. if requestUrl == "" {
  86. err = fmt.Errorf("短信API地址未配置")
  87. return
  88. }
  89. jsonData, e := json.Marshal(params)
  90. if e != nil {
  91. err = fmt.Errorf("json marshal err: %v", e)
  92. return
  93. }
  94. resp, e := http.Post(requestUrl, "application/json", bytes.NewBuffer(jsonData))
  95. if e != nil {
  96. err = fmt.Errorf("http post err: %v", e)
  97. return
  98. }
  99. defer resp.Body.Close()
  100. body, e := io.ReadAll(resp.Body)
  101. if e != nil {
  102. err = fmt.Errorf("read body err: %v", e)
  103. return
  104. }
  105. result = string(body)
  106. return
  107. }