package jpush import ( "encoding/base64" "encoding/json" "fmt" "io/ioutil" "net/http" "pure/global" "strings" ) func createAuthorization(req *http.Request) *http.Request { var ( str string ) str = global.MG_CONFIG.JPush.Appkey + ":" + global.MG_CONFIG.JPush.Secret req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(str))) return req } //JiGuangPush ... func JiGuangPush(reqParams map[string]interface{}) (result PushResult, err error) { var ( client *http.Client req *http.Request params []byte cid string resBody []byte options map[string]interface{} ) options = make(map[string]interface{}) options["time_to_live"] = 0 if global.MG_CONFIG.System.Env == "mater" { options["apns_production"] = true } else { options["apns_production"] = false } reqParams["options"] = options cid, err = jiGuangCid() if err != nil { return } reqParams["cid"] = cid params, err = json.Marshal(reqParams) if err != nil { return } client = &http.Client{} req, err = http.NewRequest(http.MethodPost, "https://api.jpush.cn/v3/push", strings.NewReader(string(params))) if err != nil { return } req.Header.Add("Content-Type", "application/json") createAuthorization(req) resp, err := client.Do(req) if err != nil { return } defer resp.Body.Close() resBody, err = ioutil.ReadAll(resp.Body) if err != nil { return } //fmt.Println(string(resBody)) global.MG_LOG.Info(string(resBody)) err = json.Unmarshal(resBody, &result) if err != nil { return } return } func jiGuangCid() (string, error) { var ( err error res string client *http.Client req *http.Request resp *http.Response resBody []byte list cidList ) client = &http.Client{} req, err = http.NewRequest(http.MethodGet, "https://api.jpush.cn/v3/push/cid?count=10&type=push", nil) if err != nil { return res, err } req.Header.Add("Content-Type", "application/json") createAuthorization(req) resp, err = client.Do(req) if err != nil { return res, err } defer resp.Body.Close() resBody, err = ioutil.ReadAll(resp.Body) if err != nil { return res, err } err = json.Unmarshal(resBody, &list) if err != nil || len(list.CidList) == 0 { return "", err } return list.CidList[0], err } func GetTagsList() (string, error) { var ( err error res string client *http.Client req *http.Request resp *http.Response resBody []byte list tags ) client = &http.Client{} req, err = http.NewRequest(http.MethodGet, "https://device.jpush.cn/v3/tags/", nil) if err != nil { return res, err } req.Header.Add("Content-Type", "application/json") createAuthorization(req) resp, err = client.Do(req) if err != nil { return res, err } defer resp.Body.Close() resBody, err = ioutil.ReadAll(resp.Body) if err != nil { return res, err } fmt.Println(string(resBody)) err = json.Unmarshal(resBody, &list) if err != nil || len(list.Tags) == 0 { return "", err } return list.Tags[0], err } func UpdateTags(tagValue string, registration map[string][]string) { var ( err error client *http.Client req *http.Request params []byte reqParams map[string]interface{} resBody []byte ) reqParams = make(map[string]interface{}) reqParams["registration_ids"] = registration params, err = json.Marshal(reqParams) if err != nil { return } client = &http.Client{} req, err = http.NewRequest(http.MethodPost, "https://device.jpush.cn/v3/tags/"+tagValue, strings.NewReader(string(params))) if err != nil { return } req.Header.Add("Content-Type", "application/json") createAuthorization(req) resp, err := client.Do(req) if err != nil { return } defer resp.Body.Close() resBody, err = ioutil.ReadAll(resp.Body) if err != nil { return } fmt.Println(string(resBody)) return } func GetRegistrationInfo(registration string) { var ( err error client *http.Client req *http.Request resp *http.Response resBody []byte ) client = &http.Client{} req, err = http.NewRequest(http.MethodGet, "https://device.jpush.cn/v3/tags/", nil) if err != nil { return } req.Header.Add("Content-Type", "application/json") createAuthorization(req) resp, err = client.Do(req) if err != nil { return } defer resp.Body.Close() resBody, err = ioutil.ReadAll(resp.Body) if err != nil { return } fmt.Println(string(resBody)) return }