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. // ruleStatusButton.value = info.RuleStatusButton || {}
  37. // delete info['RuleStatusButton']
  38. formData.value = info
  39. if (info.template) {
  40. formData.value.useTmpl = true
  41. formData.value.rule = info.template.rule
  42. } else {
  43. formData.value.useTmpl = false
  44. }
  45. }
  46. }
  47. const submitFormData = async () => {
  48. const body: AutoCampaignRule = {
  49. campaignId: formData.value.campaignId,
  50. campaignType: formData.value.campaignType,
  51. profileId: formData.value.profileId,
  52. ruleType: formData.value.ruleType,
  53. RuleStatusButton: formData.value.RuleStatusButton,
  54. rule: null,
  55. templateName: formData.value.templateName,
  56. }
  57. if (formData.value.useTmpl) {
  58. body.templateId = formData.value.template.id
  59. } else {
  60. body.rule = formData.value.rule
  61. delete body.rule['modifier_name']
  62. delete body.rule.id
  63. }
  64. await api.saveCampaignRule(body)
  65. console.log("=>(common.ts:68) body", body);
  66. }
  67. watch(
  68. () => props.RuleStatusButton,
  69. () => {
  70. formData.value.RuleStatusButton = props.RuleStatusButton
  71. },
  72. { deep: true }
  73. )
  74. onBeforeMount(async () => {
  75. await initData()
  76. })
  77. return { formData, submitFormData }
  78. }