index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. @click="selectOption(message, option)"
  38. v-once
  39. >
  40. {{ option.text }}
  41. </button>
  42. </div>
  43. </div>
  44. </div>
  45. </div>
  46. </div>
  47. </div>
  48. </view>
  49. </view>
  50. </template>
  51. <script>
  52. import Clipboard from 'clipboard' // 引入Clipboard
  53. import {getTemplateByNo, getTemplateById, callback, youkuClickCallback, iqiyiClickCallback} from '../../api/api.js'
  54. import {clicks} from '../../common/common.js'
  55. import agentAvatar from '../../static/customer.png'
  56. import userAvatar from '../../static/profile.png'
  57. export default {
  58. data() {
  59. return {
  60. userAvatar: userAvatar, // Placeholder for user avatar
  61. agentAvatar: agentAvatar,
  62. images: [],
  63. params: {},
  64. site: {},
  65. data: {},
  66. json: [],
  67. vid: '',
  68. click_id: '',
  69. aid: '',
  70. id: null,
  71. no: null,
  72. type: 0,
  73. show: false,
  74. name: '',
  75. tel: '',
  76. accountId: '',
  77. ip: '',
  78. paddingBottom: 0,
  79. order_plan_id: '',
  80. creative_id: '',
  81. impress_id: '',
  82. sign: '',
  83. isProcessing: false // 防止按钮重复点击
  84. }
  85. },
  86. mounted() {
  87. window.vueInstance = this // 将 Vue 实例暴露到全局
  88. },
  89. onLoad(option) {
  90. this.params = option;
  91. this.no = this.params.no;
  92. this.id = this.params.tid;
  93. if ((this.id == null || this.id == '' || this.id == undefined) && (this.no == null || this.no == '' || this.no == undefined)) {
  94. uni.showToast({
  95. icon: 'none',
  96. title: '推广链接错误'
  97. })
  98. return
  99. }
  100. this.accountId = this.params.accountId;
  101. this.type = this.params.type;
  102. this.no = this.params.no;
  103. if (this.id) {
  104. getTemplateById(this.id).then(e => {
  105. this.data = e.data;
  106. this.site = e.site;
  107. this.type = e.site.type;
  108. this.json = JSON.parse(e.data.json)
  109. }).finally(() => {
  110. this.setData();
  111. })
  112. } else if (this.no) {
  113. getTemplateByNo(this.no).then(e => {
  114. this.data = e.data;
  115. this.json = JSON.parse(e.data.json)
  116. }).finally(() => {
  117. this.setData();
  118. })
  119. }
  120. },
  121. beforeDestroy() {
  122. this.json.filter(e => e.type === 'h5-countdown').forEach(item => {
  123. if (item.intervalId) {
  124. clearInterval(item.intervalId)
  125. }
  126. })
  127. },
  128. methods: {
  129. setData() {
  130. // 百度
  131. if (this.type == 0) {
  132. this.vid = this.params.bd_vid;
  133. }
  134. // 优酷
  135. if (this.type == 1) {
  136. this.vid = this.params.bd_vid;
  137. this.aid = this.params.aid;
  138. this.click_id = this.params.click_id;
  139. this.ip = this.params.ip;
  140. }
  141. // 爱奇艺
  142. if (this.type == 2) {
  143. this.order_plan_id = this.params.order_plan_id;
  144. this.creative_id = this.params.creative_id;
  145. this.impress_id = this.params.impress_id;
  146. this.sign = this.params.sign;
  147. }
  148. this.loadCountdownData();
  149. this.loadChatMessage();
  150. },
  151. getChatMsgIdMax() {
  152. let h5ChatItem = this.json.filter(e => e.type === 'h5-chat')[0]
  153. let maxId = Math.max(0, ...h5ChatItem.messages.map(e => e.id))
  154. return maxId + 1
  155. },
  156. // 聊天组件选择了选项
  157. async selectOption(message, option) {
  158. try{
  159. if(this.isProcessing){
  160. return;
  161. }
  162. this.isProcessing = true;
  163. let h5ChatItem = this.json.filter(e => e.type === 'h5-chat')[0]
  164. // 防止重复点击
  165. if(message.handle){
  166. return;
  167. } else {
  168. message.handle = true;
  169. }
  170. if (!h5ChatItem) {
  171. console.error("未找到类型为 'h5-chat' 的元素");
  172. return;
  173. }
  174. // 如果有答案就直接用户消息和客服消息
  175. let nextMsg = h5ChatItem.agentMsg.shift();
  176. h5ChatItem.messages.push({
  177. id: this.getChatMsgIdMax(),
  178. sender: 'user',
  179. text: option.text,
  180. options: []
  181. })
  182. await this.delay(1000);
  183. if (option.answer) {
  184. h5ChatItem.messages.push({
  185. id: this.getChatMsgIdMax(),
  186. sender: 'agent',
  187. text: option.answer,
  188. options: []
  189. })
  190. }
  191. await this.delay(1000);
  192. // 不管有没有 answer,都添加 nextMsg(如果存在)
  193. if (nextMsg) {
  194. h5ChatItem.messages.push(nextMsg);
  195. if(nextMsg.options){
  196. return;
  197. }
  198. }
  199. while (h5ChatItem.agentMsg.length > 0) {
  200. nextMsg = h5ChatItem.agentMsg.shift();
  201. if (nextMsg && (!nextMsg.options || nextMsg.options.length === 0)) {
  202. await this.delay(500 + (Math.random() * 1000) % 500);
  203. h5ChatItem.messages.push(nextMsg);
  204. } else {
  205. //重新放回去
  206. if(nextMsg){
  207. h5ChatItem.agentMsg.unshift(nextMsg);
  208. }
  209. break;
  210. }
  211. }
  212. if (h5ChatItem.agentMsg.length === 0) {
  213. await this.delay(1000);
  214. h5ChatItem.messages.push({
  215. id: this.getChatMsgIdMax(),
  216. sender: 'agent',
  217. text: "好的,已经为您分配专业老师。<span style='color:red'>【点击下方按钮】</span>添加老师,获取免费上课链接。!",
  218. options: []
  219. });
  220. }
  221. }catch (err) {
  222. console.error(err);
  223. }finally {
  224. this.isProcessing = false;
  225. }
  226. },
  227. // 辅助函数:延迟
  228. delay(ms) {
  229. return new Promise(resolve => setTimeout(resolve, ms));
  230. },
  231. // 加载计时器数据
  232. loadCountdownData() {
  233. let countdownItems = this.json.filter(e => e.type === 'h5-countdown')
  234. countdownItems.forEach(item => {
  235. // 为每个item启动一个定时器
  236. this.startCountdown(item)
  237. })
  238. },
  239. // 加载聊天组件的消息
  240. loadChatMessage() {
  241. let h5ChatItem = this.json.filter(e => e.type === 'h5-chat')[0]
  242. h5ChatItem.messages = []
  243. // 先输出所有的欢迎信息,欢迎信息为没有options的数据,以及后面一条数据
  244. let isOutputWelcome = 0;
  245. while (h5ChatItem.agentMsg.length > 0) {
  246. let msg = h5ChatItem.agentMsg.shift()
  247. if (!msg.options || msg.options.length === 0) {
  248. isOutputWelcome = 1
  249. setTimeout(() => {
  250. h5ChatItem.messages.push(msg)
  251. }, 1000)
  252. } else {
  253. if ((isOutputWelcome === 1) || (isOutputWelcome === 0)) {
  254. isOutputWelcome = 2
  255. setTimeout(() => {
  256. h5ChatItem.messages.push(msg)
  257. }, 3000)
  258. break;
  259. }
  260. }
  261. }
  262. },
  263. startCountdown(item) {
  264. if (!item.active) {
  265. return
  266. }
  267. let intervalId = setInterval(() => {
  268. let timeLeft
  269. if (item.countdownMode === '1') {
  270. // 模式1:now() - endDateTime 的时间差
  271. timeLeft = this.calculateTimePassed(item)
  272. } else if (item.countdownMode === '2') {
  273. // 模式2:timeDisplay 分钟数倒计时
  274. timeLeft = this.calculateTimeLeftFromDisplay(item)
  275. }
  276. // 不再有 total <= 0 的判断, 因为模式1是不断增加的
  277. item.days = timeLeft.days
  278. item.hours = timeLeft.hours
  279. item.minutes = timeLeft.minutes
  280. item.seconds = timeLeft.seconds
  281. }, 1000)
  282. item.intervalId = intervalId // 存储 intervalId
  283. },
  284. calculateTimePassed(item) {
  285. // 如果已经结束,直接返回
  286. if (item.isFinished) {
  287. return {
  288. days: 0,
  289. hours: 0,
  290. minutes: 0,
  291. seconds: 0
  292. }
  293. }
  294. let endTime = new Date(item.endDateTime)
  295. let now = new Date()
  296. let diff = endTime.getTime() - now.getTime() // 毫秒差, end - now
  297. if (diff <= 0) {
  298. // 如果 endDateTime 比当前时间晚, 则显示 0
  299. clearInterval(item.intervalId)
  300. item.intervalId = null
  301. item.isFinished = true
  302. return {
  303. days: 0,
  304. hours: 0,
  305. minutes: 0,
  306. seconds: 0
  307. }
  308. }
  309. return this.millisecondsToTimeObject(diff)
  310. },
  311. calculateTimeLeftFromDisplay(item) {
  312. if (!item.startTime) {
  313. item.startTime = Date.now() // 记录开始时间
  314. }
  315. let totalSeconds = item.timeDisplay * 60 // timeDisplay 是分钟数
  316. let passedSeconds = Math.floor((Date.now() - item.startTime) / 1000)
  317. let timeLeft = totalSeconds - passedSeconds
  318. if (timeLeft <= 0) {
  319. clearInterval(item.intervalId)
  320. item.intervalId = null//清除id
  321. return {
  322. days: 0,
  323. hours: 0,
  324. minutes: 0,
  325. seconds: 0
  326. }
  327. }
  328. return this.secondsToTimeObject(timeLeft)
  329. },
  330. millisecondsToTimeObject(milliseconds) {
  331. let totalSeconds = Math.floor(milliseconds / 1000)
  332. return this.secondsToTimeObject(totalSeconds)
  333. },
  334. secondsToTimeObject(totalSeconds) {
  335. let days = Math.floor(totalSeconds / (3600 * 24))
  336. let hours = Math.floor((totalSeconds % (3600 * 24)) / 3600)
  337. let minutes = Math.floor((totalSeconds % 3600) / 60)
  338. let seconds = Math.floor(totalSeconds % 60)
  339. return {
  340. days: days,
  341. hours: hours,
  342. minutes: minutes,
  343. seconds: seconds
  344. }
  345. },
  346. getAgentAvatar(item) {
  347. if (item.style.avatar) {
  348. return item.style.avatar
  349. }
  350. return this.agentAvatar
  351. },
  352. isOptionSelected(messageId, option) {
  353. let messages = this.json.find(t => t.type === 'h5-chat').messages
  354. const message = messages.find(m => m.id === messageId)
  355. return message && message.userSelection && message.userSelection.id === option.id
  356. },
  357. handleImageLoad(index, item) {
  358. if (item.classText.indexOf("footer") != -1) {
  359. const imgElement = this.$refs["myImage" + index];
  360. // 获取真实高度(包含以下两种方式)
  361. const naturalHeight = imgElement[0].naturalHeight; // 原始高度
  362. const clientHeight = imgElement[0].clientHeight; // 渲染高度
  363. this.paddingBottom = clientHeight + "px";
  364. }
  365. },
  366. confirm() {
  367. this.show = false
  368. },
  369. clickToWx(workUrl) {
  370. // 百度
  371. if (this.type == 0) {
  372. let data = {
  373. id: this.id,
  374. url: window.location.href,
  375. vid: this.vid,
  376. clickType: 67,
  377. };
  378. baiduClickCallback(data).then(e => {
  379. window.location.href = workUrl + "?customer_channel=" + this.vid;
  380. })
  381. }
  382. // 优酷
  383. if (this.type == 1) {
  384. let data = {
  385. id: this.id,
  386. url: window.location.href,
  387. vid: this.vid,
  388. aid: this.aid,
  389. clickId: this.click_id,
  390. ip: this.ip,
  391. };
  392. youkuClickCallback(data).then(e => {
  393. window.location.href = workUrl + "?customer_channel=" + this.vid;
  394. })
  395. }
  396. // 爱奇艺
  397. if (this.type == 2) {
  398. if (this.id) {
  399. let data = {
  400. url: window.location.href,
  401. id: this.id,
  402. planId: this.order_plan_id,
  403. creativeId: this.creative_id,
  404. vid: this.impress_id,
  405. sign: this.sign,
  406. clickType: "200"
  407. };
  408. iqiyiClickCallback(data).then(e => {
  409. window.location.href = workUrl + "?customer_channel=" + this.impress_id;
  410. })
  411. } else {
  412. let data = {
  413. accountId: this.accountId,
  414. url: window.location.href,
  415. no: this.no,
  416. planId: this.order_plan_id,
  417. creativeId: this.creative_id,
  418. vid: this.impress_id,
  419. sign: this.sign,
  420. clickType: "200"
  421. };
  422. iqiyiClickCallback(data).then(e => {
  423. window.location.href = workUrl + "?customer_channel=" + this.impress_id;
  424. })
  425. }
  426. }
  427. },
  428. click(item) {
  429. if (!item.addWxFun) {
  430. return
  431. }
  432. this.clickToWx(item.workUrl)
  433. }
  434. }
  435. }
  436. </script>
  437. <style lang="scss" scoped>
  438. .chat-container {
  439. width: 100%;
  440. // max-width: 500px;
  441. display: flex;
  442. flex-direction: column;
  443. background-color: #f8f8f8;
  444. font-family: Arial, sans-serif;
  445. min-height: 500px;
  446. }
  447. .chat-messages {
  448. flex: 1;
  449. overflow-y: auto;
  450. padding: 16px;
  451. display: flex;
  452. flex-direction: column;
  453. }
  454. .message-wrapper {
  455. display: flex;
  456. margin-bottom: 16px;
  457. max-width: 80%;
  458. }
  459. .agent-message {
  460. align-self: flex-start;
  461. }
  462. .user-message {
  463. align-self: flex-end;
  464. flex-direction: row-reverse;
  465. }
  466. .avatar {
  467. font-size: 14px;
  468. display: flex;
  469. padding-right: 5px;
  470. padding-left: 0;
  471. margin-bottom: 15px;
  472. }
  473. .avatar img {
  474. width: 36px;
  475. height: 36px;
  476. border-radius: 50%;
  477. }
  478. .message-content {
  479. display: flex;
  480. flex-direction: column;
  481. }
  482. .message-text {
  483. padding: 12px;
  484. border-radius: 18px;
  485. font-size: 14px;
  486. line-height: 1.4;
  487. word-break: break-word;
  488. }
  489. .agent-message .message-text {
  490. background-color: white;
  491. color: #333;
  492. border-top-left-radius: 4px;
  493. }
  494. .user-message .message-text {
  495. background-color: #4a90e2;
  496. color: white;
  497. border-top-right-radius: 4px;
  498. }
  499. .options-container {
  500. display: flex;
  501. flex-wrap: wrap;
  502. gap: 8px;
  503. margin-top: 8px;
  504. }
  505. .option-button {
  506. background-color: #4a90e2;
  507. color: white;
  508. border: none;
  509. border-radius: 18px;
  510. padding: 1px 16px;
  511. font-size: 14px;
  512. cursor: pointer;
  513. transition: background-color 0.2s;
  514. }
  515. .option-button:hover {
  516. background-color: #045dba;
  517. }
  518. .option-button.selected {
  519. background-color: #2a70c2;
  520. }
  521. .item {
  522. display: flex;
  523. flex-direction: column;
  524. flex: 1;
  525. margin: 0;
  526. width: 100%;
  527. img {
  528. width: 100%;
  529. }
  530. p {
  531. white-space: pre-line;
  532. }
  533. }
  534. .footer {
  535. position: fixed;
  536. bottom: 0;
  537. left: 0;
  538. z-index: 999;
  539. }
  540. .countdown2-box {
  541. background: url('#') no-repeat center;
  542. background-size: 100%;
  543. width: 100%;
  544. height: 54px;
  545. line-height: 54px;
  546. color: #515A6E;
  547. font-size: 14px;
  548. text-align: center;
  549. }
  550. .countdown2-box span {
  551. display: inline-block;
  552. color: #fff;
  553. width: 22px;
  554. line-height: 20px;
  555. text-align: center;
  556. height: 20px;
  557. border-radius: 3px;
  558. margin: 7px;
  559. background: #FF5A29;
  560. }
  561. </style>