add.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <div class="landing-add">
  3. <div class="landing-container">
  4. <div class="step-container">
  5. <a-steps :current="stepCurrent">
  6. <a-step title="填写基本信息" />
  7. <a-step title="编辑内容1" />
  8. </a-steps>
  9. </div>
  10. <div class="step-content-container">
  11. <component
  12. :is="stepComponent[stepCurrent]"
  13. :content="forms"
  14. @next="onTapNext"
  15. @prev="onTapPrev"
  16. />
  17. </div>
  18. </div>
  19. </div>
  20. </template>
  21. <script lang="ts">
  22. import {
  23. defineComponent,
  24. reactive,
  25. toRefs,
  26. createVNode,
  27. ref,
  28. onMounted,
  29. } from "vue";
  30. import { onBeforeRouteLeave } from "vue-router";
  31. import { Modal } from "ant-design-vue";
  32. import { ExclamationCircleOutlined } from "@ant-design/icons-vue";
  33. import { landingAdd, getLandingInfo, landingEdit } from "@/api";
  34. import StepOne from "./stepComp/step-one.vue";
  35. import StepTwo from "./stepComp/step-two.vue";
  36. import useApp from "@/hooks/useApp";
  37. import { message } from "ant-design-vue";
  38. const LandingAddPage = defineComponent({
  39. name: "LandingAddPage",
  40. components: {
  41. StepOne,
  42. StepTwo,
  43. },
  44. setup() {
  45. const state = reactive({
  46. stepCurrent: 0,
  47. stepComponent: ["step-one", "step-two"],
  48. isOver: false,
  49. forms: ref<Record<string, any>>({}),
  50. });
  51. const { route, router } = useApp();
  52. onBeforeRouteLeave((_, __, next) => {
  53. if (!state.isOver) {
  54. Modal.confirm({
  55. title: "确认离开当前页面吗?",
  56. content: "未保存的内容将会丢失",
  57. icon: createVNode(ExclamationCircleOutlined),
  58. onOk: () => {
  59. next();
  60. },
  61. });
  62. } else {
  63. next();
  64. }
  65. });
  66. if (route.query && route.query.id) {
  67. getLandingInfo(route.query.id as string).then((res) => {
  68. state.forms = res.data;
  69. });
  70. }
  71. const onTapNext = (content: Record<string, any>) => {
  72. //log(content);
  73. state.forms = Object.assign(state.forms, content);
  74. if (state.stepCurrent >= 1) {
  75. if (route.query && route.query.id) {
  76. landingEdit(Object.assign(state.forms, { id: route.query.id })).then(
  77. (res) => {
  78. state.isOver = true;
  79. message.success("编辑成功");
  80. router.replace("/put/landing");
  81. }
  82. );
  83. } else {
  84. landingAdd(state.forms).then((res) => {
  85. state.isOver = true;
  86. message.success("创建成功");
  87. router.replace("/put/landing");
  88. });
  89. }
  90. } else {
  91. state.stepCurrent++;
  92. }
  93. };
  94. //上一步
  95. const onTapPrev = (content: Record<string, any>) => {
  96. Object.assign(state.forms, content)
  97. state.stepCurrent--;
  98. };
  99. return {
  100. ...toRefs(state),
  101. onTapNext,
  102. onTapPrev,
  103. };
  104. },
  105. });
  106. export default LandingAddPage;
  107. </script>
  108. <style lang="scss">
  109. .landing-add {
  110. padding: 22px;
  111. min-height: 100%;
  112. .landing-container {
  113. height: inherit;
  114. background: #fff;
  115. box-shadow: 0px 1px 2px 0px rgba(0, 0, 0, 0.08);
  116. padding: 16px;
  117. }
  118. .step-container {
  119. padding: 20px 60px 36px;
  120. border-bottom: 1px solid #e5e5e5;
  121. }
  122. .step-content-container {
  123. padding: 20px 42px;
  124. }
  125. }
  126. </style>