chat.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>领取奖励</title>
  8. <!--引入 element-ui 的样式,-->
  9. <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
  10. <!-- 必须先引入vue, 后使用element-ui -->
  11. <script src="./js/vue.js"></script>
  12. <!-- 引入element 的组件库-->
  13. <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  14. <script src="https://unpkg.com/vconsole/dist/vconsole.min.js"></script>
  15. <!-- <script>
  16. var vConsole = new window.VConsole();
  17. </script> -->
  18. </head>
  19. <style>
  20. body {
  21. margin: 0;
  22. padding: 0;
  23. }
  24. .box {
  25. width: 100vw;
  26. height: 100vh;
  27. box-sizing: border-box;
  28. background: #FAFAFA;
  29. }
  30. .page5 {
  31. width: 100vw;
  32. height: calc(100vh - 63px);
  33. box-sizing: border-box;
  34. padding: 18px 15px;
  35. overflow-y: auto;
  36. /* overflow-x: unset; */
  37. }
  38. .chat_list {
  39. display: flex;
  40. align-items: center;
  41. }
  42. .custom_msg {
  43. display: flex;
  44. align-items: center;
  45. justify-content: flex-start;
  46. padding-bottom: 22px;
  47. box-sizing: border-box;
  48. width: 100%;
  49. }
  50. .my_msg {
  51. display: flex;
  52. align-items: center;
  53. justify-content: flex-end;
  54. padding-bottom: 22px;
  55. box-sizing: border-box;
  56. width: 100%;
  57. }
  58. .customchat_box {
  59. padding: 10px 12px;
  60. background: #FFFFFF;
  61. border-radius: 10px;
  62. font-weight: 400;
  63. font-size: 14px;
  64. color: #222222;
  65. max-width: 227px;
  66. word-wrap: break-word;
  67. white-space: normal;
  68. }
  69. .mychat_box {
  70. padding: 10px 12px;
  71. background: #1677FF;
  72. border-radius: 10px;
  73. font-weight: 400;
  74. font-size: 14px;
  75. color: #FFFFFF;
  76. max-width: 227px;
  77. word-wrap: break-word;
  78. white-space: normal;
  79. }
  80. .chatbox_left {
  81. width: 6px;
  82. height: 12px;
  83. margin-left: 5px;
  84. }
  85. .chatbox_right {
  86. width: 10px;
  87. height: 12px;
  88. margin-left: -5px;
  89. margin-right: 5px;
  90. }
  91. .headImg {
  92. width: 44px;
  93. height: 44px;
  94. border-radius: 50%;
  95. }
  96. .chat_img {
  97. max-width: 200px;
  98. border-radius: 10px;
  99. }
  100. .input_style {
  101. padding: 10px;
  102. box-sizing: border-box;
  103. position: relative;
  104. }
  105. .el-input__inner {
  106. border-radius: 22px;
  107. height: 43px;
  108. padding-right: 60px;
  109. }
  110. .send_btn {
  111. width: 50px;
  112. height: 33px;
  113. position: absolute;
  114. right: 15px;
  115. top: 15px;
  116. }
  117. </style>
  118. <body>
  119. <div id="box" class="box">
  120. <!-- 聊天页 -->
  121. <div ref="messageContent" class="page5">
  122. <div class="chat_list" v-for="(item, index) in chatList" :key="index">
  123. <div class="my_msg" v-if="item.self">
  124. <div class="mychat_box" v-if="item.contentType === 0" v-html="item.content"></div>
  125. <image class="chat_img" mode="aspectFit" v-else-if="item.contentType === 1" :src="item.content"></image>
  126. <div class="mychat_box" v-else-if="item.contentType === 5" v-html="JSON.parse(item.content).curl"></div>
  127. <div class="mychat_box" v-else-if="item.contentType === 33">小程序信息(不支持查看)</div>
  128. <div class="mychat_box" v-else-if="item.contentType === 51">视频号信息(不支持查看)</div>
  129. <div class="mychat_box" v-else-if="item.contentType === 100">图片已过期(不支持查看)</div>
  130. <div class="mychat_box" v-else v-html="item.content"></div>
  131. <image class="chatbox_right" src="./img/chatbox_right.png"></image>
  132. <image class="headImg" :src="item.sendUrl"></image>
  133. </div>
  134. <div class="custom_msg" v-else>
  135. <image class="headImg" :src="item.sendUrl"></image>
  136. <image class="chatbox_left" src="./img/chatbox_left.png"></image>
  137. <div class="customchat_box" v-if="item.contentType === 0" v-html="item.content"></div>
  138. <image class="chat_img" mode="aspectFit" v-else-if="item.contentType === 1" :src="item.content"></image>
  139. <div class="customchat_box" v-else-if="item.contentType === 5" v-html="JSON.parse(item.content).curl"></div>
  140. <div class="customchat_box" v-else-if="item.contentType === 33">小程序信息(不支持查看)</div>
  141. <div class="customchat_box" v-else-if="item.contentType === 51">视频号信息(不支持查看)</div>
  142. <div class="customchat_box" v-else-if="item.contentType === 100">图片已过期(不支持查看)</div>
  143. <div class="customchat_box" v-else v-html="item.content"></div>
  144. </div>
  145. </div>
  146. </div>
  147. <div class="input_style">
  148. <el-input
  149. placeholder="请输入内容"
  150. v-model="msgData">
  151. </el-input>
  152. <image class="send_btn" :src="msgData ? './img/send_btn.png' : './img/no_send_btn.png'" @click="sendMsg"></image>
  153. </div>
  154. </div>
  155. </body>
  156. <script>
  157. new Vue({
  158. el: '#box',
  159. data() {
  160. return {
  161. httpUrl: '',
  162. memberId: null,
  163. corpId: null,
  164. sessionId: null,
  165. chatList: [],
  166. msgData: '',
  167. pollInterval: null,
  168. }
  169. },
  170. created() {
  171. this.httpUrl = this.getQueryParam('httpUrl')
  172. this.corpId = this.getQueryParam('corpId')
  173. this.memberId = this.getQueryParam('memberId')
  174. this.sessionId = this.getQueryParam('sessionId')
  175. this.chatData()
  176. this.startPolling()
  177. },
  178. beforeDestroy() {
  179. // 当组件销毁前停止轮询
  180. this.stopPolling();
  181. },
  182. methods: {
  183. // 截取url中的数据
  184. getQueryParam (paramName) {
  185. // 获取当前URL的查询字符串部分
  186. const queryString = window.location.search;
  187. // 创建一个URLSearchParams对象
  188. const urlParams = new URLSearchParams(queryString);
  189. // 返回指定参数的值,如果不存在则返回null
  190. return urlParams.get(paramName);
  191. },
  192. startPolling() {
  193. // 这里写你的轮询逻辑,比如发送API请求
  194. this.intervalId = setInterval(() => {
  195. this.chatData()
  196. }, 5000); // 每5秒发起一次请求
  197. },
  198. chatData () {
  199. fetch(this.httpUrl + '/scrm/v1/wxcp-live-code-counselor/p/messageListByPage', {
  200. method: 'post',
  201. body: JSON.stringify({
  202. corpId: this.corpId,
  203. memberId: this.memberId,
  204. sessionId: this.sessionId,
  205. page: 1,
  206. pageCount: 1000,
  207. }),
  208. headers: {
  209. 'Content-Type': 'application/json'
  210. }
  211. }).then(res => {
  212. return res.json()
  213. }).then(result => {
  214. let { data, code, msg } = result
  215. if (data) {
  216. this.chatList = data.records.reverse()
  217. this.$nextTick(() => {
  218. this.$refs.messageContent.scrollTop = this.$refs.messageContent.scrollHeight
  219. })
  220. } else {
  221. this.$message.error(msg)
  222. }
  223. })
  224. },
  225. sendMsg () {
  226. fetch(this.httpUrl + '/scrm/v1/wxcp-live-code-counselor/p/sendMessage', {
  227. method: 'post',
  228. body: JSON.stringify({
  229. corpId: this.corpId,
  230. memberId: this.memberId,
  231. sessionId: this.sessionId,
  232. text: this.msgData,
  233. type: 0
  234. }),
  235. headers: {
  236. 'Content-Type': 'application/json'
  237. }
  238. }).then(res => {
  239. return res.json()
  240. }).then(result => {
  241. let { data, code, msg } = result
  242. if (code === 1) {
  243. this.msgData = ''
  244. this.chatData()
  245. } else {
  246. this.$message.error(msg)
  247. }
  248. })
  249. },
  250. stopPolling() {
  251. // 清除定时器
  252. if (this.pollInterval) {
  253. clearInterval(this.pollInterval);
  254. this.pollInterval = null;
  255. }
  256. },
  257. }
  258. })
  259. </script>
  260. </html>