/**
 * 核心服务接口sdk（golang版）
 *
 * @desc 啪啪运动 核心服务接口sdk
 * ---------------------------------------------------------------------
 * @author		tang <tangzhifu@papa.com.cn>
 * @date		2022-04-20
 * @copyright	core-utils
 * ---------------------------------------------------------------------
 */
package auth

import (
	"encoding/json"
	"errors"
	"net/url"
)

//NewMiniServerLoginRequest ... 商家小程序登录，获取账号id（服务商模式）
func (that *AuthClient) NewMiniGetByAuthorizeIdRequest() *MiniGetByAuthorizeIdRequest {
	return &MiniGetByAuthorizeIdRequest{
		Request: Request{
			Client:            that,
			publicParamsSlice: []string{
				//...
			},
			apiAddress: "/wechat/open/getByAuthorizeId",
		},
	}
}

//MiniGetByAuthorizeIdRequest
type MiniGetByAuthorizeIdRequest struct {
	Request
}

func (that *MiniGetByAuthorizeIdRequest) SetAuthorizerAppId(value string) *MiniGetByAuthorizeIdRequest {
	that.initParams()
	that.params.Set("authorizer_appid", value) //小程序的AppID (授权方)
	return that
}

//Params ...
func (that *MiniGetByAuthorizeIdRequest) Params() (url.Values, error) {
	that.initParams()
	_params := that.params
	if that.publicParamsSlice != nil {
		for _, _v := range that.publicParamsSlice {
			if _params.Get(_v) == "" {
				return nil, errors.New(that.apiAddress + "缺少参数:" + _v)
			}
		}
	}
	return _params, nil
}

//Do ...
func (that *MiniGetByAuthorizeIdRequest) Do() (*MiniGetByAuthorizeIdResponse, error) {
	//返回data
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//
	_slice := []string{
		"open_app_id", //微信openID
	}
	//
	_sliceData, _ok := _data.(map[string]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确")
	}
	//
	for _, _val := range _slice {
		if _, _ok := _sliceData[_val]; !_ok {
			return nil, errors.New(that.apiAddress + "请求响应参数缺失")
		}
	}
	//
	_str, _err := json.Marshal(_sliceData)
	if _err != nil {
		return nil, errors.New(that.apiAddress + _err.Error())
	}
	//返回结果
	_rs := new(MiniGetByAuthorizeIdResponse)
	_err = json.Unmarshal(_str, &_rs)
	if _err != nil {
		return _rs, errors.New(that.apiAddress + _err.Error())
	}
	return _rs, nil
}
