package pposRequest

import (
	"PPOSUtils/utils"
	"errors"
	"net/url"
)

//第三方基础开放接口请求
func (that *PPOSClient) NewOpenBaseRequest() *OpenBaseRequest {
	return &OpenBaseRequest{
		Request: Request{
			Client:     that,
			apiAddress: "",
		},
	}
}

//接口地址
func (that *OpenBaseRequest) AdminGetOpenid() *OpenBaseRequest {
	that.apiAddress = "/base/account/getOpenidByAuthCode"
	return that
}

//接口地址
func (that *OpenBaseRequest) SetApiUrl(url string) *OpenBaseRequest {
	that.apiAddress = url
	return that
}

//
type OpenBaseRequest struct {
	Request
}

//SetPostParams...
func (that *OpenBaseRequest) SetPostParams(value url.Values) *OpenBaseRequest {
	that.initParams()
	that.params = value
	return that
}

//SetPostParamsMap...
func (that *OpenBaseRequest) SetPostParamsMap(params utils.Mp) *OpenBaseRequest {
	that.initParams()
	for _k, _v := range params {
		if utils.IsNumeric(_v) {
			that.params.Set(_k, utils.ToString(_v, true))
		} else {
			that.params.Set(_k, utils.ToString(_v))
		}
	}
	return that
}

//Params ...
func (that *OpenBaseRequest) Params() (url.Values, error) {
	var m = make(url.Values)
	m = that.params
	return m, nil
}

//Do ...
func (that *OpenBaseRequest) Do() (interface{}, error) {
	if that.apiAddress == "" {
		return "", errors.New("接口不存在")
	}
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return "", _err
	}
	return _data, nil
}
