正在显示
17 个修改的文件
包含
525 行增加
和
126 行删除
controllers/common.go
0 → 100644
| 1 | +package controllers | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "encoding/json" | ||
| 5 | + "oppmg/common/log" | ||
| 6 | + "oppmg/protocol" | ||
| 7 | + servecommon "oppmg/services/common" | ||
| 8 | +) | ||
| 9 | + | ||
| 10 | +//公共接口 | ||
| 11 | +type CommonController struct { | ||
| 12 | + BaseController | ||
| 13 | +} | ||
| 14 | + | ||
| 15 | +func (c *CommonController) URLMapping() { | ||
| 16 | + | ||
| 17 | +} | ||
| 18 | + | ||
| 19 | +//SelectorDepartment 下拉列表 -部门 | ||
| 20 | +//@router /department [post] | ||
| 21 | +func (c *CommonController) SelectorDepartment() { | ||
| 22 | + var msg *protocol.ResponseMessage | ||
| 23 | + defer func() { | ||
| 24 | + c.ResposeJson(msg) | ||
| 25 | + }() | ||
| 26 | + companyid := c.GetCompanyId() | ||
| 27 | + departs := servecommon.SelectorDepartment(companyid) | ||
| 28 | + | ||
| 29 | + msg = protocol.NewReturnResponse(departs, nil) | ||
| 30 | + return | ||
| 31 | +} | ||
| 32 | + | ||
| 33 | +//SelectorRole 下拉列表 -角色 | ||
| 34 | +//@router /role [post] | ||
| 35 | +func (c *CommonController) SelectorRole() { | ||
| 36 | + | ||
| 37 | + var msg *protocol.ResponseMessage | ||
| 38 | + defer func() { | ||
| 39 | + c.ResposeJson(msg) | ||
| 40 | + }() | ||
| 41 | + type Parameter struct { | ||
| 42 | + For string `json:"for"` | ||
| 43 | + } | ||
| 44 | + var param Parameter | ||
| 45 | + if err := json.Unmarshal(c.Ctx.Input.RequestBody, ¶m); err != nil { | ||
| 46 | + log.Error("json 解析失败 err:%s", err) | ||
| 47 | + msg = protocol.BadRequestParam("1") | ||
| 48 | + return | ||
| 49 | + } | ||
| 50 | + companyid := c.GetCompanyId() | ||
| 51 | + userid := c.GetUserId() | ||
| 52 | + if companyid <= 0 || userid <= 0 { | ||
| 53 | + msg = protocol.BadRequestParam("1") | ||
| 54 | + return | ||
| 55 | + } | ||
| 56 | + var roles []protocol.RoleBase | ||
| 57 | + switch param.For { | ||
| 58 | + case "user": | ||
| 59 | + list := servecommon.SelectorRoleAll(companyid) | ||
| 60 | + roles = servecommon.FilterRoleAll(userid, companyid, list) | ||
| 61 | + case "role": | ||
| 62 | + list := servecommon.SelectorRoleAll(companyid) | ||
| 63 | + roles = servecommon.FilterRoleGroup(userid, companyid, list) | ||
| 64 | + case "all": | ||
| 65 | + roles = servecommon.SelectorRoleAll(companyid) | ||
| 66 | + default: | ||
| 67 | + roles = servecommon.SelectorRoleAll(companyid) | ||
| 68 | + } | ||
| 69 | + msg = protocol.NewReturnResponse(roles, nil) | ||
| 70 | + return | ||
| 71 | + | ||
| 72 | +} | ||
| 73 | + | ||
| 74 | +//SelectorPosition 下拉列表 -职位 | ||
| 75 | +//@router /position [post] | ||
| 76 | +func (c *CommonController) SelectorPosition() { | ||
| 77 | + var msg *protocol.ResponseMessage | ||
| 78 | + defer func() { | ||
| 79 | + c.ResposeJson(msg) | ||
| 80 | + }() | ||
| 81 | + companyid := c.GetCompanyId() | ||
| 82 | + departs := servecommon.SelectorPosition(companyid) | ||
| 83 | + | ||
| 84 | + msg = protocol.NewReturnResponse(departs, nil) | ||
| 85 | + return | ||
| 86 | +} |
| @@ -5,7 +5,10 @@ import ( | @@ -5,7 +5,10 @@ import ( | ||
| 5 | "oppmg/common/log" | 5 | "oppmg/common/log" |
| 6 | "oppmg/protocol" | 6 | "oppmg/protocol" |
| 7 | servecompany "oppmg/services/company" | 7 | servecompany "oppmg/services/company" |
| 8 | + "oppmg/storage/redisdata" | ||
| 9 | + "oppmg/utils" | ||
| 8 | "strconv" | 10 | "strconv" |
| 11 | + "strings" | ||
| 9 | ) | 12 | ) |
| 10 | 13 | ||
| 11 | type CompanyController struct { | 14 | type CompanyController struct { |
| @@ -226,13 +229,35 @@ func (c *CompanyController) UserAdd() { | @@ -226,13 +229,35 @@ func (c *CompanyController) UserAdd() { | ||
| 226 | msg = protocol.BadRequestParam("1") | 229 | msg = protocol.BadRequestParam("1") |
| 227 | return | 230 | return |
| 228 | } | 231 | } |
| 232 | + name := []rune(strings.TrimSpace(param.Name)) | ||
| 233 | + if len(name) == 0 { | ||
| 234 | + msg = protocol.BadRequestParam("10035") | ||
| 235 | + return | ||
| 236 | + } | ||
| 237 | + if len(name) > 10 { | ||
| 238 | + msg = protocol.BadRequestParam("10034") | ||
| 239 | + return | ||
| 240 | + } | ||
| 241 | + ok := utils.PhoneMatch.MatchString(param.Phone) | ||
| 242 | + if !ok { | ||
| 243 | + msg = protocol.BadRequestParam("10036") | ||
| 244 | + return | ||
| 245 | + } | ||
| 246 | + if len(param.Departments) == 0 { | ||
| 247 | + msg = protocol.BadRequestParam("10037") | ||
| 248 | + return | ||
| 249 | + } | ||
| 250 | + if len(param.Roles) == 0 { | ||
| 251 | + msg = protocol.BadRequestParam("10038") | ||
| 252 | + return | ||
| 253 | + } | ||
| 229 | param.CompanyId = c.GetCompanyId() | 254 | param.CompanyId = c.GetCompanyId() |
| 230 | err := servecompany.UserAdd(param) | 255 | err := servecompany.UserAdd(param) |
| 231 | msg = protocol.NewReturnResponse(nil, err) | 256 | msg = protocol.NewReturnResponse(nil, err) |
| 232 | return | 257 | return |
| 233 | } | 258 | } |
| 234 | 259 | ||
| 235 | -//UserAdd 添加用户 | 260 | +//UserEdit 编辑用户 |
| 236 | //@Router /user/edit [post] | 261 | //@Router /user/edit [post] |
| 237 | func (c *CompanyController) UserEdit() { | 262 | func (c *CompanyController) UserEdit() { |
| 238 | var msg *protocol.ResponseMessage | 263 | var msg *protocol.ResponseMessage |
| @@ -245,8 +270,95 @@ func (c *CompanyController) UserEdit() { | @@ -245,8 +270,95 @@ func (c *CompanyController) UserEdit() { | ||
| 245 | msg = protocol.BadRequestParam("1") | 270 | msg = protocol.BadRequestParam("1") |
| 246 | return | 271 | return |
| 247 | } | 272 | } |
| 273 | + name := []rune(strings.TrimSpace(param.Name)) | ||
| 274 | + if len(name) == 0 { | ||
| 275 | + msg = protocol.BadRequestParam("10035") | ||
| 276 | + return | ||
| 277 | + } | ||
| 278 | + if len(name) > 10 { | ||
| 279 | + msg = protocol.BadRequestParam("10034") | ||
| 280 | + return | ||
| 281 | + } | ||
| 282 | + | ||
| 283 | + if len(param.Departments) == 0 { | ||
| 284 | + msg = protocol.BadRequestParam("10037") | ||
| 285 | + return | ||
| 286 | + } | ||
| 287 | + if len(param.Roles) == 0 { | ||
| 288 | + msg = protocol.BadRequestParam("10038") | ||
| 289 | + return | ||
| 290 | + } | ||
| 248 | param.CompanyId = c.GetCompanyId() | 291 | param.CompanyId = c.GetCompanyId() |
| 249 | err := servecompany.UserEdit(param) | 292 | err := servecompany.UserEdit(param) |
| 250 | msg = protocol.NewReturnResponse(nil, err) | 293 | msg = protocol.NewReturnResponse(nil, err) |
| 251 | return | 294 | return |
| 252 | } | 295 | } |
| 296 | + | ||
| 297 | +//UserDelete 删除用户 | ||
| 298 | +//@Router /user/delete [post] | ||
| 299 | +func (c *CompanyController) UserDelete() { | ||
| 300 | + var msg *protocol.ResponseMessage | ||
| 301 | + defer func() { | ||
| 302 | + c.ResposeJson(msg) | ||
| 303 | + }() | ||
| 304 | + type Parameter struct { | ||
| 305 | + Userid int64 `json:"user_id"` | ||
| 306 | + } | ||
| 307 | + var param Parameter | ||
| 308 | + if err := json.Unmarshal(c.Ctx.Input.RequestBody, ¶m); err != nil { | ||
| 309 | + log.Error("json 解析失败 err:%s", err) | ||
| 310 | + msg = protocol.BadRequestParam("1") | ||
| 311 | + return | ||
| 312 | + } | ||
| 313 | + companyId := c.GetCompanyId() | ||
| 314 | + err := servecompany.UserDelete(param.Userid, companyId) | ||
| 315 | + if err == nil { | ||
| 316 | + e := redisdata.DeleteLoginToken(param.Userid) | ||
| 317 | + if e != nil { | ||
| 318 | + log.Error(e.Error()) | ||
| 319 | + } | ||
| 320 | + } | ||
| 321 | + msg = protocol.NewReturnResponse(nil, err) | ||
| 322 | + return | ||
| 323 | +} | ||
| 324 | + | ||
| 325 | +//UserEdit 禁用、启用用户 TODO | ||
| 326 | +//@Router /user/enable [post] | ||
| 327 | +func (c *CompanyController) UserEnable() { | ||
| 328 | + var msg *protocol.ResponseMessage | ||
| 329 | + defer func() { | ||
| 330 | + c.ResposeJson(msg) | ||
| 331 | + }() | ||
| 332 | + var param protocol.RequestUserEdit | ||
| 333 | + if err := json.Unmarshal(c.Ctx.Input.RequestBody, ¶m); err != nil { | ||
| 334 | + log.Error("json 解析失败 err:%s", err) | ||
| 335 | + msg = protocol.BadRequestParam("1") | ||
| 336 | + return | ||
| 337 | + } | ||
| 338 | + | ||
| 339 | + param.CompanyId = c.GetCompanyId() | ||
| 340 | + err := servecompany.UserEdit(param) | ||
| 341 | + redisdata.DeleteLoginToken(0) | ||
| 342 | + msg = protocol.NewReturnResponse(nil, err) | ||
| 343 | + return | ||
| 344 | +} | ||
| 345 | + | ||
| 346 | +//UserEdit用户列表 | ||
| 347 | +//@Router /user/list [post] | ||
| 348 | +func (c *CompanyController) UserList() { | ||
| 349 | + var msg *protocol.ResponseMessage | ||
| 350 | + defer func() { | ||
| 351 | + c.ResposeJson(msg) | ||
| 352 | + }() | ||
| 353 | + var param protocol.RequestUserList | ||
| 354 | + if err := json.Unmarshal(c.Ctx.Input.RequestBody, ¶m); err != nil { | ||
| 355 | + log.Error("json 解析失败 err:%s", err) | ||
| 356 | + msg = protocol.BadRequestParam("1") | ||
| 357 | + return | ||
| 358 | + } | ||
| 359 | + | ||
| 360 | + param.Companyid = c.GetCompanyId() | ||
| 361 | + result, err := servecompany.UserList(param) | ||
| 362 | + msg = protocol.NewPageDataResponse(result, err) | ||
| 363 | + return | ||
| 364 | +} |
| @@ -10,7 +10,7 @@ import ( | @@ -10,7 +10,7 @@ import ( | ||
| 10 | type Company struct { | 10 | type Company struct { |
| 11 | Id int64 `orm:"column(id);auto"` | 11 | Id int64 `orm:"column(id);auto"` |
| 12 | Name string `orm:"column(name);size(40)"` | 12 | Name string `orm:"column(name);size(40)"` |
| 13 | - Admin_id int64 `orm:"column(admin_id)"` | 13 | + AdminId int64 `orm:"column(admin_id)"` |
| 14 | CreateAt time.Time `orm:"column(create_at);type(timestamp);auto_now"` | 14 | CreateAt time.Time `orm:"column(create_at);type(timestamp);auto_now"` |
| 15 | UpdateAt time.Time `orm:"column(update_at);type(timestamp)"` | 15 | UpdateAt time.Time `orm:"column(update_at);type(timestamp)"` |
| 16 | DeleteAt time.Time `orm:"column(delete_at);type(timestamp)"` | 16 | DeleteAt time.Time `orm:"column(delete_at);type(timestamp)"` |
| @@ -62,7 +62,9 @@ func AddUser(m *User, om ...orm.Ormer) (id int64, err error) { | @@ -62,7 +62,9 @@ func AddUser(m *User, om ...orm.Ormer) (id int64, err error) { | ||
| 62 | } else { | 62 | } else { |
| 63 | o = orm.NewOrm() | 63 | o = orm.NewOrm() |
| 64 | } | 64 | } |
| 65 | - | 65 | + m.DeleteAt = time.Unix(0, 0) |
| 66 | + m.EnableStatus = USER_ENABLE_YES | ||
| 67 | + m.CreateAt = time.Now() | ||
| 66 | id, err = o.Insert(m) | 68 | id, err = o.Insert(m) |
| 67 | return | 69 | return |
| 68 | } | 70 | } |
| @@ -80,15 +82,16 @@ func GetUserById(id int64) (v *User, err error) { | @@ -80,15 +82,16 @@ func GetUserById(id int64) (v *User, err error) { | ||
| 80 | 82 | ||
| 81 | // UpdateUser updates User by Id and returns error if | 83 | // UpdateUser updates User by Id and returns error if |
| 82 | // the record to be updated doesn't exist | 84 | // the record to be updated doesn't exist |
| 83 | -func UpdateUserById(m *User) (err error) { | ||
| 84 | - o := orm.NewOrm() | ||
| 85 | - v := User{Id: m.Id} | ||
| 86 | - // ascertain id exists in the database | ||
| 87 | - if err = o.Read(&v); err == nil { | ||
| 88 | - var num int64 | ||
| 89 | - if num, err = o.Update(m); err == nil { | ||
| 90 | - fmt.Println("Number of records updated in database:", num) | ||
| 91 | - } | 85 | +func UpdateUserById(m *User, col []string, om ...orm.Ormer) (err error) { |
| 86 | + var o orm.Ormer | ||
| 87 | + if len(om) == 0 { | ||
| 88 | + o = orm.NewOrm() | ||
| 89 | + } else { | ||
| 90 | + o = om[0] | ||
| 91 | + } | ||
| 92 | + var num int64 | ||
| 93 | + if num, err = o.Update(m, col...); err == nil { | ||
| 94 | + fmt.Println("Number of records updated in database:", num) | ||
| 92 | } | 95 | } |
| 93 | return | 96 | return |
| 94 | } | 97 | } |
| @@ -92,3 +92,23 @@ func GetUserCompanyBy(userid int64, companyId int64) (*UserCompany, error) { | @@ -92,3 +92,23 @@ func GetUserCompanyBy(userid int64, companyId int64) (*UserCompany, error) { | ||
| 92 | } | 92 | } |
| 93 | return data[0], nil | 93 | return data[0], nil |
| 94 | } | 94 | } |
| 95 | + | ||
| 96 | +func ExistUserCompany(userid int64, companyId int64) bool { | ||
| 97 | + o := orm.NewOrm() | ||
| 98 | + ok := o.QueryTable(&UserCompany{}). | ||
| 99 | + Filter("UserId", userid). | ||
| 100 | + Filter("CompanyId", companyId). | ||
| 101 | + Exist() | ||
| 102 | + return ok | ||
| 103 | +} | ||
| 104 | + | ||
| 105 | +func EnableUserCompany(userid int64, companyid int64) error { | ||
| 106 | + o := orm.NewOrm() | ||
| 107 | + _, err := o.QueryTable(&UserCompany{}). | ||
| 108 | + Filter("UserId", userid). | ||
| 109 | + Filter("CompanyId", companyid).Update(orm.Params{ | ||
| 110 | + "enable": USERCOMPANY_ENABLE_YES, | ||
| 111 | + "delete_at": 0, | ||
| 112 | + }) | ||
| 113 | + return err | ||
| 114 | +} |
| @@ -85,18 +85,16 @@ func UpdateUserDepartmentById(m *UserDepartment) (err error) { | @@ -85,18 +85,16 @@ func UpdateUserDepartmentById(m *UserDepartment) (err error) { | ||
| 85 | } | 85 | } |
| 86 | return | 86 | return |
| 87 | } | 87 | } |
| 88 | - | ||
| 89 | -// DeleteUserDepartment deletes UserDepartment by Id and returns error if | ||
| 90 | -// the record to be deleted doesn't exist | ||
| 91 | -func DeleteUserDepartment(id int64) (err error) { | 88 | +func GetUserDepartment(userid, companyid int64) ([]*UserDepartment, error) { |
| 92 | o := orm.NewOrm() | 89 | o := orm.NewOrm() |
| 93 | - v := UserDepartment{Id: id} | ||
| 94 | - // ascertain id exists in the database | ||
| 95 | - if err = o.Read(&v); err == nil { | ||
| 96 | - var num int64 | ||
| 97 | - if num, err = o.Delete(&UserDepartment{Id: id}); err == nil { | ||
| 98 | - fmt.Println("Number of records deleted in database:", num) | ||
| 99 | - } | ||
| 100 | - } | ||
| 101 | - return | 90 | + var ( |
| 91 | + err error | ||
| 92 | + result []*UserDepartment | ||
| 93 | + ) | ||
| 94 | + _, err = o.QueryTable(&UserDepartment{}). | ||
| 95 | + Filter("user_id", userid). | ||
| 96 | + Filter("company_id", companyid). | ||
| 97 | + Filter("enable_status", 1). | ||
| 98 | + All(&result) | ||
| 99 | + return result, err | ||
| 102 | } | 100 | } |
| @@ -60,43 +60,16 @@ func AddUserPosition(m *UserPosition) (id int64, err error) { | @@ -60,43 +60,16 @@ func AddUserPosition(m *UserPosition) (id int64, err error) { | ||
| 60 | return | 60 | return |
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | -// GetUserPositionById retrieves UserPosition by Id. Returns error if | ||
| 64 | -// Id doesn't exist | ||
| 65 | -func GetUserPositionById(id int64) (v *UserPosition, err error) { | 63 | +func GetUserPosition(userid, companyid int64) ([]*UserPosition, error) { |
| 66 | o := orm.NewOrm() | 64 | o := orm.NewOrm() |
| 67 | - v = &UserPosition{Id: id} | ||
| 68 | - if err = o.Read(v); err == nil { | ||
| 69 | - return v, nil | ||
| 70 | - } | ||
| 71 | - return nil, err | ||
| 72 | -} | ||
| 73 | - | ||
| 74 | -// UpdateUserPosition updates UserPosition by Id and returns error if | ||
| 75 | -// the record to be updated doesn't exist | ||
| 76 | -func UpdateUserPositionById(m *UserPosition) (err error) { | ||
| 77 | - o := orm.NewOrm() | ||
| 78 | - v := UserPosition{Id: m.Id} | ||
| 79 | - // ascertain id exists in the database | ||
| 80 | - if err = o.Read(&v); err == nil { | ||
| 81 | - var num int64 | ||
| 82 | - if num, err = o.Update(m); err == nil { | ||
| 83 | - fmt.Println("Number of records updated in database:", num) | ||
| 84 | - } | ||
| 85 | - } | ||
| 86 | - return | ||
| 87 | -} | ||
| 88 | - | ||
| 89 | -// DeleteUserPosition deletes UserPosition by Id and returns error if | ||
| 90 | -// the record to be deleted doesn't exist | ||
| 91 | -func DeleteUserPosition(id int64) (err error) { | ||
| 92 | - o := orm.NewOrm() | ||
| 93 | - v := UserPosition{Id: id} | ||
| 94 | - // ascertain id exists in the database | ||
| 95 | - if err = o.Read(&v); err == nil { | ||
| 96 | - var num int64 | ||
| 97 | - if num, err = o.Delete(&UserPosition{Id: id}); err == nil { | ||
| 98 | - fmt.Println("Number of records deleted in database:", num) | ||
| 99 | - } | ||
| 100 | - } | ||
| 101 | - return | 65 | + var ( |
| 66 | + err error | ||
| 67 | + result []*UserPosition | ||
| 68 | + ) | ||
| 69 | + _, err = o.QueryTable(&UserPosition{}). | ||
| 70 | + Filter("user_id", userid). | ||
| 71 | + Filter("company_id", companyid). | ||
| 72 | + Filter("enable_status", 1). | ||
| 73 | + All(&result) | ||
| 74 | + return result, err | ||
| 102 | } | 75 | } |
| @@ -58,43 +58,16 @@ func AddUserRole(m *UserRole) (id int64, err error) { | @@ -58,43 +58,16 @@ func AddUserRole(m *UserRole) (id int64, err error) { | ||
| 58 | return | 58 | return |
| 59 | } | 59 | } |
| 60 | 60 | ||
| 61 | -// GetUserRoleById retrieves UserRole by Id. Returns error if | ||
| 62 | -// Id doesn't exist | ||
| 63 | -func GetUserRoleById(id int) (v *UserRole, err error) { | 61 | +func GetUserRole(userid, companyid int64) ([]*UserRole, error) { |
| 64 | o := orm.NewOrm() | 62 | o := orm.NewOrm() |
| 65 | - v = &UserRole{Id: id} | ||
| 66 | - if err = o.Read(v); err == nil { | ||
| 67 | - return v, nil | ||
| 68 | - } | ||
| 69 | - return nil, err | ||
| 70 | -} | ||
| 71 | - | ||
| 72 | -// UpdateUserRole updates UserRole by Id and returns error if | ||
| 73 | -// the record to be updated doesn't exist | ||
| 74 | -func UpdateUserRoleById(m *UserRole) (err error) { | ||
| 75 | - o := orm.NewOrm() | ||
| 76 | - v := UserRole{Id: m.Id} | ||
| 77 | - // ascertain id exists in the database | ||
| 78 | - if err = o.Read(&v); err == nil { | ||
| 79 | - var num int64 | ||
| 80 | - if num, err = o.Update(m); err == nil { | ||
| 81 | - fmt.Println("Number of records updated in database:", num) | ||
| 82 | - } | ||
| 83 | - } | ||
| 84 | - return | ||
| 85 | -} | ||
| 86 | - | ||
| 87 | -// DeleteUserRole deletes UserRole by Id and returns error if | ||
| 88 | -// the record to be deleted doesn't exist | ||
| 89 | -func DeleteUserRole(id int) (err error) { | ||
| 90 | - o := orm.NewOrm() | ||
| 91 | - v := UserRole{Id: id} | ||
| 92 | - // ascertain id exists in the database | ||
| 93 | - if err = o.Read(&v); err == nil { | ||
| 94 | - var num int64 | ||
| 95 | - if num, err = o.Delete(&UserRole{Id: id}); err == nil { | ||
| 96 | - fmt.Println("Number of records deleted in database:", num) | ||
| 97 | - } | ||
| 98 | - } | ||
| 99 | - return | 63 | + var ( |
| 64 | + err error | ||
| 65 | + result []*UserRole | ||
| 66 | + ) | ||
| 67 | + _, err = o.QueryTable(&UserRole{}). | ||
| 68 | + Filter("user_id", userid). | ||
| 69 | + Filter("company_id", companyid). | ||
| 70 | + Filter("enable_status", 1). | ||
| 71 | + All(&result) | ||
| 72 | + return result, err | ||
| 100 | } | 73 | } |
| @@ -12,3 +12,26 @@ type ResponsePageInfo struct { | @@ -12,3 +12,26 @@ type ResponsePageInfo struct { | ||
| 12 | CurrentPage int `json:"pageNumber"` | 12 | CurrentPage int `json:"pageNumber"` |
| 13 | // ListData interface{} `json:"list"` | 13 | // ListData interface{} `json:"list"` |
| 14 | } | 14 | } |
| 15 | + | ||
| 16 | +// DepartmentBase下拉选择列表-部门 | ||
| 17 | +type DepartmentBase struct { | ||
| 18 | + Id int64 `json:"id" orm:"column(id)"` | ||
| 19 | + Name string `json:"name" orm:"column(name)"` | ||
| 20 | + ParentId int64 `json:"parent_id" orm:"column(parent_id)"` | ||
| 21 | +} | ||
| 22 | + | ||
| 23 | +//RoleBase 下拉选择列表-角色 | ||
| 24 | +type RoleBase struct { | ||
| 25 | + Id int64 `json:"id" orm:"column(id)"` | ||
| 26 | + Name string `json:"name" orm:"column(name)"` | ||
| 27 | + IsDefault int8 `json:"is_default" orm:"column(is_default)"` | ||
| 28 | + ParentId int64 `json:"parent_id" orm:"column(pid)"` | ||
| 29 | + Types int8 `json:"types" orm:"column(types)"` | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +//PositionBase 下拉选择列表-职位 | ||
| 33 | +type PositionBase struct { | ||
| 34 | + Id int64 `json:"id" orm:"column(id)"` | ||
| 35 | + Name string `json:"name" orm:"column(name)"` | ||
| 36 | + ParentId int64 `json:"parent_id" orm:"column(parent_id)"` | ||
| 37 | +} |
| @@ -89,7 +89,8 @@ type RequestUserEdit struct { | @@ -89,7 +89,8 @@ type RequestUserEdit struct { | ||
| 89 | //RequestUserList 获取用户列表 | 89 | //RequestUserList 获取用户列表 |
| 90 | type RequestUserList struct { | 90 | type RequestUserList struct { |
| 91 | RequestPageInfo | 91 | RequestPageInfo |
| 92 | - NickName string `json:"nick_name"` | 92 | + NickName string `json:"nick_name"` |
| 93 | + Companyid int64 `json:"company_id"` | ||
| 93 | } | 94 | } |
| 94 | 95 | ||
| 95 | //ResponseUserList 响应的用户列表 | 96 | //ResponseUserList 响应的用户列表 |
| @@ -99,10 +100,10 @@ type ResponseUserList struct { | @@ -99,10 +100,10 @@ type ResponseUserList struct { | ||
| 99 | } | 100 | } |
| 100 | 101 | ||
| 101 | type UserListItem struct { | 102 | type UserListItem struct { |
| 102 | - Id int64 `json:"id"` | ||
| 103 | - NickName string `json:"nick_name"` | ||
| 104 | - Position string `json:"position"` | ||
| 105 | - Role string `json:"role"` | ||
| 106 | - Department string `json:"department"` | ||
| 107 | - Status int8 `json:"status"` | 103 | + UserId int64 `json:"user_id" orm:"column(user_id)"` |
| 104 | + NickName string `json:"nick_name" orm:"column(nick_name)"` | ||
| 105 | + Positions string `json:"positions" orm:"-"` | ||
| 106 | + Roles string `json:"roles" orm:"-"` | ||
| 107 | + Departments string `json:"departments" orm:"-"` | ||
| 108 | + Enable int8 `json:"enable" orm:"column(enable)"` | ||
| 108 | } | 109 | } |
| @@ -21,6 +21,11 @@ var errmessge ErrorMap = map[string]string{ | @@ -21,6 +21,11 @@ var errmessge ErrorMap = map[string]string{ | ||
| 21 | "10031": "无效角色", | 21 | "10031": "无效角色", |
| 22 | "10032": "无效部门", | 22 | "10032": "无效部门", |
| 23 | "10033": "无效职位", | 23 | "10033": "无效职位", |
| 24 | + "10034": "名字限制10个字符以内", | ||
| 25 | + "10035": "名字是必填项", | ||
| 26 | + "10036": "请输入正确的手机格式", | ||
| 27 | + "10037": "用户的部门必填", | ||
| 28 | + "10038": "用户的角色必填", | ||
| 24 | } | 29 | } |
| 25 | 30 | ||
| 26 | //错误码转换 ,兼容需要 | 31 | //错误码转换 ,兼容需要 |
| @@ -12,7 +12,7 @@ func init() { | @@ -12,7 +12,7 @@ func init() { | ||
| 12 | nsV1 := beego.NewNamespace("v1", | 12 | nsV1 := beego.NewNamespace("v1", |
| 13 | beego.NSBefore(middleware.AllowOption), | 13 | beego.NSBefore(middleware.AllowOption), |
| 14 | beego.NSBefore(middleware.LogRequestData), | 14 | beego.NSBefore(middleware.LogRequestData), |
| 15 | - beego.NSBefore(middleware.AuthToken), | 15 | + //beego.NSBefore(middleware.AuthToken), |
| 16 | beego.NSNamespace("/department", | 16 | beego.NSNamespace("/department", |
| 17 | beego.NSRouter("/list", &controllers.CompanyController{}, "get:DepartmentList"), | 17 | beego.NSRouter("/list", &controllers.CompanyController{}, "get:DepartmentList"), |
| 18 | beego.NSRouter("/add", &controllers.CompanyController{}, "post:DepartmentAdd"), | 18 | beego.NSRouter("/add", &controllers.CompanyController{}, "post:DepartmentAdd"), |
| @@ -35,10 +35,10 @@ func init() { | @@ -35,10 +35,10 @@ func init() { | ||
| 35 | beego.NSRouter("/role", &controllers.RbacController{}, "get:RoleList"), | 35 | beego.NSRouter("/role", &controllers.RbacController{}, "get:RoleList"), |
| 36 | ), | 36 | ), |
| 37 | beego.NSNamespace("/user/", | 37 | beego.NSNamespace("/user/", |
| 38 | - // beego.NSRouter("/list", &controllers.CompanyController{}, "post:UserList"), | 38 | + beego.NSRouter("/list", &controllers.CompanyController{}, "post:UserList"), |
| 39 | beego.NSRouter("/add", &controllers.CompanyController{}, "post:UserAdd"), | 39 | beego.NSRouter("/add", &controllers.CompanyController{}, "post:UserAdd"), |
| 40 | beego.NSRouter("/edit", &controllers.CompanyController{}, "post:UserEdit"), | 40 | beego.NSRouter("/edit", &controllers.CompanyController{}, "post:UserEdit"), |
| 41 | - // beego.NSRouter("/delete", &controllers.CompanyController{}, "post:PositionDelete"), | 41 | + beego.NSRouter("/delete", &controllers.CompanyController{}, "post:UserDelete"), |
| 42 | ), | 42 | ), |
| 43 | beego.NSNamespace("/auth", | 43 | beego.NSNamespace("/auth", |
| 44 | beego.NSRouter("/change_company", &controllers.AuthController{}, "post:ChangeCompany"), | 44 | beego.NSRouter("/change_company", &controllers.AuthController{}, "post:ChangeCompany"), |
| @@ -50,6 +50,11 @@ func init() { | @@ -50,6 +50,11 @@ func init() { | ||
| 50 | beego.NSRouter("/:id([0-9]+)", &controllers.BulletinController{}, "get:GetBulletin"), | 50 | beego.NSRouter("/:id([0-9]+)", &controllers.BulletinController{}, "get:GetBulletin"), |
| 51 | beego.NSRouter("/update", &controllers.BulletinController{}, "post:UpdateBulletin"), | 51 | beego.NSRouter("/update", &controllers.BulletinController{}, "post:UpdateBulletin"), |
| 52 | ), | 52 | ), |
| 53 | + beego.NSNamespace("common", | ||
| 54 | + beego.NSRouter("/department", &controllers.CommonController{}, "post:SelectorDepartment"), | ||
| 55 | + beego.NSRouter("/role", &controllers.CommonController{}, "post:SelectorRole"), | ||
| 56 | + beego.NSRouter("/position", &controllers.CommonController{}, "post:SelectorPosition"), | ||
| 57 | + ), | ||
| 53 | ) | 58 | ) |
| 54 | 59 | ||
| 55 | nsAuth := beego.NewNamespace("/auth", | 60 | nsAuth := beego.NewNamespace("/auth", |
services/common/selector.go
0 → 100644
| 1 | +package common | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + "oppmg/common/log" | ||
| 6 | + "oppmg/models" | ||
| 7 | + "oppmg/protocol" | ||
| 8 | + "oppmg/utils" | ||
| 9 | +) | ||
| 10 | + | ||
| 11 | +func SelectorDepartment(companyid int64) []protocol.DepartmentBase { | ||
| 12 | + const dataSql string = `SELECT id,name,parent_id FROM department WHERE company_id = ? AND delete_at = 0` | ||
| 13 | + var ( | ||
| 14 | + err error | ||
| 15 | + departs []protocol.DepartmentBase | ||
| 16 | + ) | ||
| 17 | + err = utils.ExecuteQueryAll(&departs, dataSql, companyid) | ||
| 18 | + if err != nil { | ||
| 19 | + e := fmt.Errorf("EXECUTE SQL err:%s", err) | ||
| 20 | + log.Error(e.Error()) | ||
| 21 | + } | ||
| 22 | + return departs | ||
| 23 | +} | ||
| 24 | + | ||
| 25 | +func SelectorRoleAll(companyid int64) []protocol.RoleBase { | ||
| 26 | + const datasql string = `SELECT id,pid,name,types,is_default FROM role WHERE company_id = ? AND delete_at =0` | ||
| 27 | + var ( | ||
| 28 | + err error | ||
| 29 | + roles []protocol.RoleBase | ||
| 30 | + ) | ||
| 31 | + err = utils.ExecuteQueryAll(&roles, datasql, companyid) | ||
| 32 | + if err != nil { | ||
| 33 | + e := fmt.Errorf("EXECUTE SQL err:%s", err) | ||
| 34 | + log.Error(e.Error()) | ||
| 35 | + } | ||
| 36 | + return roles | ||
| 37 | +} | ||
| 38 | + | ||
| 39 | +func FilterRoleAll(adminid int64, companyid int64, list []protocol.RoleBase) []protocol.RoleBase { | ||
| 40 | + var ( | ||
| 41 | + newlist []protocol.RoleBase | ||
| 42 | + admingoroupId int64 | ||
| 43 | + ) | ||
| 44 | + companyInfo, err := models.GetCompanyById(companyid) | ||
| 45 | + if err != nil { | ||
| 46 | + e := fmt.Errorf("GetCompanyById(%d) err:%s", companyid, err) | ||
| 47 | + log.Error(e.Error()) | ||
| 48 | + return newlist | ||
| 49 | + } | ||
| 50 | + if companyInfo.AdminId == adminid { | ||
| 51 | + return list | ||
| 52 | + } | ||
| 53 | + for _, v := range list { | ||
| 54 | + if v.IsDefault == models.ROLE_DEFAULR && v.Types == models.ROLETYPES_GROUP { | ||
| 55 | + admingoroupId = v.Id | ||
| 56 | + } | ||
| 57 | + } | ||
| 58 | + for k, v := range list { | ||
| 59 | + if v.Id == admingoroupId || v.ParentId == admingoroupId { | ||
| 60 | + continue | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + newlist = append(newlist, list[k]) | ||
| 64 | + } | ||
| 65 | + return newlist | ||
| 66 | +} | ||
| 67 | + | ||
| 68 | +func FilterRoleGroup(adminid int64, companyid int64, list []protocol.RoleBase) []protocol.RoleBase { | ||
| 69 | + var ( | ||
| 70 | + newlist []protocol.RoleBase | ||
| 71 | + ) | ||
| 72 | + companyInfo, err := models.GetCompanyById(companyid) | ||
| 73 | + if err != nil { | ||
| 74 | + e := fmt.Errorf("GetCompanyById(%d) err:%s", companyid, err) | ||
| 75 | + log.Error(e.Error()) | ||
| 76 | + return newlist | ||
| 77 | + } | ||
| 78 | + if companyInfo.AdminId != adminid { | ||
| 79 | + for k, v := range list { | ||
| 80 | + if v.Types == models.ROLETYPES_GROUP && v.IsDefault == models.ROLE_DEFAULR_NOT { | ||
| 81 | + newlist = append(newlist, list[k]) | ||
| 82 | + } | ||
| 83 | + } | ||
| 84 | + return newlist | ||
| 85 | + } | ||
| 86 | + for k, v := range list { | ||
| 87 | + if v.Types == models.ROLETYPES_GROUP { | ||
| 88 | + newlist = append(newlist, list[k]) | ||
| 89 | + } | ||
| 90 | + } | ||
| 91 | + return newlist | ||
| 92 | +} | ||
| 93 | + | ||
| 94 | +func SelectorPosition(companyid int64) []protocol.PositionBase { | ||
| 95 | + const datasql string = `SELECT id,parent_id,name FROM position WHERE company_id =? AND delete_at =0` | ||
| 96 | + var ( | ||
| 97 | + err error | ||
| 98 | + positions []protocol.PositionBase | ||
| 99 | + ) | ||
| 100 | + err = utils.ExecuteQueryAll(&positions, datasql, companyid) | ||
| 101 | + if err != nil { | ||
| 102 | + e := fmt.Errorf("EXECUTE SQL err:%s", err) | ||
| 103 | + log.Error(e.Error()) | ||
| 104 | + } | ||
| 105 | + return positions | ||
| 106 | + | ||
| 107 | +} |
| @@ -137,32 +137,41 @@ func validCompanyPosition(companyid int64, positionid []int64) error { | @@ -137,32 +137,41 @@ func validCompanyPosition(companyid int64, positionid []int64) error { | ||
| 137 | //registUser 注册用户 | 137 | //registUser 注册用户 |
| 138 | func registUser(userIn *models.User, companyid int64, o orm.Ormer) error { | 138 | func registUser(userIn *models.User, companyid int64, o orm.Ormer) error { |
| 139 | var ( | 139 | var ( |
| 140 | - err error | ||
| 141 | - // usrData *models.User | 140 | + err error |
| 141 | + usrData *models.User | ||
| 142 | ) | 142 | ) |
| 143 | - _, err = models.GetUserByPhone(userIn.Phone) | ||
| 144 | - if err == nil { | ||
| 145 | - return nil | ||
| 146 | - } | 143 | + usrData, err = models.GetUserByPhone(userIn.Phone) |
| 147 | if err != nil && err != orm.ErrNoRows { | 144 | if err != nil && err != orm.ErrNoRows { |
| 148 | return err | 145 | return err |
| 149 | } | 146 | } |
| 150 | if err == orm.ErrNoRows { | 147 | if err == orm.ErrNoRows { |
| 148 | + //用户不存在 添加用户 | ||
| 151 | _, err := models.AddUser(userIn, o) | 149 | _, err := models.AddUser(userIn, o) |
| 152 | if err != nil { | 150 | if err != nil { |
| 153 | return err | 151 | return err |
| 154 | } | 152 | } |
| 155 | } | 153 | } |
| 154 | + if err == nil { | ||
| 155 | + // 用户存在,更新用户 | ||
| 156 | + usrData.NickName = userIn.NickName | ||
| 157 | + err = models.UpdateUserById(usrData, []string{"NickName"}, o) | ||
| 158 | + if err != nil { | ||
| 159 | + return err | ||
| 160 | + } | ||
| 161 | + } | ||
| 156 | musercompany := &models.UserCompany{ | 162 | musercompany := &models.UserCompany{ |
| 157 | CompanyId: companyid, | 163 | CompanyId: companyid, |
| 158 | UserId: userIn.Id, | 164 | UserId: userIn.Id, |
| 159 | } | 165 | } |
| 160 | - _, err = models.AddUserCompany(musercompany, o) | ||
| 161 | - if err != nil { | ||
| 162 | - return err | 166 | + ok := models.ExistUserCompany(usrData.Id, companyid) |
| 167 | + if !ok { | ||
| 168 | + _, err = models.AddUserCompany(musercompany, o) | ||
| 169 | + if err != nil { | ||
| 170 | + return err | ||
| 171 | + } | ||
| 163 | } | 172 | } |
| 164 | - //更新 | ||
| 165 | - return nil | 173 | + err = models.EnableUserCompany(usrData.Id, companyid) |
| 174 | + return err | ||
| 166 | } | 175 | } |
| 167 | 176 | ||
| 168 | func editUserDepart(userid int64, companyid int64, departids []int64, o orm.Ormer) error { | 177 | func editUserDepart(userid int64, companyid int64, departids []int64, o orm.Ormer) error { |
| @@ -338,10 +347,77 @@ func UserEdit(param protocol.RequestUserEdit) error { | @@ -338,10 +347,77 @@ func UserEdit(param protocol.RequestUserEdit) error { | ||
| 338 | return nil | 347 | return nil |
| 339 | } | 348 | } |
| 340 | 349 | ||
| 341 | -func UserDelete(param protocol.RequestUserAdd) error { | 350 | +func UserDelete(userid, companyid int64) error { |
| 351 | + ok := models.ExistUserCompany(userid, companyid) | ||
| 352 | + if !ok { | ||
| 353 | + e := fmt.Errorf("ExistUserCompany(userid, companyid) [%d,%d] ==false ", userid, companyid) | ||
| 354 | + log.Error(e.Error()) | ||
| 355 | + return protocol.NewErrWithMessage("1") | ||
| 356 | + } | ||
| 357 | + o := orm.NewOrm() | ||
| 358 | + _, err := o.QueryTable(&models.UserCompany{}). | ||
| 359 | + Filter("user_id", userid). | ||
| 360 | + Filter("company_id"). | ||
| 361 | + Update(orm.Params{ | ||
| 362 | + "delete_at": time.Now().String(), | ||
| 363 | + }) | ||
| 364 | + if err != nil { | ||
| 365 | + e := fmt.Errorf("UserDelete err:%s", err) | ||
| 366 | + log.Error(e.Error()) | ||
| 367 | + return protocol.NewErrWithMessage("1") | ||
| 368 | + } | ||
| 342 | return nil | 369 | return nil |
| 343 | } | 370 | } |
| 344 | 371 | ||
| 345 | -func UserList() error { | ||
| 346 | - return nil | 372 | +func UserList(param protocol.RequestUserList) (protocol.ResponseUserList, error) { |
| 373 | + | ||
| 374 | + datasql := `SELECT a.company_id,a.user_id,a.enable, b.phone,b.nick_name | ||
| 375 | + FROM user_company AS a | ||
| 376 | + LEFT JOIN user AS b ON a.user_id = b.id | ||
| 377 | + WHERE a.company_id=? AND a.delete_at = 0 ` | ||
| 378 | + countSql := `SELECT count(*) | ||
| 379 | + FROM user_company AS a | ||
| 380 | + LEFT JOIN user AS b ON a.user_id = b.id | ||
| 381 | + WHERE a.company_id=? AND a.delete_at = 0 ` | ||
| 382 | + var ( | ||
| 383 | + whereString string | ||
| 384 | + cond []interface{} | ||
| 385 | + ) | ||
| 386 | + cond = append(cond, param.Companyid) | ||
| 387 | + if len(param.NickName) > 0 { | ||
| 388 | + whereString += ` AND b.nick_name LIKE ? ` | ||
| 389 | + likeCond := "%" + param.NickName + "%" | ||
| 390 | + cond = append(cond, likeCond) | ||
| 391 | + } | ||
| 392 | + var ( | ||
| 393 | + result []protocol.UserListItem | ||
| 394 | + pageInfo protocol.ResponsePageInfo | ||
| 395 | + err error | ||
| 396 | + responseData protocol.ResponseUserList | ||
| 397 | + ) | ||
| 398 | + p := utils.NewQueryDataByPage(countSql+whereString, datasql+whereString) | ||
| 399 | + p.AddParam(cond...) | ||
| 400 | + p.LimitPage(param.PageIndex, param.PageSize) | ||
| 401 | + pageInfo, err = p.Query(&result) | ||
| 402 | + if err != nil { | ||
| 403 | + e := fmt.Errorf("page data err:%s", err) | ||
| 404 | + log.Error(e.Error()) | ||
| 405 | + return responseData, protocol.NewErrWithMessage("0") | ||
| 406 | + } | ||
| 407 | + // for k, v := range result { | ||
| 408 | + // positions, _ := models.GetUserPosition(v.UserId, param.Companyid) | ||
| 409 | + // departments, _ := models.GetUserDepartment(v.UserId, param.Companyid) | ||
| 410 | + // roles, _ := models.GetUserRole(v.UserId, param.Companyid) | ||
| 411 | + // } | ||
| 412 | + responseData.ResponsePageInfo = pageInfo | ||
| 413 | + responseData.List = result | ||
| 414 | + return responseData, nil | ||
| 415 | +} | ||
| 416 | + | ||
| 417 | +func GetUserDepartment(user int64, companyid int64) { | ||
| 418 | + | ||
| 419 | +} | ||
| 420 | + | ||
| 421 | +func GetUserPosition(user int64, companyid int64) { | ||
| 422 | + | ||
| 347 | } | 423 | } |
| @@ -64,4 +64,11 @@ func ExistLoginToken(userid int64) bool { | @@ -64,4 +64,11 @@ func ExistLoginToken(userid int64) bool { | ||
| 64 | return false | 64 | return false |
| 65 | } | 65 | } |
| 66 | 66 | ||
| 67 | +func DeleteLoginToken(userid int64) error { | ||
| 68 | + client := redis.GetRedis() | ||
| 69 | + key := fmt.Sprintf("%s%s:%d", KEY_PREFIX, KEY_USER_TOKEN, userid) | ||
| 70 | + err:=client.Del(key).Err() | ||
| 71 | + return err | ||
| 72 | +} | ||
| 73 | + | ||
| 67 | //消息发布订阅 | 74 | //消息发布订阅 |
utils/jwt.go
已删除
100644 → 0
| 1 | -package utils |
-
请 注册 或 登录 后发表评论