userModal.vue 8.6 KB

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