userModal.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <template>
  2. <div>
  3. <a-spin :spinning="spinning" tip="Loading...">
  4. <a-modal
  5. centered
  6. class="userEdit-modal"
  7. :footer="null"
  8. :maskClosable="false"
  9. :title="titleText"
  10. v-model="isshow"
  11. @cancel="isshow=false"
  12. :width="800">
  13. <a-form-model
  14. id="userEdit-form"
  15. ref="ruleForm"
  16. :model="form"
  17. :rules="rules"
  18. :label-col="formItemLayout.labelCol"
  19. :wrapper-col="formItemLayout.wrapperCol"
  20. >
  21. <a-row :gutter="24">
  22. <a-col :span="12">
  23. <a-form-model-item label="用户名称" prop="name">
  24. <a-input
  25. id="userEdit-name"
  26. v-model.trim="form.name"
  27. @change="filterEmpty"
  28. :maxLength="30"
  29. allowClear
  30. placeholder="请输入用户名称(最多30个字符)"/>
  31. </a-form-model-item>
  32. </a-col>
  33. <a-col :span="12">
  34. <a-form-model-item label="手机号码" prop="phone">
  35. <a-input id="userEdit-phone" v-model.trim="form.phone":maxLength="11" allowClear placeholder="请输入手机号码"/>
  36. </a-form-model-item>
  37. </a-col>
  38. </a-row>
  39. <a-row :gutter="20">
  40. <a-col :span="12">
  41. <a-form-model-item label="用户账号" prop="loginName">
  42. <a-input id="userEdit-loginName" v-model.trim="form.loginName" :maxLength="50" allowClear placeholder="请输入用户账号(最多50个字符)"/>
  43. </a-form-model-item>
  44. </a-col>
  45. <a-col :span="12">
  46. <a-form-model-item label="账号密码" prop="password">
  47. <a-input
  48. id="userEdit-password"
  49. v-model.trim="form.password"
  50. :maxLength="50"
  51. :minLength="6"
  52. allowClear
  53. placeholder="请输入账号密码(6-50个字符)"/>
  54. </a-form-model-item>
  55. </a-col>
  56. </a-row>
  57. <a-row :gutter="24">
  58. <!-- 性别 -->
  59. <a-col :span="12">
  60. <a-form-model-item label="性别" prop="sex">
  61. <v-select
  62. ref="sex"
  63. id="userEdit-sex"
  64. code="SEX"
  65. v-model="form.sex"
  66. placeholder="请选择性别"
  67. allowClear ></v-select>
  68. </a-form-model-item>
  69. </a-col>
  70. <!-- 状态 -->
  71. <a-col :span="12">
  72. <a-form-model-item label="状态" prop="loginFlag">
  73. <a-radio-group id="userEdit-flag" name="radioGroup" v-model="form.loginFlag" placeholder="请选择状态">
  74. <a-radio :value="1" id="userEdit-flag1">是</a-radio>
  75. <a-radio :value="0" id="userEdit-flag0">否</a-radio>
  76. </a-radio-group>
  77. </a-form-model-item>
  78. </a-col>
  79. </a-row>
  80. <a-row :gutter="24">
  81. <a-col :span="24">
  82. <a-form-model-item label="角色" prop="roleNames" :label-col="{span: 3}" :wrapper-col="{span: 20}">
  83. <a-select
  84. placeholder="请选择角色"
  85. id="userSyncEdit-roleNames"
  86. allowClear
  87. v-model="form.roleNames"
  88. mode="multiple">
  89. <a-select-option v-for="item in roleList" :id="item.id" :key="item.id" :value="item.id" :disabled="item.isEnable == '0' ? true : false">{{ item.name }}</a-select-option>
  90. </a-select>
  91. </a-form-model-item>
  92. </a-col>
  93. </a-row>
  94. <a-row :gutter="24">
  95. <a-col :span="24">
  96. <a-form-model-item label="备注" prop="remarks" :label-col="{span: 3}" :wrapper-col="{span: 20}">
  97. <a-textarea id="userEdit-remarks" v-model.trim="form.remarks":maxLength="500" allowClear placeholder="请输入备注(最多500个字符)"/>
  98. </a-form-model-item>
  99. </a-col>
  100. </a-row>
  101. <a-form-model-item :label-col="{span: 0}" :wrapper-col="{span: 24}" style="text-align: center;">
  102. <a-button id="userEdit-save" type="primary" @click="handleSubmit()" :style="{ marginRight: '8px' }">保存</a-button>
  103. <a-button id="userEdit-cancel" :style="{ marginLeft: '8px' }" @click="isshow=false">取消</a-button>
  104. </a-form-model-item>
  105. </a-form-model>
  106. </a-modal>
  107. </a-spin>
  108. </div>
  109. </template>
  110. <script>
  111. import { STable, VSelect } from '@/components'
  112. import { getRoleList, saveUserPower } from '@/api/power-user.js'
  113. export default {
  114. name: 'UserModal',
  115. components: {
  116. STable, VSelect
  117. },
  118. props: {
  119. visible: {
  120. type: Boolean,
  121. default: false
  122. },
  123. nowData: { // 当前编辑数据
  124. type: Object,
  125. default: function () {
  126. return {}
  127. }
  128. }
  129. },
  130. data () {
  131. return {
  132. spinning: false,
  133. titleText: '新增用户',
  134. isshow: this.visible,
  135. roleList: [],
  136. formItemLayout: {
  137. labelCol: { span: 6 },
  138. wrapperCol: { span: 16 }
  139. },
  140. form: {
  141. name: '', // 用户名称
  142. phone: '', // 手机号码
  143. loginName: '', // 用户账号
  144. password: undefined, // 账号密码
  145. sex: undefined, // 性别
  146. loginFlag: '', // 状态
  147. roleNames: undefined, // 角色
  148. remarks: ''// 备注
  149. },
  150. rules: {
  151. name: [ { required: true, message: '请输入用户名称', trigger: 'blur' }, { pattern: '^[^<|>]{1,30}$', message: '请输入不含<或>的字符,最多30个字符' } ],
  152. phone: [ { required: true, message: '请输入手机号码', trigger: 'blur' }, { pattern: /^[1][0-9]{10}$/, message: '请输入正确的手机号码!' } ],
  153. loginName: [ { required: true, message: '请输入用户账号', trigger: 'blur' }, { pattern: /^[0-9A-Za-z]+$/, message: '用户账号为数字或大小写字母组成!' } ],
  154. password: [ { message: '请输入账号密码', trigger: 'blur' }, { pattern: '^.{6,50}$', message: '请输入6~50位密码' } ],
  155. sex: [ { required: true, message: '请选择性别', trigger: 'change' } ],
  156. loginFlag: [ { required: true, message: '请选择状态', trigger: 'change' } ],
  157. roleNames: [ { required: true, message: '请选择角色', trigger: 'change' } ]
  158. }
  159. }
  160. },
  161. methods: {
  162. // 过滤空格
  163. filterEmpty () {
  164. let str = this.form.name
  165. str = str.replace(/\ +/g, '')
  166. str = str.replace(/[ ]/g, '')
  167. str = str.replace(/[\r\n]/g, '')
  168. this.form.name = str
  169. },
  170. // 保存提交
  171. handleSubmit () {
  172. const _this = this
  173. this.$refs.ruleForm.validate(valid => {
  174. if (valid) {
  175. const params = JSON.parse(JSON.stringify(_this.form))
  176. params.roleNames = _this.form.roleNames.join(',')
  177. params.id = _this.nowData && _this.nowData.id ? _this.nowData.id : null
  178. params.ownerOrgFlag = _this.nowData && _this.nowData.ownerOrgFlag
  179. _this.spinning = true
  180. saveUserPower(params).then(res => {
  181. if (res.status == 200) {
  182. _this.$message.success(res.message)
  183. _this.$emit('refresh')
  184. setTimeout(function () {
  185. _this.isshow = false
  186. _this.spinning = false
  187. }, 300)
  188. } else {
  189. _this.spinning = false
  190. }
  191. })
  192. } else {
  193. return false
  194. }
  195. })
  196. },
  197. // 获取角色列表接口
  198. getRoleList () {
  199. getRoleList().then(res => {
  200. if (res.status == 200) {
  201. this.roleList = res.data
  202. } else {
  203. this.$message.warning(res.message)
  204. }
  205. })
  206. }
  207. },
  208. watch: {
  209. visible (newValue, oldValue) {
  210. this.isshow = newValue
  211. },
  212. isshow (newValue, oldValue) {
  213. if (!newValue) {
  214. this.$emit('close')
  215. // 重置
  216. this.$refs.ruleForm.resetFields()
  217. this.form.name = ''
  218. this.form.phone = ''
  219. this.form.password = undefined
  220. this.form.loginName = ''
  221. this.form.sex = undefined
  222. this.form.loginFlag = ''
  223. this.form.roleNames = undefined
  224. this.form.remarks = ''
  225. this.titleText = '新增用户'
  226. } else {
  227. this.getRoleList()
  228. }
  229. },
  230. nowData: {
  231. handler (newValue, oldValue) {
  232. if (this.isshow && newValue) {
  233. if (this.nowData.id) { // 编辑
  234. this.titleText = '编辑用户'
  235. this.form = Object.assign({}, this.nowData)
  236. if (this.nowData.roleIds) {
  237. this.form.roleNames = this.nowData.roleIds.split(',')
  238. }
  239. this.form.loginFlag = Number(this.nowData.loginFlag)
  240. }
  241. }
  242. },
  243. deep: true
  244. }
  245. }
  246. }
  247. </script>