短信发送方法
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.

109 lines
2.3 KiB

  1. package sms
  2. import (
  3. "log"
  4. "git.tetele.net/tgo/helper"
  5. )
  6. /**
  7. * 创蓝253发送短信
  8. *
  9. * @param string $mobile 手机号码
  10. * @param string $msg 短信内容
  11. * @param string $sms_sign 短信签名
  12. * @param string $needstatus 是否需要状态报告
  13. * @return success bool, reply map[string]string, err error
  14. */
  15. var CL_ACCOUNT string = "N3402351"
  16. var CL_PASSWORD string = "zmkj64U5A"
  17. // const CL_VARIABLE_SMS_URL string = "http://XXX/msg/variable/json"
  18. func SendByCL(account, password, mobile, sms_sign, msg string) (bool, map[string]interface{}, error) {
  19. const CL_SIMPLE_SMS_URL string = "https://smssh1.253.com/msg/v1/send/json" // "http://smssh1.253.com/msg/send/json"
  20. if account == "" {
  21. account = CL_ACCOUNT
  22. }
  23. if password == "" {
  24. password = CL_PASSWORD
  25. }
  26. //创蓝接口参数
  27. data := map[string]string{
  28. "account": account,
  29. "password": password,
  30. "msg": StringJoin("【", sms_sign, "】", msg),
  31. "phone": mobile,
  32. "report": "true",
  33. }
  34. response, err := PostJsonData(CL_SIMPLE_SMS_URL, data)
  35. if err != nil {
  36. return false, nil, err
  37. }
  38. var reply map[string]interface{}
  39. err = json.Unmarshal(response, &reply)
  40. if err != nil {
  41. log.Println(string(response), err)
  42. return false, nil, err
  43. }
  44. if helper.ToStr(reply["code"]) != "0" {
  45. return false, reply, nil
  46. }
  47. return true, reply, err
  48. }
  49. /**
  50. * 查询额度
  51. *
  52. */
  53. func QueryRemaining(account, password string) (bool, string, map[string]interface{}, error) {
  54. const URL string = "https://smssh1.253.com/msg/balance/json"
  55. if account == "" {
  56. account = CL_ACCOUNT
  57. }
  58. if password == "" {
  59. password = CL_PASSWORD
  60. }
  61. data := map[string]string{
  62. "account": account,
  63. "password": password,
  64. }
  65. response, err := PostJsonData(URL, data)
  66. /** 返回示例
  67. {"code":0,"balance":"9156","time":"20230217150940","errorMsg":""}
  68. {"code":"130","msgId":"","time":"20230217145243","errorMsg":"请求参数错误"}
  69. {"code":"101","msgId":"","time":"20230217151029","errorMsg":"无此用户"}
  70. */
  71. if err != nil {
  72. return false, "", nil, err
  73. }
  74. var reply map[string]interface{}
  75. err = json.Unmarshal(response, &reply)
  76. if err != nil {
  77. log.Println(string(response), err)
  78. return false, "", nil, err
  79. }
  80. if helper.ToStr(reply["code"]) != "0" {
  81. return false, "", reply, nil
  82. }
  83. return true, helper.ToStr(reply["balance"]), reply, err
  84. }