作者 唐旭辉

接口完成提交

1 ### 接口完成进度 1 ### 接口完成进度
2 2
3 | 功能 | 完成状态 | 时间 | 接口路径 | 请求方式 | 3 | 功能 | 完成状态 | 时间 | 接口路径 | 请求方式 |
4 -| -------------- | ------------------ | --------------- | -------------------------- | -------- | 4 +| -------------- | ------------------ | --------------- | --------------------------------- | -------- |
5 | 登录 | 进行中 | | | 5 | 登录 | 进行中 | | |
6 | 短信验证码 | 未开始 | | | 6 | 短信验证码 | 未开始 | | |
7 | 获取 token | 未开始 | | | 7 | 获取 token | 未开始 | | |
8 | 刷新 token | 未开始 | | | 8 | 刷新 token | 未开始 | | |
  9 +| 获取角色列表 | 进行中 | | /v1/rbac/:companyid/role | get |
9 | 添加角色 | 完成 | 2019.11.26 | /v1/rbac/role | post | 10 | 添加角色 | 完成 | 2019.11.26 | /v1/rbac/role | post |
10 | 编辑角色 | 完成 | 2019.11.26 | /v1/rbac/role | put | 11 | 编辑角色 | 完成 | 2019.11.26 | /v1/rbac/role | put |
11 | ~~删除角色~~ | ~~完成(待变更)~~ | ~~ 2019.11.26~~ | /v1/rbac/role | delete | 12 | ~~删除角色~~ | ~~完成(待变更)~~ | ~~ 2019.11.26~~ | /v1/rbac/role | delete |
@@ -13,10 +14,12 @@ @@ -13,10 +14,12 @@
13 | 添加角色组 | 完成 | 2019.12.04 | /v1/rbac/role_group | post | 14 | 添加角色组 | 完成 | 2019.12.04 | /v1/rbac/role_group | post |
14 | 编辑角色组 | 完成 | 2019.12.04 | /v1/rbac/role_group | put | 15 | 编辑角色组 | 完成 | 2019.12.04 | /v1/rbac/role_group | put |
15 | 删除角色组 | 完成 | 2019.12.04 | /v1/rbac/role_group | delete | 16 | 删除角色组 | 完成 | 2019.12.04 | /v1/rbac/role_group | delete |
  17 +| 获取部门列表 | 完成 | 2019.12.5 | /v1/company/:companyid/department | get |
16 | 添加部门 | 完成 | 2019.11.28 | /v1/company/department | post | 18 | 添加部门 | 完成 | 2019.11.28 | /v1/company/department | post |
17 | 编辑部门 | 完成 | 2019.11.29 | /v1/company/department | put | 19 | 编辑部门 | 完成 | 2019.11.29 | /v1/company/department | put |
18 | ~~删除部门~~ | ~~完成(待变更)~~ | ~~2019.11.29~~ | ~~/v1/company/department~~ | delete | 20 | ~~删除部门~~ | ~~完成(待变更)~~ | ~~2019.11.29~~ | ~~/v1/company/department~~ | delete |
19 | 删除部门 | 完成 | 2019.12.2 | /v1/company/department | delete | 21 | 删除部门 | 完成 | 2019.12.2 | /v1/company/department | delete |
  22 +| 获取职位列表 | 进行中 | | /v1/company/:companyid/position | get |
