role-add-or-update.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <el-dialog
  3. :title="!dataForm.id ? '新增' : '修改'"
  4. :close-on-click-modal="false"
  5. :visible.sync="visible">
  6. <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
  7. <el-form-item label="角色名称" prop="roleName">
  8. <el-input v-model="dataForm.roleName" placeholder="角色名称"></el-input>
  9. </el-form-item>
  10. <el-form-item label="备注" prop="remark">
  11. <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
  12. </el-form-item>
  13. <el-form-item label="所属机构" prop="orgId">
  14. <org-component v-model="dataForm.orgId" @orgSelected="validateField('orgId')"/>
  15. </el-form-item>
  16. <div class="my-item">
  17. <el-form-item size="mini" label="授权">
  18. <el-tree
  19. :data="menuList"
  20. :props="menuListTreeProps"
  21. node-key="menuId"
  22. ref="menuListTree"
  23. :default-expand-all="true"
  24. show-checkbox>
  25. </el-tree>
  26. </el-form-item>
  27. </div>
  28. <div class="my-item">
  29. <el-form-item size="mini" label="数据权限">
  30. <el-tree
  31. :data="orgList"
  32. :props="orgListTreeProps"
  33. node-key="orgId"
  34. ref="orgListData"
  35. :default-expand-all="true"
  36. :highlight-current="true"
  37. show-checkbox>
  38. </el-tree>
  39. </el-form-item>
  40. </div>
  41. </el-form>
  42. <span slot="footer" class="dialog-footer">
  43. <el-button @click="visible = false">取消</el-button>
  44. <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
  45. </span>
  46. </el-dialog>
  47. </template>
  48. <script>
  49. import OrgComponent from '../common/org-component'
  50. export default {
  51. components: {OrgComponent},
  52. data () {
  53. return {
  54. visible: false,
  55. menuList: [],
  56. orgList: [],
  57. menuListTreeProps: {
  58. label: 'name',
  59. children: 'list'
  60. },
  61. orgListTreeProps: {
  62. label: 'name',
  63. children: 'children'
  64. },
  65. dataForm: {
  66. id: 0,
  67. roleName: '',
  68. remark: '',
  69. isCommon: false,
  70. orgId: '',
  71. orgName: ''
  72. },
  73. dataRule: {
  74. roleName: [
  75. { required: true, message: '角色名称不能为空', trigger: 'blur' }
  76. ],
  77. orgId: [
  78. { required: true, message: '所属机构不能为空', trigger: 'change' }
  79. ]
  80. },
  81. tempKey: -666666 // 临时key, 用于解决tree半选中状态项不能传给后台接口问题. # 待优化
  82. }
  83. },
  84. methods: {
  85. init (id) {
  86. this.dataForm.id = id || 0
  87. this.$http({
  88. url: this.$http.adornUrl('/user-service/menu/tree'),
  89. method: 'get',
  90. params: this.$http.adornParams()
  91. }).then(({data}) => {
  92. if (data.code === '200') {
  93. this.menuList = data.data
  94. }
  95. }).then(() => {
  96. this.$http({
  97. url: this.$http.adornUrl('/user-service/org/queryTree'),
  98. method: 'get',
  99. params: this.$http.adornParams()
  100. }).then(({data}) => {
  101. if (data.code === '200') {
  102. this.orgList = data.data
  103. }
  104. })
  105. }).then(() => {
  106. this.visible = true
  107. this.$nextTick(() => {
  108. this.$refs['dataForm'].resetFields()
  109. this.$refs.menuListTree.setCheckedKeys([])
  110. this.$refs.orgListData.setCheckedKeys([])
  111. })
  112. }).then(() => {
  113. if (this.dataForm.id) {
  114. this.$http({
  115. url: this.$http.adornUrl(`/user-service/role/info/${this.dataForm.id}`),
  116. method: 'get',
  117. params: this.$http.adornParams()
  118. }).then(({data}) => {
  119. if (data && data.code === '200') {
  120. this.dataForm.roleName = data.data.roleName
  121. this.dataForm.remark = data.data.remark
  122. this.dataForm.orgId = data.data.orgId
  123. if (data.data.menuIdList) {
  124. data.data.menuIdList.forEach(i => {
  125. const node = this.$refs.menuListTree.getNode(i)
  126. if (node.isLeaf) {
  127. this.$refs.menuListTree.setChecked(node, true)
  128. }
  129. })
  130. }
  131. if (data.data.orgIdList) {
  132. // console.log('orgIdList = ' + data.data.orgIdList)
  133. data.data.orgIdList.forEach(i => {
  134. const node = this.$refs.orgListData.getNode(i)
  135. // console.log('i = ' + i)
  136. // console.log('node.isLeaf = ' + node.isLeaf)
  137. if (node.isLeaf) {
  138. this.$refs.orgListData.setChecked(node, true, false)
  139. }
  140. })
  141. }
  142. }
  143. })
  144. }
  145. })
  146. },
  147. // 表单提交
  148. dataFormSubmit () {
  149. this.$refs['dataForm'].validate((valid) => {
  150. if (valid) {
  151. this.$http({
  152. url: this.$http.adornUrl(`/user-service/role/${!this.dataForm.id ? 'save' : 'update'}`),
  153. method: this.dataForm.id ? 'put' : 'post',
  154. data: this.$http.adornData({
  155. 'roleId': this.dataForm.id || undefined,
  156. 'roleName': this.dataForm.roleName,
  157. 'remark': this.dataForm.remark,
  158. 'isCommon': this.dataForm.isCommon,
  159. 'orgId': this.dataForm.orgId,
  160. 'menuIdList': [].concat(this.$refs.menuListTree.getCheckedKeys(), this.$refs.menuListTree.getHalfCheckedKeys()),
  161. 'orgIdList': [].concat(this.$refs.orgListData.getCheckedKeys(), this.$refs.orgListData.getHalfCheckedKeys())
  162. })
  163. }).then(({data}) => {
  164. if (data && data.code === '200') {
  165. this.$message({
  166. message: '操作成功',
  167. type: 'success',
  168. duration: 1500,
  169. onClose: () => {
  170. this.visible = false
  171. this.$emit('refreshDataList')
  172. }
  173. })
  174. } else {
  175. this.$message.error(data.msg || '系统错误,请联系管理员')
  176. }
  177. })
  178. }
  179. })
  180. },
  181. validateField (type) {
  182. this.$refs.dataForm.validateField(type)
  183. }
  184. }
  185. }
  186. </script>
  187. <style lang="scss" scoped>
  188. .my-item{
  189. width: 49%;
  190. overflow: auto;
  191. display: inline-block;
  192. vertical-align: top;
  193. }
  194. </style>