common.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { Ref, ref, watch, onMounted } from 'vue'
  2. import * as api from './api'
  3. interface Props {
  4. data: {
  5. campaignId: string
  6. campaignType: string
  7. profileId: string
  8. ruleType: number
  9. }
  10. RuleStatusButton?: { [key: string]: any }
  11. }
  12. export const userFormData = (props: Props) => {
  13. const formData: Ref<AutoCampaignRule> = ref({
  14. campaignId: props.data.campaignId,
  15. campaignType: props.data.campaignType,
  16. profileId: props.data.profileId,
  17. ruleType: props.data.ruleType,
  18. templateName: '',
  19. useTmpl: false,
  20. rule: {
  21. type: props.data.ruleType,
  22. campaignType: props.data.campaignType,
  23. campaignAd: [],
  24. action: {},
  25. activeModel: '',
  26. setTime: '',
  27. weekdays: [],
  28. conditions: [],
  29. },
  30. RuleStatusButton: props.RuleStatusButton,
  31. })
  32. const initData = async () => {
  33. const resp = await api.getCampaignRuleInfo(props.data)
  34. const info = resp.data[0]
  35. if (info) {
  36. delete info['RuleStatusButton']
  37. formData.value = info
  38. if (info.template) {
  39. formData.value.useTmpl = true
  40. formData.value.rule = info.template.rule
  41. } else {
  42. formData.value.useTmpl = false
  43. }
  44. }
  45. }
  46. const submitFormData = async () => {
  47. const body: AutoCampaignRule = {
  48. campaignId: formData.value.campaignId,
  49. campaignType: formData.value.campaignType,
  50. profileId: formData.value.profileId,
  51. ruleType: formData.value.ruleType,
  52. RuleStatusButton: formData.value.RuleStatusButton,
  53. rule: null,
  54. templateName: formData.value.templateName,
  55. }
  56. if (formData.value.useTmpl) {
  57. body.templateId = formData.value.template.id
  58. } else {
  59. body.rule = formData.value.rule
  60. delete body.rule['modifier_name']
  61. delete body.rule.id
  62. }
  63. //console.log('body',body);
  64. await api.saveCampaignRule(body)
  65. }
  66. watch(
  67. () => props.RuleStatusButton,
  68. () => {
  69. formData.value.RuleStatusButton = props.RuleStatusButton
  70. },
  71. { deep: true }
  72. )
  73. onMounted(async () => {
  74. await initData()
  75. })
  76. return { formData, submitFormData }
  77. }