ModifyOrg.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <template>
  2. <Modal
  3. class="modify-org"
  4. v-model="modalIsShow"
  5. :title="title"
  6. :mask-closable="false"
  7. :width="modalWidth"
  8. @on-visible-change="visibleChange"
  9. >
  10. <div :class="['form-list', { 'form-list-1': isEdit }]">
  11. <Form
  12. class="modal-form"
  13. v-for="(itemForm, findex) in modalForms"
  14. :key="findex"
  15. :ref="`modalFormComp${findex}`"
  16. :model="itemForm"
  17. :rules="rules"
  18. :label-width="100"
  19. >
  20. <FormItem
  21. prop="name"
  22. :label="itemForm.userType === '1' ? '机构名称' : '项目经理'"
  23. >
  24. <Input
  25. size="large"
  26. v-model.trim="itemForm.name"
  27. placeholder="请输入名称"
  28. clearable
  29. ></Input>
  30. </FormItem>
  31. <FormItem prop="loginName" label="账号">
  32. <Input
  33. size="large"
  34. v-model.trim="itemForm.loginName"
  35. placeholder="请输入账号"
  36. :disabled="isEdit"
  37. clearable
  38. ></Input>
  39. </FormItem>
  40. <FormItem prop="password" label="密码" v-if="isEdit">
  41. <Input
  42. size="large"
  43. type="text"
  44. v-model.trim="itemForm.password"
  45. placeholder="输入密码"
  46. clearable
  47. ></Input>
  48. </FormItem>
  49. </Form>
  50. </div>
  51. <div slot="footer">
  52. <Button shape="circle" type="primary" :disabled="isSubmit" @click="submit"
  53. >确认</Button
  54. >
  55. <Button shape="circle" @click="cancel">取消</Button>
  56. </div>
  57. </Modal>
  58. </template>
  59. <script>
  60. import { createOrg, updateOrgUser } from "@/api";
  61. import { password } from "@/plugins/formRules";
  62. const initModalForms = [
  63. {
  64. name: "",
  65. loginName: "",
  66. userType: "1"
  67. },
  68. {
  69. name: "",
  70. loginName: "",
  71. userType: "2"
  72. }
  73. ];
  74. export default {
  75. name: "modify-org",
  76. props: {
  77. instance: {
  78. type: Array,
  79. default() {
  80. return [];
  81. }
  82. }
  83. },
  84. computed: {
  85. isEdit() {
  86. return !!this.instance.length;
  87. },
  88. title() {
  89. return (this.isEdit ? "编辑" : "新增") + "机构信息";
  90. },
  91. modalWidth() {
  92. return this.isEdit ? 500 : 800;
  93. }
  94. },
  95. data() {
  96. return {
  97. modalIsShow: false,
  98. isSubmit: false,
  99. modalForms: [],
  100. rules: {
  101. name: [
  102. {
  103. required: true,
  104. message: "请输入名称",
  105. trigger: "change"
  106. }
  107. ],
  108. loginName: [
  109. {
  110. required: true,
  111. pattern: /^[a-zA-Z0-9_-]{3,20}$/,
  112. message: "账号只能包含字母、数字、下划线以及短横线,长度3-20位",
  113. trigger: "change"
  114. }
  115. ],
  116. password
  117. }
  118. };
  119. },
  120. methods: {
  121. initData(val) {
  122. if (this.instance.length) {
  123. this.modalForms = this.instance.map(item => {
  124. return { ...item };
  125. });
  126. } else {
  127. this.modalForms = initModalForms.map(item => {
  128. return { ...item };
  129. });
  130. }
  131. // this.modalForms.forEach((item, index) => {
  132. // this.$refs[`modalFormComp${index}`].resetFields();
  133. // });
  134. },
  135. visibleChange(visible) {
  136. if (visible) {
  137. this.initData();
  138. }
  139. },
  140. cancel() {
  141. this.modalIsShow = false;
  142. },
  143. open() {
  144. this.modalIsShow = true;
  145. },
  146. async submit() {
  147. const validAll = this.modalForms.map((item, index) => {
  148. return this.$refs[`modalFormComp${index}`][0].validate();
  149. });
  150. const res = await Promise.all(validAll).catch(() => {});
  151. console.log(res);
  152. if (res.some(item => !item)) return;
  153. if (this.isSubmit) return;
  154. this.isSubmit = true;
  155. let data = null;
  156. if (this.isEdit) {
  157. data = await updateOrgUser(this.modalForms[0]).catch(() => {});
  158. } else {
  159. data = await createOrg({
  160. markUsers: this.modalForms
  161. }).catch(() => {});
  162. }
  163. this.isSubmit = false;
  164. if (!data) return;
  165. this.$Message.success(this.title + "成功!");
  166. this.$emit("modified");
  167. this.cancel();
  168. }
  169. }
  170. };
  171. </script>