GtmAdd.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <a-drawer
  3. :title="modalTitle"
  4. :maskClosable="true"
  5. width="850"
  6. placement="right"
  7. :closable="true"
  8. @close="handleCancel"
  9. :visible="modalVisible"
  10. :ok-button-props="okBtnStatus"
  11. destroy-on-close
  12. style="overflow: auto; padding-bottom: 53px"
  13. >
  14. <!-- Gtm代码-->
  15. <template v-if="modalGtmStatus">
  16. <a-spin :spinning="spinning" :tip="tipTitle">
  17. <a-button type="primary" @click="addGTMBySys" style="margin-right: 20px">系统添加</a-button>
  18. </a-spin>
  19. </template>
  20. <template v-if="modalSysAddGTMStatus">
  21. <a-spin :spinning="spinning" :tip="tipTitle">
  22. <a-form ref="ruleForm" :model="googleGtmForm" :label-col="labelCol" :wrapper-col="wrapperCol">
  23. <div>
  24. <p style="text-align: center; font-weight: bold">系统生成的跟踪代码已添加至网站,请勿重复添加跟踪代码。</p>
  25. </div>
  26. <a-form-item label="GTM Head" name="head">
  27. <a-textarea
  28. v-model:value="googleGtmForm.head"
  29. id="googleGtmHead"
  30. type="textarea"
  31. :autosize="{ minRows: 10, maxRows: 15 }"
  32. :readOnly="true"
  33. />
  34. </a-form-item>
  35. <a-form-item label="GTM Body" name="body">
  36. <a-textarea
  37. v-model:value="googleGtmForm.body"
  38. id="googleGtmBody"
  39. type="textarea"
  40. :autosize="{ minRows: 10, maxRows: 15 }"
  41. :readOnly="true"
  42. />
  43. </a-form-item>
  44. </a-form>
  45. </a-spin>
  46. </template>
  47. <template v-if="modalManualAddGTMStatus">
  48. <a-spin :spinning="spinning" :tip="tipTitle">
  49. <a-form ref="ruleForm" :model="googleGtmForm" :rules="googleGtmFormRules" :label-col="labelCol" :wrapper-col="wrapperCol">
  50. <a-form-item label="Gtm Head" prop="head">
  51. <a-input
  52. v-model="googleGtmForm.head"
  53. type="textarea"
  54. placeholder="请输入Gtm跟踪代码 Head部分"
  55. :auto-size="{ minRows: 10, maxRows: 15 }"
  56. />
  57. </a-form-item>
  58. <a-form-item label="Gtm Body" prop="body">
  59. <a-input
  60. v-model="googleGtmForm.body"
  61. type="textarea"
  62. placeholder="请输入Gtm跟踪代码 Body部分"
  63. :auto-size="{ minRows: 10, maxRows: 15 }"
  64. />
  65. </a-form-item>
  66. </a-form>
  67. </a-spin>
  68. </template>
  69. </a-drawer>
  70. </template>
  71. <script setup lang="ts">
  72. import { reactive, ref } from 'vue';
  73. import { getAction, postActionForm } from '/@/api/manage/manage';
  74. import { useMessage } from '@/hooks/web/useMessage';
  75. const { createMessage } = useMessage();
  76. const modalTitle = ref('跟踪代码');
  77. const labelCol = reactive({ span: 3 });
  78. const wrapperCol = reactive({ span: 21 });
  79. const spinning = ref(false);
  80. const tipTitle = ref('');
  81. const modalGtmStatus = ref(false);
  82. const modalManualAddGTMStatus = ref(false);
  83. let siteInfo = reactive({ id: '', code: '', domain: '', name: '' });
  84. const modalSysAddGTMStatus = ref(false);
  85. const okBtnStatus = ref(true);
  86. const modalVisible = ref(false);
  87. let googleGtmForm = reactive({ head: '', body: '' });
  88. const googleGtmFormRules = reactive({
  89. head: [{ required: true, message: 'GTM Head不能为空', trigger: 'blur' }],
  90. body: [{ required: true, message: 'GTM Body不能为空', trigger: 'blur' }],
  91. });
  92. function init(record) {
  93. modalVisible.value = true;
  94. modalTitle.value = record.name;
  95. siteInfo = record;
  96. addGTM(record);
  97. }
  98. // 添加gtm
  99. function addGTM(r) {
  100. let params = {
  101. id: r.id,
  102. };
  103. let gtmListUrl = `/gtm/list`;
  104. spinning.value = true;
  105. getAction(gtmListUrl, params)
  106. .then(function (res) {
  107. if (res.code === 200) {
  108. if (res.result.gtmHead != null && res.result.gtmBody != null) {
  109. modalVisible.value = true;
  110. modalTitle.value += ' 系统生成跟踪代码';
  111. modalGtmStatus.value = false;
  112. modalManualAddGTMStatus.value = false;
  113. modalSysAddGTMStatus.value = true;
  114. okBtnStatus.value = false;
  115. googleGtmForm.head = res.result.gtmHead;
  116. googleGtmForm.body = res.result.gtmBody;
  117. } else {
  118. modalVisible.value = true;
  119. modalTitle.value += ' 添加跟踪代码';
  120. modalGtmStatus.value = true;
  121. modalManualAddGTMStatus.value = false;
  122. modalSysAddGTMStatus.value = false;
  123. okBtnStatus.value = false;
  124. }
  125. } else if (res.code == 102) {
  126. createMessage.warning('该网站已分配GA账号,不可生成跟踪代码!');
  127. } else {
  128. createMessage.error('系统异常,请重试!');
  129. }
  130. spinning.value = false;
  131. })
  132. .catch(function (err) {
  133. cleanAllFormData();
  134. createMessage.error('网络超时,请重试!' + err.message());
  135. spinning.value = false;
  136. });
  137. }
  138. function handleCancel() {
  139. cleanAllFormData();
  140. }
  141. function cleanAllFormData() {
  142. okBtnStatus.value = true;
  143. spinning.value = false;
  144. tipTitle.value = '';
  145. modalVisible.value = false;
  146. modalTitle.value = '';
  147. modalGtmStatus.value = false;
  148. googleGtmForm = {
  149. head: '',
  150. body: '',
  151. };
  152. modalSysAddGTMStatus.value = false;
  153. modalManualAddGTMStatus.value = false;
  154. modalGtmStatus.value = false;
  155. }
  156. function addGTMBySys() {
  157. modalVisible.value = false;
  158. modalTitle.value = '';
  159. modalGtmStatus.value = false;
  160. modalManualAddGTMStatus.value = false;
  161. modalSysAddGTMStatus.value = false;
  162. let params = {
  163. siteCode: siteInfo.code,
  164. domain: siteInfo.domain,
  165. domainName: siteInfo.name,
  166. };
  167. let gtmAddUrl = `/gtm/add`;
  168. spinning.value = true;
  169. postActionForm(gtmAddUrl, params, 300000)
  170. .then(function (res) {
  171. if (res.code == 200) {
  172. createMessage.success('跟踪代码已生成并已添加至网站,请重新发布网站,查看跟踪代码安装情况!');
  173. modalVisible.value = true;
  174. modalTitle.value = 'GTM网站跟踪代码';
  175. modalGtmStatus.value = false;
  176. modalSysAddGTMStatus.value = true;
  177. modalManualAddGTMStatus.value = false;
  178. googleGtmForm.head = res.data.gtmHead;
  179. googleGtmForm.body = res.data.gtmBody;
  180. } else if (res.code == 102) {
  181. createMessage.warning('该网站已分配GA账号,不可生成跟踪代码!');
  182. } else {
  183. createMessage.error('网站跟踪代码生成失败!');
  184. }
  185. spinning.value = false;
  186. })
  187. .catch(function () {
  188. createMessage.error('网络超时,请重试!');
  189. });
  190. }
  191. defineExpose({ init });
  192. </script>