userModal.vue 7.9 KB

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