作者 yangfu

注入token

@@ -104,24 +104,33 @@ func FilterComm(ctx *context.Context) { @@ -104,24 +104,33 @@ func FilterComm(ctx *context.Context) {
104 //统计 104 //统计
105 MetricCounter(ctx) 105 MetricCounter(ctx)
106 106
107 - if beego.BConfig.RunMode == "dev" && (ctx.Input.Header("x-mmm-uid") != "" || ctx.Input.Header("uid") != "") {  
108 - return  
109 - } 107 + //if beego.BConfig.RunMode == "dev" && (ctx.Input.Header("x-mmm-uid") != "" || ctx.Input.Header("uid") != "") {
  108 + // return
  109 + //}
110 //TODO:注入账号,后期移除掉 110 //TODO:注入账号,后期移除掉
111 - if beego.BConfig.RunMode != "prod" {  
112 - ctx.Request.Header.Add("x-mmm-uid", fmt.Sprintf("%v", 1))  
113 - ctx.Request.Header.Add("x-mmm-cid", fmt.Sprintf("%v", 1))  
114 - ctx.Request.Header.Add("x-mmm-id", fmt.Sprintf("%v", 1))  
115 - return 111 + if beego.BConfig.RunMode != "prod" || ctx.Input.Header("x-mmm-accesstoken") == "" {
  112 + ctx.Request.Header.Set("x-mmm-accesstoken", "6839602f1d8211eabd85000c29ad8d6d")
  113 + if ctx.Input.Header("x-mmm-accesstoken") == "" {
  114 + ctx.Request.Header.Add("x-mmm-accesstoken", "6839602f1d8211eabd85000c29ad8d6d")
116 } 115 }
  116 + //return
  117 + } else {
117 //1.检查签名 118 //1.检查签名
118 if !CheckSign(ctx) { 119 if !CheckSign(ctx) {
119 return 120 return
120 } 121 }
121 - //2.检查token是否有效 122 + }
122 if !CheckToken(ctx) { 123 if !CheckToken(ctx) {
123 return 124 return
124 } 125 }
  126 +
  127 + //if !CheckSign(ctx) {
  128 + // return
  129 + //}
  130 + ////2.检查token是否有效
  131 + //if !CheckToken(ctx) {
  132 + // return
  133 + //}
125 //3.查重uuid 134 //3.查重uuid
126 //if !CheckUuid(ctx) { 135 //if !CheckUuid(ctx) {
127 // return 136 // return
@@ -189,9 +198,9 @@ func CheckToken(ctx *context.Context) (result bool) { @@ -189,9 +198,9 @@ func CheckToken(ctx *context.Context) (result bool) {
189 } else { 198 } else {
190 if rsp.UserInfo != nil { 199 if rsp.UserInfo != nil {
191 //设置附加数据 200 //设置附加数据
192 - ctx.Request.Header.Add("x-mmm-uid", fmt.Sprintf("%v", rsp.UserInfo.UserId))  
193 - ctx.Request.Header.Add("x-mmm-cid", fmt.Sprintf("%v", rsp.UserInfo.CurrentCompanyId))  
194 - ctx.Request.Header.Add("x-mmm-id", fmt.Sprintf("%v", rsp.UserInfo.CurrentUserCompanyId)) 201 + ctx.Request.Header.Set("x-mmm-uid", fmt.Sprintf("%v", rsp.UserInfo.UserId))
  202 + ctx.Request.Header.Set("x-mmm-cid", fmt.Sprintf("%v", rsp.UserInfo.CurrentCompanyId))
  203 + ctx.Request.Header.Set("x-mmm-id", fmt.Sprintf("%v", rsp.UserInfo.CurrentUserCompanyId))
195 } 204 }
196 } 205 }
197 return 206 return
@@ -34,7 +34,7 @@ func UpdateTableByMap(tabeleStruct interface{}, changeMap map[string]interface{} @@ -34,7 +34,7 @@ func UpdateTableByMap(tabeleStruct interface{}, changeMap map[string]interface{}
34 log.Error(err) 34 log.Error(err)
35 return err 35 return err
36 } 36 }
37 - log.Info(fmt.Sprintf("UpdateTableByMap: table:%s effect records:%d column:%v", GetTableName(tabeleStruct), num, changeColumn)) 37 + log.Info(fmt.Sprintf("UpdateTableByMap: table:%s effect records:%d column:%v values:%v", GetTableName(tabeleStruct), num, changeColumn, changeMap))
38 return nil 38 return nil
39 } 39 }
40 40
@@ -158,11 +158,10 @@ func DeleteCompany(id int64) (err error) { @@ -158,11 +158,10 @@ func DeleteCompany(id int64) (err error) {
158 func GetCompanyByPermission(uid int64) (v []*Company, err error) { 158 func GetCompanyByPermission(uid int64) (v []*Company, err error) {
159 o := orm.NewOrm() 159 o := orm.NewOrm()
160 sql := `select * from company where id in( 160 sql := `select * from company where id in(
161 - select company_id from user_company where user_id=? and enable=1 161 + select company_id from user_company where user_id=? and enable=1 order by create_at desc
162 ) and enable=1` // 162 ) and enable=1` //
163 - if _,err = o.Raw(sql, uid).QueryRows(&v); err == nil { 163 + if _, err = o.Raw(sql, uid).QueryRows(&v); err == nil {
164 return v, nil 164 return v, nil
165 } 165 }
166 return nil, err 166 return nil, err
167 } 167 }
168 -  
@@ -87,9 +87,19 @@ func GetUserCompanyByUserId(uid int64, companyId int64) (v *UserCompany, err err @@ -87,9 +87,19 @@ func GetUserCompanyByUserId(uid int64, companyId int64) (v *UserCompany, err err
87 return nil, err 87 return nil, err
88 } 88 }
89 89
90 -func GetUserCompanyFirst(uid int64) (v *UserCompany, err error) { 90 +//按user_company.id获取公司信息
  91 +func GetUserCompanyBy(id int64, companyId int64) (v *UserCompany, err error) {
91 o := orm.NewOrm() 92 o := orm.NewOrm()
92 - sql := "select * from user_company where user_id=? order by create_at desc limit 1" // 93 + sql := "select * from user_company where id=? and company_id=? and enable=1" //
  94 + if err = o.Raw(sql, id, companyId).QueryRow(&v); err == nil {
  95 + return v, nil
  96 + }
  97 + return nil, err
  98 +}
  99 +
  100 +func GetUserCompanys(uid int64) (v *UserCompany, err error) {
  101 + o := orm.NewOrm()
  102 + sql := "select * from user_company where user_id=? and enable=1 order by create_at desc limit 1" //
93 if err = o.Raw(sql, uid).QueryRow(&v); err == nil { 103 if err = o.Raw(sql, uid).QueryRow(&v); err == nil {
94 return v, nil 104 return v, nil
95 } 105 }
@@ -73,13 +73,13 @@ func DeleteUserDepartment(id int64) (err error) { @@ -73,13 +73,13 @@ func DeleteUserDepartment(id int64) (err error) {
73 return 73 return
74 } 74 }
75 75
76 -func GetUserDepartments(userId int64, companyId int64, v interface{}) (err error) { 76 +func GetUserDepartments(id int64, companyId int64, v interface{}) (err error) {
77 o := orm.NewOrm() 77 o := orm.NewOrm()
78 sql := ` 78 sql := `
79 select a.department_id,b.name,b.parent_id,b.managers,b.relation,a.create_time 79 select a.department_id,b.name,b.parent_id,b.managers,b.relation,a.create_time
80 from user_department a INNER JOIN department b on a.department_id = b.id 80 from user_department a INNER JOIN department b on a.department_id = b.id
81 -where a.user_id =? and a.company_id =? and enable_status =1 and b.delete_at =0`  
82 - if _, err = o.Raw(sql, userId, companyId).QueryRows(v); err == nil { 81 +where a.user_company_id =? and a.company_id =? and enable_status =1 and b.delete_at =0`
  82 + if _, err = o.Raw(sql, id, companyId).QueryRows(v); err == nil {
83 return 83 return
84 } 84 }
85 return 85 return
@@ -73,13 +73,13 @@ func DeleteUserPosition(id int) (err error) { @@ -73,13 +73,13 @@ func DeleteUserPosition(id int) (err error) {
73 return 73 return
74 } 74 }
75 75
76 -func GetUserPositions(userId int64, companyId int64, v interface{}) (err error) { 76 +func GetUserPositions(id int64, companyId int64, v interface{}) (err error) {
77 o := orm.NewOrm() 77 o := orm.NewOrm()
78 sql := ` 78 sql := `
79 select a.position_id,b.name,b.relation,a.create_at 79 select a.position_id,b.name,b.relation,a.create_at
80 from user_position a INNER JOIN position b on a.position_id = b.id 80 from user_position a INNER JOIN position b on a.position_id = b.id
81 -where a.user_id =? and a.company_id =? and a.enable_status =1 and b.enable_status =1`  
82 - if _, err = o.Raw(sql, userId, companyId).QueryRows(v); err == nil { 81 +where a.user_company_id =? and a.company_id =? and a.enable_status =1 and b.enable_status =1`
  82 + if _, err = o.Raw(sql, id, companyId).QueryRows(v); err == nil {
83 return 83 return
84 } 84 }
85 return 85 return
@@ -53,60 +53,33 @@ type UserInfoResponse struct { @@ -53,60 +53,33 @@ type UserInfoResponse struct {
53 User User `json:"user"` 53 User User `json:"user"`
54 } 54 }
55 55
56 -/*  
57 -user object  
58 -必须  
59 -用户对象信息  
60 -备注: 用户对象信息  
61 -  
62 -必须  
63 -用户名称  
64 -phone string  
65 -必须  
66 -手机号码  
67 -image object  
68 -非必须  
69 -用户头像  
70 -备注: 用户头像  
71 -  
72 -did integer  
73 -必须  
74 -部门ID  
75 -department string  
76 -必须  
77 -部门名称  
78 -position string  
79 -必须  
80 -职位名称  
81 -level integer  
82 -必须  
83 -职位级别(员工0,老板1)  
84 -employeeAttr object  
85 -必须  
86 -员工属性  
87 -备注: 员工属性  
88 -  
89 -imToken string  
90 -必须  
91 -网易云信IM Token  
92 -filterModule  
93 -*/  
94 type User struct { 56 type User struct {
95 UserId int64 `json:"uid"` 57 UserId int64 `json:"uid"`
96 Name string `json:"uname"` 58 Name string `json:"uname"`
97 Phone string `json:"phone"` 59 Phone string `json:"phone"`
98 Image Picture `json:"image"` 60 Image Picture `json:"image"`
99 - Department string `json:"department"`  
100 - Position string `json:"position"` 61 + Department Dep `json:"dep"`
  62 + Position Job `json:"job"`
101 ImToken string `json:"imToken"` 63 ImToken string `json:"imToken"`
102 64
103 //companys 65 //companys
104 CompanyId int `json:"companyId"` 66 CompanyId int `json:"companyId"`
105 - Company string `json:"company"` //公司名称 67 + Company Company `json:"company"` //公司名称
  68 + Companys []Company `json:"companys"`
106 } 69 }
107 70
108 -  
109 type Company struct { 71 type Company struct {
110 Id int64 `json:"id"` 72 Id int64 `json:"id"`
111 Name string `json:"name"` 73 Name string `json:"name"`
112 } 74 }
  75 +
  76 +type Dep struct {
  77 + Id int `json:"id"`
  78 + Name string `json:"name"`
  79 +}
  80 +
  81 +type Job struct {
  82 + Id int `json:"id"`
  83 + Name string `json:"name"`
  84 + Level int `json:"level"`
  85 +}
@@ -12,10 +12,13 @@ import ( @@ -12,10 +12,13 @@ import (
12 "sync" 12 "sync"
13 ) 13 )
14 14
15 -func GetUserBaseInfoAggregation(uid int64, companyId int64) (v *protocol.UserBaseInfoAggregation, err error) { 15 +//id :user_company.id
  16 +func GetUserBaseInfoAggregation(id int64, companyId int64) (v *protocol.UserBaseInfoAggregation, err error) {
16 v = &protocol.UserBaseInfoAggregation{} 17 v = &protocol.UserBaseInfoAggregation{}
17 - var wg sync.WaitGroup  
18 - if v.UserCompany, err = models.GetUserCompanyByUserId(uid, companyId); err != nil { 18 + var (
  19 + wg sync.WaitGroup
  20 + )
  21 + if v.UserCompany, err = models.GetUserCompanyBy(id, companyId); err != nil {
19 log.Error(err) 22 log.Error(err)
20 return 23 return
21 } 24 }
@@ -34,7 +37,7 @@ func GetUserBaseInfoAggregation(uid int64, companyId int64) (v *protocol.UserBas @@ -34,7 +37,7 @@ func GetUserBaseInfoAggregation(uid int64, companyId int64) (v *protocol.UserBas
34 37
35 go func() { 38 go func() {
36 defer wg.Done() 39 defer wg.Done()
37 - if err = models.GetUserDepartments(uid, companyId, &v.Department); err != nil { 40 + if err = models.GetUserDepartments(id, companyId, &v.Department); err != nil {
38 log.Error(err) 41 log.Error(err)
39 return 42 return
40 } 43 }
@@ -42,7 +45,7 @@ func GetUserBaseInfoAggregation(uid int64, companyId int64) (v *protocol.UserBas @@ -42,7 +45,7 @@ func GetUserBaseInfoAggregation(uid int64, companyId int64) (v *protocol.UserBas
42 45
43 go func() { 46 go func() {
44 defer wg.Done() 47 defer wg.Done()
45 - if err = models.GetUserPositions(uid, companyId, &v.Position); err != nil { 48 + if err = models.GetUserPositions(id, companyId, &v.Position); err != nil {
46 log.Error(err) 49 log.Error(err)
47 return 50 return
48 } 51 }
@@ -164,6 +164,7 @@ func Templates(header *protocol.RequestHeader, request *protocol.TemplatesReques @@ -164,6 +164,7 @@ func Templates(header *protocol.RequestHeader, request *protocol.TemplatesReques
164 return 164 return
165 } 165 }
166 if len(templates) == 0 { 166 if len(templates) == 0 {
  167 + log.Error(fmt.Sprintf("公司:%v chance_type_id:%v 无模板", header.CompanyId, request.ChanceTypeId))
167 return 168 return
168 } 169 }
169 for i := range templates { 170 for i := range templates {
@@ -172,6 +173,7 @@ func Templates(header *protocol.RequestHeader, request *protocol.TemplatesReques @@ -172,6 +173,7 @@ func Templates(header *protocol.RequestHeader, request *protocol.TemplatesReques
172 // 173 //
173 //查询表单 174 //查询表单
174 if forms, err = models.GetAuditForms(header.CompanyId, item.Id); err != nil { 175 if forms, err = models.GetAuditForms(header.CompanyId, item.Id); err != nil {
  176 + log.Error(err)
175 continue 177 continue
176 } 178 }
177 template := &protocol.Template{ 179 template := &protocol.Template{
@@ -26,7 +26,7 @@ func Departments(header *protocol.RequestHeader, request *protocol.DepartmentsRe @@ -26,7 +26,7 @@ func Departments(header *protocol.RequestHeader, request *protocol.DepartmentsRe
26 } 26 }
27 rsp.Departments = tmpDepartment.Departments 27 rsp.Departments = tmpDepartment.Departments
28 case protocol.DepartmentUser: 28 case protocol.DepartmentUser:
29 - if err = repository.UserDepartment.GetUserDepartment(header.Uid, header.CompanyId, &rsp.Departments); err != nil { 29 + if err = models.GetUserDepartments(header.UserId, header.CompanyId, &rsp.Departments); err != nil {
30 log.Error(err) 30 log.Error(err)
31 return 31 return
32 } 32 }
@@ -198,13 +198,12 @@ func UserCompanys(header *protocol.RequestHeader, request *protocol.UserCompanys @@ -198,13 +198,12 @@ func UserCompanys(header *protocol.RequestHeader, request *protocol.UserCompanys
198 } 198 }
199 199
200 //切换公司 200 //切换公司
201 -func SwitchCompany(header *protocol.RequestHeader, request *protocol.SwitchCompanyRequest) (rsp *protocol.SwitchCompanyResponse, err error) { 201 +func SwitchCompany(header *protocol.RequestHeader, request *protocol.SwitchCompanyRequest) (rsp *protocol.UserInfoResponse, err error) {
202 var ( 202 var (
203 - company *models.UserCompany 203 + userCompany *models.UserCompany
204 auth *models.UserAuth 204 auth *models.UserAuth
205 ) 205 )
206 - rsp = &protocol.SwitchCompanyResponse{}  
207 - if company, err = repository.UserCompany.GetUserCompanyByUserId(header.Uid, int64(request.CompanyId)); err != nil { 206 + if userCompany, err = repository.UserCompany.GetUserCompanyByUserId(header.Uid, int64(request.CompanyId)); err != nil {
208 log.Error(err) 207 log.Error(err)
209 err = protocol.NewErrWithMessage(4201) //找不到这家公司 208 err = protocol.NewErrWithMessage(4201) //找不到这家公司
210 return 209 return
@@ -215,9 +214,16 @@ func SwitchCompany(header *protocol.RequestHeader, request *protocol.SwitchCompa @@ -215,9 +214,16 @@ func SwitchCompany(header *protocol.RequestHeader, request *protocol.SwitchCompa
215 } 214 }
216 if auth.CurrentCompanyId == request.CompanyId { 215 if auth.CurrentCompanyId == request.CompanyId {
217 log.Error(fmt.Sprintf("uid:%v 当前公司已经是:%v", header.Uid, request.CompanyId)) 216 log.Error(fmt.Sprintf("uid:%v 当前公司已经是:%v", header.Uid, request.CompanyId))
  217 + //return
  218 + } else {
  219 + if err = utils.UpdateTableByMap(&models.UserAuth{Id: auth.Id}, map[string]interface{}{"CurrentCompanyId": userCompany.CompanyId, "CurrentUserCompanyId": userCompany.Id}); err != nil {
  220 + log.Error(err)
218 return 221 return
219 } 222 }
220 - if err = utils.UpdateTableByMap(&models.UserAuth{Id: auth.Id}, map[string]interface{}{"CurrentCompanyId": company.CompanyId, "CurrentCompanyUserId": company.Id}); err != nil { 223 + header.CompanyId = userCompany.CompanyId
  224 + header.UserId = userCompany.Id
  225 + }
  226 + if rsp, err = UserInfo(header, &protocol.UserInfoRequest{}); err != nil {
221 log.Error(err) 227 log.Error(err)
222 return 228 return
223 } 229 }
@@ -229,51 +235,70 @@ func UserInfo(header *protocol.RequestHeader, request *protocol.UserInfoRequest) @@ -229,51 +235,70 @@ func UserInfo(header *protocol.RequestHeader, request *protocol.UserInfoRequest)
229 var ( 235 var (
230 companyId = header.CompanyId 236 companyId = header.CompanyId
231 userCompany *models.UserCompany 237 userCompany *models.UserCompany
232 - baseInfo *protocol.BaseUserInfo  
233 userAuth *models.UserAuth 238 userAuth *models.UserAuth
234 - user *models.User 239 + userBaseAgg *protocol.UserBaseInfoAggregation
  240 + companys []*models.Company
235 ) 241 )
236 -  
237 - if companyId == 0 {  
238 - if userCompany, err = models.GetUserCompanyFirst(header.Uid); err != nil { 242 + if userCompany, err = models.GetUserCompanys(header.Uid); err != nil {
239 log.Error(err) 243 log.Error(err)
240 return 244 return
241 } 245 }
242 - if userAuth, err = models.GetUserAuthByUserId(header.Uid, 1); err != nil { 246 + if companyId == 0 {
  247 + if userAuth, err = models.GetUserAuthByUserId(header.Uid, protocol.DeviceType); err != nil {
243 log.Error(err) 248 log.Error(err)
244 return 249 return
245 } 250 }
246 - if err = utils.UpdateTableByMap(&models.UserAuth{Id: userAuth.Id}, map[string]interface{}{"CurrentCompanyId": userCompany.CompanyId, "CurrentUserCompanyId": userCompany.Id}); err != nil { 251 + if err = utils.UpdateTableByMap(&models.UserAuth{Id: userAuth.Id}, map[string]interface{}{
  252 + "CurrentCompanyId": userCompany.CompanyId, "CurrentUserCompanyId": userCompany.Id}); err != nil {
247 log.Error(err) 253 log.Error(err)
248 return 254 return
249 } 255 }
250 companyId = int64(userCompany.CompanyId) 256 companyId = int64(userCompany.CompanyId)
  257 + header.UserId = userCompany.Id
251 } 258 }
252 - if user, err = models.GetUsersById(header.Uid); err != nil { 259 + if companys, err = models.GetCompanyByPermission(header.Uid); err != nil {
253 log.Error(err) 260 log.Error(err)
254 return 261 return
255 } 262 }
256 - if baseInfo, err = agg.GetUserBaseInfo(header.UserId, companyId); err != nil {  
257 - log.Error(err) 263 + if userBaseAgg, err = agg.GetUserBaseInfoAggregation(header.UserId, companyId); err != nil {
258 return 264 return
259 } 265 }
260 rsp = &protocol.UserInfoResponse{ 266 rsp = &protocol.UserInfoResponse{
261 User: protocol.User{ 267 User: protocol.User{
262 - UserId: baseInfo.UserId,  
263 - Name: baseInfo.NickName,  
264 - Phone: user.Phone, 268 + UserId: header.UserId,
  269 + Name: userBaseAgg.User.NickName,
  270 + Phone: userBaseAgg.User.Phone,
265 Image: protocol.Picture{ 271 Image: protocol.Picture{
266 - Path: user.Icon, 272 + Path: userBaseAgg.User.Icon,
267 //TODO:图片裁剪 273 //TODO:图片裁剪
268 H: 0, 274 H: 0,
269 W: 0, 275 W: 0,
270 }, 276 },
271 - Department: baseInfo.Department,  
272 - Position: baseInfo.Position,  
273 - ImToken: user.ImToken, 277 + ImToken: userBaseAgg.User.ImToken,
274 CompanyId: int(companyId), 278 CompanyId: int(companyId),
275 - Company: baseInfo.CompanyName, 279 + Company: protocol.Company{
  280 + Id: userBaseAgg.Company.Id,
  281 + Name: userBaseAgg.Company.Name,
  282 + },
276 }, 283 },
277 } 284 }
  285 + for i := range companys {
  286 + rsp.User.Companys = append(rsp.User.Companys, protocol.Company{
  287 + Id: companys[i].Id,
  288 + Name: companys[i].Name,
  289 + })
  290 + }
  291 + if topDep := agg.GetTopDepartment(userBaseAgg.Department); topDep.DepartmentId != 0 {
  292 + rsp.User.Department = protocol.Dep{
  293 + Id: topDep.DepartmentId,
  294 + Name: topDep.Name,
  295 + }
  296 + }
  297 + if topPos := agg.GetTopPosition(userBaseAgg.Position); topPos.PositionId != 0 {
  298 + rsp.User.Position = protocol.Job{
  299 + Id: topPos.PositionId,
  300 + Name: topPos.Name,
  301 + }
  302 + }
278 return 303 return
279 } 304 }