user-add-or-update.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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="userName">
  8. <el-input v-model="dataForm.userName" :disabled="dataForm.id !== 0" placeholder="登录帐号"></el-input>
  9. </el-form-item>
  10. <el-form-item label="姓名" prop="name">
  11. <el-input v-model="dataForm.name" :disabled="dataForm.id !== 0" placeholder="姓名"></el-input>
  12. </el-form-item>
  13. <el-form-item label="密码" prop="password" :class="{ 'is-required': !dataForm.id }">
  14. <el-input v-model="dataForm.password" type="password" placeholder="密码"></el-input>
  15. </el-form-item>
  16. <el-form-item label="确认密码" prop="confirmPassword" :class="{ 'is-required': !dataForm.id }">
  17. <el-input v-model="dataForm.confirmPassword" type="password" placeholder="确认密码"></el-input>
  18. </el-form-item>
  19. <el-form-item label="所属机构" prop="orgId">
  20. <org-component v-model="dataForm.orgId" @orgSelected="validateField('orgId')"/>
  21. </el-form-item>
  22. <el-form-item label="邮箱" prop="email">
  23. <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
  24. </el-form-item>
  25. <el-form-item label="手机号" prop="mobile">
  26. <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
  27. </el-form-item>
  28. <el-form-item label="角色" size="mini" prop="roleIdList">
  29. <el-checkbox-group v-model="dataForm.roleIdList">
  30. <el-checkbox v-for="role in roleList" :key="role.roleId" :label="role.roleId">{{ role.roleName }}</el-checkbox>
  31. </el-checkbox-group>
  32. </el-form-item>
  33. <el-form-item label="状态" size="mini" prop="status">
  34. <el-radio-group v-model="dataForm.status">
  35. <el-radio :label="0">禁用</el-radio>
  36. <el-radio :label="1">正常</el-radio>
  37. </el-radio-group>
  38. </el-form-item>
  39. </el-form>
  40. <span slot="footer" class="dialog-footer">
  41. <el-button @click="visible = false">取消</el-button>
  42. <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
  43. </span>
  44. </el-dialog>
  45. </template>
  46. <script>
  47. import { isEmail, isMobile } from '@/utils/validate'
  48. import OrgComponent from '../common/org-component'
  49. export default {
  50. components: {OrgComponent},
  51. data () {
  52. let validatePassword = (rule, value, callback) => {
  53. if (!this.dataForm.id && !/\S/.test(value)) {
  54. callback(new Error('密码不能为空'))
  55. } else {
  56. callback()
  57. }
  58. }
  59. let validateConfirmPassword = (rule, value, callback) => {
  60. if (!this.dataForm.id && !/\S/.test(value)) {
  61. callback(new Error('确认密码不能为空'))
  62. } else if (this.dataForm.password !== value) {
  63. callback(new Error('确认密码与密码输入不一致'))
  64. } else {
  65. callback()
  66. }
  67. }
  68. let validateEmail = (rule, value, callback) => {
  69. if (value !== '' && !isEmail(value)) {
  70. callback(new Error('邮箱格式错误'))
  71. } else {
  72. callback()
  73. }
  74. }
  75. let validateMobile = (rule, value, callback) => {
  76. if (!isMobile(value)) {
  77. callback(new Error('手机号格式错误'))
  78. } else {
  79. callback()
  80. }
  81. }
  82. return {
  83. visible: false,
  84. roleList: [],
  85. orgList: [],
  86. dataForm: {
  87. id: 0,
  88. userName: '',
  89. name: '',
  90. password: '',
  91. confirmPassword: '',
  92. salt: '',
  93. email: '',
  94. mobile: '',
  95. roleIdList: [],
  96. status: 1,
  97. orgId: ''
  98. },
  99. dataRule: {
  100. userName: [
  101. { required: true, message: '用户名不能为空', trigger: 'blur' }
  102. ],
  103. password: [
  104. { validator: validatePassword, trigger: 'blur' }
  105. ],
  106. confirmPassword: [
  107. { validator: validateConfirmPassword, trigger: 'blur' }
  108. ],
  109. email: [
  110. { validator: validateEmail, trigger: 'blur' }
  111. ],
  112. name: [
  113. { required: true, message: '手机号不能为空', trigger: 'blur' }
  114. ],
  115. mobile: [
  116. { required: true, message: '手机号不能为空', trigger: 'blur' },
  117. { validator: validateMobile, trigger: 'blur' }
  118. ],
  119. orgId: [
  120. { required: true, message: '所属机构不能为空', trigger: 'blur' }
  121. ]
  122. }
  123. }
  124. },
  125. methods: {
  126. init (id) {
  127. this.dataForm.id = id || 0
  128. this.$http({
  129. url: this.$http.adornUrl('/user-service/role/listAll'),
  130. method: 'get',
  131. params: this.$http.adornParams()
  132. }).then(({data}) => {
  133. if (data.code === '200') {
  134. this.roleList = data && data.code === '200' ? data.data : []
  135. } else {
  136. this.$message.error(data.msg)
  137. }
  138. }).then(() => {
  139. this.visible = true
  140. this.$nextTick(() => {
  141. this.$refs['dataForm'].resetFields()
  142. })
  143. }).then(() => {
  144. if (this.dataForm.id) {
  145. this.$http({
  146. url: this.$http.adornUrl(`/user-service/user/info/${this.dataForm.id}`),
  147. method: 'get',
  148. params: this.$http.adornParams()
  149. }).then(({data}) => {
  150. if (data && data.code === '200') {
  151. this.dataForm.userName = data.data.username
  152. this.dataForm.salt = data.data.salt
  153. this.dataForm.email = data.data.email
  154. this.dataForm.mobile = data.data.mobile
  155. this.dataForm.roleIdList = data.data.roleIdList
  156. this.dataForm.status = Number(data.data.status)
  157. this.dataForm.name = data.data.name
  158. this.dataForm.orgId = data.data.orgId
  159. }
  160. })
  161. }
  162. })
  163. },
  164. // 表单提交
  165. dataFormSubmit () {
  166. this.$refs['dataForm'].validate((valid) => {
  167. if (valid) {
  168. this.$http({
  169. url: this.$http.adornUrl(`/user-service/user/${!this.dataForm.id ? 'save' : 'update'}`),
  170. method: !this.dataForm.id ? 'post' : 'put',
  171. data: this.$http.adornData(!this.dataForm.id ? {
  172. 'userId': undefined,
  173. 'username': this.dataForm.userName,
  174. 'password': this.dataForm.password,
  175. // 'salt': this.dataForm.salt,
  176. 'email': this.dataForm.email,
  177. 'mobile': this.dataForm.mobile,
  178. 'status': this.dataForm.status,
  179. 'roleIdList': this.dataForm.roleIdList,
  180. 'name': this.dataForm.name,
  181. 'orgId': this.dataForm.orgId
  182. } : {
  183. 'userId': this.dataForm.id,
  184. 'password': this.dataForm.password,
  185. 'email': this.dataForm.email,
  186. 'mobile': this.dataForm.mobile,
  187. 'status': this.dataForm.status,
  188. 'roleIdList': this.dataForm.roleIdList,
  189. 'name': this.dataForm.name,
  190. 'orgId': this.dataForm.orgId
  191. })
  192. }).then(({data}) => {
  193. if (data && data.code === '200') {
  194. this.$message({
  195. message: '操作成功',
  196. type: 'success',
  197. duration: 1500,
  198. onClose: () => {
  199. this.visible = false
  200. this.$emit('refreshDataList')
  201. }
  202. })
  203. } else {
  204. this.$message.error(data.msg)
  205. }
  206. })
  207. }
  208. })
  209. },
  210. validateField (type) {
  211. this.$refs.dataForm.validateField(type)
  212. }
  213. }
  214. }
  215. </script>