1
|
package service_gateway
|
1
|
package service_gateway
|
2
|
|
2
|
|
3
|
import (
|
3
|
import (
|
|
|
4
|
+ "bytes"
|
4
|
"crypto/sha1"
|
5
|
"crypto/sha1"
|
|
|
6
|
+ "encoding/json"
|
5
|
"fmt"
|
7
|
"fmt"
|
|
|
8
|
+ "io/ioutil"
|
6
|
"net/http"
|
9
|
"net/http"
|
|
|
10
|
+ "net/url"
|
7
|
"time"
|
11
|
"time"
|
8
|
|
12
|
|
9
|
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/constant"
|
13
|
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/constant"
|
10
|
)
|
14
|
)
|
11
|
|
15
|
|
|
|
16
|
+type UCenterCommonMsg struct {
|
|
|
17
|
+ Code int `json:"code"`
|
|
|
18
|
+ Msg string `json:"msg"`
|
|
|
19
|
+}
|
|
|
20
|
+
|
|
|
21
|
+func (msg UCenterCommonMsg) IsOK() error {
|
|
|
22
|
+ if msg.Code != 0 {
|
|
|
23
|
+ return fmt.Errorf("统一用户中心响应数据异常,code:%d,msg:%s", msg.Code, msg.Msg)
|
|
|
24
|
+ }
|
|
|
25
|
+ return nil
|
|
|
26
|
+}
|
|
|
27
|
+
|
12
|
type MmmUserCenterServiceGateway struct {
|
28
|
type MmmUserCenterServiceGateway struct {
|
13
|
- httplibBaseServiceGateway
|
29
|
+ baseURL string
|
14
|
}
|
30
|
}
|
15
|
|
31
|
|
16
|
func NewMmmUserCenterServiceGateway() *MmmUserCenterServiceGateway {
|
32
|
func NewMmmUserCenterServiceGateway() *MmmUserCenterServiceGateway {
|
17
|
return &MmmUserCenterServiceGateway{
|
33
|
return &MmmUserCenterServiceGateway{
|
18
|
- httplibBaseServiceGateway{
|
|
|
19
|
- baseURL: constant.UCENTER_HOST,
|
|
|
20
|
- connectTimeout: 100 * time.Second,
|
|
|
21
|
- readWriteTimeout: 30 * time.Second,
|
|
|
22
|
- }}
|
34
|
+ baseURL: constant.UCENTER_HOST,
|
|
|
35
|
+ }
|
23
|
}
|
36
|
}
|
24
|
|
37
|
|
25
|
func (gateway MmmUserCenterServiceGateway) buildHeader() http.Header {
|
38
|
func (gateway MmmUserCenterServiceGateway) buildHeader() http.Header {
|
|
@@ -37,9 +50,34 @@ func (gateway MmmUserCenterServiceGateway) buildHeader() http.Header { |
|
@@ -37,9 +50,34 @@ func (gateway MmmUserCenterServiceGateway) buildHeader() http.Header { |
37
|
return h
|
50
|
return h
|
38
|
}
|
51
|
}
|
39
|
|
52
|
|
|
|
53
|
+func (gateway MmmUserCenterServiceGateway) httpDo(reqURL string, mathod string, bodyData interface{}) ([]byte, error) {
|
|
|
54
|
+ httpclient := http.Client{
|
|
|
55
|
+ Timeout: 60 * time.Second, //请求超时时间60秒
|
|
|
56
|
+ }
|
|
|
57
|
+ bt := &bytes.Buffer{}
|
|
|
58
|
+ if bodyData != nil {
|
|
|
59
|
+ enc := json.NewEncoder(bt)
|
|
|
60
|
+ enc.Encode(bodyData)
|
|
|
61
|
+ }
|
|
|
62
|
+ req, err := http.NewRequest(mathod, reqURL, bt)
|
|
|
63
|
+ if err != nil {
|
|
|
64
|
+ return nil, err
|
|
|
65
|
+ }
|
|
|
66
|
+ req.Header = gateway.buildHeader()
|
|
|
67
|
+ resp, err := httpclient.Do(req)
|
|
|
68
|
+ if err != nil {
|
|
|
69
|
+ return nil, err
|
|
|
70
|
+ }
|
|
|
71
|
+ defer resp.Body.Close()
|
|
|
72
|
+ body, err := ioutil.ReadAll(resp.Body)
|
|
|
73
|
+ if err != nil {
|
|
|
74
|
+ return nil, err
|
|
|
75
|
+ }
|
|
|
76
|
+ return body, nil
|
|
|
77
|
+}
|
|
|
78
|
+
|
40
|
type ResponseLogin struct {
|
79
|
type ResponseLogin struct {
|
41
|
- Code int `json:"code"`
|
|
|
42
|
- Msg string `json:"msg"`
|
80
|
+ UCenterCommonMsg
|
43
|
Data struct {
|
81
|
Data struct {
|
44
|
Id int64 `json:"id"` //统一用户中心的id,对应本系统中users表的open_id
|
82
|
Id int64 `json:"id"` //统一用户中心的id,对应本系统中users表的open_id
|
45
|
Phone string `json:"phone"`
|
83
|
Phone string `json:"phone"`
|
|
@@ -53,13 +91,18 @@ type ResponseLogin struct { |
|
@@ -53,13 +91,18 @@ type ResponseLogin struct { |
53
|
} `json:"data"`
|
91
|
} `json:"data"`
|
54
|
}
|
92
|
}
|
55
|
|
93
|
|
|
|
94
|
+//RequestUCenterLoginBySecret 使用密钥方式登录统一用户中心
|
56
|
func (gateway MmmUserCenterServiceGateway) RequestUCenterLoginBySecret(secret string) (*ResponseLogin, error) {
|
95
|
func (gateway MmmUserCenterServiceGateway) RequestUCenterLoginBySecret(secret string) (*ResponseLogin, error) {
|
57
|
- // param := map[string]interface{}{
|
|
|
58
|
- // "type": 3, //登录方式 固定值
|
|
|
59
|
- // "secret": url.QueryEscape(secret), //必要的转换
|
|
|
60
|
- // }
|
|
|
61
|
- // url := "/auth/serverLogin"
|
|
|
62
|
- // httpRequest := gateway.createRequest(url, "post")
|
|
|
63
|
- // httpRequest.
|
|
|
64
|
- return nil, nil
|
96
|
+ param := map[string]interface{}{
|
|
|
97
|
+ "type": 3, //登录方式 固定值
|
|
|
98
|
+ "secret": url.QueryEscape(secret), //必要的转换
|
|
|
99
|
+ }
|
|
|
100
|
+ url := constant.UCENTER_HOST + "/auth/serverLogin"
|
|
|
101
|
+ byteData, err := gateway.httpDo(url, "post", param)
|
|
|
102
|
+ if err != nil {
|
|
|
103
|
+ return nil, err
|
|
|
104
|
+ }
|
|
|
105
|
+ respData := &ResponseLogin{}
|
|
|
106
|
+ err = json.Unmarshal(byteData, respData)
|
|
|
107
|
+ return respData, err
|
65
|
} |
108
|
} |