阿里推送Go版本

2018-12-19  本文已影响8人  大神留条命
package push

import (
    "crypto/hmac"
    "crypto/sha1"
    "encoding/base64"
    "fmt"
    "io/ioutil"
    "net/http"
    "net/url"
    "sort"
    "strconv"
    "strings"
    "time"
)

type AliPush struct {
    SecretAccessKey string
    AccessKeyId     string
    AppKey          string

    Target      string
    TargetValue string

    PushType string

    Title         string
    Body          string
    ExtParameters string

    Sound string

    IOSApnsEnv string //DEV   PRODUCT
}

func PushToAli(data AliPush) (string, error) {

    fmt.Println(data)

    // 参数
    var params = map[string]string{}
    // 公共参数
    params["Format"] = "JSON"
    params["Version"] = "2016-08-01"         // 版本号-默认
    params["RegionId"] = "cn-hangzhou"       // 地区-默认
    params["AccessKeyId"] = data.AccessKeyId // 访问服务所用的密钥ID

    params["Action"] = "Push"
    params["AppKey"] = data.AppKey
    params["Target"] = data.Target // "ACCOUNT"
    params["TargetValue"] = data.TargetValue
    params["Title"] = data.Title
    params["Body"] = data.Body
    params["PushType"] = data.PushType //"NOTICE"

    params["DeviceType"] = "ALL"

    params["AndroidOpenType"] = "APPLICATION"
    params["AndroidExtParameters"] = data.ExtParameters

    params["iOSRemind"] = "true"
    params["iOSRemindBody"] = "iOSBody"
    params["iOSMusic"] = data.Sound
    params["iOSApnsEnv"] = data.IOSApnsEnv

    params["ExtParameters"] = data.ExtParameters

    params["StoreOffline"] = "false"

    // 签名
    params["SignatureMethod"] = "HMAC-SHA1" // 签名方式-默认
    t := time.Now().UTC().Format("2006-01-02T15:04:05Z")
    params["Timestamp"] = t
    params["SignatureVersion"] = "1.0"                    // 签名版本
    nonce := strconv.FormatInt(time.Now().UnixNano(), 10) //uuid.Must(uuid.NewV1()).String()
    params["SignatureNonce"] = nonce
    // 排序Query
    var keys []string
    for k := range params {
        keys = append(keys, k)
    }
    sort.Strings(keys)

    // 排序拼接
    sortQuery := ""
    for _, k := range keys {
        sortQuery += k + "=" + ContentEscape(params[k]) + "&"
    }
    sortQuery = strings.TrimSuffix(sortQuery, "&")

    secretAccessKey := data.SecretAccessKey + "&"

    stringToSign := "GET" + "&" + url.QueryEscape("/") + "&" + url.QueryEscape(sortQuery) // 签名拼接

    signature := Hmac(secretAccessKey, stringToSign) // 签名结果

    // 请求
    endPoint := "http://cloudpush.aliyuncs.com/?" + sortQuery + "&Signature=" + url.QueryEscape(signature)

    response, err := http.Get(endPoint)
    if err != nil {
        fmt.Println(err.Error())
        return "", err
    }

    res, e := ioutil.ReadAll(response.Body)
    if e != nil {
        return "", e
    }

    fmt.Println()
    fmt.Println(string(res))
    return string(res), nil
}

// ContentEscape 内容替换
func ContentEscape(str string) string {
    str = strings.Replace(str, " ", "%20", -1)
    str = url.QueryEscape(str)
    return str
}

// Hmac sha1 加密
func Hmac(key, data string) string {
    mac := hmac.New(sha1.New, []byte(key))
    mac.Write([]byte(data))
    encode := base64.StdEncoding.EncodeToString(mac.Sum([]byte("")))
    return encode
}

上一篇下一篇

猜你喜欢

热点阅读