|
|
@@ -17,7 +17,8 @@ var ( |
|
|
|
const ( |
|
|
|
GRANT_TYPE = "client_credential" |
|
|
|
ACCESS_TOKEN_PATH = "/cgi-bin/token" |
|
|
|
QR_CODE_Path = "/cgi-bin/qrcode/create" |
|
|
|
QR_CODE_PATH = "/cgi-bin/qrcode/create" |
|
|
|
GET_MATERIAL_PATH = "/cgi-bin/material/batchget_material" |
|
|
|
ACTION_QR_STR_SCENE = "QR_STR_SCENE" |
|
|
|
|
|
|
|
ERR_CODE_ACCESSTOKEN_EXPIRE = 42001 |
|
|
@@ -40,6 +41,11 @@ type QRCodeRequest struct { |
|
|
|
Action_info ActionInfo `json:"action_info"` |
|
|
|
Expire_seconds int `json:"expire_seconds"` |
|
|
|
} |
|
|
|
type MaterialRequest struct { |
|
|
|
Type string `json:"type"` |
|
|
|
Offset int `json:"offset"` |
|
|
|
Count int `json:"count"` |
|
|
|
} |
|
|
|
|
|
|
|
type ActionInfo struct { |
|
|
|
Scene Scene `json:"scene"` |
|
|
@@ -97,7 +103,7 @@ func callQRCodeCreate(sceneStr string) (*QRCodeResponse, bool) { |
|
|
|
SetQueryParam("access_token", GetWechatAccessToken()). |
|
|
|
SetBody(bodyJson). |
|
|
|
SetResult(&result). |
|
|
|
Post(setting.WechatApiHost + QR_CODE_Path) |
|
|
|
Post(setting.WechatApiHost + QR_CODE_PATH) |
|
|
|
if err != nil { |
|
|
|
log.Error("create QR code failed,e=%v", err) |
|
|
|
return nil, false |
|
|
@@ -113,6 +119,37 @@ func callQRCodeCreate(sceneStr string) (*QRCodeResponse, bool) { |
|
|
|
return &result, false |
|
|
|
} |
|
|
|
|
|
|
|
//getMaterial |
|
|
|
// api doc: https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Get_materials_list.html |
|
|
|
func getMaterial(mType string, offset, count int) (interface{}, bool) { |
|
|
|
client := getWechatRestyClient() |
|
|
|
|
|
|
|
body := &MaterialRequest{ |
|
|
|
Type: mType, |
|
|
|
Offset: offset, |
|
|
|
Count: count, |
|
|
|
} |
|
|
|
bodyJson, _ := json.Marshal(body) |
|
|
|
r, err := client.R(). |
|
|
|
SetHeader("Content-Type", "application/json"). |
|
|
|
SetQueryParam("access_token", GetWechatAccessToken()). |
|
|
|
SetBody(bodyJson). |
|
|
|
Post(setting.WechatApiHost + GET_MATERIAL_PATH) |
|
|
|
if err != nil { |
|
|
|
log.Error("create QR code failed,e=%v", err) |
|
|
|
return nil, false |
|
|
|
} |
|
|
|
a := r.Body() |
|
|
|
resultMap := make(map[string]interface{}, 0) |
|
|
|
json.Unmarshal(a, &resultMap) |
|
|
|
errcode := resultMap["errcode"] |
|
|
|
if errcode == fmt.Sprint(ERR_CODE_ACCESSTOKEN_EXPIRE) || errcode == fmt.Sprint(ERR_CODE_ACCESSTOKEN_INVALID) { |
|
|
|
return nil, true |
|
|
|
} |
|
|
|
log.Info("%v", r) |
|
|
|
return &resultMap, false |
|
|
|
} |
|
|
|
|
|
|
|
func getErrorCodeFromResponse(r *resty.Response) int { |
|
|
|
a := r.Body() |
|
|
|
resultMap := make(map[string]interface{}, 0) |
|
|
|