index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. <template>
  2. <view class="content" :style="{paddingBottom: paddingBottom}">
  3. <view class="item" v-for="(item, index) in json" :class="item.classText.join(' ')" @click="click(item)">
  4. <p v-if="item.type === 'h5-text'" :style="item.style">{{ item.content }}</p>
  5. <img :ref="'myImage' + index" @load="handleImageLoad(index, item)" v-if="item.type === 'h5-image'"
  6. :src="item.url"/>
  7. <div v-if="item.type === 'h5-sep'" :class="item.classText.join(' ')"></div>
  8. <div class="countdown2-box"
  9. :style="{ backgroundImage: item.bgImage && item.bgImage !== '#' ? `url(${item.bgImage})` : '' }"
  10. v-if="item.type==='h5-countdown'"
  11. >
  12. 距结束<span id="days" :style="{background: item.mainColor}">{{ item.days }}</span>天
  13. <span id="hours" :style="{background: item.mainColor}">{{ item.hours }}</span>时
  14. <span id="minutes" :style="{background: item.mainColor}">{{ item.minutes }}</span>分
  15. <span id="seconds" :style="{background: item.mainColor}">{{ item.seconds }}</span>秒
  16. </div>
  17. <div :class="item.classText.join(' ')" v-if="item.type==='h5-chat'">
  18. <div class="chat-container">
  19. <div class="chat-messages">
  20. <!-- Messages -->
  21. <div v-for="(message, index) in item.messages" :key="index"
  22. :class="['message-wrapper', message.sender === 'user' ? 'user-message' : 'agent-message']"
  23. >
  24. <div class="avatar">
  25. <img :src="message.sender === 'user' ? userAvatar : getAgentAvatar(item)" alt="Avatar"/>
  26. </div>
  27. <div class="message-content">
  28. <div v-if="message.text" class="message-text" v-html="message.text"></div>
  29. <!-- Options buttons -->
  30. <div v-if="message.options && message.options.length > 0" class="options-container">
  31. <button
  32. v-for="(option, optIndex) in message.options"
  33. :key="optIndex"
  34. :style="{color: item.style.btnTextColor,backgroundColor: item.style.buttonColor}"
  35. class="option-button"
  36. :class="{ 'selected': isOptionSelected(message.id, option) }"
  37. >
  38. {{ option.text }}
  39. </button>
  40. </div>
  41. </div>
  42. </div>
  43. </div>
  44. </div>
  45. </div>
  46. </view>
  47. </view>
  48. </template>
  49. <script>
  50. import Clipboard from 'clipboard' // 引入Clipboard
  51. import {getTemplateByNo, getTemplateById, callback, youkuClickCallback, iqiyiClickCallback} from '../../api/api.js'
  52. import {clicks} from '../../common/common.js'
  53. import agentAvatar from '../../static/customer.png'
  54. import userAvatar from '../../static/profile.png'
  55. export default {
  56. data() {
  57. return {
  58. userAvatar: userAvatar, // Placeholder for user avatar
  59. agentAvatar: agentAvatar,
  60. images: [],
  61. params: {},
  62. site: {},
  63. data: {},
  64. json: [],
  65. vid: '',
  66. click_id: '',
  67. aid: '',
  68. id: null,
  69. no: null,
  70. type: 0,
  71. show: false,
  72. name: '',
  73. tel: '',
  74. accountId: '',
  75. ip: '',
  76. paddingBottom: 0,
  77. order_plan_id: '',
  78. creative_id: '',
  79. impress_id: '',
  80. sign: ''
  81. }
  82. },
  83. mounted() {
  84. window.vueInstance = this // 将 Vue 实例暴露到全局
  85. },
  86. onLoad(option) {
  87. this.params = option;
  88. this.no = this.params.no;
  89. this.id = this.params.tid;
  90. if ((this.id == null || this.id == '' || this.id == undefined) && (this.no == null || this.no == '' || this.no == undefined)) {
  91. uni.showToast({
  92. icon: 'none',
  93. title: '推广链接错误'
  94. })
  95. return
  96. }
  97. this.accountId = this.params.accountId;
  98. this.type = this.params.type;
  99. this.no = this.params.no;
  100. if (this.id) {
  101. getTemplateById(this.id).then(e => {
  102. this.data = e.data;
  103. this.site = e.site;
  104. this.type = e.site.type;
  105. this.json = JSON.parse(e.data.json)
  106. }).finally(() => {
  107. this.setData();
  108. })
  109. } else if (this.no) {
  110. getTemplateByNo(this.no).then(e => {
  111. this.data = e.data;
  112. this.json = JSON.parse(e.data.json)
  113. }).finally(() => {
  114. this.setData();
  115. })
  116. }
  117. },
  118. beforeDestroy() {
  119. this.json.filter(e => e.type === 'h5-countdown').forEach(item => {
  120. if (item.intervalId) {
  121. clearInterval(item.intervalId)
  122. }
  123. })
  124. },
  125. methods: {
  126. setData() {
  127. // 百度
  128. if (this.type == 0) {
  129. this.vid = this.params.bd_vid;
  130. }
  131. // 优酷
  132. if (this.type == 1) {
  133. this.vid = this.params.bd_vid;
  134. this.aid = this.params.aid;
  135. this.click_id = this.params.click_id;
  136. this.ip = this.params.ip;
  137. }
  138. // 爱奇艺
  139. if (this.type == 2) {
  140. this.order_plan_id = this.params.order_plan_id;
  141. this.creative_id = this.params.creative_id;
  142. this.impress_id = this.params.impress_id;
  143. this.sign = this.params.sign;
  144. }
  145. this.loadCountdownData();
  146. this.loadChatMessage();
  147. },
  148. // 加载计时器数据
  149. loadCountdownData() {
  150. let countdownItems = this.json.filter(e => e.type === 'h5-countdown')
  151. countdownItems.forEach(item => {
  152. // 为每个item启动一个定时器
  153. this.startCountdown(item)
  154. })
  155. },
  156. // 加载聊天组件的消息
  157. loadChatMessage() {
  158. let h5ChatItem = this.json.filter(e => e.type === 'h5-chat')[0]
  159. h5ChatItem.messages = []
  160. // 先输出所有的欢迎信息,欢迎信息为没有options的数据,以及后面一条数据
  161. let isOutputWelcome = 0;
  162. while (h5ChatItem.agentMsg.length > 0) {
  163. let msg = h5ChatItem.agentMsg.shift()
  164. if (!msg.options || msg.options.length === 0) {
  165. isOutputWelcome = 1
  166. setTimeout(() => {
  167. h5ChatItem.messages.push(msg)
  168. }, 1000)
  169. } else {
  170. if ((isOutputWelcome === 1) || (isOutputWelcome === 0)) {
  171. isOutputWelcome = 2
  172. setTimeout(() => {
  173. h5ChatItem.messages.push(msg)
  174. }, 3000)
  175. break;
  176. }
  177. }
  178. }
  179. },
  180. startCountdown(item) {
  181. if (!item.active) {
  182. return
  183. }
  184. let intervalId = setInterval(() => {
  185. let timeLeft
  186. if (item.countdownMode === '1') {
  187. // 模式1:now() - endDateTime 的时间差
  188. timeLeft = this.calculateTimePassed(item)
  189. } else if (item.countdownMode === '2') {
  190. // 模式2:timeDisplay 分钟数倒计时
  191. timeLeft = this.calculateTimeLeftFromDisplay(item)
  192. }
  193. // 不再有 total <= 0 的判断, 因为模式1是不断增加的
  194. item.days = timeLeft.days
  195. item.hours = timeLeft.hours
  196. item.minutes = timeLeft.minutes
  197. item.seconds = timeLeft.seconds
  198. }, 1000)
  199. item.intervalId = intervalId // 存储 intervalId
  200. },
  201. calculateTimePassed(item) {
  202. // 如果已经结束,直接返回
  203. if (item.isFinished) {
  204. return {
  205. days: 0,
  206. hours: 0,
  207. minutes: 0,
  208. seconds: 0
  209. }
  210. }
  211. let endTime = new Date(item.endDateTime)
  212. let now = new Date()
  213. let diff = endTime.getTime() - now.getTime() // 毫秒差, end - now
  214. if (diff <= 0) {
  215. // 如果 endDateTime 比当前时间晚, 则显示 0
  216. clearInterval(item.intervalId)
  217. item.intervalId = null
  218. item.isFinished = true
  219. return {
  220. days: 0,
  221. hours: 0,
  222. minutes: 0,
  223. seconds: 0
  224. }
  225. }
  226. return this.millisecondsToTimeObject(diff)
  227. },
  228. calculateTimeLeftFromDisplay(item) {
  229. if (!item.startTime) {
  230. item.startTime = Date.now() // 记录开始时间
  231. }
  232. let totalSeconds = item.timeDisplay * 60 // timeDisplay 是分钟数
  233. let passedSeconds = Math.floor((Date.now() - item.startTime) / 1000)
  234. let timeLeft = totalSeconds - passedSeconds
  235. if (timeLeft <= 0) {
  236. clearInterval(item.intervalId)
  237. item.intervalId = null//清除id
  238. return {
  239. days: 0,
  240. hours: 0,
  241. minutes: 0,
  242. seconds: 0
  243. }
  244. }
  245. return this.secondsToTimeObject(timeLeft)
  246. },
  247. millisecondsToTimeObject(milliseconds) {
  248. let totalSeconds = Math.floor(milliseconds / 1000)
  249. return this.secondsToTimeObject(totalSeconds)
  250. },
  251. secondsToTimeObject(totalSeconds) {
  252. let days = Math.floor(totalSeconds / (3600 * 24))
  253. let hours = Math.floor((totalSeconds % (3600 * 24)) / 3600)
  254. let minutes = Math.floor((totalSeconds % 3600) / 60)
  255. let seconds = Math.floor(totalSeconds % 60)
  256. return {
  257. days: days,
  258. hours: hours,
  259. minutes: minutes,
  260. seconds: seconds
  261. }
  262. },
  263. getAgentAvatar(item) {
  264. if (item.style.avatar) {
  265. return item.style.avatar
  266. }
  267. return this.agentAvatar
  268. },
  269. isOptionSelected(messageId, option) {
  270. let messages = this.json.find(t => t.type === 'h5-chat').messages
  271. const message = messages.find(m => m.id === messageId)
  272. return message && message.userSelection && message.userSelection.id === option.id
  273. },
  274. handleImageLoad(index, item) {
  275. if (item.classText.indexOf("footer") != -1) {
  276. const imgElement = this.$refs["myImage" + index];
  277. // 获取真实高度(包含以下两种方式)
  278. const naturalHeight = imgElement[0].naturalHeight; // 原始高度
  279. const clientHeight = imgElement[0].clientHeight; // 渲染高度
  280. this.paddingBottom = clientHeight + "px";
  281. }
  282. },
  283. confirm() {
  284. this.show = false
  285. },
  286. clickToWx(workUrl) {
  287. // 百度
  288. if (this.type == 0) {
  289. let data = {
  290. id: this.id,
  291. url: window.location.href,
  292. vid: this.vid,
  293. clickType: 67,
  294. };
  295. baiduClickCallback(data).then(e => {
  296. window.location.href = workUrl + "?customer_channel=" + this.vid;
  297. })
  298. }
  299. // 优酷
  300. if (this.type == 1) {
  301. let data = {
  302. id: this.id,
  303. url: window.location.href,
  304. vid: this.vid,
  305. aid: this.aid,
  306. clickId: this.click_id,
  307. ip: this.ip,
  308. };
  309. youkuClickCallback(data).then(e => {
  310. window.location.href = workUrl + "?customer_channel=" + this.vid;
  311. })
  312. }
  313. // 爱奇艺
  314. if (this.type == 2) {
  315. if (this.id) {
  316. let data = {
  317. url: window.location.href,
  318. id: this.id,
  319. planId: this.order_plan_id,
  320. creativeId: this.creative_id,
  321. vid: this.impress_id,
  322. sign: this.sign,
  323. clickType: "200"
  324. };
  325. iqiyiClickCallback(data).then(e => {
  326. window.location.href = workUrl + "?customer_channel=" + this.impress_id;
  327. })
  328. } else {
  329. let data = {
  330. accountId: this.accountId,
  331. url: window.location.href,
  332. no: this.no,
  333. planId: this.order_plan_id,
  334. creativeId: this.creative_id,
  335. vid: this.impress_id,
  336. sign: this.sign,
  337. clickType: "200"
  338. };
  339. iqiyiClickCallback(data).then(e => {
  340. window.location.href = workUrl + "?customer_channel=" + this.impress_id;
  341. })
  342. }
  343. }
  344. },
  345. click(item) {
  346. if (!item.addWxFun) {
  347. return
  348. }
  349. this.clickToWx(item.workUrl)
  350. }
  351. }
  352. }
  353. </script>
  354. <style lang="scss" scoped>
  355. .chat-container {
  356. width: 100%;
  357. // max-width: 500px;
  358. display: flex;
  359. flex-direction: column;
  360. background-color: #f8f8f8;
  361. font-family: Arial, sans-serif;
  362. }
  363. .chat-messages {
  364. flex: 1;
  365. overflow-y: auto;
  366. padding: 16px;
  367. display: flex;
  368. flex-direction: column;
  369. }
  370. .message-wrapper {
  371. display: flex;
  372. margin-bottom: 16px;
  373. max-width: 80%;
  374. }
  375. .agent-message {
  376. align-self: flex-start;
  377. }
  378. .user-message {
  379. align-self: flex-end;
  380. flex-direction: row-reverse;
  381. }
  382. .avatar {
  383. font-size: 14px;
  384. display: flex;
  385. padding-right: 5px;
  386. padding-left: 0;
  387. margin-bottom: 15px;
  388. }
  389. .avatar img {
  390. width: 36px;
  391. height: 36px;
  392. border-radius: 50%;
  393. }
  394. .message-content {
  395. display: flex;
  396. flex-direction: column;
  397. }
  398. .message-text {
  399. padding: 12px;
  400. border-radius: 18px;
  401. font-size: 14px;
  402. line-height: 1.4;
  403. word-break: break-word;
  404. }
  405. .agent-message .message-text {
  406. background-color: white;
  407. color: #333;
  408. border-top-left-radius: 4px;
  409. }
  410. .user-message .message-text {
  411. background-color: #4a90e2;
  412. color: white;
  413. border-top-right-radius: 4px;
  414. }
  415. .options-container {
  416. display: flex;
  417. flex-wrap: wrap;
  418. gap: 8px;
  419. margin-top: 8px;
  420. }
  421. .option-button {
  422. background-color: #4a90e2;
  423. color: white;
  424. border: none;
  425. border-radius: 18px;
  426. padding: 8px 16px;
  427. font-size: 14px;
  428. cursor: pointer;
  429. transition: background-color 0.2s;
  430. }
  431. .option-button:hover {
  432. background-color: #3a80d2;
  433. }
  434. .option-button.selected {
  435. background-color: #2a70c2;
  436. }
  437. .item {
  438. display: flex;
  439. flex-direction: column;
  440. flex: 1;
  441. margin: 0;
  442. width: 100%;
  443. img {
  444. width: 100%;
  445. }
  446. p {
  447. white-space: pre-line;
  448. }
  449. }
  450. .footer {
  451. position: fixed;
  452. bottom: 0;
  453. left: 0;
  454. }
  455. .countdown2-box {
  456. background: url('#') no-repeat center;
  457. background-size: 100%;
  458. width: 100%;
  459. height: 54px;
  460. line-height: 54px;
  461. color: #515A6E;
  462. font-size: 14px;
  463. text-align: center;
  464. }
  465. .countdown2-box span {
  466. display: inline-block;
  467. color: #fff;
  468. width: 22px;
  469. line-height: 20px;
  470. text-align: center;
  471. height: 20px;
  472. border-radius: 3px;
  473. margin: 7px;
  474. background: #FF5A29;
  475. }
  476. </style>