select-tmpl.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <script lang="ts" setup>
  2. import { ref, onMounted, watch } from 'vue';
  3. import * as api from './api';
  4. import XEUtils from 'xe-utils';
  5. interface Props {
  6. data: AutoCampaignRule;
  7. }
  8. const props = defineProps<Props>();
  9. const ruleUsage = ref('custom');
  10. const tmplId = ref(0);
  11. if (props.data.template) {
  12. tmplId.value = props.data.template.id;
  13. }
  14. const tmplList = ref([]);
  15. // const emits = defineEmits(['change-tmpl'])
  16. const getTmplList = async () => {
  17. const resp = await api.getTemplateList({
  18. page: 1,
  19. pageSize: 999,
  20. type: props.data.ruleType,
  21. });
  22. tmplList.value = resp.data;
  23. };
  24. const changeTmpl = () => {
  25. const tmpl = XEUtils.find(tmplList.value, (item) => item.id === tmplId.value);
  26. props.data.rule = XEUtils.clone(tmpl.rule, true);
  27. props.data.template = tmpl;
  28. // emits('change-tmpl', tmpl)
  29. };
  30. const changeRuleUsage = () => {
  31. props.data.useTmpl = ruleUsage.value === 'tmpl';
  32. if (props.data.useTmpl) {
  33. if (!tmplId.value && tmplList.value.length > 0) {
  34. tmplId.value = tmplList.value[0].id;
  35. }
  36. changeTmpl();
  37. }
  38. };
  39. onMounted(async () => {
  40. await getTmplList();
  41. });
  42. watch(
  43. () => props.data.useTmpl,
  44. () => {
  45. if (props.data.useTmpl) {
  46. ruleUsage.value = 'tmpl';
  47. tmplId.value = props.data.template.id;
  48. } else {
  49. ruleUsage.value = 'custom';
  50. }
  51. }
  52. );
  53. defineExpose({ getTmplList });
  54. </script>
  55. <template>
  56. <el-card class="mt-2">
  57. <div class="asj-h3">
  58. <span class="custom-title-icon"></span>
  59. 选择模板
  60. </div>
  61. <el-radio-group v-model="ruleUsage" @change="changeRuleUsage">
  62. <div style="display: flex; justify-content: flex-start; flex-direction: column">
  63. <el-radio label="custom">自定义规则</el-radio>
  64. <el-input
  65. v-model="data.templateName"
  66. v-show="ruleUsage === 'custom'"
  67. style="margin-left: 22px"
  68. placeholder="将规则同时保存为模板(可选)"></el-input>
  69. <el-radio label="tmpl">使用已有模板</el-radio>
  70. <el-select v-show="ruleUsage === 'tmpl'" @change="changeTmpl" v-model="tmplId" style="margin-left: 22px">
  71. <el-option v-for="info in tmplList" :label="info.name" :value="info.id" :key="info.id"></el-option>
  72. </el-select>
  73. </div>
  74. </el-radio-group>
  75. </el-card>
  76. </template>
  77. <style scoped>
  78. .custom-title-icon {
  79. padding: 0 5px;
  80. }
  81. .custom-title-icon:before {
  82. content: '';
  83. width: 4px;
  84. height: 15px;
  85. background: #3569d6;
  86. position: absolute;
  87. top: 50%;
  88. transform: translateY(-50%);
  89. left: 0;
  90. }
  91. </style>