UserListDetail.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <template>
  2. <div class="height-100">
  3. <el-card class="card-detail-title margin-bottom-20">
  4. <span v-if="pageType==='detail'">用户查看</span>
  5. <span v-if="pageType==='edit'">用户编辑</span>
  6. <span v-if="pageType==='add'">用户新增</span>
  7. </el-card>
  8. <el-card class="card-detail-main-title">
  9. <div slot="header">
  10. <span class="list-title">用户信息</span>
  11. </div>
  12. <el-form ref="form" class="form-detail my-form" label-width="20%" :model="form" size="medium" :rules="rule" :hide-required-asterisk="pageType==='detail'">
  13. <el-row>
  14. <el-col :span="12" :offset="6">
  15. <el-form-item label="账号" prop="userName">
  16. <el-input v-model="form.userName" :disabled="pageType!=='add'"/>
  17. </el-form-item>
  18. </el-col>
  19. </el-row>
  20. <!-- <el-row>
  21. <el-col :span="12" :offset="6">
  22. <el-form-item label="工号" prop="jobNum">
  23. <el-input v-model="form.jobNum" :disabled="pageType==='detail'"/>
  24. </el-form-item>
  25. </el-col>
  26. </el-row> -->
  27. <el-row>
  28. <el-col :span="12" :offset="6">
  29. <el-form-item label="姓名" prop="name">
  30. <el-input v-model="form.name" :disabled="pageType==='detail'"/>
  31. </el-form-item>
  32. </el-col>
  33. </el-row>
  34. <el-row>
  35. <el-col :span="12" :offset="6">
  36. <el-form-item label="组织机构" prop="unitId">
  37. <el-select v-model="form.unitId" placeholder="请选择" filterable :disabled="pageType==='detail'">
  38. <el-option v-for="item in departmentArr" :key="item.id" :label="item.name" :value="item.id"/>
  39. </el-select>
  40. </el-form-item>
  41. </el-col>
  42. </el-row>
  43. <el-row>
  44. <el-col :span="12" :offset="6">
  45. <el-form-item label="联系电话">
  46. <el-input v-model="form.contact" :disabled="pageType==='detail'"/>
  47. </el-form-item>
  48. </el-col>
  49. </el-row>
  50. <el-row>
  51. <el-col :span="12" :offset="6">
  52. <el-form-item label="用户角色">
  53. <el-select v-model="form.roleArr" multiple placeholder="无" :disabled="pageType==='detail'">
  54. <el-option v-for="item in roleArr" :key="item.roleId" :label="item.roleName" :value="item.roleId"/>
  55. </el-select>
  56. </el-form-item>
  57. </el-col>
  58. </el-row>
  59. </el-form>
  60. </el-card>
  61. <el-card class="card-button-bottom">
  62. <el-button v-if="pageType!=='detail'" type="primary" size="medium" @click="save">保存</el-button>
  63. <el-button size="medium" @click="close">返回</el-button>
  64. </el-card>
  65. </div>
  66. </template>
  67. <script>
  68. import { GetUserInfoById, UpdateUserRole, GetDepartmentTree, AddUser } from '../../utils/service/user'
  69. import { GetRoleList } from '../../utils/service/role'
  70. export default {
  71. name: "UserListDetail",
  72. data() {
  73. return {
  74. form: {
  75. userName: '',
  76. name: '',
  77. unitId: '',
  78. contact: '',
  79. roleArr: [],
  80. },
  81. roleArr: [],
  82. treeData: [],
  83. rule: {
  84. userName: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  85. name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  86. unitId: [{ required: true, message: '请选择组织机构', trigger: 'blur' }],
  87. }
  88. }
  89. },
  90. computed: {
  91. pageType() {
  92. let flag = ''
  93. switch (this.$route.name) {
  94. case 'UserListDetail':
  95. flag = 'detail'
  96. break
  97. case 'UserListEdit':
  98. flag = 'edit'
  99. break
  100. case 'UserListAdd':
  101. flag = 'add'
  102. break
  103. }
  104. return flag
  105. },
  106. departmentArr() {
  107. let arr = []
  108. this.treeData.forEach(level1 => {
  109. arr.push({id: level1.id, name: level1.departmentname})
  110. if(level1.children && level1.children.length > 0) {
  111. level1.children.forEach(level2 => {
  112. arr.push({id: level2.id, name: level1.departmentname+'-'+level2.departmentname})
  113. if(level2.children && level2.children.length > 0) {
  114. level2.children.forEach(level3 => {
  115. arr.push({id: level3.id, name: level1.departmentname+'-'+level2.departmentname+'-'+level3.departmentname})
  116. })
  117. }
  118. })
  119. }
  120. })
  121. return arr
  122. }
  123. },
  124. methods: {
  125. save() {
  126. this.$refs['form'].validate((valid => {
  127. if(valid) {
  128. if(this.pageType==='add') {
  129. AddUser(this.form).then(res => {
  130. if(res.code == 200) {
  131. this.$message({type: 'success', message: '用户添加成功'});
  132. this.$router.push({
  133. name: "UserList"
  134. })
  135. } else {
  136. this.$message({type: 'error', message: '用户添加失败, '+res.msg});
  137. }
  138. })
  139. } else if(this.pageType==='edit') {
  140. UpdateUserRole({userId: this.$route.params.id, ...this.form}).then(res => {
  141. if (res.code == 200) {
  142. this.$message({type: 'success', message: '用户修改成功'});
  143. this.$router.push({
  144. name: "UserList"
  145. })
  146. } else {
  147. this.$message({type: 'error', message: '用户修改失败'});
  148. }
  149. })
  150. }
  151. }
  152. }))
  153. },
  154. close() {
  155. this.$router.push({
  156. name: "UserList"
  157. })
  158. },
  159. getRoleList() {
  160. GetRoleList(1000, 1).then(res => {
  161. if(res.code == 200) {
  162. this.roleArr = res.data.Rows
  163. }
  164. })
  165. },
  166. getUserInfo() {
  167. if(this.pageType==='add') { return }
  168. GetUserInfoById(this.$route.params.id).then(res => {
  169. if(res.code == 200) {
  170. let formTemp = JSON.parse(JSON.stringify(res.data))
  171. formTemp.role = []
  172. if(res.data.role[0]) {
  173. res.data.role.forEach(i => {
  174. formTemp.role.push(i.roleId)
  175. })
  176. }
  177. this.form = {
  178. userName: formTemp.userName,
  179. name: formTemp.lastName,
  180. unitId: formTemp.unitId,
  181. contact: formTemp.mobile,
  182. roleArr: formTemp.role,
  183. }
  184. }
  185. })
  186. },
  187. getTreeData() {
  188. GetDepartmentTree().then(res => {
  189. if(res.code == 200) {
  190. this.treeData = res.data.data
  191. }
  192. })
  193. },
  194. },
  195. created() {
  196. this.getRoleList()
  197. this.getUserInfo()
  198. this.getTreeData()
  199. }
  200. }
  201. </script>
  202. <style scoped lang="scss">
  203. .my-form {
  204. height: 85%;
  205. display: flex;
  206. flex-direction: column;
  207. justify-content: space-around;
  208. ::v-deep .el-form-item{
  209. margin-bottom: 0;
  210. .el-form-item__label {
  211. text-align: right;
  212. }
  213. }
  214. }
  215. </style>