index.vue 15 KB

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