package qrcode import ( "bytes" "context" "encoding/json" "fmt" "io" "net/http" "net/url" "time" "lone-services/pkg/utils" ) const wxaCodeURL = "https://api.weixin.qq.com/wxa/getwxacode" func BuildPath(page string, params map[string]string) string { if len(params) == 0 { return page } query := url.Values{} for k, v := range params { query.Add(k, v) } return page + "?" + query.Encode() } func (c *Client) GetWxaCode(accessToken string, req *WxaCodeRequest) ([]byte, error) { if accessToken == utils.StringEmpty { return nil, fmt.Errorf("wechat access_token empty") } if req == nil || req.Path == utils.StringEmpty { return nil, fmt.Errorf("wechat qrcode path empty") } payload, err := json.Marshal(req) if err != nil { return nil, err } apiURL := fmt.Sprintf("%s?access_token=%s", wxaCodeURL, accessToken) httpClient := &http.Client{Timeout: 15 * time.Second} resp, err := httpClient.Post(apiURL, "application/json", bytes.NewReader(payload)) if err != nil { return nil, err } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { return nil, err } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("wechat qrcode http status %d", resp.StatusCode) } if err := parseImageOrError(body); err != nil { return nil, err } return body, nil } func (c *Client) CreateWxaCode(ctx context.Context, req *WxaCodeRequest) ([]byte, error) { if c == nil || c.Auth == nil { return nil, fmt.Errorf("wechat qrcode auth client nil") } token, err := c.Auth.AccessToken(ctx) if err != nil { return nil, err } return c.GetWxaCode(token.AccessToken, req) }