正在显示
9 个修改的文件
包含
77 行增加
和
21 行删除
@@ -37,7 +37,10 @@ func (c *AuthController) RefreshToken() { | @@ -37,7 +37,10 @@ func (c *AuthController) RefreshToken() { | ||
37 | return | 37 | return |
38 | } | 38 | } |
39 | err = serveauth.ResetLoginTokenRedis(logintoken) | 39 | err = serveauth.ResetLoginTokenRedis(logintoken) |
40 | - msg = protocol.NewReturnResponse(logintoken, err) | 40 | + data := protocol.ResponseLogin{ |
41 | + Access: logintoken, | ||
42 | + } | ||
43 | + msg = protocol.NewReturnResponse(data, err) | ||
41 | return | 44 | return |
42 | } | 45 | } |
43 | 46 | ||
@@ -72,13 +75,51 @@ func (c *AuthController) Login() { | @@ -72,13 +75,51 @@ func (c *AuthController) Login() { | ||
72 | if err != nil { | 75 | if err != nil { |
73 | log.Error("token 信息记录redis失败") | 76 | log.Error("token 信息记录redis失败") |
74 | } | 77 | } |
75 | - data := map[string]interface{}{ | ||
76 | - "access": logintoken, | 78 | + data := protocol.ResponseLogin{ |
79 | + Access: logintoken, | ||
77 | } | 80 | } |
78 | msg = protocol.NewReturnResponse(data, nil) | 81 | msg = protocol.NewReturnResponse(data, nil) |
79 | return | 82 | return |
80 | } | 83 | } |
81 | 84 | ||
85 | +// LoginSms 登录 | ||
86 | +// @router /login [post] | ||
87 | +func (c *AuthController) LoginSms() { | ||
88 | + var msg *protocol.ResponseMessage | ||
89 | + defer func() { | ||
90 | + c.ResposeJson(msg) | ||
91 | + }() | ||
92 | + | ||
93 | + var param protocol.RequestLogin | ||
94 | + if err := json.Unmarshal(c.Ctx.Input.RequestBody, ¶m); err != nil { | ||
95 | + log.Error("json 解析失败", err) | ||
96 | + msg = protocol.BadRequestParam("1") | ||
97 | + return | ||
98 | + } | ||
99 | + // if len(param.Account) == 0 || len(param.Password) == 0 { | ||
100 | + // msg = protocol.BadRequestParam("10021") | ||
101 | + // return | ||
102 | + // } | ||
103 | + // logintoken, err := serveauth.LoginAuthByUCenter(param.Account, param.Password) | ||
104 | + // if err != nil { | ||
105 | + // msg = protocol.NewReturnResponse(nil, err) | ||
106 | + // return | ||
107 | + // } | ||
108 | + // err = serveauth.ResetLoginToken(logintoken) | ||
109 | + // if err != nil { | ||
110 | + // log.Error("token 信息记录数据库失败") | ||
111 | + // } | ||
112 | + // err = serveauth.ResetLoginTokenRedis(logintoken) | ||
113 | + // if err != nil { | ||
114 | + // log.Error("token 信息记录redis失败") | ||
115 | + // } | ||
116 | + // data := map[string]interface{}{ | ||
117 | + // "access": logintoken, | ||
118 | + // } | ||
119 | + // msg = protocol.NewReturnResponse(data, nil) | ||
120 | + return | ||
121 | +} | ||
122 | + | ||
82 | //ChangeCompany 切换公司 | 123 | //ChangeCompany 切换公司 |
83 | //@Router /change_company [post] | 124 | //@Router /change_company [post] |
84 | func (c *AuthController) ChangeCompany() { | 125 | func (c *AuthController) ChangeCompany() { |
@@ -103,11 +144,18 @@ func (c *AuthController) ChangeCompany() { | @@ -103,11 +144,18 @@ func (c *AuthController) ChangeCompany() { | ||
103 | msg = protocol.NewReturnResponse(nil, err) | 144 | msg = protocol.NewReturnResponse(nil, err) |
104 | return | 145 | return |
105 | } | 146 | } |
147 | + err = serveauth.ResetLoginToken(logintoken) | ||
148 | + if err != nil { | ||
149 | + log.Error("token 信息记录数据库失败") | ||
150 | + } | ||
106 | err = redisdata.SetLoginToken(logintoken, userid, param.CompanyId) | 151 | err = redisdata.SetLoginToken(logintoken, userid, param.CompanyId) |
107 | if err != nil { | 152 | if err != nil { |
108 | log.Error("redisdata.SetLoginToken err:%s", err) | 153 | log.Error("redisdata.SetLoginToken err:%s", err) |
109 | } | 154 | } |
110 | - msg = protocol.NewReturnResponse(logintoken, err) | 155 | + data := protocol.ResponseLogin{ |
156 | + Access: logintoken, | ||
157 | + } | ||
158 | + msg = protocol.NewReturnResponse(data, err) | ||
111 | return | 159 | return |
112 | } | 160 | } |
113 | 161 |
@@ -51,7 +51,7 @@ var AuthToken = func(ctx *context.Context) { | @@ -51,7 +51,7 @@ var AuthToken = func(ctx *context.Context) { | ||
51 | } | 51 | } |
52 | mtoken, err = serveauth.ValidJWTToken(accesstoken) | 52 | mtoken, err = serveauth.ValidJWTToken(accesstoken) |
53 | if err == nil { | 53 | if err == nil { |
54 | - storetoken, err = redisdata.GetLoginToken(mtoken.UID) | 54 | + storetoken, err = redisdata.GetLoginToken(mtoken.UID, mtoken.CompanyID) |
55 | if err != nil { | 55 | if err != nil { |
56 | log.Error("redisdata.GetLoginToken err:%s", err) | 56 | log.Error("redisdata.GetLoginToken err:%s", err) |
57 | msg = protocol.NewMessage("10024") | 57 | msg = protocol.NewMessage("10024") |
@@ -28,7 +28,7 @@ type RequestLogin struct { | @@ -28,7 +28,7 @@ type RequestLogin struct { | ||
28 | 28 | ||
29 | //ResponseLogin 登录响应 | 29 | //ResponseLogin 登录响应 |
30 | type ResponseLogin struct { | 30 | type ResponseLogin struct { |
31 | - LoginAuthToken | 31 | + Access LoginAuthToken `json:"access"` |
32 | } | 32 | } |
33 | 33 | ||
34 | type LoginAuthToken struct { | 34 | type LoginAuthToken struct { |
@@ -158,7 +158,7 @@ func RefreshLoginToken(refreshtoken string) (protocol.LoginAuthToken, error) { | @@ -158,7 +158,7 @@ func RefreshLoginToken(refreshtoken string) (protocol.LoginAuthToken, error) { | ||
158 | log.Debug("token失效 err:%s", err) | 158 | log.Debug("token失效 err:%s", err) |
159 | return logintoken, protocol.NewErrWithMessage("10024") | 159 | return logintoken, protocol.NewErrWithMessage("10024") |
160 | } | 160 | } |
161 | - storetoken, err = redisdata.GetLoginToken(mtoken.UID) | 161 | + storetoken, err = redisdata.GetLoginToken(mtoken.UID, mtoken.CompanyID) |
162 | if err != nil { | 162 | if err != nil { |
163 | log.Error("redis err:%s", err) | 163 | log.Error("redis err:%s", err) |
164 | return logintoken, protocol.NewErrWithMessage("10024") | 164 | return logintoken, protocol.NewErrWithMessage("10024") |
@@ -155,9 +155,10 @@ func initAdminUser(centerCompany protocol.CenterCompanyInfo, o orm.Ormer) (*mode | @@ -155,9 +155,10 @@ func initAdminUser(centerCompany protocol.CenterCompanyInfo, o orm.Ormer) (*mode | ||
155 | return nil, err | 155 | return nil, err |
156 | } | 156 | } |
157 | } else if err == orm.ErrNoRows { | 157 | } else if err == orm.ErrNoRows { |
158 | - ucenterUser, err := ucenter.RequestUCenterAddUser(centerCompany.AdminAccount, centerCompany.AdminName, "") | 158 | + ucenterUser, err := ucenter.RequestUCenterAddUser(centerCompany.AdminAccount, centerCompany.AdminName, "http://这是默认图.jpeg") |
159 | if err != nil { | 159 | if err != nil { |
160 | log.Error("获取统一用户中心数据失败 :%s", err) | 160 | log.Error("获取统一用户中心数据失败 :%s", err) |
161 | + return nil, err | ||
161 | } | 162 | } |
162 | //添加用户 | 163 | //添加用户 |
163 | newUser = &models.User{ | 164 | newUser = &models.User{ |
@@ -493,7 +493,7 @@ func UserDelete(userCompanyids []int64, companyid int64) error { | @@ -493,7 +493,7 @@ func UserDelete(userCompanyids []int64, companyid int64) error { | ||
493 | return protocol.NewErrWithMessage("1") | 493 | return protocol.NewErrWithMessage("1") |
494 | } | 494 | } |
495 | for _, v := range updateIds { | 495 | for _, v := range updateIds { |
496 | - e := redisdata.DeleteLoginToken(v.UserId) | 496 | + e := redisdata.DeleteLoginToken(v.UserId, companyid) |
497 | if e != nil { | 497 | if e != nil { |
498 | log.Error(e.Error()) | 498 | log.Error(e.Error()) |
499 | } | 499 | } |
@@ -676,7 +676,7 @@ func UserForbid(userCompanyids []int64, companyid int64) error { | @@ -676,7 +676,7 @@ func UserForbid(userCompanyids []int64, companyid int64) error { | ||
676 | } | 676 | } |
677 | log.Info("更新记录数:%d", num) | 677 | log.Info("更新记录数:%d", num) |
678 | for _, v := range updateIds { | 678 | for _, v := range updateIds { |
679 | - e := redisdata.DeleteLoginToken(v.UserId) | 679 | + e := redisdata.DeleteLoginToken(v.UserId, companyid) |
680 | if e != nil { | 680 | if e != nil { |
681 | log.Error(e.Error()) | 681 | log.Error(e.Error()) |
682 | } | 682 | } |
@@ -43,6 +43,7 @@ func (client UCenterClient) buildHeader() http.Header { | @@ -43,6 +43,7 @@ func (client UCenterClient) buildHeader() http.Header { | ||
43 | h.Set("nonce", "") | 43 | h.Set("nonce", "") |
44 | h.Set("curTime", nowTime) | 44 | h.Set("curTime", nowTime) |
45 | h.Set("checkSum", checksum) | 45 | h.Set("checkSum", checksum) |
46 | + h.Set("Accept", "application/json") | ||
46 | return h | 47 | return h |
47 | } | 48 | } |
48 | 49 | ||
@@ -64,7 +65,7 @@ func (client UCenterClient) httpDo(path string, mathod string, posts []byte) ([] | @@ -64,7 +65,7 @@ func (client UCenterClient) httpDo(path string, mathod string, posts []byte) ([] | ||
64 | } | 65 | } |
65 | req.Header = client.buildHeader() | 66 | req.Header = client.buildHeader() |
66 | resp, err := httpclient.Do(req) | 67 | resp, err := httpclient.Do(req) |
67 | - | 68 | + log.Info("====>Send To URL:%s", reqURL) |
68 | log.Info("====>Send To UCenter:%s", string(posts)) | 69 | log.Info("====>Send To UCenter:%s", string(posts)) |
69 | // log.Info("====>Send Header:%v", req.Header) | 70 | // log.Info("====>Send Header:%v", req.Header) |
70 | if err != nil { | 71 | if err != nil { |
1 | package redisdata | 1 | package redisdata |
2 | 2 | ||
3 | +import "fmt" | ||
4 | + | ||
3 | const ( | 5 | const ( |
4 | KEY_PREFIX string = "oppmg_" //统一前缀 | 6 | KEY_PREFIX string = "oppmg_" //统一前缀 |
5 | KEY_USER_TOKEN string = "token" //登录token存储 | 7 | KEY_USER_TOKEN string = "token" //登录token存储 |
@@ -10,3 +12,8 @@ type RedisLoginToken struct { | @@ -10,3 +12,8 @@ type RedisLoginToken struct { | ||
10 | RefreshToken string `json:"refresh_token"` | 12 | RefreshToken string `json:"refresh_token"` |
11 | CurrentCompany int64 `json:"current_company"` | 13 | CurrentCompany int64 `json:"current_company"` |
12 | } | 14 | } |
15 | + | ||
16 | +func GetKeyLoginToken(userid int64, companyid int64) string { | ||
17 | + key := fmt.Sprintf("%s%s:%d_%d", KEY_PREFIX, KEY_USER_TOKEN, userid, companyid) | ||
18 | + return key | ||
19 | +} |
@@ -2,7 +2,6 @@ package redisdata | @@ -2,7 +2,6 @@ package redisdata | ||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "encoding/json" | 4 | "encoding/json" |
5 | - "fmt" | ||
6 | "oppmg/common/log" | 5 | "oppmg/common/log" |
7 | "oppmg/common/redis" | 6 | "oppmg/common/redis" |
8 | "oppmg/protocol" | 7 | "oppmg/protocol" |
@@ -26,12 +25,12 @@ func SetLoginToken(param protocol.LoginAuthToken, userid int64, companyid int64) | @@ -26,12 +25,12 @@ func SetLoginToken(param protocol.LoginAuthToken, userid int64, companyid int64) | ||
26 | } | 25 | } |
27 | value, _ = json.Marshal(data) | 26 | value, _ = json.Marshal(data) |
28 | exp = param.RefreshExpires - nowTime | 27 | exp = param.RefreshExpires - nowTime |
29 | - key = fmt.Sprintf("%s%s:%d", KEY_PREFIX, KEY_USER_TOKEN, userid) | 28 | + key = GetKeyLoginToken(userid, companyid) |
30 | err = client.Set(key, string(value), time.Duration(exp)*time.Second).Err() | 29 | err = client.Set(key, string(value), time.Duration(exp)*time.Second).Err() |
31 | return err | 30 | return err |
32 | } | 31 | } |
33 | 32 | ||
34 | -func GetLoginToken(userid int64) (RedisLoginToken, error) { | 33 | +func GetLoginToken(userid int64, companyid int64) (RedisLoginToken, error) { |
35 | client := redis.GetRedis() | 34 | client := redis.GetRedis() |
36 | var ( | 35 | var ( |
37 | key string | 36 | key string |
@@ -39,7 +38,7 @@ func GetLoginToken(userid int64) (RedisLoginToken, error) { | @@ -39,7 +38,7 @@ func GetLoginToken(userid int64) (RedisLoginToken, error) { | ||
39 | err error | 38 | err error |
40 | result RedisLoginToken | 39 | result RedisLoginToken |
41 | ) | 40 | ) |
42 | - key = fmt.Sprintf("%s%s:%d", KEY_PREFIX, KEY_USER_TOKEN, userid) | 41 | + key = GetKeyLoginToken(userid, companyid) |
43 | value, err = client.Get(key).Result() | 42 | value, err = client.Get(key).Result() |
44 | if err != nil { | 43 | if err != nil { |
45 | return result, err | 44 | return result, err |
@@ -53,9 +52,9 @@ func GetLoginToken(userid int64) (RedisLoginToken, error) { | @@ -53,9 +52,9 @@ func GetLoginToken(userid int64) (RedisLoginToken, error) { | ||
53 | return result, nil | 52 | return result, nil |
54 | } | 53 | } |
55 | 54 | ||
56 | -func ExistLoginToken(userid int64) bool { | 55 | +func ExistLoginToken(userid int64, companyid int64) bool { |
57 | client := redis.GetRedis() | 56 | client := redis.GetRedis() |
58 | - key := fmt.Sprintf("%s%s:%d", KEY_PREFIX, KEY_USER_TOKEN, userid) | 57 | + key := GetKeyLoginToken(userid, companyid) |
59 | value := client.Exists(key).Val() | 58 | value := client.Exists(key).Val() |
60 | if value > 0 { | 59 | if value > 0 { |
61 | return true | 60 | return true |
@@ -63,17 +62,17 @@ func ExistLoginToken(userid int64) bool { | @@ -63,17 +62,17 @@ func ExistLoginToken(userid int64) bool { | ||
63 | return false | 62 | return false |
64 | } | 63 | } |
65 | 64 | ||
66 | -func DeleteLoginToken(userid int64) error { | 65 | +func DeleteLoginToken(userid int64, companyid int64) error { |
67 | client := redis.GetRedis() | 66 | client := redis.GetRedis() |
68 | - key := fmt.Sprintf("%s%s:%d", KEY_PREFIX, KEY_USER_TOKEN, userid) | 67 | + key := GetKeyLoginToken(userid, companyid) |
69 | err := client.Del(key).Err() | 68 | err := client.Del(key).Err() |
70 | return err | 69 | return err |
71 | } | 70 | } |
72 | 71 | ||
73 | // 刷新token 的有效期 | 72 | // 刷新token 的有效期 |
74 | -func RefreshLoginTokenExpires(userid int64) error { | 73 | +func RefreshLoginTokenExpires(userid int64, companyid int64) error { |
75 | client := redis.GetRedis() | 74 | client := redis.GetRedis() |
76 | - key := fmt.Sprintf("%s%s:%d", KEY_PREFIX, KEY_USER_TOKEN, userid) | 75 | + key := GetKeyLoginToken(userid, companyid) |
77 | result, err := client.Exists(key).Result() | 76 | result, err := client.Exists(key).Result() |
78 | if err != nil { | 77 | if err != nil { |
79 | return err | 78 | return err |
-
请 注册 或 登录 后发表评论