审查视图

pkg/infrastructure/serviceGateway/httplib_usercenter_service.go 3.4 KB
唐旭辉 authored
1
package serviceGateway
2 3

import (
唐旭辉 authored
4
	"bytes"
5
	"crypto/sha1"
唐旭辉 authored
6
	"encoding/json"
7
	"fmt"
唐旭辉 authored
8
	"io/ioutil"
9
	"net/http"
唐旭辉 authored
10
	"net/url"
11 12
	"time"
唐旭辉 authored
13
	"github.com/astaxie/beego/logs"
14 15 16
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/constant"
)
唐旭辉 authored
17 18 19 20 21 22 23 24 25 26 27 28
type UCenterCommonMsg struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
}

func (msg UCenterCommonMsg) IsOK() error {
	if msg.Code != 0 {
		return fmt.Errorf("统一用户中心响应数据异常,code:%d,msg:%s", msg.Code, msg.Msg)
	}
	return nil
}
29
type MmmUserCenterServiceGateway struct {
唐旭辉 authored
30
	baseURL string
31 32 33 34
}

func NewMmmUserCenterServiceGateway() *MmmUserCenterServiceGateway {
	return &MmmUserCenterServiceGateway{
唐旭辉 authored
35 36
		baseURL: constant.UCENTER_HOST,
	}
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
}

func (gateway MmmUserCenterServiceGateway) buildHeader() http.Header {
	var h = http.Header{}
	nowTime := fmt.Sprint(time.Now().Unix())
	str := fmt.Sprintf("%s%s%s", nowTime, constant.UCENTER_SECRET, constant.UCENTER_CHECK_ALT)
	bt := sha1.Sum([]byte(str))
	checksum := fmt.Sprintf("%x", bt)
	h.Set("Content-Type", "application/json")
	h.Set("appKey", constant.UCENTER_APP_KEY)
	h.Set("nonce", "")
	h.Set("curTime", nowTime)
	h.Set("checkSum", checksum)
	h.Set("Accept", "application/json")
	return h
}
唐旭辉 authored
54 55 56 57 58 59 60 61 62
func (gateway MmmUserCenterServiceGateway) 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)
	}
唐旭辉 authored
63 64
	logs.Info("====>Send To URL:%s", reqURL)
	logs.Info("====>Send To BusinessAdmin:%s", bt.String())
唐旭辉 authored
65 66 67 68 69 70 71 72 73 74 75 76 77 78
	req, err := http.NewRequest(mathod, reqURL, bt)
	if err != nil {
		return nil, err
	}
	req.Header = gateway.buildHeader()
	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
	}
唐旭辉 authored
79
	logs.Info("<====BusinessAdmin Return:%s", string(body))
唐旭辉 authored
80 81 82
	return body, nil
}
83
type ResponseLogin struct {
唐旭辉 authored
84
	UCenterCommonMsg
85
	Data struct {
唐旭辉 authored
86 87
		Id              int64  `json:"id"`              //统一用户中心的id,对应本系统中users表的open_id
		Phone           string `json:"phone"`           //手机号 ,账号
88 89 90 91 92
		NickName        string `json:"nickname"`        //昵称
		Avatar          string `json:"avatar"`          //头像
		Imtoken         string `json:"imtoken"`         //网易云imtoken
		Accid           int64  `json:"accid"`           //网易云id
		CustomerAccount int64  `json:"customerAccount"` //客服id
唐旭辉 authored
93
		CompanyId       int64  `json:"companyId"`       //总后台的公司id ,对应company表中的admin_company_id
94 95 96 97
		Muid            int64  `json:"muid"`            //企业平台的用户id,对应本系统中users表的id
	} `json:"data"`
}
唐旭辉 authored
98
//RequestUCenterLoginBySecret 使用密钥方式登录统一用户中心
99
func (gateway MmmUserCenterServiceGateway) RequestUCenterLoginBySecret(secret string) (*ResponseLogin, error) {
唐旭辉 authored
100 101 102 103
	param := map[string]interface{}{
		"type":   3,                       //登录方式 固定值
		"secret": url.QueryEscape(secret), //必要的转换
	}
唐旭辉 authored
104
	url := gateway.baseURL + "/auth/serverLogin"
唐旭辉 authored
105
	byteData, err := gateway.httpDo(url, "POST", param)
唐旭辉 authored
106 107 108 109 110
	if err != nil {
		return nil, err
	}
	respData := &ResponseLogin{}
	err = json.Unmarshal(byteData, respData)
唐旭辉 authored
111 112 113 114
	if err != nil {
		return nil, fmt.Errorf("body data %s; err:%s", string(byteData), err)
	}
	return respData, nil
115
}
唐旭辉 authored
116 117

//企业鉴权 接口