package wechat import ( "bytes" "encoding/json" "errors" "git.hlsq.asia/mmorpg/service-common/log" "io" "net/http" "net/url" "time" ) type ErrorCode int32 const ( ErrorCodeOK = 0 ) func RequestWechatMini(path string, method string, params *url.Values, values []byte, response any) error { u := &url.URL{} u.Scheme = "https" u.Host = "api.weixin.qq.com" u.Path = path client := &http.Client{Timeout: 10 * time.Second} if params != nil { u.RawQuery = params.Encode() } var resp *http.Response var err error if method == http.MethodGet { resp, err = client.Get(u.String()) } else { resp, err = client.Post(u.String(), "application/json", bytes.NewBuffer(values)) } if err != nil { log.Errorf("[WechatMini] %v err: %v", path, err) return err } if resp.StatusCode != http.StatusOK { log.Errorf("[WechatMini] %v err: resp.StatusCode = %v", path, resp.StatusCode) return errors.New(resp.Status) } body, err := io.ReadAll(resp.Body) if err != nil { log.Errorf("[WechatMini] %v err: %v", path, err) return err } err = json.Unmarshal(body, response) if err != nil { log.Errorf("[WechatMini] %v err: %v", path, err) return err } return nil }