userModal.vue 6.9 KB

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