Skip to content

Commit

Permalink
Merge pull request #23 from dchaofei/master
Browse files Browse the repository at this point in the history
新增角色验证名称不能重复
  • Loading branch information
dreamans authored Jan 17, 2019
2 parents e98da76 + 4e41638 commit 15b36f5
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 2 deletions.
1 change: 1 addition & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
API_USER_MY_UPDATE = "POST /api/user/my/update"
API_USER_MY_PASSWORD = "POST /api/user/my/password"

API_USER_GROUP_EXISTS = "GET /api/user/group/checkexists"
API_USER_GROUP_NEW = "POST /api/user/group/new"
API_USER_GROUP_UPDATE = "POST /api/user/group/update"
API_USER_GROUP_PRIV = "GET /api/user/group/priv"
Expand Down
6 changes: 6 additions & 0 deletions model/user_group/user_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,9 @@ func Delete(id int) bool {
ok := model.DeleteByPk(TableName, UserGroup{ID: id})
return ok
}

func GetOne(query model.QueryParam) (UserGroup, bool) {
var data UserGroup
ok := model.GetOne(TableName, &data, query)
return data, ok
}
22 changes: 22 additions & 0 deletions module/user/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ func groupUpdate(c *goweb.Context, id int) error {
Name: name,
Priv: gostring.StrSlice2IntSlice(gostring.StrFilterSliceEmpty(priv)),
}
exists, err := userGroup.CheckGroupExists()
if err != nil {
return syncd.RenderAppError(err.Error())
}
if exists {
return syncd.RenderCustomerError(syncd.CODE_ERR_DATA_REPEAT, "group exists")
}
if err := userGroup.CreateOrUpdate(); err != nil {
return syncd.RenderAppError(err.Error())
}
Expand Down Expand Up @@ -77,3 +84,18 @@ func GroupDelete(c *goweb.Context) error {
}
return syncd.RenderJson(c, nil)
}

func GroupExists(c *goweb.Context) error {
keyword, id := c.Query("keyword"), c.QueryInt("id")
group := userService.Group{
ID: id,
Name: keyword,
}
exists, err := group.CheckGroupExists()
if err != nil {
return syncd.RenderAppError(err.Error())
}
return syncd.RenderJson(c, goweb.JSON{
"exists": exists,
})
}
1 change: 1 addition & 0 deletions route/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func handler() {
syncd.API_USER_MY_UPDATE: userModule.MyUpdate,
syncd.API_USER_MY_PASSWORD: userModule.MyPasswordUpdate,

syncd.API_USER_GROUP_EXISTS: userModule.GroupExists,
syncd.API_USER_GROUP_NEW: userModule.GroupNew,
syncd.API_USER_GROUP_UPDATE: userModule.GroupEdit,
syncd.API_USER_GROUP_PRIV: userModule.GroupPlainPriv,
Expand Down
24 changes: 24 additions & 0 deletions service/user/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,27 @@ func (g *Group) GetNameByIds(ids []int) (map[int]string, error){
}
return groupNameList, nil
}

func (g *Group) CheckGroupExists() (bool, error){
var where []baseModel.WhereParam
if g.Name != "" {
where = append(where, baseModel.WhereParam{
Field: "name",
Prepare: g.Name,
})
}
if g.ID > 0 {
where = append(where, baseModel.WhereParam{
Field: "id",
Tag: "!=",
Prepare: g.ID,
})
}
detail, ok := userGroupModel.GetOne(baseModel.QueryParam{
Where: where,
})
if !ok {
return false, errors.New("get group one data failed")
}
return detail.ID > 0, nil
}
2 changes: 2 additions & 0 deletions service/user/priv.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ var privToApiMap = map[int][]string{
USER_ROLE_NEW: []string{
syncd.API_USER_GROUP_NEW,
syncd.API_USER_GROUP_PRIV,
syncd.API_USER_GROUP_EXISTS,
},
USER_ROLE_EDIT: []string{
syncd.API_USER_GROUP_PRIV,
syncd.API_USER_GROUP_DETAIL,
syncd.API_USER_GROUP_UPDATE,
syncd.API_USER_GROUP_EXISTS,
},
USER_ROLE_DEL: []string{
syncd.API_USER_GROUP_DELETE,
Expand Down
4 changes: 4 additions & 0 deletions web/src/api/user.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {get, post} from './fetch.js'

export function checkGroupExistsApi(data) {
return get('/user/group/checkexists', data)
}

export function newGroupApi(data) {
return post('/user/group/new', data)
}
Expand Down
2 changes: 1 addition & 1 deletion web/src/views/user/Group.vue
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export default {
this.dialogDetail = {}
},
handleOpenEditDialog(id) {
this.dialogTitle = '编辑服务器信息'
this.dialogTitle = '编辑角色信息'
this.dialogVisible = true
this.dialogDetail = {}
this.getDataDetail(id)
Expand Down
30 changes: 29 additions & 1 deletion web/src/views/user/GroupUpdateComponent.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Form } from 'ant-design-vue'
import { getPrivListApi } from '@/api/user.js'
import { getPrivListApi, checkGroupExistsApi } from '@/api/user.js'
const GroupUpdate = {
render() {
const { getFieldDecorator, getFieldValue } = this.form
Expand Down Expand Up @@ -30,16 +30,43 @@ const GroupUpdate = {
)
})

let vm = this
let groupExistsCb = function(type, errmsg) {
return function(rule, value, callback) {
if (!value) {
callback()
return
}
let title = '角色名称'
vm.$set(vm.helps, type, `正在验证 ${value} 是否被占用...`)
checkGroupExistsApi({id: vm.detail.id, keyword: value}).then(res => {
if (!res.exists) {
vm.$set(vm.helps, type, undefined)
callback()
} else {
vm.$set(vm.helps, type, `抱歉!${title}已被占用,请重新输入`)
callback(errmsg)
}
}).catch(err => {
vm.$set(vm.helps, type, '网络错误, 校验失败')
callback('网络错误, 校验失败')
})
}
}

return (
<a-form>
<a-form-item
{...{ props: formItemLayout }}
help={this.helps.name}
label='角色名称'>
{getFieldDecorator('name', {
rules: [
{ required: true, message: '角色名称不能为空' },
{ validator: groupExistsCb('name', '角色名称已经存在')},
],
initialValue: this.detail.name,
validateTrigger: 'blur',
})(
<a-input autocomplete="off" placeholder='请输入角色名称' />
)}
Expand Down Expand Up @@ -84,6 +111,7 @@ const GroupUpdate = {
indeterminate: false,
checkedPrivList: [],
plainPrivCheckList: [],
helps: {},
}
},
methods: {
Expand Down

0 comments on commit 15b36f5

Please sign in to comment.