httplib_business-admin_service.go 2.0 KB
package serviceGateway

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"time"

	"github.com/astaxie/beego/logs"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/constant"
)

type MmmBusinessAdminServiceGateway struct {
	baseURL string
}

func NewMmmBusinessAdminServiceGateway() *MmmBusinessAdminServiceGateway {
	return &MmmBusinessAdminServiceGateway{
		baseURL: constant.BUSINESS_ADMIN_HOST,
	}
}
func (client MmmBusinessAdminServiceGateway) buildHeader() http.Header {
	var h = http.Header{}
	h.Set("Content-Type", "application/json")
	h.Set("Accept", "application/json")
	return h
}

type ResponseGetUserAuth struct {
	UCenterCommonMsg
	Data struct {
		UserAuth bool `json:"userAuth"`
	} `json:"data"`
}

func (gateway MmmBusinessAdminServiceGateway) httpDo(reqURL string, mathod string, bodyData interface{}) ([]byte, error) {
	httpclient := http.Client{
		Timeout: 60 * time.Second, //请求超时时间60秒
	}
	bt := &bytes.Buffer{}
	if bodyData != nil {
		enc := json.NewEncoder(bt)
		enc.Encode(bodyData)
	}
	logs.Info("====>Send To URL:%s", reqURL)
	logs.Info("====>Send To BusinessAdmin:%s", bt.String())
	req, err := http.NewRequest(mathod, reqURL, bt)
	req.Header = gateway.buildHeader()
	if err != nil {
		return nil, err
	}
	resp, err := httpclient.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}
	logs.Info("<====BusinessAdmin Return:%s", string(body))
	return body, nil
}

func (gateway MmmBusinessAdminServiceGateway) GetUserAuth(userId int64) (*ResponseGetUserAuth, error) {
	param := map[string]interface{}{
		"userId":     fmt.Sprint(userId),
		"platformId": "25", //天联共创固定值:25
	}
	url := gateway.baseURL + "/auth/get-user-auth"
	byteData, err := gateway.httpDo(url, "POST", param)
	if err != nil {
		return nil, err
	}
	respData := &ResponseGetUserAuth{}
	err = json.Unmarshal(byteData, respData)
	if err != nil {
		return nil, fmt.Errorf("body data %s; err:%s", string(byteData), err)
	}
	return respData, nil
}