package authorization import ( "encoding/json" "errors" "net/url" wxerr "gitee.com/yansen_zh/wxcomponent/errors" "gitee.com/yansen_zh/wxcomponent/utils/request" ) // PreAuthCodeParam 获取预授权码参数 type PreAuthCodeParam struct { ComponentAppId string `json:"component_appid"` } // PreAuthCodeResult 获取预授权码结果 type PreAuthCodeResult struct { wxerr.Error PreAuthCode string `json:"pre_auth_code"` ExpiresIn float64 `json:"expires_in"` } const ( apiCreatePreauthcode = "https://apies.weixin.qq.com/cgi-bin/component/api_create_preauthcode" ) // CreatePreAuthCode 获取预授权码 func CreatePreAuthCode(componentAccessToken string, data PreAuthCodeParam) (*PreAuthCodeResult, error) { if componentAccessToken == "" { return nil, errors.New("获取预授权码 第三方平台的 component_access_token 不能为空") } if data.ComponentAppId == "" { return nil, errors.New("获取预授权码 第三方平台的 appid 不能为空") } queryParam := url.Values{} queryParam.Set("component_access_token", componentAccessToken) apiUrl, _ := request.ParseURL(apiCreatePreauthcode, &queryParam) resp, e2 := request.PostJSON(apiUrl.String(), data) if e2 != nil { return nil, e2 } result := PreAuthCodeResult{} if err := json.Unmarshal(resp, &result); err != nil { return nil, err } if result.Code != 0 { return &result, result.Error } return &result, nil }