httplib_ucenter_service_gateway.go
4.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package svr
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/constant"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log"
"strconv"
"strings"
"time"
)
type HttplibUCenterApiServiceGateway struct {
httplibBaseServiceGateway
}
// 服务登录
func (serviceGateway *HttplibUCenterApiServiceGateway) ServerLogin(phone, password string, loginType int) (int64, error) {
url := strings.Join([]string{serviceGateway.baseURL, "auth", "serverLogin"}, "/")
request := serviceGateway.createRequest(url, "post")
request.Header("appKey", constant.UCENTER_APP_KEY)
options := make(map[string]interface{})
options["phone"] = strings.TrimSpace(phone)
options["password"] = strings.TrimSpace(password)
options["type"] = loginType
request.JSONBody(options)
response := make(map[string]interface{})
err := request.ToJSON(&response)
var openid int64
if err != nil {
log.Error("Service Gateway Fail:", err)
return 0, err
}
if data, ok := response["data"]; ok {
if data, ok := data.(map[string]interface{}); ok {
if data, ok := data["id"]; ok {
openid = int64(data.(float64))
//openid,err=strconv.ParseInt(fmt.Sprintf("%v",data.(float64)),10,64)
}
}
}
_, err = serviceGateway.handlerError(response)
return openid, err
}
// 修改密码
func (serviceGateway *HttplibUCenterApiServiceGateway) UpdateUser(uid int64, phone, password string) (int, error) {
url := strings.Join([]string{serviceGateway.baseURL, "users", fmt.Sprintf("%v", uid)}, "/")
request := serviceGateway.createRequest(url, "put")
//request.Header("appKey", constant.UCENTER_APP_KEY)
options := make(map[string]interface{})
if len(phone) > 0 {
options["phone"] = strings.TrimSpace(phone)
}
if len(password) > 0 {
options["password"] = strings.TrimSpace(password)
}
request.JSONBody(options)
response := make(map[string]interface{})
err := request.ToJSON(&response)
if err != nil {
log.Error("Service Gateway Fail:", err)
return 0, err
}
return serviceGateway.handlerError(response)
}
func (serviceGateway *HttplibUCenterApiServiceGateway) ChangePassword(phone, newPwd, oldPwd string) (int, error) {
url := strings.Join([]string{serviceGateway.baseURL, "users", "changePassword"}, "/")
request := serviceGateway.createRequest(url, "post")
request.Header("appKey", constant.UCENTER_APP_KEY)
options := make(map[string]interface{})
options["phone"] = strings.TrimSpace(phone)
options["newPassword"] = strings.TrimSpace(newPwd)
//options["confirmPwd"] = strings.TrimSpace(confirmPwd)
options["password"] = strings.TrimSpace(oldPwd)
request.JSONBody(options)
response := make(map[string]interface{})
err := request.ToJSON(&response)
if err != nil {
log.Error("Service Gateway Fail:", err)
return 0, err
}
return serviceGateway.handlerError(response)
}
func (serviceGateway *HttplibUCenterApiServiceGateway) ChangePhone(newPhone, oldPhone string) (int, error) {
url := strings.Join([]string{serviceGateway.baseURL, "user", "change-phone"}, "/")
request := serviceGateway.createRequest(url, "post")
options := make(map[string]interface{})
options["phone"] = strings.TrimSpace(newPhone)
options["oldPhone"] = strings.TrimSpace(oldPhone)
request.JSONBody(options)
response := make(map[string]interface{})
err := request.ToJSON(&response)
if err != nil {
log.Error("Service Gateway Fail:", err)
return 0, err
}
return serviceGateway.handlerError(response)
}
func (serviceGateway *HttplibUCenterApiServiceGateway) handlerError(in map[string]interface{}) (int, error) {
var rspCode int
var err error
if code, ok := in["code"]; ok {
rspCode, _ = strconv.Atoi(fmt.Sprintf("%v", code))
} else {
err = fmt.Errorf("网关解析错误")
}
if msg, ok := in["msg"]; ok {
msg := msg.(string)
if rspCode != 0 && len(msg) > 0 {
err = fmt.Errorf(msg)
}
}
return rspCode, err
}
func NewHttplibUCenterApiServiceGateway() *HttplibUCenterApiServiceGateway {
return &HttplibUCenterApiServiceGateway{
httplibBaseServiceGateway: httplibBaseServiceGateway{
baseURL: constant.UCENTER_SERVICE_HOST,
connectTimeout: 100 * time.Second,
readWriteTimeout: 30 * time.Second,
},
}
}