SysPositionModal.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <a-modal
  3. :title="title"
  4. :width="800"
  5. :visible="visible"
  6. :maskClosable="false"
  7. :confirmLoading="confirmLoading"
  8. @ok="handleOk"
  9. @cancel="handleCancel"
  10. cancelText="关闭">
  11. <a-spin :spinning="confirmLoading">
  12. <a-form :form="form">
  13. <a-form-item
  14. :labelCol="labelCol"
  15. :wrapperCol="wrapperCol"
  16. label="职务编码">
  17. <a-input :maxLength="10" placeholder="请输入职位编码" v-decorator="['code', validatorRules.code]" :read-only="readOnly"/>
  18. </a-form-item>
  19. <a-form-item
  20. :labelCol="labelCol"
  21. :wrapperCol="wrapperCol"
  22. label="职务名称">
  23. <a-input :maxLength="15" placeholder="请输入职位名称" v-decorator="['name', validatorRules.name]"/>
  24. </a-form-item>
  25. <a-form-item label="部门" :labelCol="labelCol" :wrapperCol="wrapperCol" >
  26. <a-input-search placeholder="点击选择部门" v-decorator="['deptName',validatorRules.deptname]" @search="onSearch">
  27. <a-button slot="enterButton" icon="search">选择</a-button>
  28. </a-input-search>
  29. </a-form-item>
  30. </a-form>
  31. <depart-window ref="departWindow" @ok="modalFormOk"></depart-window>
  32. </a-spin>
  33. </a-modal>
  34. </template>
  35. <script>
  36. import pick from 'lodash.pick'
  37. import { httpAction } from '@/api/manage'
  38. import { duplicateCheck } from '@/api/api'
  39. import JDictSelectTag from '@/components/dict/JDictSelectTag'
  40. import departWindow from './DepartWindow'
  41. import { queryIdTree } from '@/api/api'
  42. let validatorCodeTimer = null
  43. export default {
  44. name: 'SysPositionModal',
  45. components: {
  46. JDictSelectTag,
  47. departWindow
  48. },
  49. data() {
  50. return {
  51. departTree:[],
  52. checkedDepartKeys:[],
  53. selectDeptKeys:[],
  54. visibles:false,
  55. title: '操作',
  56. visible: false,
  57. deptname:"",
  58. deptid:"",
  59. model: {},
  60. labelCol: {
  61. xs: { span: 24 },
  62. sm: { span: 5 },
  63. },
  64. wrapperCol: {
  65. xs: { span: 24 },
  66. sm: { span: 16 },
  67. },
  68. confirmLoading: false,
  69. form: this.$form.createForm(this),
  70. validatorRules: {
  71. code: {
  72. rules: [
  73. { required: true, message: '请输入职位编码' },
  74. {
  75. validator: (rule, value, callback) => {
  76. // 函数消抖的简单实现,防止一段时间内发送多次请求
  77. if (validatorCodeTimer) {
  78. // 停止上次开启的定时器
  79. clearTimeout(validatorCodeTimer)
  80. }
  81. validatorCodeTimer = setTimeout(() => {
  82. duplicateCheck({
  83. tableName: 'sys_position',
  84. fieldName: 'code',
  85. fieldVal: value,
  86. dataId: this.model.id
  87. }).then((res) => {
  88. if (res.success) {
  89. callback()
  90. } else {
  91. callback(res.message)
  92. }
  93. }).catch(console.error)
  94. }, 300)
  95. }
  96. }
  97. ]
  98. },
  99. name: { rules: [{ required: true, message: '请输入职位名称' }] },
  100. deptname: { rules: [{ required: true, message: '请选择部门' }] },
  101. },
  102. url: {
  103. add: '/sys/position/add',
  104. edit: '/sys/position/edit',
  105. },
  106. readOnly:false
  107. }
  108. },
  109. created() {
  110. },
  111. methods: {
  112. add() {
  113. this.edit({})
  114. },
  115. edit(record) {
  116. this.selectDeptKeys=[];
  117. this.form.resetFields()
  118. console.log(record)
  119. this.model = Object.assign({}, record)
  120. this.visible = true
  121. if(record.id){
  122. this.readOnly=true
  123. }else{
  124. this.readOnly=false
  125. }
  126. this.deptid=record.deptId;
  127. this.selectDeptKeys.push(record.deptId);
  128. this.$nextTick(() => {
  129. this.form.setFieldsValue(pick(this.model,
  130. 'code',
  131. 'name',
  132. 'postRank',
  133. 'deptName'
  134. ))
  135. })
  136. },
  137. close() {
  138. this.$emit('close')
  139. this.visible = false
  140. },
  141. handleOk() {
  142. const that = this
  143. // 触发表单验证
  144. // if(this.model.deptId==null||this.model.deptId==""){
  145. // that.$message.warning("请选择部门!");
  146. // return;
  147. // }
  148. this.form.validateFields((err, values) => {
  149. if (!err) {
  150. that.confirmLoading = true
  151. let httpurl = ''
  152. let method = ''
  153. if (!this.model.id) {
  154. httpurl += this.url.add
  155. method = 'post'
  156. } else {
  157. httpurl += this.url.edit
  158. method = 'put'
  159. }
  160. let formData = Object.assign(this.model, values)
  161. formData.deptId=this.deptid;
  162. httpAction(httpurl, formData, method).then((res) => {
  163. if (res.success) {
  164. that.$message.success(res.message)
  165. that.$emit('ok')
  166. } else {
  167. that.$message.warning(res.message)
  168. }
  169. }).finally(() => {
  170. that.confirmLoading = false
  171. that.close()
  172. })
  173. }
  174. })
  175. },
  176. handleCancel() {
  177. this.close()
  178. },
  179. // 搜索用户对应的部门API
  180. onSearch() {
  181. this.$refs.departWindow.add(this.selectDeptKeys, "");
  182. },
  183. // 获取用户对应部门弹出框提交给返回的数据
  184. modalFormOk(formData) {
  185. this.form.setFieldsValue({
  186. deptName:formData.departIdList[0].title
  187. })
  188. this.deptid=formData.departIdList[0].value;
  189. this.selectDeptKeys.push(formData.departIdList[0].value)
  190. },
  191. }
  192. }
  193. </script>
  194. <style lang="less" scoped>
  195. </style>