edit.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <template>
  2. <component
  3. is="a-modal"
  4. :width="tool.getDevice() === 'mobile' ? '100%' : '600px'"
  5. v-model:visible="visible"
  6. :title="title"
  7. :mask-closable="false"
  8. :ok-loading="loading"
  9. @cancel="close"
  10. @before-ok="submit"
  11. >
  12. <!-- 表单信息 start -->
  13. <a-form
  14. ref="formRef"
  15. :model="formData"
  16. :rules="rules"
  17. :auto-label-width="true"
  18. >
  19. <a-form-item label="媒体ID" field="media_id">
  20. <a-select
  21. v-model="formData.media_id"
  22. :options="mediaOptions"
  23. placeholder="请选择媒体ID"
  24. allow-clear
  25. />
  26. </a-form-item>
  27. <a-form-item label="渠道ID" field="agent_id">
  28. <a-select
  29. v-model="formData.agent_id"
  30. :options="agentOptions"
  31. placeholder="请选择渠道ID"
  32. allow-clear
  33. />
  34. </a-form-item>
  35. <a-form-item label="负责人ID" field="auth_id">
  36. <a-select
  37. v-model="formData.auth_id"
  38. :options="authOptions"
  39. placeholder="请选择负责人ID"
  40. allow-clear
  41. />
  42. </a-form-item>
  43. <a-form-item label="广告位名称" field="name">
  44. <a-input v-model="formData.name" placeholder="请输入广告位名称" />
  45. </a-form-item>
  46. </a-form>
  47. <!-- 表单信息 end -->
  48. </component>
  49. </template>
  50. <script setup>
  51. import { ref, reactive, computed } from "vue";
  52. import tool from "@/utils/tool";
  53. import { Message, Modal } from "@arco-design/web-vue";
  54. import api from "../../api/advert/agentSite";
  55. import advertCommonApi from "../../api/advert/common";
  56. import { useUserStore } from "@/store";
  57. const emit = defineEmits(["success"]);
  58. // 引用定义
  59. const visible = ref(false);
  60. const loading = ref(false);
  61. const formRef = ref();
  62. const mode = ref("");
  63. const userStore = useUserStore();
  64. const mediaOptions = ref([]);
  65. const authOptions = ref([]);
  66. const agentOptions = ref([]);
  67. let title = computed(() => {
  68. return "广告位" + (mode.value == "add" ? "-新增" : "-编辑");
  69. });
  70. // 表单初始值
  71. const initialFormData = {
  72. id: null,
  73. media_id: null,
  74. agent_id: null,
  75. auth_id: null,
  76. name: "",
  77. };
  78. // 表单信息
  79. const formData = reactive({ ...initialFormData });
  80. // 验证规则
  81. const rules = {
  82. media_id: [{ required: true, message: "媒体ID必需填写" }],
  83. agent_id: [{ required: true, message: "渠道ID必需填写" }],
  84. auth_id: [{ required: true, message: "负责人ID必需填写" }],
  85. name: [{ required: true, message: "广告位名称必需填写" }],
  86. };
  87. // 获取媒体列表Options
  88. const getMediaOptions = async () => {
  89. const res = await advertCommonApi.getMediaOptionsApi();
  90. if (res.code == 200) {
  91. mediaOptions.value = res.data;
  92. }
  93. };
  94. // 获取渠道列表Options
  95. const getAgentOptions = async () => {
  96. const res = await advertCommonApi.getAgentOptionsApi();
  97. if (res.code == 200) {
  98. agentOptions.value = res.data;
  99. }
  100. };
  101. // 获取后台归属人列表
  102. const getAuthList = async () => {
  103. const res = await advertCommonApi.getAuthOptionsApi();
  104. if (res.code == 200) {
  105. authOptions.value = res.data;
  106. }
  107. };
  108. // 打开弹框
  109. const open = async (type = "add") => {
  110. mode.value = type;
  111. // 重置表单数据
  112. Object.assign(formData, initialFormData);
  113. formRef.value.clearValidate();
  114. visible.value = true;
  115. if (mode.value === "add") {
  116. formData.auth_id = userStore.user.id;
  117. }
  118. await initPage();
  119. };
  120. // 初始化页面数据
  121. const initPage = async () => {
  122. await getMediaOptions();
  123. await getAgentOptions();
  124. await getAuthList();
  125. };
  126. // 设置数据
  127. const setFormData = async (data) => {
  128. for (const key in formData) {
  129. if (data[key] != null && data[key] != undefined) {
  130. formData[key] = data[key];
  131. }
  132. }
  133. };
  134. // 数据保存
  135. const submit = async (done) => {
  136. const validate = await formRef.value?.validate();
  137. if (!validate) {
  138. loading.value = true;
  139. let data = { ...formData };
  140. let result = {};
  141. if (mode.value === "add") {
  142. // 添加数据
  143. data.id = undefined;
  144. result = await api.save(data);
  145. } else {
  146. // 修改数据
  147. result = await api.update(data.id, data);
  148. }
  149. if (result.code === 200) {
  150. Message.success("操作成功");
  151. emit("success");
  152. done(true);
  153. }
  154. // 防止连续点击提交
  155. setTimeout(() => {
  156. loading.value = false;
  157. }, 500);
  158. }
  159. done(false);
  160. };
  161. // 关闭弹窗
  162. const close = () => (visible.value = false);
  163. defineExpose({ open, setFormData });
  164. </script>