1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-09 15:12:26 +08:00

增加小程序-云开发http api

This commit is contained in:
silenceper
2019-11-26 13:09:36 +08:00
parent 7ea817a7c6
commit 96678d2279
12 changed files with 831 additions and 9 deletions

View File

@@ -3,6 +3,7 @@ package util
import (
"encoding/json"
"fmt"
"reflect"
)
// CommonError 微信返回的通用错误json
@@ -23,3 +24,28 @@ func DecodeWithCommonError(response []byte, apiName string) (err error) {
}
return nil
}
// DecodeWithError 将返回值按照解析
func DecodeWithError(response []byte, obj interface{}, apiName string) error {
err := json.Unmarshal(response, obj)
if err != nil {
return fmt.Errorf("json Unmarshal Error, err=%v", err)
}
responseObj := reflect.ValueOf(obj)
if !responseObj.IsValid() {
return fmt.Errorf("obj is invalid")
}
commonError := responseObj.Elem().FieldByName("CommonError")
if !commonError.IsValid() || commonError.Kind() != reflect.Struct {
return fmt.Errorf("commonError is invalid or not struct")
}
errCode := commonError.FieldByName("ErrCode")
errMsg := commonError.FieldByName("ErrMsg")
if !errCode.IsValid() || !errMsg.IsValid() {
return fmt.Errorf("errcode or errmsg is invalid")
}
if errCode.Int() != 0 {
return fmt.Errorf("%s Error , errcode=%d , errmsg=%s", apiName, errCode.Int(), errMsg.String())
}
return nil
}