index.vue 15 KB

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