-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathuser.go
52 lines (44 loc) · 1020 Bytes
/
user.go
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
package models
import (
"encoding/json"
"fmt"
"github.com/astaxie/beego/httplib"
"github.com/dinp/builder/g"
)
type User struct {
Id int64 `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Im string `json:"im"`
Phone string `json:"phone"`
}
func GetUser(sig string) (*User, error) {
key := fmt.Sprintf("u:%s", sig)
u := g.Cache.Get(key)
if u != nil {
uobj := u.(User)
return &uobj, nil
}
uri := fmt.Sprintf("%s/sso/user/%s", g.UicInternal, sig)
req := httplib.Get(uri)
req.Param("token", g.Token)
resp, err := req.Response()
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("StatusCode: %d", resp.StatusCode)
}
decoder := json.NewDecoder(resp.Body)
type TmpStruct struct {
User *User `json:"user"`
}
var t TmpStruct
err = decoder.Decode(&t)
if err != nil {
return nil, err
}
// don't worry cache expired. we just use username which can not modify
g.Cache.Put(key, *t.User, int64(360000))
return t.User, nil
}