20 | 添加职位 | 完成 | 2019.12.2 | /v1/company/position | post | 23 | 添加职位 | 完成 | 2019.12.2 | /v1/company/position | post |
21 | 编辑职位 | 完成 | 2019.12.2 | /v1/company/position | put | 24 | 编辑职位 | 完成 | 2019.12.2 | /v1/company/position | put |
22 | 删除职位 | 完成 | 2019.12.2 | /v1/company/position | delete | 25 | 删除职位 | 完成 | 2019.12.2 | /v1/company/position | delete |
@@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
2 config_name = "dev" 2 config_name = "dev"
3 #----beego的默认配置 开始--- 3 #----beego的默认配置 开始---
4 #端口号 4 #端口号
5 -httpport = 8081 5 +httpport = 8080
6 #开启应用内监控 6 #开启应用内监控
7 EnableAdmin = false 7 EnableAdmin = false
8 AdminPort = 8088 8 AdminPort = 8088
@@ -3,7 +3,6 @@ package controllers @@ -3,7 +3,6 @@ package controllers
3 import ( 3 import (
4 "encoding/json" 4 "encoding/json"
5 "fmt" 5 "fmt"
6 - "net/http"  
7 "oppmg/common/log" 6 "oppmg/common/log"
8 "oppmg/protocol" 7 "oppmg/protocol"
9 "strconv" 8 "strconv"
@@ -18,11 +17,13 @@ type BaseController struct { @@ -18,11 +17,13 @@ type BaseController struct {
18 } 17 }
19 18
20 //Options 实现beego.ControllerInterface 的接口 19 //Options 实现beego.ControllerInterface 的接口
21 -func (this *BaseController) Options() {  
22 -  
23 - this.Ctx.Abort(http.StatusBadRequest, "")  
24 -  
25 -} 20 +// func (this *BaseController) Options() {
  21 +// this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
  22 +// this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
  23 +// this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "*")
  24 +// this.Ctx.Output.SetStatus(http.StatusOK)
  25 +// return
  26 +// }
26 27
27 //Prepare 实现beego.ControllerInterface 的接口 28 //Prepare 实现beego.ControllerInterface 的接口
28 func (this *BaseController) Prepare() { 29 func (this *BaseController) Prepare() {
@@ -5,6 +5,7 @@ import ( @@ -5,6 +5,7 @@ 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 + "strconv"
8 ) 9 )
9 10
10 type CompanyController struct { 11 type CompanyController struct {
@@ -16,6 +17,63 @@ func (c *CompanyController) URLMapping() { @@ -16,6 +17,63 @@ func (c *CompanyController) URLMapping() {
16 //c.Mapping("AccessToken", c.AccessToken) 17 //c.Mapping("AccessToken", c.AccessToken)
17 } 18 }
18 19
  20 +// DepartmentAdd 部门列表
  21 +// @router /:companyid/department [get]
  22 +func (c *CompanyController) DepartmentList() {
  23 + log.Debug("DepartmentList param:%v", c.Ctx.Input.Param(":companyid"))
  24 +
  25 + var msg *protocol.ResponseMessage
  26 + defer func() {
  27 + c.ResposeJson(msg)
  28 + }()
  29 + var (
  30 + param string
  31 + companyid int64
  32 + err error
  33 + )
  34 + param = c.Ctx.Input.Param(":companyid")
  35 + companyid, err = strconv.ParseInt(param, 10, 64)
  36 + if err != nil {
  37 + msg = protocol.BadRequestParam("1")
  38 + return
  39 + }
  40 + if companyid <= 0 {
  41 + msg = protocol.BadRequestParam("1")
  42 + return
  43 + }
  44 + departs, err := servecompany.DepartmentListAll(companyid)
  45 + listdata := protocol.ResponseDepartmentList{
  46 + List: departs,
  47 + }
  48 + msg = protocol.NewReturnResponse(listdata, err)
  49 +}
  50 +
  51 +// DepartmentAdd 部门列表
  52 +// @router /department/:id [get]
  53 +func (c *CompanyController) DepartmentOne() {
  54 + log.Debug("DepartmentOne param:%v", c.Ctx.Input.Param(":id"))
  55 + var msg *protocol.ResponseMessage
  56 + defer func() {
  57 + c.ResposeJson(msg)
  58 + }()
  59 + var (
  60 + param string
  61 + id int64
  62 + err error
  63 + )
  64 + param = c.Ctx.Input.Param(":id")
  65 + id, err = strconv.ParseInt(param, 10, 64)
  66 + if err != nil {
  67 + msg = protocol.BadRequestParam("1")
  68 + return
  69 + }
  70 + if id <= 0 {
  71 + msg = protocol.BadRequestParam("1")
  72 + return
  73 + }
  74 + msg = protocol.NewReturnResponse(nil, nil)
  75 +}
  76 +
19 // DepartmentAdd 添加部门 77 // DepartmentAdd 添加部门
20 // @router /department [post] 78 // @router /department [post]
21 func (c *CompanyController) DepartmentAdd() { 79 func (c *CompanyController) DepartmentAdd() {
@@ -137,3 +195,16 @@ func (c *CompanyController) PositionDelete() { @@ -137,3 +195,16 @@ func (c *CompanyController) PositionDelete() {
137 msg = protocol.NewReturnResponse(nil, err) 195 msg = protocol.NewReturnResponse(nil, err)
138 return 196 return
139 } 197 }
  198 +
  199 +// PositionList 获取职位列表
  200 +// @router /:companyid/position [delete]
  201 +func (c *CompanyController) PositionList() {
  202 + var msg *protocol.ResponseMessage
  203 + defer func() {
  204 + c.ResposeJson(msg)
  205 + }()
  206 +
  207 + var err error
  208 + msg = protocol.NewReturnResponse(nil, err)
  209 + return
  210 +}
@@ -161,4 +161,15 @@ func (c *RbacController) RoleGroupDelete() { @@ -161,4 +161,15 @@ func (c *RbacController) RoleGroupDelete() {
161 return 161 return
162 } 162 }
163 163
164 -// RoleGroupDelete 164 +// RoleList
  165 +// @router /:companyid/role [get]
  166 +func (c *RbacController) RoleList() {
  167 + var msg *protocol.ResponseMessage
  168 + defer func() {
  169 + c.ResposeJson(msg)
  170 + }()
  171 +
  172 + var err error
  173 + msg = protocol.NewReturnResponse(nil, err)
  174 + return
  175 +}
@@ -21,6 +21,7 @@ func main() { @@ -21,6 +21,7 @@ func main() {
21 beego.BConfig.WebConfig.DirectoryIndex = true 21 beego.BConfig.WebConfig.DirectoryIndex = true
22 beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger" 22 beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
23 } 23 }
  24 +
24 log.Debug("应用启动") 25 log.Debug("应用启动")
25 beego.Run() 26 beego.Run()
26 } 27 }
@@ -8,6 +8,8 @@ import ( @@ -8,6 +8,8 @@ import (
8 "oppmg/protocol" 8 "oppmg/protocol"
9 "strings" 9 "strings"
10 10
  11 + "github.com/astaxie/beego/plugins/cors"
  12 +
11 "github.com/astaxie/beego/context" 13 "github.com/astaxie/beego/context"
12 ) 14 )
13 15
@@ -58,3 +60,20 @@ var AppSwitch = func(ctx *context.Context) { @@ -58,3 +60,20 @@ var AppSwitch = func(ctx *context.Context) {
58 60
59 return 61 return
60 } 62 }
  63 +
  64 +//AllowOption 允许跨域请求
  65 +var AllowOption = func(ctx *context.Context) {
  66 + if ctx.Request.Method != "OPTIONS" {
  67 + return
  68 + }
  69 + f := cors.Allow(&cors.Options{
  70 + AllowOrigins: []string{"*"}, //允许的请求来源
  71 + AllowMethods: []string{"POST", "GET", "OPTIONS", "PUT", "DELETE"}, //允许的请求类型
  72 + AllowHeaders: []string{"*"}, //允许的头部信息
  73 + ExposeHeaders: []string{"Content-Length"}, //允许暴露的头信息
  74 + AllowCredentials: false, //不允许共享AuthTuffic证书
  75 + })
  76 + f(ctx)
  77 + ctx.Output.Body([]byte("{}"))
  78 + return
  79 +}
@@ -5,6 +5,7 @@ import ( @@ -5,6 +5,7 @@ import (
5 "errors" 5 "errors"
6 "fmt" 6 "fmt"
7 "oppmg/common/log" 7 "oppmg/common/log"
  8 + "oppmg/protocol"
8 "time" 9 "time"
9 10
10 "github.com/astaxie/beego/orm" 11 "github.com/astaxie/beego/orm"
@@ -35,7 +36,7 @@ func (t *Department) GetManagesIds() []int64 { @@ -35,7 +36,7 @@ func (t *Department) GetManagesIds() []int64 {
35 var r []int64 36 var r []int64
36 err := json.Unmarshal([]byte(t.Manages), &r) 37 err := json.Unmarshal([]byte(t.Manages), &r)
37 if err != nil { 38 if err != nil {
38 - log.Error(err.Error()) 39 + log.Warn(err.Error())
39 } 40 }
40 return r 41 return r
41 } 42 }
@@ -58,6 +59,23 @@ func (t *Department) SetRelation(parent *Department) error { @@ -58,6 +59,23 @@ func (t *Department) SetRelation(parent *Department) error {
58 return nil 59 return nil
59 } 60 }
60 61
  62 +func (t *Department) GetManages() []protocol.DepartmentManager {
  63 + ids := t.GetManagesIds()
  64 + users, err := getUserNameByIds(ids)
  65 + if err != nil {
  66 + log.Error("GetUserNameByIds err :%s", err)
  67 + return nil
  68 + }
  69 + var managesdata []protocol.DepartmentManager
  70 + for _, v := range users {
  71 + m := protocol.DepartmentManager{
  72 + Id: v.Id, Name: v.NickName,
  73 + }
  74 + managesdata = append(managesdata, m)
  75 + }
  76 + return managesdata
  77 +}
  78 +
61 // AddDepartment insert a new Department into database and returns 79 // AddDepartment insert a new Department into database and returns
62 // last inserted Id on success. 80 // last inserted Id on success.
63 func AddDepartment(m *Department, om ...orm.Ormer) (id int64, err error) { 81 func AddDepartment(m *Department, om ...orm.Ormer) (id int64, err error) {
@@ -2,6 +2,7 @@ package models @@ -2,6 +2,7 @@ package models
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 + "strings"
5 "time" 6 "time"
6 7
7 "github.com/astaxie/beego/orm" 8 "github.com/astaxie/beego/orm"
@@ -19,6 +20,7 @@ type User struct { @@ -19,6 +20,7 @@ type User struct {
19 LastLoginTime time.Time `orm:"column(last_login_time);type(timestamp)" description:"最后一次登录时间"` 20 LastLoginTime time.Time `orm:"column(last_login_time);type(timestamp)" description:"最后一次登录时间"`
20 CreateAt time.Time `orm:"column(create_at);type(timestamp);auto_now_add" description:"创建时间"` 21 CreateAt time.Time `orm:"column(create_at);type(timestamp);auto_now_add" description:"创建时间"`
21 EnableStatus int8 `orm:"column(enable_status)" description:"是否有效"` 22 EnableStatus int8 `orm:"column(enable_status)" description:"是否有效"`
  23 + DeleteAt time.Time `orm:"column(delete_time);type(timestamp)"`
22 } 24 }
23 25
24 func (t *User) TableName() string { 26 func (t *User) TableName() string {
@@ -92,3 +94,26 @@ func GetUserByPhone(phone string) (v *User, err error) { @@ -92,3 +94,26 @@ func GetUserByPhone(phone string) (v *User, err error) {
92 } 94 }
93 return nil, err 95 return nil, err
94 } 96 }
  97 +
  98 +func getUserNameByIds(ids []int64) ([]User, error) {
  99 + var (
  100 + users []User
  101 + err error
  102 + )
  103 + if len(ids) == 0 {
  104 + return users, nil
  105 + }
  106 + const (
  107 + dataSql string = `SELECT id,nick_name FROM user WHERE id IN (?) AND delete_at =0`
  108 + )
  109 + var s []string
  110 + for _, v := range ids {
  111 + s = append(s, fmt.Sprint(v))
  112 + }
  113 + o := orm.NewOrm()
  114 + _, err = o.Raw(dataSql, strings.Join(s, ",")).QueryRows(&users)
  115 + if err != nil {
  116 + return nil, err
  117 + }
  118 + return users, nil
  119 +}
@@ -8,19 +8,8 @@ type RequestDepartmentAdd struct { @@ -8,19 +8,8 @@ type RequestDepartmentAdd struct {
8 Managers []int64 `json:"manages"` //主管userid 8 Managers []int64 `json:"manages"` //主管userid
9 } 9 }
10 10
11 -//ResponseDepartmentInfo ...  
12 -type ResponseDepartmentInfo struct {  
13 - ID int64 `json:"id`  
14 - CompanyID int64 `json:"company_id"` //公司  
15 - Name string `json:"name"` //部门名字  
16 - ParantID int64 `json:"parant_id"` //父级部门Id  
17 - Manages []DepartmentManager `json:"manages"` //部门管理员  
18 - Member int `json:"member"` //成员数  
19 - ParantName string `json:"parant_name"` //父级部门名字  
20 -}  
21 -  
22 type DepartmentManager struct { 11 type DepartmentManager struct {
23 - UserId int `json:"user_id"` 12 + Id int64 `json:"id"`
24 Name string `json:"name` 13 Name string `json:"name`
25 } 14 }
26 15
@@ -36,6 +25,16 @@ type RequestDepartmentDelete struct { @@ -36,6 +25,16 @@ type RequestDepartmentDelete struct {
36 CompanyID int64 `json:"company_id"` //公司 25 CompanyID int64 `json:"company_id"` //公司
37 } 26 }
38 27
  28 +//ResponseDepartmentInfo ...
  29 +type ResponseDepartmentInfo struct {
  30 + ID int64 `json:"id"`
  31 + CompanyID int64 `json:"company_id"` //公司
  32 + Name string `json:"name"` //部门名字
  33 + ParantID int64 `json:"parant_id"` //父级部门Id
  34 + Manages []DepartmentManager `json:"manages"` //部门管理员
  35 + Member int `json:"member"` //成员数
  36 +}
  37 +
39 //ResponseDepartmentList .... 38 //ResponseDepartmentList ....
40 type ResponseDepartmentList struct { 39 type ResponseDepartmentList struct {
41 List []ResponseDepartmentInfo 40 List []ResponseDepartmentInfo
@@ -81,10 +80,4 @@ type RequestUserAdd struct { @@ -81,10 +80,4 @@ type RequestUserAdd struct {
81 type RequestUserEdit struct { 80 type RequestUserEdit struct {
82 ID int64 `json:"id"` 81 ID int64 `json:"id"`
83 RequestUserAdd 82 RequestUserAdd
84 - // Name string `json:"name"`  
85 - // CompanyId int64 `json:"company_id"`  
86 - // Phone string `json:"phone`  
87 - // Departments []int `json:"departments"`  
88 - // Positions []int `json:"positions"`  
89 - // Roles []int `json:"roles"`  
90 } 83 }
@@ -2,7 +2,7 @@ package protocol @@ -2,7 +2,7 @@ package protocol
2 2
3 var errmessge ErrorMap = map[string]string{ 3 var errmessge ErrorMap = map[string]string{
4 "00000": "成功", 4 "00000": "成功",
5 - "1": "系统异常", 5 + "1": "无效请求",
6 "101": "clientId或clientSecret无效", 6 "101": "clientId或clientSecret无效",
7 "113": "签名验证失败", 7 "113": "签名验证失败",
8 //角色相关 8 //角色相关
@@ -10,13 +10,18 @@ import ( @@ -10,13 +10,18 @@ import (
10 10
11 func init() { 11 func init() {
12 nsV1 := beego.NewNamespace("v1", 12 nsV1 := beego.NewNamespace("v1",
  13 + beego.NSBefore(middleware.AllowOption),
13 beego.NSBefore(middleware.LogRequestData, middleware.AuthToken), 14 beego.NSBefore(middleware.LogRequestData, middleware.AuthToken),
14 beego.NSNamespace("/company", 15 beego.NSNamespace("/company",
  16 + beego.NSRouter("/:companyid/department", &controllers.CompanyController{}, "get:DepartmentList"),
  17 + beego.NSRouter("/department/:id", &controllers.CompanyController{}, "get:DepartmentOne"),
15 beego.NSRouter("/department", &controllers.CompanyController{}, "post:DepartmentAdd"), 18 beego.NSRouter("/department", &controllers.CompanyController{}, "post:DepartmentAdd"),
16 beego.NSRouter("/department", &controllers.CompanyController{}, "put:DepartmentUpdate"), 19 beego.NSRouter("/department", &controllers.CompanyController{}, "put:DepartmentUpdate"),
17 beego.NSRouter("/department", &controllers.CompanyController{}, "delete:DepartmentDelete"), 20 beego.NSRouter("/department", &controllers.CompanyController{}, "delete:DepartmentDelete"),
  21 + beego.NSRouter("/:companyid/position", &controllers.CompanyController{}, "get:PositionList"),
18 beego.NSRouter("/position", &controllers.CompanyController{}, "post:PositionAdd"), 22 beego.NSRouter("/position", &controllers.CompanyController{}, "post:PositionAdd"),
19 beego.NSRouter("/position", &controllers.CompanyController{}, "put:PositionEdit"), 23 beego.NSRouter("/position", &controllers.CompanyController{}, "put:PositionEdit"),
  24 + beego.NSRouter("/position", &controllers.CompanyController{}, "delete:PositionDelete"),
20 ), 25 ),
21 beego.NSNamespace("/rbac", 26 beego.NSNamespace("/rbac",
22 beego.NSRouter("/role", &controllers.RbacController{}, "post:RoleAdd"), 27 beego.NSRouter("/role", &controllers.RbacController{}, "post:RoleAdd"),
@@ -25,6 +30,7 @@ func init() { @@ -25,6 +30,7 @@ func init() {
25 beego.NSRouter("/role_group", &controllers.RbacController{}, "post:RoleGroupAdd"), 30 beego.NSRouter("/role_group", &controllers.RbacController{}, "post:RoleGroupAdd"),
26 beego.NSRouter("/role_group", &controllers.RbacController{}, "put:RoleGroupUpdate"), 31 beego.NSRouter("/role_group", &controllers.RbacController{}, "put:RoleGroupUpdate"),
27 beego.NSRouter("/role_group", &controllers.RbacController{}, "delete:RoleGroupDelete"), 32 beego.NSRouter("/role_group", &controllers.RbacController{}, "delete:RoleGroupDelete"),
  33 + beego.NSRouter("/:companyid/role", &controllers.RbacController{}, "get:RoleList"),
28 ), 34 ),
29 ) 35 )
30 36
@@ -326,8 +326,34 @@ func DepartmentDelete(param protocol.RequestDepartmentDelete) error { @@ -326,8 +326,34 @@ func DepartmentDelete(param protocol.RequestDepartmentDelete) error {
326 return nil 326 return nil
327 } 327 }
328 328
329 -func DepartmentListAll(companyId int64) (protocol.ResponseDepartmentList, error) { 329 +//DepartmentListAll 获取公司的部门
  330 +func DepartmentListAll(companyId int64) ([]protocol.ResponseDepartmentInfo, error) {
  331 + var (
  332 + departs []protocol.ResponseDepartmentInfo
  333 + departmodels []*models.Department
  334 + err error
  335 + )
  336 + const (
  337 + datasql0 string = `SELECT id, company_id,name,parent_id,member,managers ` +
  338 + ` FROM department WHERE company_id = ? AND delete_at = 0`
  339 + )
  340 + err = utils.ExecuteQueryAll(&departmodels, datasql0, companyId)
  341 + if err != nil {
  342 + e := fmt.Errorf("EXECUTE SQL err:%s", err)
  343 + log.Error(e.Error())
  344 + return nil, protocol.NewErrWithMessage("1", err)
  345 + }
  346 + for _, v := range departmodels {
  347 + depart := protocol.ResponseDepartmentInfo{
  348 + ID: v.Id, CompanyID: v.CompanyId,
  349 + Name: v.Name, Member: v.Member,
  350 + ParantID: v.ParentId,
  351 + }
  352 + var manage []protocol.DepartmentManager
  353 + manage = v.GetManages()
  354 + depart.Manages = manage
  355 + departs = append(departs, depart)
  356 + }
330 357
331 - r := protocol.ResponseDepartmentList{}  
332 - return r, nil 358 + return departs, nil
333 } 359 }