index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. await this.delay(1000);
  191. h5ChatItem.messages.push(nextMsg);
  192. } else {
  193. await this.delay(1000);
  194. h5ChatItem.messages.push(nextMsg);
  195. }
  196. while((nextMsg = h5ChatItem.agentMsg.shift()) && (!nextMsg.options || nextMsg.options.length === 0)) {
  197. await this.delay(500+(Math.random()*1000)%500);
  198. h5ChatItem.messages.push(nextMsg);
  199. }
  200. if (h5ChatItem.agentMsg.length === 0) {
  201. await this.delay(1000);
  202. h5ChatItem.messages.push({
  203. id: this.getChatMsgIdMax(),
  204. sender: 'agent',
  205. text: "好的,已经为您分配专业老师。<span style='color:red'>【点击下方按钮】</span>添加老师,获取免费上课链接。!",
  206. options: []
  207. });
  208. }
  209. }catch (err) {
  210. console.error(err);
  211. }finally {
  212. this.isProcessing = false;
  213. }
  214. },
  215. // 辅助函数:延迟
  216. delay(ms) {
  217. return new Promise(resolve => setTimeout(resolve, ms));
  218. },
  219. // 加载计时器数据
  220. loadCountdownData() {
  221. let countdownItems = this.json.filter(e => e.type === 'h5-countdown')
  222. countdownItems.forEach(item => {
  223. // 为每个item启动一个定时器
  224. this.startCountdown(item)
  225. })
  226. },
  227. // 加载聊天组件的消息
  228. loadChatMessage() {
  229. let h5ChatItem = this.json.filter(e => e.type === 'h5-chat')[0]
  230. h5ChatItem.messages = []
  231. // 先输出所有的欢迎信息,欢迎信息为没有options的数据,以及后面一条数据
  232. let isOutputWelcome = 0;
  233. while (h5ChatItem.agentMsg.length > 0) {
  234. let msg = h5ChatItem.agentMsg.shift()
  235. if (!msg.options || msg.options.length === 0) {
  236. isOutputWelcome = 1
  237. setTimeout(() => {
  238. h5ChatItem.messages.push(msg)
  239. }, 1000)
  240. } else {
  241. if ((isOutputWelcome === 1) || (isOutputWelcome === 0)) {
  242. isOutputWelcome = 2
  243. setTimeout(() => {
  244. h5ChatItem.messages.push(msg)
  245. }, 3000)
  246. break;
  247. }
  248. }
  249. }
  250. },
  251. startCountdown(item) {
  252. if (!item.active) {
  253. return
  254. }
  255. let intervalId = setInterval(() => {
  256. let timeLeft
  257. if (item.countdownMode === '1') {
  258. // 模式1:now() - endDateTime 的时间差
  259. timeLeft = this.calculateTimePassed(item)
  260. } else if (item.countdownMode === '2') {
  261. // 模式2:timeDisplay 分钟数倒计时
  262. timeLeft = this.calculateTimeLeftFromDisplay(item)
  263. }
  264. // 不再有 total <= 0 的判断, 因为模式1是不断增加的
  265. item.days = timeLeft.days
  266. item.hours = timeLeft.hours
  267. item.minutes = timeLeft.minutes
  268. item.seconds = timeLeft.seconds
  269. }, 1000)
  270. item.intervalId = intervalId // 存储 intervalId
  271. },
  272. calculateTimePassed(item) {
  273. // 如果已经结束,直接返回
  274. if (item.isFinished) {
  275. return {
  276. days: 0,
  277. hours: 0,
  278. minutes: 0,
  279. seconds: 0
  280. }
  281. }
  282. let endTime = new Date(item.endDateTime)
  283. let now = new Date()
  284. let diff = endTime.getTime() - now.getTime() // 毫秒差, end - now
  285. if (diff <= 0) {
  286. // 如果 endDateTime 比当前时间晚, 则显示 0
  287. clearInterval(item.intervalId)
  288. item.intervalId = null
  289. item.isFinished = true
  290. return {
  291. days: 0,
  292. hours: 0,
  293. minutes: 0,
  294. seconds: 0
  295. }
  296. }
  297. return this.millisecondsToTimeObject(diff)
  298. },
  299. calculateTimeLeftFromDisplay(item) {
  300. if (!item.startTime) {
  301. item.startTime = Date.now() // 记录开始时间
  302. }
  303. let totalSeconds = item.timeDisplay * 60 // timeDisplay 是分钟数
  304. let passedSeconds = Math.floor((Date.now() - item.startTime) / 1000)
  305. let timeLeft = totalSeconds - passedSeconds
  306. if (timeLeft <= 0) {
  307. clearInterval(item.intervalId)
  308. item.intervalId = null//清除id
  309. return {
  310. days: 0,
  311. hours: 0,
  312. minutes: 0,
  313. seconds: 0
  314. }
  315. }
  316. return this.secondsToTimeObject(timeLeft)
  317. },
  318. millisecondsToTimeObject(milliseconds) {
  319. let totalSeconds = Math.floor(milliseconds / 1000)
  320. return this.secondsToTimeObject(totalSeconds)
  321. },
  322. secondsToTimeObject(totalSeconds) {
  323. let days = Math.floor(totalSeconds / (3600 * 24))
  324. let hours = Math.floor((totalSeconds % (3600 * 24)) / 3600)
  325. let minutes = Math.floor((totalSeconds % 3600) / 60)
  326. let seconds = Math.floor(totalSeconds % 60)
  327. return {
  328. days: days,
  329. hours: hours,
  330. minutes: minutes,
  331. seconds: seconds
  332. }
  333. },
  334. getAgentAvatar(item) {
  335. if (item.style.avatar) {
  336. return item.style.avatar
  337. }
  338. return this.agentAvatar
  339. },
  340. isOptionSelected(messageId, option) {
  341. let messages = this.json.find(t => t.type === 'h5-chat').messages
  342. const message = messages.find(m => m.id === messageId)
  343. return message && message.userSelection && message.userSelection.id === option.id
  344. },
  345. handleImageLoad(index, item) {
  346. if (item.classText.indexOf("footer") != -1) {
  347. const imgElement = this.$refs["myImage" + index];
  348. // 获取真实高度(包含以下两种方式)
  349. const naturalHeight = imgElement[0].naturalHeight; // 原始高度
  350. const clientHeight = imgElement[0].clientHeight; // 渲染高度
  351. this.paddingBottom = clientHeight + "px";
  352. }
  353. },
  354. confirm() {
  355. this.show = false
  356. },
  357. clickToWx(workUrl) {
  358. // 百度
  359. if (this.type == 0) {
  360. let data = {
  361. id: this.id,
  362. url: window.location.href,
  363. vid: this.vid,
  364. clickType: 67,
  365. };
  366. baiduClickCallback(data).then(e => {
  367. window.location.href = workUrl + "?customer_channel=" + this.vid;
  368. })
  369. }
  370. // 优酷
  371. if (this.type == 1) {
  372. let data = {
  373. id: this.id,
  374. url: window.location.href,
  375. vid: this.vid,
  376. aid: this.aid,
  377. clickId: this.click_id,
  378. ip: this.ip,
  379. };
  380. youkuClickCallback(data).then(e => {
  381. window.location.href = workUrl + "?customer_channel=" + this.vid;
  382. })
  383. }
  384. // 爱奇艺
  385. if (this.type == 2) {
  386. if (this.id) {
  387. let data = {
  388. url: window.location.href,
  389. id: this.id,
  390. planId: this.order_plan_id,
  391. creativeId: this.creative_id,
  392. vid: this.impress_id,
  393. sign: this.sign,
  394. clickType: "200"
  395. };
  396. iqiyiClickCallback(data).then(e => {
  397. window.location.href = workUrl + "?customer_channel=" + this.impress_id;
  398. })
  399. } else {
  400. let data = {
  401. accountId: this.accountId,
  402. url: window.location.href,
  403. no: this.no,
  404. planId: this.order_plan_id,
  405. creativeId: this.creative_id,
  406. vid: this.impress_id,
  407. sign: this.sign,
  408. clickType: "200"
  409. };
  410. iqiyiClickCallback(data).then(e => {
  411. window.location.href = workUrl + "?customer_channel=" + this.impress_id;
  412. })
  413. }
  414. }
  415. },
  416. click(item) {
  417. if (!item.addWxFun) {
  418. return
  419. }
  420. this.clickToWx(item.workUrl)
  421. }
  422. }
  423. }
  424. </script>
  425. <style lang="scss" scoped>
  426. .chat-container {
  427. width: 100%;
  428. // max-width: 500px;
  429. display: flex;
  430. flex-direction: column;
  431. background-color: #f8f8f8;
  432. font-family: Arial, sans-serif;
  433. min-height: 500px;
  434. }
  435. .chat-messages {
  436. flex: 1;
  437. overflow-y: auto;
  438. padding: 16px;
  439. display: flex;
  440. flex-direction: column;
  441. }
  442. .message-wrapper {
  443. display: flex;
  444. margin-bottom: 16px;
  445. max-width: 80%;
  446. }
  447. .agent-message {
  448. align-self: flex-start;
  449. }
  450. .user-message {
  451. align-self: flex-end;
  452. flex-direction: row-reverse;
  453. }
  454. .avatar {
  455. font-size: 14px;
  456. display: flex;
  457. padding-right: 5px;
  458. padding-left: 0;
  459. margin-bottom: 15px;
  460. }
  461. .avatar img {
  462. width: 36px;
  463. height: 36px;
  464. border-radius: 50%;
  465. }
  466. .message-content {
  467. display: flex;
  468. flex-direction: column;
  469. }
  470. .message-text {
  471. padding: 12px;
  472. border-radius: 18px;
  473. font-size: 14px;
  474. line-height: 1.4;
  475. word-break: break-word;
  476. }
  477. .agent-message .message-text {
  478. background-color: white;
  479. color: #333;
  480. border-top-left-radius: 4px;
  481. }
  482. .user-message .message-text {
  483. background-color: #4a90e2;
  484. color: white;
  485. border-top-right-radius: 4px;
  486. }
  487. .options-container {
  488. display: flex;
  489. flex-wrap: wrap;
  490. gap: 8px;
  491. margin-top: 8px;
  492. }
  493. .option-button {
  494. background-color: #4a90e2;
  495. color: white;
  496. border: none;
  497. border-radius: 18px;
  498. padding: 8px 16px;
  499. font-size: 14px;
  500. cursor: pointer;
  501. transition: background-color 0.2s;
  502. }
  503. .option-button:hover {
  504. background-color: #045dba;
  505. }
  506. .option-button.selected {
  507. background-color: #2a70c2;
  508. }
  509. .item {
  510. display: flex;
  511. flex-direction: column;
  512. flex: 1;
  513. margin: 0;
  514. width: 100%;
  515. img {
  516. width: 100%;
  517. }
  518. p {
  519. white-space: pre-line;
  520. }
  521. }
  522. .footer {
  523. position: fixed;
  524. bottom: 0;
  525. left: 0;
  526. }
  527. .countdown2-box {
  528. background: url('#') no-repeat center;
  529. background-size: 100%;
  530. width: 100%;
  531. height: 54px;
  532. line-height: 54px;
  533. color: #515A6E;
  534. font-size: 14px;
  535. text-align: center;
  536. }
  537. .countdown2-box span {
  538. display: inline-block;
  539. color: #fff;
  540. width: 22px;
  541. line-height: 20px;
  542. text-align: center;
  543. height: 20px;
  544. border-radius: 3px;
  545. margin: 7px;
  546. background: #FF5A29;
  547. }
  548. </style>