index.vue 18 KB

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