ucpaas.go 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package sms_service
  2. import (
  3. "Kya_New/utils"
  4. "bytes"
  5. "encoding/json"
  6. "errors"
  7. "io/ioutil"
  8. "net/http"
  9. "strconv"
  10. "strings"
  11. "github.com/astaxie/beego"
  12. )
  13. // 获取短信平台信息
  14. func getUCPaasConfig() (string, string, string) {
  15. return beego.AppConfig.String("sms_appId"),
  16. beego.AppConfig.String("sms_sid"),
  17. beego.AppConfig.String("sms_token")
  18. }
  19. // 向云之讯短信平台创建模板,返回新模板的 ID
  20. // autograph: 签名,即为机构简称
  21. // content: 模板内容
  22. // type_: 短信类型:0:通知短信 5:会员服务短信
  23. func createTemplate2UCPaas(autograph string, content string, type_ int) (int, error) {
  24. sms_api := beego.AppConfig.String("sms_baseUrl") + "addsmstemplate"
  25. appID, sid, token := getUCPaasConfig()
  26. params := make(map[string]interface{})
  27. params["appid"] = appID
  28. params["sid"] = sid
  29. params["token"] = token
  30. params["type"] = type_
  31. params["autograph"] = autograph
  32. params["content"] = content
  33. paramsBytes, _ := json.Marshal(params)
  34. resp, requestErr := http.Post(sms_api, "application/json", bytes.NewBuffer(paramsBytes))
  35. if requestErr != nil {
  36. utils.ErrorLog("短信平台增加模板接口调用失败: %v", requestErr)
  37. return 0, requestErr
  38. }
  39. defer resp.Body.Close()
  40. body, ioErr := ioutil.ReadAll(resp.Body)
  41. if ioErr != nil {
  42. utils.ErrorLog("短信平台增加模板接口返回数据读取失败: %v", ioErr)
  43. return 0, ioErr
  44. }
  45. var respJSON map[string]interface{}
  46. utils.InfoLog(string(body))
  47. if err := json.Unmarshal([]byte(string(body)), &respJSON); err != nil {
  48. utils.ErrorLog("短信平台增加模板接口返回数据解析JSON失败: %v", err)
  49. return 0, err
  50. }
  51. if respJSON["code"].(string) != "000000" {
  52. msg := respJSON["msg"].(string)
  53. utils.ErrorLog("短信平台增加模板接口请求失败: %v", msg)
  54. return 0, errors.New("短信平台增加模板接口请求失败")
  55. }
  56. templateID, _ := strconv.Atoi(respJSON["templateid"].(string))
  57. utils.SuccessLog("新短信模板 ID: %v", templateID)
  58. return templateID, nil
  59. }
  60. // 用云之讯群发模板短信
  61. // 返回值为发送了 n 条短信
  62. func batchSendMessageUseUCPaas(templateID int, params []string, mobiles []string) (int, []interface{}, error) {
  63. sms_api := beego.AppConfig.String("sms_baseUrl") + "sendsms_batch"
  64. mobileStr := strings.Join(mobiles, ",")
  65. appID, sid, token := getUCPaasConfig()
  66. requestParams := make(map[string]interface{})
  67. requestParams["appid"] = appID
  68. requestParams["sid"] = sid
  69. requestParams["token"] = token
  70. requestParams["templateid"] = strconv.Itoa(templateID)
  71. requestParams["mobile"] = mobileStr
  72. if params != nil && len(params) != 0 {
  73. paramStr := strings.Join(params, ",")
  74. requestParams["param"] = paramStr
  75. }
  76. paramsBytes, _ := json.Marshal(requestParams)
  77. resp, requestErr := http.Post(sms_api, "application/json", bytes.NewBuffer(paramsBytes))
  78. if requestErr != nil {
  79. utils.ErrorLog("短信平台模板群发接口调用失败: %v", requestErr)
  80. return 0, nil, requestErr
  81. }
  82. defer resp.Body.Close()
  83. body, ioErr := ioutil.ReadAll(resp.Body)
  84. if ioErr != nil {
  85. utils.ErrorLog("短信平台模板群发接口返回数据读取失败: %v", ioErr)
  86. return 0, nil, ioErr
  87. }
  88. var respJSON map[string]interface{}
  89. utils.InfoLog(string(body))
  90. if err := json.Unmarshal([]byte(string(body)), &respJSON); err != nil {
  91. utils.ErrorLog("短信平台模板群发接口返回数据解析JSON失败: %v", err)
  92. return 0, nil, err
  93. }
  94. if respJSON["code"].(string) != "000000" {
  95. msg := respJSON["msg"].(string)
  96. utils.ErrorLog("短信平台模板群发接口请求失败: %v", msg)
  97. return 0, nil, errors.New("短信平台模板群发接口请求失败")
  98. } else {
  99. utils.SuccessLog("短信发送成功 report: %v", respJSON["report"])
  100. if len(mobiles) > 1 {
  101. count, _ := strconv.Atoi(respJSON["count_sum"].(string))
  102. return count, respJSON["report"].([]interface{}), nil
  103. } else {
  104. return 1, nil, nil
  105. }
  106. }
  107. }
  108. // 用云之讯单发模板短信
  109. // 返回值分别是 是否成功,code,msg,error
  110. func singleSendMessageUseUCPaas(templateID int, params []string, mobile string) (bool, string, string, error) {
  111. sms_api := beego.AppConfig.String("sms_baseUrl") + "sendsms"
  112. appID, sid, token := getUCPaasConfig()
  113. requestParams := make(map[string]interface{})
  114. requestParams["appid"] = appID
  115. requestParams["sid"] = sid
  116. requestParams["token"] = token
  117. requestParams["templateid"] = strconv.Itoa(templateID)
  118. requestParams["mobile"] = mobile
  119. if params != nil && len(params) != 0 {
  120. paramStr := strings.Join(params, ",")
  121. requestParams["param"] = paramStr
  122. }
  123. paramsBytes, _ := json.Marshal(requestParams)
  124. resp, requestErr := http.Post(sms_api, "application/json", bytes.NewBuffer(paramsBytes))
  125. if requestErr != nil {
  126. utils.ErrorLog("短信平台模板群发接口调用失败: %v", requestErr)
  127. return false, "", "", requestErr
  128. }
  129. defer resp.Body.Close()
  130. body, ioErr := ioutil.ReadAll(resp.Body)
  131. if ioErr != nil {
  132. utils.ErrorLog("短信平台模板群发接口返回数据读取失败: %v", ioErr)
  133. return false, "", "", ioErr
  134. }
  135. var respJSON map[string]interface{}
  136. utils.InfoLog(string(body))
  137. if err := json.Unmarshal([]byte(string(body)), &respJSON); err != nil {
  138. utils.ErrorLog("短信平台模板群发接口返回数据解析JSON失败: %v", err)
  139. return false, "", "", err
  140. }
  141. if respJSON["code"].(string) != "000000" {
  142. msg := respJSON["msg"].(string)
  143. utils.ErrorLog("短信平台模板群发接口请求失败: %v", msg)
  144. return false, "", "", errors.New("短信平台模板群发接口请求失败")
  145. } else {
  146. utils.SuccessLog("短信发送成功")
  147. return true, respJSON["code"].(string), respJSON["msg"].(string), nil
  148. }
  149. }