123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- package sms_service
-
- import (
- "SCRM/utils"
- "bytes"
- "encoding/json"
- "errors"
- "io/ioutil"
- "net/http"
- "strconv"
- "strings"
-
- "github.com/astaxie/beego"
- )
-
- // 获取短信平台信息
- func getUCPaasConfig() (string, string, string) {
- return beego.AppConfig.String("sms_appId"),
- beego.AppConfig.String("sms_sid"),
- beego.AppConfig.String("sms_token")
- }
-
- // 向云之讯短信平台创建模板,返回新模板的 ID
- // autograph: 签名,即为机构简称
- // content: 模板内容
- // type_: 短信类型:0:通知短信 5:会员服务短信
- func createTemplate2UCPaas(autograph string, content string, type_ int) (int, error) {
- sms_api := beego.AppConfig.String("sms_baseUrl") + "addsmstemplate"
- appID, sid, token := getUCPaasConfig()
- params := make(map[string]interface{})
- params["appid"] = appID
- params["sid"] = sid
- params["token"] = token
- params["type"] = type_
- params["autograph"] = autograph
- params["content"] = content
-
- paramsBytes, _ := json.Marshal(params)
- resp, requestErr := http.Post(sms_api, "application/json", bytes.NewBuffer(paramsBytes))
-
- if requestErr != nil {
- utils.ErrorLog("短信平台增加模板接口调用失败: %v", requestErr)
- return 0, requestErr
- }
- defer resp.Body.Close()
- body, ioErr := ioutil.ReadAll(resp.Body)
- if ioErr != nil {
- utils.ErrorLog("短信平台增加模板接口返回数据读取失败: %v", ioErr)
- return 0, ioErr
- }
- var respJSON map[string]interface{}
- utils.InfoLog(string(body))
- if err := json.Unmarshal([]byte(string(body)), &respJSON); err != nil {
- utils.ErrorLog("短信平台增加模板接口返回数据解析JSON失败: %v", err)
- return 0, err
- }
- if respJSON["code"].(string) != "000000" {
- msg := respJSON["msg"].(string)
- utils.ErrorLog("短信平台增加模板接口请求失败: %v", msg)
- return 0, errors.New("短信平台增加模板接口请求失败")
- }
-
- templateID, _ := strconv.Atoi(respJSON["templateid"].(string))
- utils.SuccessLog("新短信模板 ID: %v", templateID)
- return templateID, nil
- }
-
- // 用云之讯群发模板短信
- // 返回值为发送了 n 条短信
- func batchSendMessageUseUCPaas(templateID int, params []string, mobiles []string) (int, []interface{}, error) {
- sms_api := beego.AppConfig.String("sms_baseUrl") + "sendsms_batch"
- mobileStr := strings.Join(mobiles, ",")
- appID, sid, token := getUCPaasConfig()
- requestParams := make(map[string]interface{})
- requestParams["appid"] = appID
- requestParams["sid"] = sid
- requestParams["token"] = token
- requestParams["templateid"] = strconv.Itoa(templateID)
- requestParams["mobile"] = mobileStr
- if params != nil && len(params) != 0 {
- paramStr := strings.Join(params, ",")
- requestParams["param"] = paramStr
- }
-
- paramsBytes, _ := json.Marshal(requestParams)
- resp, requestErr := http.Post(sms_api, "application/json", bytes.NewBuffer(paramsBytes))
-
- if requestErr != nil {
- utils.ErrorLog("短信平台模板群发接口调用失败: %v", requestErr)
- return 0, nil, requestErr
- }
- defer resp.Body.Close()
- body, ioErr := ioutil.ReadAll(resp.Body)
- if ioErr != nil {
- utils.ErrorLog("短信平台模板群发接口返回数据读取失败: %v", ioErr)
- return 0, nil, ioErr
- }
- var respJSON map[string]interface{}
- utils.InfoLog(string(body))
- if err := json.Unmarshal([]byte(string(body)), &respJSON); err != nil {
- utils.ErrorLog("短信平台模板群发接口返回数据解析JSON失败: %v", err)
- return 0, nil, err
- }
- if respJSON["code"].(string) != "000000" {
- msg := respJSON["msg"].(string)
- utils.ErrorLog("短信平台模板群发接口请求失败: %v", msg)
- return 0, nil, errors.New("短信平台模板群发接口请求失败")
-
- } else {
- utils.SuccessLog("短信发送成功 report: %v", respJSON["report"])
- if len(mobiles) > 1 {
- count, _ := strconv.Atoi(respJSON["count_sum"].(string))
- return count, respJSON["report"].([]interface{}), nil
- } else {
- return 1, nil, nil
- }
- }
- }
-
- // 用云之讯单发模板短信
- // 返回值分别是 是否成功,code,msg,error
- func singleSendMessageUseUCPaas(templateID int, params []string, mobile string) (bool, string, string, error) {
- sms_api := beego.AppConfig.String("sms_baseUrl") + "sendsms"
- appID, sid, token := getUCPaasConfig()
- requestParams := make(map[string]interface{})
- requestParams["appid"] = appID
- requestParams["sid"] = sid
- requestParams["token"] = token
- requestParams["templateid"] = strconv.Itoa(templateID)
- requestParams["mobile"] = mobile
- if params != nil && len(params) != 0 {
- paramStr := strings.Join(params, ",")
- requestParams["param"] = paramStr
- }
-
- paramsBytes, _ := json.Marshal(requestParams)
- resp, requestErr := http.Post(sms_api, "application/json", bytes.NewBuffer(paramsBytes))
-
- if requestErr != nil {
- utils.ErrorLog("短信平台模板群发接口调用失败: %v", requestErr)
- return false, "", "", requestErr
- }
- defer resp.Body.Close()
- body, ioErr := ioutil.ReadAll(resp.Body)
- if ioErr != nil {
- utils.ErrorLog("短信平台模板群发接口返回数据读取失败: %v", ioErr)
- return false, "", "", ioErr
- }
- var respJSON map[string]interface{}
- utils.InfoLog(string(body))
- if err := json.Unmarshal([]byte(string(body)), &respJSON); err != nil {
- utils.ErrorLog("短信平台模板群发接口返回数据解析JSON失败: %v", err)
- return false, "", "", err
- }
- if respJSON["code"].(string) != "000000" {
- msg := respJSON["msg"].(string)
- utils.ErrorLog("短信平台模板群发接口请求失败: %v", msg)
- return false, "", "", errors.New("短信平台模板群发接口请求失败")
-
- } else {
- utils.SuccessLog("短信发送成功")
- return true, respJSON["code"].(string), respJSON["msg"].(string), nil
- }
- }
|