common.ts 2.2 KB

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