作者 唐旭辉

切换分支

... ... @@ -283,3 +283,26 @@ func (c *RbacController) RoleMenuEdit() {
msg = protocol.NewReturnResponse(nil, err)
return
}
//RoleMenuEdit 设置
//@router /role/menu/opportunity
func (c *RbacController) RoleMenuOpportunity() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
type Parameter struct {
RoleId int64 `json:"role_id"`
MenuIds []int64 `json:"menu_ids"`
}
var param Parameter
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
log.Error("json 解析失败 err:%s", err)
msg = protocol.BadRequestParam("1")
return
}
companyid := c.GetCompanyId()
err := serverbac.RoleMenuEdit(companyid, param.RoleId, param.MenuIds)
msg = protocol.NewReturnResponse(nil, err)
return
}
... ...
... ... @@ -49,3 +49,18 @@ func GetMenuByIds(ids []int64) (v []*Menu, err error) {
}
return v, nil
}
func GetMenuByCode(code string) (*Menu, error) {
var (
menuinfo = &Menu{}
err error
)
o := orm.NewOrm()
err = o.QueryTable(&Menu{}).
Filter("code", code).
One(menuinfo)
if err != nil {
return menuinfo, nil
}
return nil, err
}
... ...
... ... @@ -36,12 +36,34 @@ func GetRoleMenuByRole(roleid int64) ([]RoleMenu, error) {
}
func GetRoleMenuByCode(roleId int64, code string) (*RoleMenu, error) {
// data := []RoleMenu{}
// o := orm.NewOrm()
// _, err := o.QueryTable(&Role{}).
// Filter("role_id", roleId).
// Filter("code", code).
// All(&data)
return nil, nil
data := []*RoleMenu{}
o := orm.NewOrm()
_, err := o.QueryTable(&Role{}).
Filter("role_id", roleId).
Filter("code", code).
All(&data)
if err != nil {
return nil, err
}
if len(data) > 0 {
return data[0], nil
}
return &RoleMenu{}, nil
}
func AddRoleMenu(m *RoleMenu, om ...orm.Ormer) (id int64, err error) {
var o orm.Ormer
if len(om) == 0 {
o = om[0]
} else {
o = orm.NewOrm()
}
id, err = o.Insert(m)
return
}
func UpdateRoleMenuById(m *RoleMenu, col []string) error {
o := orm.NewOrm()
_, err := o.Update(m, col...)
return err
}
... ...
... ... @@ -160,7 +160,56 @@ func RoleMenuEdit(companyid int64, roleId int64, menuids []int64) error {
}
//机会管理高级设置
func UpdateSetOpportunity() error {
func UpdateSetOpportunity(param OptionOpportunity, roleid int64, companyid int64) error {
var (
//code = M_SYSTEM_OPPORTUNITY
rolemenu *models.RoleMenu
roleInfo *models.Role
err error
menuinfo *models.Menu
)
menuinfo, err = models.GetMenuByCode(M_SYSTEM_OPPORTUNITY)
if err != nil {
log.Error("获取菜单数据失败GetMenuByCode(%s):%s", M_SYSTEM_OPPORTUNITY, err)
return protocol.NewErrWithMessage("1")
}
roleInfo, err = models.GetRoleById(roleid)
if err != nil {
log.Error("获取角色数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
if roleInfo.CompanyId != companyid {
log.Error("角色公司不匹配")
return protocol.NewErrWithMessage("1")
}
rolemenu, err = models.GetRoleMenuByCode(roleInfo.Id, M_SYSTEM_OPPORTUNITY)
if err != nil && err != orm.ErrNoRows {
log.Debug("为获取到role_menu数据:%s", err)
return protocol.NewErrWithMessage("1")
}
setToString, _ := json.Marshal(param)
if err == orm.ErrNoRows {
//没有数据 新增
rolemenu = &models.RoleMenu{
RoleId: roleid,
Opption: string(setToString),
Code: M_SYSTEM_OPPORTUNITY,
MenuId: menuinfo.Id,
}
_, err = models.AddRoleMenu(rolemenu)
if err != nil {
log.Error("添加角色菜单失败:%s", err)
return protocol.NewErrWithMessage("1")
}
return nil
}
// 有数据更新
rolemenu.Opption = string(setToString)
err = models.UpdateRoleMenuById(rolemenu, []string{"Opption"})
if err != nil {
log.Error("更新角色菜单失败:%s", err)
return protocol.NewErrWithMessage("1")
}
return nil
}
... ... @@ -173,6 +222,7 @@ func GetSetOpportunity(roleid int64, companyid int64) (*OptionOpportunity, error
}
var (
//code = M_SYSTEM_OPPORTUNITY
rolemenu *models.RoleMenu
roleInfo *models.Role
err error
)
... ... @@ -185,5 +235,29 @@ func GetSetOpportunity(roleid int64, companyid int64) (*OptionOpportunity, error
log.Error("角色公司不匹配")
return setting, protocol.NewErrWithMessage("1")
}
rolemenu, err = models.GetRoleMenuByCode(roleInfo.Id, M_SYSTEM_OPPORTUNITY)
if err != nil {
log.Debug("为获取到role_menu数据:%s", err)
return setting, nil
}
err = json.Unmarshal([]byte(rolemenu.Opption), setting)
if err != nil {
log.Error("解析配置json失败:%s", err)
}
departInSetting := []CheckDeparment{}
for k, v := range setting.CheckOption.Departments {
d, err := models.GetDepartmentById(v.Id)
if err != nil {
log.Error("获取部门失败GetDepartmentById(%d):%s", v.Id, err)
continue
}
if d.DeleteAt.Unix() <= 0 {
//部门已被删除
continue
}
setting.CheckOption.Departments[k].Name = d.Name
departInSetting = append(departInSetting, setting.CheckOption.Departments[k])
}
setting.CheckOption.Departments = departInSetting
return setting, nil
}
... ...