短信发送方法
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.7 KiB

  1. package sms
  2. import (
  3. "errors"
  4. "git.tetele.net/tgo/helper"
  5. "log"
  6. "time"
  7. )
  8. /**
  9. * 企业信息服务平台--一信通短信健帆用
  10. * @spCode 企业编号
  11. * @appKey 登录的用户名
  12. * @secretKey 接口业务--接口申请里的秘钥
  13. * @templateId 短信模板id
  14. * @mobiles 手机号多个用逗号隔开
  15. * @content 短信内容
  16. */
  17. func SendByUms(spCode, appKey, secretKey, mobiles, templateId, content string) (bool, error) {
  18. if spCode == "" || appKey == "" || secretKey == "" || mobiles == "" || templateId == "" || content == "" {
  19. return false, errors.New("参数不全")
  20. }
  21. api := "https://api.ums86.com/api/sms/send"
  22. timestamp := helper.ToStr(time.Now().UnixNano() / 1e6)
  23. num_rand := helper.GetRandomNumber(7)
  24. serialNumber := timestamp + num_rand
  25. // 短信请求字段
  26. body_data := map[string]string{
  27. "userNumber": mobiles,
  28. "templateId": templateId,
  29. "messageContent": content,
  30. "serialNumber": serialNumber,
  31. }
  32. // 请求头封装
  33. six_rand := helper.GetRandomString(6)
  34. request_id := timestamp + six_rand
  35. headers := map[string]string{
  36. "x-sp-code": spCode,
  37. "x-app-key": appKey,
  38. "x-request-id": request_id,
  39. "X-Forwarded-For": "120.78.230.86",
  40. }
  41. // 签名
  42. sign := UmsSetSign(body_data, headers, secretKey)
  43. headers["x-sign"] = sign
  44. reply, err := PostJsonData(api, body_data, headers)
  45. if err != nil {
  46. log.Println("post err", err)
  47. return false, err
  48. }
  49. var reply_data map[string]interface{}
  50. err = json.Unmarshal(reply, &reply_data)
  51. if err != nil {
  52. log.Println(string(reply), err)
  53. return false, err
  54. }
  55. if helper.ToStr(reply_data["code"]) != "0" {
  56. return false, errors.New("短信发送失败," + helper.ToStr(reply_data["msg"]))
  57. }
  58. return true, nil
  59. }