正在显示
30 个修改的文件
包含
800 行增加
和
72 行删除
@@ -27,6 +27,7 @@ require ( | @@ -27,6 +27,7 @@ require ( | ||
27 | github.com/yudai/gojsondiff v1.0.0 // indirect | 27 | github.com/yudai/gojsondiff v1.0.0 // indirect |
28 | github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect | 28 | github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect |
29 | github.com/yudai/pp v2.0.1+incompatible // indirect | 29 | github.com/yudai/pp v2.0.1+incompatible // indirect |
30 | + github.com/dgrijalva/jwt-go v3.2.0+incompatible | ||
30 | ) | 31 | ) |
31 | 32 | ||
32 | replace gitlab.fjmaimaimai.com/mmm-go/partner => ../partner | 33 | replace gitlab.fjmaimaimai.com/mmm-go/partner => ../partner |
@@ -3,7 +3,7 @@ package main | @@ -3,7 +3,7 @@ package main | ||
3 | import ( | 3 | import ( |
4 | "github.com/astaxie/beego" | 4 | "github.com/astaxie/beego" |
5 | _ "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg" | 5 | _ "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg" |
6 | - _ "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/beego" | 6 | + _ "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr" |
7 | ) | 7 | ) |
8 | 8 | ||
9 | func main() { | 9 | func main() { |
@@ -8,7 +8,8 @@ import ( | @@ -8,7 +8,8 @@ import ( | ||
8 | 8 | ||
9 | type GetPartnerInfoQuery struct { | 9 | type GetPartnerInfoQuery struct { |
10 | // 合伙人ID | 10 | // 合伙人ID |
11 | - Id int `json:"id" valid:"Required"` | 11 | + Id int `json:"id,omitempty"` |
12 | + Account string `json:"account,omitempty"` | ||
12 | } | 13 | } |
13 | 14 | ||
14 | func (q *GetPartnerInfoQuery) ValidateQuery() error { | 15 | func (q *GetPartnerInfoQuery) ValidateQuery() error { |
@@ -7,6 +7,8 @@ import ( | @@ -7,6 +7,8 @@ import ( | ||
7 | ) | 7 | ) |
8 | 8 | ||
9 | type ListPartnerInfoQuery struct { | 9 | type ListPartnerInfoQuery struct { |
10 | + //账号 | ||
11 | + Account string `json:"account,omitempty"` | ||
10 | //状态 | 12 | //状态 |
11 | Status int `json:"status,omitempty"` | 13 | Status int `json:"status,omitempty"` |
12 | // 合伙人类别 | 14 | // 合伙人类别 |
@@ -8,6 +8,7 @@ import ( | @@ -8,6 +8,7 @@ import ( | ||
8 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/command" | 8 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/command" |
9 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/query" | 9 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/query" |
10 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain" | 10 | "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain" |
11 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils" | ||
11 | "time" | 12 | "time" |
12 | ) | 13 | ) |
13 | 14 | ||
@@ -16,7 +17,7 @@ type PartnerInfoService struct { | @@ -16,7 +17,7 @@ type PartnerInfoService struct { | ||
16 | } | 17 | } |
17 | 18 | ||
18 | // 创建客户价值 | 19 | // 创建客户价值 |
19 | -func (PartnerInfoService *PartnerInfoService) CreatePartnerInfo(command *command.CreatePartnerInfoCommand) (data interface{}, err error) { | 20 | +func (PartnerInfoService *PartnerInfoService) CreatePartnerInfo(command *command.CreatePartnerInfoCommand) (data *domain.PartnerInfo, err error) { |
20 | var ( | 21 | var ( |
21 | count int64 | 22 | count int64 |
22 | transactionContext, _ = factory.CreateTransactionContext(nil) | 23 | transactionContext, _ = factory.CreateTransactionContext(nil) |
@@ -56,7 +57,7 @@ func (PartnerInfoService *PartnerInfoService) CreatePartnerInfo(command *command | @@ -56,7 +57,7 @@ func (PartnerInfoService *PartnerInfoService) CreatePartnerInfo(command *command | ||
56 | } | 57 | } |
57 | 58 | ||
58 | // 返回客户价值 | 59 | // 返回客户价值 |
59 | -func (PartnerInfoService *PartnerInfoService) GetPartnerInfo(command *query.GetPartnerInfoQuery) (data interface{}, err error) { | 60 | +func (PartnerInfoService *PartnerInfoService) GetPartnerInfo(command *query.GetPartnerInfoQuery) (data *domain.PartnerInfo, err error) { |
60 | var ( | 61 | var ( |
61 | transactionContext, _ = factory.CreateTransactionContext(nil) | 62 | transactionContext, _ = factory.CreateTransactionContext(nil) |
62 | ) | 63 | ) |
@@ -75,7 +76,7 @@ func (PartnerInfoService *PartnerInfoService) GetPartnerInfo(command *query.GetP | @@ -75,7 +76,7 @@ func (PartnerInfoService *PartnerInfoService) GetPartnerInfo(command *query.GetP | ||
75 | if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{"transactionContext": transactionContext}); err != nil { | 76 | if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{"transactionContext": transactionContext}); err != nil { |
76 | return nil, err | 77 | return nil, err |
77 | } | 78 | } |
78 | - data, err = PartnerInfoRepository.FindOne(map[string]interface{}{"Id": command.Id}) | 79 | + data, err = PartnerInfoRepository.FindOne(utils.ObjectJsonToMap(command)) |
79 | if err != nil { | 80 | if err != nil { |
80 | return nil, err | 81 | return nil, err |
81 | } | 82 | } |
@@ -87,12 +88,12 @@ func (PartnerInfoService *PartnerInfoService) GetPartnerInfo(command *query.GetP | @@ -87,12 +88,12 @@ func (PartnerInfoService *PartnerInfoService) GetPartnerInfo(command *query.GetP | ||
87 | } | 88 | } |
88 | 89 | ||
89 | // 更新客户价值 | 90 | // 更新客户价值 |
90 | -func (PartnerInfoService *PartnerInfoService) UpdatePartnerInfo(updatePartnerInfoCommand *command.UpdatePartnerInfoCommand) (data interface{}, err error) { | 91 | +func (PartnerInfoService *PartnerInfoService) UpdatePartnerInfo(command *command.UpdatePartnerInfoCommand) (data *domain.PartnerInfo, err error) { |
91 | var ( | 92 | var ( |
92 | transactionContext, _ = factory.CreateTransactionContext(nil) | 93 | transactionContext, _ = factory.CreateTransactionContext(nil) |
93 | PartnerInfo *domain.PartnerInfo | 94 | PartnerInfo *domain.PartnerInfo |
94 | ) | 95 | ) |
95 | - if err = updatePartnerInfoCommand.ValidateCommand(); err != nil { | 96 | + if err = command.ValidateCommand(); err != nil { |
96 | return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | 97 | return nil, application.ThrowError(application.ARG_ERROR, err.Error()) |
97 | } | 98 | } |
98 | if err := transactionContext.StartTransaction(); err != nil { | 99 | if err := transactionContext.StartTransaction(); err != nil { |
@@ -107,12 +108,12 @@ func (PartnerInfoService *PartnerInfoService) UpdatePartnerInfo(updatePartnerInf | @@ -107,12 +108,12 @@ func (PartnerInfoService *PartnerInfoService) UpdatePartnerInfo(updatePartnerInf | ||
107 | if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{"transactionContext": transactionContext}); err != nil { | 108 | if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{"transactionContext": transactionContext}); err != nil { |
108 | return nil, err | 109 | return nil, err |
109 | } | 110 | } |
110 | - PartnerInfo, err = PartnerInfoRepository.FindOne(map[string]interface{}{"Id": updatePartnerInfoCommand.Id}) | 111 | + PartnerInfo, err = PartnerInfoRepository.FindOne(map[string]interface{}{"Id": command.Id}) |
111 | if err != nil { | 112 | if err != nil { |
112 | return nil, err | 113 | return nil, err |
113 | } | 114 | } |
114 | if PartnerInfo == nil { | 115 | if PartnerInfo == nil { |
115 | - return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updatePartnerInfoCommand.Id))) | 116 | + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(command.Id))) |
116 | } | 117 | } |
117 | //if count, PartnerInfos, err := PartnerInfoRepository.Find(map[string]interface{}{ | 118 | //if count, PartnerInfos, err := PartnerInfoRepository.Find(map[string]interface{}{ |
118 | // "PartnerInfoName": updatePartnerInfoCommand.PartnerInfoName, | 119 | // "PartnerInfoName": updatePartnerInfoCommand.PartnerInfoName, |
@@ -124,7 +125,7 @@ func (PartnerInfoService *PartnerInfoService) UpdatePartnerInfo(updatePartnerInf | @@ -124,7 +125,7 @@ func (PartnerInfoService *PartnerInfoService) UpdatePartnerInfo(updatePartnerInf | ||
124 | // return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "标签不可重复名称") | 125 | // return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "标签不可重复名称") |
125 | // } | 126 | // } |
126 | //} | 127 | //} |
127 | - if err := PartnerInfo.Update(tool_funs.SimpleStructToMap(updatePartnerInfoCommand)); err != nil { | 128 | + if err := PartnerInfo.Update(tool_funs.SimpleStructToMap(command)); err != nil { |
128 | return nil, err | 129 | return nil, err |
129 | } | 130 | } |
130 | 131 | ||
@@ -136,12 +137,12 @@ func (PartnerInfoService *PartnerInfoService) UpdatePartnerInfo(updatePartnerInf | @@ -136,12 +137,12 @@ func (PartnerInfoService *PartnerInfoService) UpdatePartnerInfo(updatePartnerInf | ||
136 | } | 137 | } |
137 | 138 | ||
138 | // 移除客户价值 | 139 | // 移除客户价值 |
139 | -func (PartnerInfoService *PartnerInfoService) RemovePartnerInfo(removePartnerInfoCommand *command.RemovePartnerInfoCommand) (data interface{}, err error) { | 140 | +func (PartnerInfoService *PartnerInfoService) RemovePartnerInfo(command *command.RemovePartnerInfoCommand) (data *domain.PartnerInfo, err error) { |
140 | var ( | 141 | var ( |
141 | transactionContext, _ = factory.CreateTransactionContext(nil) | 142 | transactionContext, _ = factory.CreateTransactionContext(nil) |
142 | PartnerInfo *domain.PartnerInfo | 143 | PartnerInfo *domain.PartnerInfo |
143 | ) | 144 | ) |
144 | - if err = removePartnerInfoCommand.ValidateCommand(); err != nil { | 145 | + if err = command.ValidateCommand(); err != nil { |
145 | return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | 146 | return nil, application.ThrowError(application.ARG_ERROR, err.Error()) |
146 | } | 147 | } |
147 | if err = transactionContext.StartTransaction(); err != nil { | 148 | if err = transactionContext.StartTransaction(); err != nil { |
@@ -156,12 +157,12 @@ func (PartnerInfoService *PartnerInfoService) RemovePartnerInfo(removePartnerInf | @@ -156,12 +157,12 @@ func (PartnerInfoService *PartnerInfoService) RemovePartnerInfo(removePartnerInf | ||
156 | if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{"transactionContext": transactionContext}); err != nil { | 157 | if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{"transactionContext": transactionContext}); err != nil { |
157 | return nil, err | 158 | return nil, err |
158 | } | 159 | } |
159 | - PartnerInfo, err = PartnerInfoRepository.FindOne(map[string]interface{}{"Id": removePartnerInfoCommand.Id}) | 160 | + PartnerInfo, err = PartnerInfoRepository.FindOne(map[string]interface{}{"Id": command.Id}) |
160 | if err != nil { | 161 | if err != nil { |
161 | return nil, err | 162 | return nil, err |
162 | } | 163 | } |
163 | if PartnerInfo == nil { | 164 | if PartnerInfo == nil { |
164 | - return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removePartnerInfoCommand.Id))) | 165 | + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(command.Id))) |
165 | } | 166 | } |
166 | if data, err = PartnerInfoRepository.Remove(PartnerInfo); err != nil { | 167 | if data, err = PartnerInfoRepository.Remove(PartnerInfo); err != nil { |
167 | return nil, err | 168 | return nil, err |
@@ -171,13 +172,13 @@ func (PartnerInfoService *PartnerInfoService) RemovePartnerInfo(removePartnerInf | @@ -171,13 +172,13 @@ func (PartnerInfoService *PartnerInfoService) RemovePartnerInfo(removePartnerInf | ||
171 | } | 172 | } |
172 | 173 | ||
173 | // 返回客户价值列表 | 174 | // 返回客户价值列表 |
174 | -func (PartnerInfoService *PartnerInfoService) ListPartnerInfo(listPartnerInfoQuery *query.ListPartnerInfoQuery) (data interface{}, err error) { | 175 | +func (PartnerInfoService *PartnerInfoService) ListPartnerInfo(command *query.ListPartnerInfoQuery) (data interface{}, err error) { |
175 | var ( | 176 | var ( |
176 | transactionContext, _ = factory.CreateTransactionContext(nil) | 177 | transactionContext, _ = factory.CreateTransactionContext(nil) |
177 | PartnerInfos []*domain.PartnerInfo | 178 | PartnerInfos []*domain.PartnerInfo |
178 | count int64 | 179 | count int64 |
179 | ) | 180 | ) |
180 | - if err = listPartnerInfoQuery.ValidateQuery(); err != nil { | 181 | + if err = command.ValidateQuery(); err != nil { |
181 | return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | 182 | return nil, application.ThrowError(application.ARG_ERROR, err.Error()) |
182 | } | 183 | } |
183 | if err := transactionContext.StartTransaction(); err != nil { | 184 | if err := transactionContext.StartTransaction(); err != nil { |
@@ -192,7 +193,7 @@ func (PartnerInfoService *PartnerInfoService) ListPartnerInfo(listPartnerInfoQue | @@ -192,7 +193,7 @@ func (PartnerInfoService *PartnerInfoService) ListPartnerInfo(listPartnerInfoQue | ||
192 | if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{"transactionContext": transactionContext}); err != nil { | 193 | if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{"transactionContext": transactionContext}); err != nil { |
193 | return nil, err | 194 | return nil, err |
194 | } | 195 | } |
195 | - if count, PartnerInfos, err = PartnerInfoRepository.Find(tool_funs.SimpleStructToMap(listPartnerInfoQuery)); err != nil { | 196 | + if count, PartnerInfos, err = PartnerInfoRepository.Find(tool_funs.SimpleStructToMap(command)); err != nil { |
196 | return nil, err | 197 | return nil, err |
197 | } | 198 | } |
198 | if err = transactionContext.CommitTransaction(); err != nil { | 199 | if err = transactionContext.CommitTransaction(); err != nil { |
pkg/domain/order.go
0 → 100644
1 | +package domain | ||
2 | + | ||
3 | +//Order 订单信息 | ||
4 | +type Order struct { | ||
5 | + //id | ||
6 | + OrderId int64 `json:"orderId"` | ||
7 | + //账号 | ||
8 | + OrderName string `json:"orderName"` | ||
9 | + //密码 | ||
10 | + OrderCount int `json:"orderCount"` | ||
11 | + //管理员名称 | ||
12 | + OrderAmount string `json:"admin_name"` | ||
13 | + //是否是默认系统账号 | ||
14 | + IsDefault bool `json:"is_default"` | ||
15 | + //账号是否可用 | ||
16 | + IsUsable bool `json:"is_userable"` | ||
17 | + //创建时间 | ||
18 | + CreateAt string `json:"create_at"` | ||
19 | + //用户权限 | ||
20 | + Permission []string `json:"permission"` | ||
21 | +} |
@@ -23,6 +23,10 @@ type PartnerInfo struct { | @@ -23,6 +23,10 @@ type PartnerInfo struct { | ||
23 | // 合伙类别 (1.研发合伙人 2.业务合伙人 3.事业) | 23 | // 合伙类别 (1.研发合伙人 2.业务合伙人 3.事业) |
24 | PartnerCategory int `json:"partnerCategory"` | 24 | PartnerCategory int `json:"partnerCategory"` |
25 | 25 | ||
26 | + //所属区域信息 | ||
27 | + RegionInfo *RegionInfo `json:"regionId"` | ||
28 | + //合作时间 | ||
29 | + CooperateTime time.Time `json:"cooperateTime"` | ||
26 | //创建时间 | 30 | //创建时间 |
27 | CreateAt time.Time `json:"createAt"` | 31 | CreateAt time.Time `json:"createAt"` |
28 | //更新时间 | 32 | //更新时间 |
pkg/domain/region_info.go
0 → 100644
@@ -7,7 +7,7 @@ import ( | @@ -7,7 +7,7 @@ import ( | ||
7 | ) | 7 | ) |
8 | 8 | ||
9 | type PartnerInfo struct { | 9 | type PartnerInfo struct { |
10 | - tableName struct{} `pg:"partner_infos,alias:partner_info"` | 10 | + tableName struct{} `pg:"partner_info"` |
11 | // 合伙人ID | 11 | // 合伙人ID |
12 | Id int64 `pg:",pk"` | 12 | Id int64 `pg:",pk"` |
13 | // 合伙人姓名 | 13 | // 合伙人姓名 |
@@ -21,6 +21,10 @@ type PartnerInfo struct { | @@ -21,6 +21,10 @@ type PartnerInfo struct { | ||
21 | // 合伙类别 (1.研发合伙人 2.业务合伙人 3.事业) | 21 | // 合伙类别 (1.研发合伙人 2.业务合伙人 3.事业) |
22 | PartnerCategory int `pg:",notnull,default:1"` | 22 | PartnerCategory int `pg:",notnull,default:1"` |
23 | 23 | ||
24 | + //所属区域信息 | ||
25 | + RegionInfo *domain.RegionInfo | ||
26 | + //合作时间 | ||
27 | + CooperateTime time.Time | ||
24 | //创建时间 | 28 | //创建时间 |
25 | CreateAt time.Time | 29 | CreateAt time.Time |
26 | //更新时间 | 30 | //更新时间 |
@@ -20,10 +20,10 @@ func (repository *PartnerInfoRepository) Save(dm *domain.PartnerInfo) (*domain.P | @@ -20,10 +20,10 @@ func (repository *PartnerInfoRepository) Save(dm *domain.PartnerInfo) (*domain.P | ||
20 | return nil, err | 20 | return nil, err |
21 | } | 21 | } |
22 | if dm.Identify() == nil { | 22 | if dm.Identify() == nil { |
23 | - if dm.Id, err = NewSnowflakeId(); err != nil { | ||
24 | - return dm, err | ||
25 | - } | ||
26 | - m.Id = dm.Id | 23 | + //if dm.Id, err = NewSnowflakeId(); err != nil { |
24 | + // return dm, err | ||
25 | + //} | ||
26 | + //m.Id = dm.Id | ||
27 | if err = tx.Insert(m); err != nil { | 27 | if err = tx.Insert(m); err != nil { |
28 | return nil, err | 28 | return nil, err |
29 | } | 29 | } |
@@ -51,6 +51,7 @@ func (repository *PartnerInfoRepository) FindOne(queryOptions map[string]interfa | @@ -51,6 +51,7 @@ func (repository *PartnerInfoRepository) FindOne(queryOptions map[string]interfa | ||
51 | PartnerInfoModel := new(models.PartnerInfo) | 51 | PartnerInfoModel := new(models.PartnerInfo) |
52 | query := NewQuery(tx.Model(PartnerInfoModel), queryOptions) | 52 | query := NewQuery(tx.Model(PartnerInfoModel), queryOptions) |
53 | query.SetWhere("partner_info.id = ?", "id") | 53 | query.SetWhere("partner_info.id = ?", "id") |
54 | + query.SetWhere("partner_info.account = ?", "account") | ||
54 | if err := query.First(); err != nil { | 55 | if err := query.First(); err != nil { |
55 | return nil, query.HandleError(err, "没有此合伙人") | 56 | return nil, query.HandleError(err, "没有此合伙人") |
56 | } | 57 | } |
pkg/infrastructure/utils/jwt.go
0 → 100644
1 | +package utils | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/dgrijalva/jwt-go" | ||
5 | + "time" | ||
6 | +) | ||
7 | + | ||
8 | +type UserTokenClaims struct { | ||
9 | + UserId int64 `json:"userId"` | ||
10 | + jwt.StandardClaims | ||
11 | +} | ||
12 | + | ||
13 | +var jwtSecret = []byte("123456") | ||
14 | + | ||
15 | +//解析 UserTokenClaims | ||
16 | +func ParseJWTToken(token string) (*UserTokenClaims, error) { | ||
17 | + tokenClaims, err := jwt.ParseWithClaims(token, &UserTokenClaims{}, func(token *jwt.Token) (interface{}, error) { | ||
18 | + return jwtSecret, nil | ||
19 | + }) | ||
20 | + if err != nil { | ||
21 | + return nil, err | ||
22 | + } | ||
23 | + if tokenClaims != nil { | ||
24 | + if claim, ok := tokenClaims.Claims.(*UserTokenClaims); ok && tokenClaims.Valid { | ||
25 | + return claim, nil | ||
26 | + } | ||
27 | + } | ||
28 | + | ||
29 | + return nil, err | ||
30 | +} | ||
31 | + | ||
32 | +//生成token | ||
33 | +//@uid 用户变化 | ||
34 | +//@password 密码 | ||
35 | +//@expire 过期时长 | ||
36 | +func GenerateToken(uid int64, expire time.Duration) (string, error) { | ||
37 | + now := time.Now() | ||
38 | + expireTime := now.Add(expire) | ||
39 | + claims := UserTokenClaims{ | ||
40 | + UserId: uid, | ||
41 | + StandardClaims: jwt.StandardClaims{ | ||
42 | + ExpiresAt: expireTime.Unix(), | ||
43 | + Issuer: "jwt", | ||
44 | + }, | ||
45 | + } | ||
46 | + | ||
47 | + tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) | ||
48 | + token, err := tokenClaims.SignedString(jwtSecret) | ||
49 | + return token, err | ||
50 | +} |
pkg/infrastructure/utils/struct_tool.go
0 → 100644
1 | +package utils | ||
2 | + | ||
3 | +import ( | ||
4 | + "bytes" | ||
5 | + "encoding/json" | ||
6 | +) | ||
7 | + | ||
8 | +func ObjectJsonToMap(obj interface{}) (v map[string]interface{}) { | ||
9 | + var data bytes.Buffer | ||
10 | + enc := json.NewEncoder(&data) | ||
11 | + enc.Encode(obj) | ||
12 | + | ||
13 | + decoder := json.NewDecoder(&data) | ||
14 | + decoder.UseNumber() | ||
15 | + decoder.Decode(&v) | ||
16 | + return | ||
17 | +} |
1 | -package beego | 1 | +package appsvr |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "github.com/astaxie/beego" | 4 | "github.com/astaxie/beego" |
5 | "github.com/linmadan/egglib-go/web/beego/filters" | 5 | "github.com/linmadan/egglib-go/web/beego/filters" |
6 | . "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log" | 6 | . "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log" |
7 | - _ "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/beego/routers" | ||
8 | - _ "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/beego/routers/manage" | 7 | + _ "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr/routers" |
9 | ) | 8 | ) |
10 | 9 | ||
11 | func init() { | 10 | func init() { |
pkg/port/appsvr/controllers/auth.go
0 → 100644
1 | +package controllers | ||
2 | + | ||
3 | +import ( | ||
4 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr/protocol" | ||
5 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr/services/auth" | ||
6 | +) | ||
7 | + | ||
8 | +type AuthController struct { | ||
9 | + BaseController | ||
10 | +} | ||
11 | + | ||
12 | +// Login | ||
13 | +// @router /login [post] | ||
14 | +func (this *AuthController) Login() { | ||
15 | + var msg *protocol.ResponseMessage | ||
16 | + defer func() { | ||
17 | + this.Resp(msg) | ||
18 | + }() | ||
19 | + var request *protocol.LoginRequest | ||
20 | + if err := this.JsonUnmarshal(&request); err != nil { | ||
21 | + msg = protocol.BadRequestParam(1) | ||
22 | + return | ||
23 | + } | ||
24 | + if b, m := this.Valid(request); !b { | ||
25 | + msg = m | ||
26 | + return | ||
27 | + } | ||
28 | + header := this.GetRequestHeader(this.Ctx) | ||
29 | + msg = protocol.NewReturnResponse(auth.Login(header, request)) | ||
30 | +} | ||
31 | + | ||
32 | +//SmsCode | ||
33 | +// @router /smsCode [post] | ||
34 | +func (this *AuthController) SmsCode() { | ||
35 | + var msg *protocol.ResponseMessage | ||
36 | + defer func() { | ||
37 | + this.Resp(msg) | ||
38 | + }() | ||
39 | + var request *protocol.SmsCodeRequest | ||
40 | + if err := this.JsonUnmarshal(&request); err != nil { | ||
41 | + msg = protocol.BadRequestParam(1) | ||
42 | + return | ||
43 | + } | ||
44 | + if b, m := this.Valid(request); !b { | ||
45 | + msg = m | ||
46 | + return | ||
47 | + } | ||
48 | + msg = protocol.NewReturnResponse(auth.SmsCode(request)) | ||
49 | +} | ||
50 | + | ||
51 | +//AccessToken | ||
52 | +// @router /accessToken [post] | ||
53 | +func (this *AuthController) AccessToken() { | ||
54 | + var msg *protocol.ResponseMessage | ||
55 | + defer func() { | ||
56 | + this.Resp(msg) | ||
57 | + }() | ||
58 | + var request *protocol.AccessTokenRequest | ||
59 | + if err := this.JsonUnmarshal(&request); err != nil { | ||
60 | + msg = protocol.BadRequestParam(1) | ||
61 | + return | ||
62 | + } | ||
63 | + if b, m := this.Valid(request); !b { | ||
64 | + msg = m | ||
65 | + return | ||
66 | + } | ||
67 | + msg = protocol.NewReturnResponse(auth.AccessToken(request)) | ||
68 | +} | ||
69 | + | ||
70 | +//RefreshToken | ||
71 | +// @router /refreshToken [post] | ||
72 | +func (this *AuthController) RefreshToken() { | ||
73 | + var msg *protocol.ResponseMessage | ||
74 | + defer func() { | ||
75 | + this.Resp(msg) | ||
76 | + }() | ||
77 | + var request *protocol.RefreshTokenRequest | ||
78 | + if err := this.JsonUnmarshal(&request); err != nil { | ||
79 | + msg = protocol.BadRequestParam(1) | ||
80 | + return | ||
81 | + } | ||
82 | + if b, m := this.Valid(request); !b { | ||
83 | + msg = m | ||
84 | + return | ||
85 | + } | ||
86 | + msg = protocol.NewReturnResponse(auth.RefreshToken(request)) | ||
87 | +} | ||
88 | + | ||
89 | +//Revoke | ||
90 | +//@router /revoke [post] | ||
91 | +func (this *AuthController) Revoke() { | ||
92 | + var msg *protocol.ResponseMessage | ||
93 | + defer func() { | ||
94 | + this.Resp(msg) | ||
95 | + }() | ||
96 | + //var request *protocol.RevokeRequest | ||
97 | + //if err := json.Unmarshal(this.ByteBody, &request); err != nil { | ||
98 | + // log.Error(err) | ||
99 | + // msg = protocol.BadRequestParam(1) | ||
100 | + // return | ||
101 | + //} | ||
102 | + //if b, m := this.Valid(request); !b { | ||
103 | + // msg = m | ||
104 | + // return | ||
105 | + //} | ||
106 | + //header := controllers.GetRequestHeader(this.Ctx) | ||
107 | + //msg = protocol.NewReturnResponse(auth.Revoke(header, request)) | ||
108 | +} | ||
109 | + | ||
110 | +//UpdateDevice | ||
111 | +//@router /updateDevice [post] | ||
112 | +func (this *AuthController) UpdateDevice() { | ||
113 | + var msg *protocol.ResponseMessage | ||
114 | + defer func() { | ||
115 | + this.Resp(msg) | ||
116 | + }() | ||
117 | + //var request *protocol.UpdateDeviceRequest | ||
118 | + //if err := json.Unmarshal(this.ByteBody, &request); err != nil { | ||
119 | + // log.Error(err) | ||
120 | + // msg = protocol.BadRequestParam(1) | ||
121 | + // return | ||
122 | + //} | ||
123 | + //if b, m := this.Valid(request); !b { | ||
124 | + // msg = m | ||
125 | + // return | ||
126 | + //} | ||
127 | + //header := controllers.GetRequestHeader(this.Ctx) | ||
128 | + //msg = protocol.NewReturnResponse(auth.UpdateDevice(header, request)) | ||
129 | +} |
@@ -3,8 +3,13 @@ package controllers | @@ -3,8 +3,13 @@ package controllers | ||
3 | import ( | 3 | import ( |
4 | "encoding/json" | 4 | "encoding/json" |
5 | "github.com/astaxie/beego" | 5 | "github.com/astaxie/beego" |
6 | + "github.com/astaxie/beego/context" | ||
7 | + "github.com/astaxie/beego/validation" | ||
6 | "github.com/linmadan/egglib-go/core/application" | 8 | "github.com/linmadan/egglib-go/core/application" |
7 | "github.com/linmadan/egglib-go/web/beego/utils" | 9 | "github.com/linmadan/egglib-go/web/beego/utils" |
10 | + comm "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils" | ||
11 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr/protocol" | ||
12 | + "strconv" | ||
8 | ) | 13 | ) |
9 | 14 | ||
10 | type BaseController struct { | 15 | type BaseController struct { |
@@ -30,6 +35,11 @@ func (controller BaseController) HandlerResponse(data interface{}, err error) { | @@ -30,6 +35,11 @@ func (controller BaseController) HandlerResponse(data interface{}, err error) { | ||
30 | if _, ok := err.(*application.ServiceError); !ok { | 35 | if _, ok := err.(*application.ServiceError); !ok { |
31 | err = application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | 36 | err = application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) |
32 | } | 37 | } |
38 | + if x, ok := err.(protocol.CustomErrParse); ok { | ||
39 | + m := x.ParseToMessage() | ||
40 | + response = ResponseCustomData(controller.Ctx, data, m) | ||
41 | + return | ||
42 | + } | ||
33 | response = utils.ResponseError(controller.Ctx, err) | 43 | response = utils.ResponseError(controller.Ctx, err) |
34 | } | 44 | } |
35 | 45 | ||
@@ -38,3 +48,63 @@ func (controller BaseController) GetLimitInfo() (offset int, limit int) { | @@ -38,3 +48,63 @@ func (controller BaseController) GetLimitInfo() (offset int, limit int) { | ||
38 | limit, _ = controller.GetInt("limit") | 48 | limit, _ = controller.GetInt("limit") |
39 | return | 49 | return |
40 | } | 50 | } |
51 | + | ||
52 | +func ResponseCustomData(ctx *context.Context, data interface{}, msg *protocol.ResponseMessage) utils.JsonResponse { | ||
53 | + jsonResponse := utils.JsonResponse{} | ||
54 | + jsonResponse["code"] = 0 | ||
55 | + jsonResponse["msg"] = "ok" | ||
56 | + jsonResponse["data"] = data | ||
57 | + ctx.Input.SetData("outputData", jsonResponse) | ||
58 | + return jsonResponse | ||
59 | +} | ||
60 | + | ||
61 | +//Valid valid struct | ||
62 | +func (controller *BaseController) Valid(obj interface{}) (result bool, msg *protocol.ResponseMessage) { | ||
63 | + /*校验*/ | ||
64 | + var err error | ||
65 | + valid := validation.Validation{} | ||
66 | + result, err = valid.Valid(obj) | ||
67 | + if err != nil { | ||
68 | + } | ||
69 | + if !result { | ||
70 | + msg = protocol.BadRequestParam(2) | ||
71 | + return | ||
72 | + } | ||
73 | + | ||
74 | + return | ||
75 | +} | ||
76 | + | ||
77 | +func (this *BaseController) Resp(msg *protocol.ResponseMessage) { | ||
78 | + this.Data["json"] = msg | ||
79 | + this.Ctx.Input.SetData("outputData", msg) | ||
80 | + this.ServeJSON() | ||
81 | +} | ||
82 | + | ||
83 | +func (this *BaseController) RespH5(msg *protocol.ResponseMessage) { | ||
84 | + if msg.Errno != 0 { | ||
85 | + msg.Errno = -1 | ||
86 | + } | ||
87 | + this.Data["json"] = msg | ||
88 | + this.Ctx.Input.SetData("outputData", msg) | ||
89 | + this.ServeJSON() | ||
90 | +} | ||
91 | + | ||
92 | +//获取请求头信息 | ||
93 | +func (this *BaseController) GetRequestHeader(ctx *context.Context) *protocol.RequestHeader { | ||
94 | + h := &protocol.RequestHeader{} | ||
95 | + h.AccessToken = ctx.Input.Header("x-mmm-accesstoken") | ||
96 | + h.AppProject = ctx.Input.Header("x-mmm-appproject") | ||
97 | + h.DeviceType, _ = strconv.Atoi(ctx.Input.Header("x-mmm-devicetype")) | ||
98 | + h.Sign = ctx.Input.Header("x-mmm-sign") | ||
99 | + h.Uuid = ctx.Input.Header("x-mmm-uuid") | ||
100 | + h.TimeStamp = ctx.Input.Header("x-mmm-timestamp") | ||
101 | + h.Version = ctx.Input.Header("x-mmm-version") | ||
102 | + h.UserId, _ = strconv.ParseInt(ctx.Input.Header("x-mmm-id"), 10, 64) | ||
103 | + | ||
104 | + if len(h.AccessToken) > 0 { | ||
105 | + if claim, err := comm.ParseJWTToken(h.AccessToken); err == nil { | ||
106 | + h.UserId = claim.UserId | ||
107 | + } | ||
108 | + } | ||
109 | + return h | ||
110 | +} |
pkg/port/appsvr/controllers/user.go
0 → 100644
1 | +package controllers | ||
2 | + | ||
3 | +import ( | ||
4 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr/protocol" | ||
5 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr/services/user" | ||
6 | +) | ||
7 | + | ||
8 | +type UserController struct { | ||
9 | + BaseController | ||
10 | +} | ||
11 | + | ||
12 | +//UserInfo | ||
13 | +//@router /userInfo [post] | ||
14 | +func (this *UserController) UserInfo() { | ||
15 | + var msg *protocol.ResponseMessage | ||
16 | + defer func() { | ||
17 | + this.Resp(msg) | ||
18 | + }() | ||
19 | + var request *protocol.UserInfoRequest | ||
20 | + if err := this.JsonUnmarshal(&request); err != nil { | ||
21 | + msg = protocol.BadRequestParam(1) | ||
22 | + return | ||
23 | + } | ||
24 | + if b, m := this.Valid(request); !b { | ||
25 | + msg = m | ||
26 | + return | ||
27 | + } | ||
28 | + header := this.GetRequestHeader(this.Ctx) | ||
29 | + msg = protocol.NewReturnResponse(user.UserInfo(header, request)) | ||
30 | +} |
pkg/port/appsvr/middleware/check_token.go
0 → 100644
1 | +package middleware | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/astaxie/beego/context" | ||
5 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils" | ||
6 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr/protocol" | ||
7 | + "strings" | ||
8 | +) | ||
9 | + | ||
10 | +//检查access_token | ||
11 | +func CheckJWTToken(ctx *context.Context) { | ||
12 | + var ( | ||
13 | + msg *protocol.ResponseMessage | ||
14 | + ) | ||
15 | + token := ctx.Input.Header("x-mmm-accesstoken") | ||
16 | + if strings.HasSuffix(ctx.Request.RequestURI, "login") || | ||
17 | + strings.HasSuffix(ctx.Request.RequestURI, "accessToken") || | ||
18 | + strings.HasSuffix(ctx.Request.RequestURI, "refreshToken") || | ||
19 | + strings.HasSuffix(ctx.Request.RequestURI, "smsCode") { | ||
20 | + return | ||
21 | + } | ||
22 | + defer func() { | ||
23 | + if msg != nil { | ||
24 | + ctx.Output.JSON(msg, false, false) | ||
25 | + } | ||
26 | + }() | ||
27 | + if _, err := utils.ParseJWTToken(token); err != nil { | ||
28 | + msg = protocol.NewMesage(4141) | ||
29 | + return | ||
30 | + } | ||
31 | + return | ||
32 | +} |
pkg/port/appsvr/protocol/auth.go
0 → 100644
1 | +package protocol | ||
2 | + | ||
3 | +const ( | ||
4 | + LoginByPassword = iota + 1 | ||
5 | + LoginBySmsCode | ||
6 | +) | ||
7 | + | ||
8 | +const AuthCodeExpire = 60 * 15 | ||
9 | +const TokenExpire = 60 * 15 | ||
10 | +const RefreshTokenExipre = 3600 * 24 * 30 * 3 //刷新token 三个月过期 | ||
11 | + | ||
12 | +/*Login */ | ||
13 | +type LoginRequest struct { | ||
14 | + Phone string `json:"phone" valid:"Required;"` | ||
15 | + Password string `json:"password"` | ||
16 | + SmsCode string `json:"smsCode"` | ||
17 | + Type int `json:"type"` // 登录类型 1:密码 2:验证码 | ||
18 | +} | ||
19 | +type LoginResponse struct { | ||
20 | + AuthCode string `json:"authCode"` | ||
21 | +} | ||
22 | + | ||
23 | +//JWT用户信息 | ||
24 | +type JWTUserInfo struct { | ||
25 | + UserId string `json:"id"` //用户id | ||
26 | + PassWord string `json:"passWord"` //密码 | ||
27 | +} | ||
28 | + | ||
29 | +/*SmsCode*/ | ||
30 | +type SmsCodeRequest struct { | ||
31 | + Phone string `json:"phone" valid:"Required;Mobile"` | ||
32 | + Content string `json:"-"` | ||
33 | + SendType string `json:"send_type"` //sms_login_code sms_change_mobile | ||
34 | + | ||
35 | +} | ||
36 | +type SmsCodeResponse struct { | ||
37 | + Code string `json:"-"` | ||
38 | +} | ||
39 | + | ||
40 | +/*UpdateDevice*/ | ||
41 | +type UpdateDeviceRequest struct { | ||
42 | + ClientId string `json:"clientId" valid:"Required"` | ||
43 | + DeviceToken string `json:"deviceToken"` | ||
44 | +} | ||
45 | +type UpdateDeviceResponse struct { | ||
46 | +} | ||
47 | + | ||
48 | +/*AccessToken */ | ||
49 | +type AccessTokenRequest struct { | ||
50 | + //ClientId string `json:"clientId" valid:"Required"` | ||
51 | + //ClientSecret string `json:"clientSecret" valid:"Required"` | ||
52 | + AuthCode string `json:"authCode" valid:"Required"` | ||
53 | +} | ||
54 | +type AccessTokenResponse struct { | ||
55 | + RefreshToken string `json:"refreshToken"` | ||
56 | + AccessToken string `json:"accessToken"` | ||
57 | + ExpiresIn int `json:"expiresIn"` | ||
58 | +} | ||
59 | + | ||
60 | +/*RefreshToken */ | ||
61 | +type RefreshTokenRequest struct { | ||
62 | + ClientId string `json:"clientId" valid:"Required"` | ||
63 | + ClientSecret string `json:"clientSecret" valid:"Required"` | ||
64 | + RefreshToken string `json:"refreshToken" valid:"Required"` | ||
65 | + | ||
66 | + //Uid int64 `json:"-"` | ||
67 | + //LoginType string `json:"-"` | ||
68 | +} | ||
69 | +type RefreshTokenResponse struct { | ||
70 | + RefreshToken string `json:"refreshToken"` | ||
71 | + AccessToken string `json:"accessToken"` | ||
72 | + ExpiresIn int `json:"expiresIn"` | ||
73 | +} | ||
74 | + | ||
75 | +/*Revoke */ | ||
76 | +type RevokeRequest struct { | ||
77 | +} | ||
78 | +type RevokeResponse struct { | ||
79 | +} |
pkg/port/appsvr/protocol/error.go
0 → 100644
1 | +package protocol | ||
2 | + | ||
3 | +import ( | ||
4 | + "encoding/json" | ||
5 | + "fmt" | ||
6 | +) | ||
7 | + | ||
8 | +var ( | ||
9 | + ERR_DB_NOT_FOUND = fmt.Errorf("db:not found") | ||
10 | +) | ||
11 | + | ||
12 | +//CustomErrParse 解析自定义错误结构体 | ||
13 | +type CustomErrParse interface { | ||
14 | + ParseToMessage() *ResponseMessage | ||
15 | +} | ||
16 | + | ||
17 | +//ErrorMap 统一消息错误编码 | ||
18 | +type ErrorMap map[int]string | ||
19 | + | ||
20 | +//Search 搜索错误描述 | ||
21 | +func (m ErrorMap) Search(code int) ErrorCode { | ||
22 | + if v, ok := m[code]; ok { | ||
23 | + return ErrorCode{ | ||
24 | + Errno: code, | ||
25 | + Errmsg: v, | ||
26 | + } | ||
27 | + } | ||
28 | + return ErrorCode{Errno: code, Errmsg: "错误码未定义"} | ||
29 | +} | ||
30 | + | ||
31 | +//ErrorCode 统一错误结构 | ||
32 | +type ErrorCode struct { | ||
33 | + Errno int `json:"code"` | ||
34 | + Errmsg string `json:"msg"` | ||
35 | +} | ||
36 | + | ||
37 | +//ResponseMessage 统一返回消息结构体 | ||
38 | +type ResponseMessage struct { | ||
39 | + ErrorCode | ||
40 | + Data interface{} `json:"data"` | ||
41 | +} | ||
42 | + | ||
43 | +func NewMesage(code int) *ResponseMessage { | ||
44 | + return &ResponseMessage{ | ||
45 | + ErrorCode: SearchErr(code), | ||
46 | + Data: nil, | ||
47 | + } | ||
48 | +} | ||
49 | + | ||
50 | +//ErrWithMessage 自定义错误结构 | ||
51 | +type ErrWithMessage struct { | ||
52 | + Err error `json:"-"` | ||
53 | + ErrorCode | ||
54 | +} | ||
55 | + | ||
56 | +var ( | ||
57 | + _ CustomErrParse = new(ErrWithMessage) | ||
58 | + _ error = new(ErrWithMessage) | ||
59 | +) | ||
60 | + | ||
61 | +//NewErrWithMessage 构建错误返回 | ||
62 | +//code:用于匹配统一消息错误编码 eRR:填充嵌套错误 | ||
63 | +func NewErrWithMessage(code int, eRR ...error) *ErrWithMessage { | ||
64 | + r := &ErrWithMessage{ | ||
65 | + ErrorCode: SearchErr(code), | ||
66 | + } | ||
67 | + if len(eRR) > 0 { | ||
68 | + r.Err = eRR[0] | ||
69 | + } | ||
70 | + return r | ||
71 | +} | ||
72 | + | ||
73 | +//Error 实现接口error 中的方法 | ||
74 | +//将ErrorCode转为json数据,建议用于日志记录 | ||
75 | +func (e ErrWithMessage) Error() string { | ||
76 | + bt, _ := json.Marshal(e.ErrorCode) | ||
77 | + return string(bt) | ||
78 | +} | ||
79 | + | ||
80 | +//Unwrap 接口实现 | ||
81 | +func (e ErrWithMessage) Unwrap() error { | ||
82 | + return e.Err | ||
83 | +} | ||
84 | + | ||
85 | +//ParseToMessage 实现CustomErrParse的接口 | ||
86 | +func (e ErrWithMessage) ParseToMessage() *ResponseMessage { | ||
87 | + return &ResponseMessage{ | ||
88 | + ErrorCode: e.ErrorCode, | ||
89 | + Data: nil, | ||
90 | + } | ||
91 | +} | ||
92 | + | ||
93 | +func SearchErr(code int) ErrorCode { | ||
94 | + return errmessge.Search(code) | ||
95 | +} | ||
96 | +func NewReturnResponse(data interface{}, eRR error) *ResponseMessage { | ||
97 | + var msg *ResponseMessage | ||
98 | + if eRR == nil { | ||
99 | + msg = NewMesage(0) | ||
100 | + msg.Data = data | ||
101 | + return msg | ||
102 | + } | ||
103 | + // fmt.Println("日志:" + eRR.Error()) | ||
104 | + if x, ok := eRR.(CustomErrParse); ok { | ||
105 | + msg = x.ParseToMessage() | ||
106 | + msg.Data = data | ||
107 | + return msg | ||
108 | + } | ||
109 | + return NewMesage(1) | ||
110 | +} | ||
111 | + | ||
112 | +func BadRequestParam(code int) *ResponseMessage { | ||
113 | + return NewMesage(code) | ||
114 | +} | ||
115 | + | ||
116 | +func NewSuccessWithMessage(msg string) *ErrWithMessage { | ||
117 | + return &ErrWithMessage{ | ||
118 | + Err: nil, | ||
119 | + ErrorCode: ErrorCode{0, msg}, | ||
120 | + } | ||
121 | +} | ||
122 | + | ||
123 | +func NewCustomMessage(code int, msg string) *ErrWithMessage { | ||
124 | + return &ErrWithMessage{ | ||
125 | + Err: nil, | ||
126 | + ErrorCode: ErrorCode{code, msg}, | ||
127 | + } | ||
128 | +} |
pkg/port/appsvr/protocol/message.go
0 → 100644
pkg/port/appsvr/protocol/request_header.go
0 → 100644
1 | +package protocol | ||
2 | + | ||
3 | +type RequestHeader struct { | ||
4 | + TimeStamp string | ||
5 | + Version string | ||
6 | + Uuid string | ||
7 | + Sign string | ||
8 | + DeviceType int | ||
9 | + AppProject string | ||
10 | + AccessToken string | ||
11 | + Uid int64 //用户基本信息Id | ||
12 | + CompanyId int64 | ||
13 | + UserId int64 //UserId 唯一标识,唯一关联所有用户信息(=user_company.id) | ||
14 | + | ||
15 | + requestId string //请求编号 md5 | ||
16 | + reqIndex int64 //请求链序号 | ||
17 | +} |
pkg/port/appsvr/protocol/user.go
0 → 100644
1 | +package protocol | ||
2 | + | ||
3 | +import ( | ||
4 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain" | ||
5 | +) | ||
6 | + | ||
7 | +/*用户信息 UserInfo */ | ||
8 | +type UserInfoRequest struct { | ||
9 | +} | ||
10 | +type UserInfoResponse struct { | ||
11 | + User User `json:"user"` | ||
12 | +} | ||
13 | + | ||
14 | +type User struct { | ||
15 | + Id int64 `json:"id"` | ||
16 | + // 合伙类别 (1.研发合伙人 2.业务合伙人 3.事业) | ||
17 | + PartnerCategory int `json:"partnerCategory"` | ||
18 | + PartnerName string `json:"partnerName"` | ||
19 | + //所属区域信息 | ||
20 | + RegionInfo *domain.RegionInfo `json:"regionInfo"` | ||
21 | + CooperateCompany Company `json:"cooperateCompany"` | ||
22 | + CooperateTime int64 `json:"cooperateTime"` | ||
23 | + Salesman *domain.Salesman `json:"salesman"` | ||
24 | +} | ||
25 | + | ||
26 | +type Company struct { | ||
27 | + Id int `json:"id"` | ||
28 | + Name string `json:"name"` | ||
29 | +} |
pkg/port/appsvr/routers/router.go
0 → 100644
1 | +package routers | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/astaxie/beego" | ||
5 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr/controllers" | ||
6 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr/middleware" | ||
7 | +) | ||
8 | + | ||
9 | +func init() { | ||
10 | + nsV1 := beego.NewNamespace("v1", beego.NSBefore(middleware.CheckJWTToken))//beego.NSNamespace("auth", beego.NSInclude(&controllers.AuthController{})), | ||
11 | + | ||
12 | + nsV1.Router("auth/login", &controllers.AuthController{}, "Post:Login") | ||
13 | + nsV1.Router("auth/smsCode", &controllers.AuthController{}, "Post:SmsCode") | ||
14 | + nsV1.Router("auth/accessToken", &controllers.AuthController{}, "Post:AccessToken") | ||
15 | + nsV1.Router("auth/refreshToken", &controllers.AuthController{}, "Post:RefreshToken") | ||
16 | + nsV1.Router("auth/revoke", &controllers.AuthController{}, "Post:Revoke") | ||
17 | + nsV1.Router("auth/updateDevice", &controllers.AuthController{}, "Post:UpdateDevice") | ||
18 | + | ||
19 | + nsV1.Router("user/userInfo", &controllers.UserController{}, "Post:UserInfo") | ||
20 | + beego.AddNamespace(nsV1) | ||
21 | +} |
pkg/port/appsvr/services/auth/auth.go
0 → 100644
1 | +package auth | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/query" | ||
6 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/service" | ||
7 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain" | ||
8 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils" | ||
9 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr/protocol" | ||
10 | + "strings" | ||
11 | + "time" | ||
12 | +) | ||
13 | + | ||
14 | +func Login(header *protocol.RequestHeader, request *protocol.LoginRequest) (rsp *protocol.LoginResponse, err error) { | ||
15 | + var ( | ||
16 | + PartnerInfoService = service.NewPartnerInfoService(nil) | ||
17 | + partnerInfo *domain.PartnerInfo | ||
18 | + result bool = false | ||
19 | + ) | ||
20 | + rsp = &protocol.LoginResponse{} | ||
21 | + if partnerInfo, err = PartnerInfoService.GetPartnerInfo(&query.GetPartnerInfoQuery{Account: request.Phone}); err != nil { | ||
22 | + err = protocol.NewErrWithMessage(502, err) //账号不存在 | ||
23 | + return | ||
24 | + } | ||
25 | + switch request.Type { | ||
26 | + case protocol.LoginByPassword: | ||
27 | + if len(partnerInfo.Password) == 0 { | ||
28 | + err = protocol.NewCustomMessage(1, "密码不能为空!") | ||
29 | + return | ||
30 | + } | ||
31 | + if result = strings.EqualFold(partnerInfo.Password, request.Password); result { | ||
32 | + err = protocol.NewCustomMessage(1, "密码输入有误!") | ||
33 | + return | ||
34 | + } | ||
35 | + break | ||
36 | + case protocol.LoginBySmsCode: | ||
37 | + break | ||
38 | + } | ||
39 | + rsp.AuthCode, _ = utils.GenerateToken(partnerInfo.Id, protocol.AuthCodeExpire*time.Second) | ||
40 | + return | ||
41 | +} | ||
42 | + | ||
43 | +func SmsCode(request *protocol.SmsCodeRequest) (rsp *protocol.LoginResponse, err error) { | ||
44 | + return | ||
45 | +} | ||
46 | + | ||
47 | +func AccessToken(request *protocol.AccessTokenRequest) (rsp *protocol.AccessTokenResponse, err error) { | ||
48 | + var ( | ||
49 | + claim *utils.UserTokenClaims | ||
50 | + ) | ||
51 | + rsp = &protocol.AccessTokenResponse{} | ||
52 | + if claim, err = utils.ParseJWTToken(request.AuthCode); err != nil { | ||
53 | + err = protocol.NewErrWithMessage(4139, err) | ||
54 | + return | ||
55 | + } | ||
56 | + if claim.UserId <= 0 { | ||
57 | + err = protocol.NewErrWithMessage(1, fmt.Errorf("jwt authCode (%v) valid", request.AuthCode)) | ||
58 | + return | ||
59 | + } | ||
60 | + rsp.AccessToken, _ = utils.GenerateToken(claim.UserId, protocol.TokenExpire*time.Second) | ||
61 | + rsp.RefreshToken, _ = utils.GenerateToken(claim.UserId, protocol.RefreshTokenExipre*time.Second) | ||
62 | + rsp.ExpiresIn = protocol.TokenExpire | ||
63 | + return | ||
64 | +} | ||
65 | + | ||
66 | +func RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshTokenResponse, err error) { | ||
67 | + var ( | ||
68 | + claim *utils.UserTokenClaims | ||
69 | + ) | ||
70 | + rsp = &protocol.RefreshTokenResponse{} | ||
71 | + if claim, err = utils.ParseJWTToken(request.RefreshToken); err != nil { | ||
72 | + err = protocol.NewErrWithMessage(4139, err) | ||
73 | + return | ||
74 | + } | ||
75 | + if claim.UserId <= 0 { | ||
76 | + err = protocol.NewErrWithMessage(1, fmt.Errorf("jwt refrshToken (%v) valid", request.RefreshToken)) | ||
77 | + return | ||
78 | + } | ||
79 | + rsp.AccessToken, _ = utils.GenerateToken(claim.UserId, protocol.TokenExpire*time.Second) | ||
80 | + rsp.RefreshToken, _ = utils.GenerateToken(claim.UserId, protocol.RefreshTokenExipre*time.Second) | ||
81 | + rsp.ExpiresIn = protocol.TokenExpire | ||
82 | + return | ||
83 | + return | ||
84 | +} |
pkg/port/appsvr/services/user/user.go
0 → 100644
1 | +package user | ||
2 | + | ||
3 | +import ( | ||
4 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/query" | ||
5 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/service" | ||
6 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain" | ||
7 | + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr/protocol" | ||
8 | +) | ||
9 | + | ||
10 | +//用户信息 | ||
11 | +func UserInfo(header *protocol.RequestHeader, request *protocol.UserInfoRequest) (rsp *protocol.UserInfoResponse, err error) { | ||
12 | + var ( | ||
13 | + PartnerInfoService = service.NewPartnerInfoService(nil) | ||
14 | + partnerInfo *domain.PartnerInfo | ||
15 | + ) | ||
16 | + rsp = &protocol.UserInfoResponse{} | ||
17 | + if partnerInfo, err = PartnerInfoService.GetPartnerInfo(&query.GetPartnerInfoQuery{Id: int(header.UserId)}); err != nil { | ||
18 | + err = protocol.NewErrWithMessage(502, err) //账号不存在 | ||
19 | + return | ||
20 | + } | ||
21 | + rsp.User = protocol.User{ | ||
22 | + Id: partnerInfo.Id, | ||
23 | + PartnerCategory: partnerInfo.PartnerCategory, | ||
24 | + PartnerName: partnerInfo.PartnerName, | ||
25 | + RegionInfo: partnerInfo.RegionInfo, | ||
26 | + CooperateCompany: protocol.Company{ | ||
27 | + Id: 1, | ||
28 | + Name: "福州素天下有限公司", | ||
29 | + }, | ||
30 | + CooperateTime: partnerInfo.CooperateTime.Unix() * 1000, | ||
31 | + } | ||
32 | + if len(partnerInfo.Salesman) > 0 { | ||
33 | + rsp.User.Salesman = partnerInfo.Salesman[0] | ||
34 | + } | ||
35 | + return | ||
36 | +} |
1 | -package middleware | ||
2 | - | ||
3 | -import ( | ||
4 | - "github.com/astaxie/beego/context" | ||
5 | -) | ||
6 | - | ||
7 | -//PermissionCheck 根据权限编码检验请求是否可以执行 | ||
8 | -//permissionCode:权限编码 | ||
9 | -//excludeURL:跳过url | ||
10 | -func AdminPermissionCheck(permissionCode string, excludeURL ...string) func(ctx *context.Context) { | ||
11 | - | ||
12 | - return func(ctx *context.Context) { | ||
13 | - //根据code检查权限数据,并排除excludeUrl指定的url | ||
14 | - //获取user 数据 | ||
15 | - } | ||
16 | -} |
1 | -package manage | ||
2 | - | ||
3 | -import ( | ||
4 | - "github.com/astaxie/beego" | ||
5 | - "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain" | ||
6 | - "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/beego/middleware" | ||
7 | -) | ||
8 | - | ||
9 | -func init() { | ||
10 | - adminRouter := beego.NewNamespace("/admin_user", | ||
11 | - //验证权限 | ||
12 | - beego.NSBefore(middleware.AdminPermissionCheck(domain.PERMINSSION_ADMIN_USER)), | ||
13 | - //其余的路由 | ||
14 | - //... | ||
15 | - ) | ||
16 | - beego.AddNamespace(adminRouter) | ||
17 | -} |
1 | -package routers | ||
2 | - | ||
3 | -import ( | ||
4 | - "github.com/astaxie/beego" | ||
5 | - "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/beego/controllers" | ||
6 | -) | ||
7 | - | ||
8 | -func init() { | ||
9 | - beego.Router("/partnerInfos/", &controllers.PartnerInfoController{}, "Post:CreatePartnerInfo") | ||
10 | - beego.Router("/partnerInfos/:id", &controllers.PartnerInfoController{}, "Put:UpdatePartnerInfo") | ||
11 | - beego.Router("/partnerInfos/:id", &controllers.PartnerInfoController{}, "Get:GetPartnerInfo") | ||
12 | - beego.Router("/partnerInfos/:id", &controllers.PartnerInfoController{}, "Delete:RemovePartnerInfo") | ||
13 | - beego.Router("/partnerInfos/", &controllers.PartnerInfoController{}, "Get:ListPartnerInfo") | ||
14 | -} |
-
请 注册 或 登录 后发表评论