index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <template>
  2. <div id="index">
  3. <el-input v-model="customer_id" placeholder=""></el-input>
  4. <el-input v-model="customer_name" placeholder=""></el-input>
  5. <el-button type="primary" @click="toChannel()">连接</el-button>
  6. <el-row>
  7. <el-col :span="24" class="main">
  8. <el-col :span="4" class="left">
  9. <el-col :span="24" class="left_1">
  10. <el-input placeholder="请输入查询内容" v-model="searchName" class="input-with-select">
  11. <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
  12. </el-input>
  13. </el-col>
  14. <el-col :span="24" class="left_2">
  15. <el-col
  16. :span="24"
  17. class="list"
  18. :class="`${index === activeIndex ? 'active' : ''}`"
  19. v-for="(item, index) in userList"
  20. :key="index"
  21. @click.native="chatBtn(item, index)"
  22. >
  23. <el-col :span="4" class="icon">
  24. <el-image :src="leftIcon"></el-image>
  25. </el-col>
  26. <el-col :span="20" class="info">
  27. <el-col :span="24" class="name textOver">
  28. {{ item.client_name }}
  29. </el-col>
  30. <el-col :span="24" class="date textOver">
  31. {{ getTime(item) }}
  32. </el-col>
  33. </el-col>
  34. </el-col>
  35. </el-col>
  36. </el-col>
  37. <el-col :span="20" class="right">
  38. <el-col :span="24" class="right_1">
  39. <el-col :span="1" class="icon">
  40. <el-image :src="leftIcon"></el-image>
  41. </el-col>
  42. <el-col :span="23" class="info">
  43. <el-col :span="24" class="name">
  44. {{ chatUserInfo.client_name }}
  45. </el-col>
  46. <el-col :span="24" class="date textOver">
  47. {{ getTime(chatUserInfo) }}
  48. </el-col>
  49. </el-col>
  50. </el-col>
  51. <el-col :span="24" class="right_2">
  52. <el-col :span="24" class="list" v-for="(item, index) in chatList" :key="index">
  53. <template v-if="isSender(item, index)">
  54. <el-col :span="24" class="list_left">
  55. <el-col :span="1" class="icon">
  56. <el-image :src="leftIcon"></el-image>
  57. </el-col>
  58. <el-col :span="23" class="info">
  59. <el-col :span="24" class="name">{{ item.sender_name }}[{{ getTime(item) }}]</el-col>
  60. <el-col :span="24" class="list_leftCont">
  61. {{ item.content }}
  62. </el-col>
  63. </el-col>
  64. </el-col>
  65. </template>
  66. <template v-else>
  67. <el-col :span="24" class="list_left list_right">
  68. <el-col :span="23" class="info">
  69. <el-col :span="24" class="name">[{{ getTime(item) }}]{{ item.sender_name }}</el-col>
  70. <el-col :span="24" class="list_leftCont">
  71. {{ item.content }}
  72. </el-col>
  73. </el-col>
  74. <el-col :span="1" class="icon">
  75. <el-image :src="rightIcon"></el-image>
  76. </el-col>
  77. </el-col>
  78. </template>
  79. </el-col>
  80. </el-col>
  81. <el-col :span="24" class="right_3">
  82. <el-col :span="24" class="right_3cont">
  83. <el-input type="textarea" placeholder="请输入内容" :autosize="{ minRows: 3, maxRows: 5 }" v-model="content"> </el-input>
  84. </el-col>
  85. <el-col :span="24" class="btn">
  86. <el-button type="primary" @click="send">发送</el-button>
  87. </el-col>
  88. </el-col>
  89. </el-col>
  90. </el-col>
  91. </el-row>
  92. </div>
  93. </template>
  94. <script>
  95. const _ = require('lodash');
  96. const moment = require('moment');
  97. import { mapState, createNamespacedHelpers } from 'vuex';
  98. const { mapActions: customer } = createNamespacedHelpers('customer');
  99. export default {
  100. name: 'index',
  101. props: {},
  102. components: {},
  103. data: function() {
  104. return {
  105. // 查询好友
  106. searchName: '',
  107. // 客服头像
  108. leftIcon: require('@a/rightIcon.png'),
  109. // 用户头像
  110. rightIcon: require('@a/leftIcon.png'),
  111. // 用户列表
  112. activeIndex: '',
  113. userList: [],
  114. // 聊天列表
  115. chatUserInfo: {},
  116. chatList: [],
  117. content: '',
  118. customer_id: '5f6ad08fbe2d3b407b41a693',
  119. customer_name: '客服1',
  120. };
  121. },
  122. created() {
  123. // this.toOnline();
  124. this.getClientList();
  125. },
  126. mounted() {
  127. // this.channel();
  128. },
  129. methods: {
  130. ...customer(['online', 'record', 'createChat', 'chatRecord']),
  131. search() {},
  132. // 点击获取客户聊天列表
  133. async chatBtn(data, index) {
  134. this.$set(this, `activeIndex`, index);
  135. this.$set(this, `chatUserInfo`, data);
  136. let arr = await this.chatRecord({ client_id: data.client_id, customer_id: data.customer_id });
  137. if (this.$checkRes(arr)) {
  138. this.$set(this, `chatList`, arr.data);
  139. this.scrollToBottom();
  140. }
  141. this.chatChannel();
  142. },
  143. // 区分发言人
  144. isSender(data) {
  145. return data.sender_id != this.user.id;
  146. },
  147. // 查询客服列表
  148. channel() {
  149. this.$stomp({
  150. [`/exchange/${this._tenant}/customer.${this.user.id}`]: this.onMessage,
  151. });
  152. },
  153. onMessage(message) {
  154. // customer/5f6ad08fbe2d3b407b41a693
  155. console.log('receive a message: ', message.body);
  156. if (message.body === 'research') this.getClientList();
  157. },
  158. async getClientList() {
  159. const res = await this.record({ _tenant: this._tenant, customer_id: this.user.id });
  160. if (this.$checkRes(res)) {
  161. this.$set(this, `userList`, res.data);
  162. }
  163. },
  164. async toOnline() {
  165. console.log('in function:');
  166. const time = 5000; //1min 600000
  167. setInterval(() => {
  168. this.online({ _tenant: this._tenant, customer_id: this.customer_id, customer_name: this.customer_name });
  169. }, time);
  170. },
  171. getTime(item) {
  172. const ct = _.get(item.meta, 'createdAt');
  173. if (!ct) return moment().format('YYYY-MM-DD HH:mm:ss');
  174. return moment(ct).format('YYYY-MM-DD HH:mm:ss');
  175. },
  176. // 发言
  177. async send() {
  178. if (!this.content) {
  179. this.$message({
  180. message: '发言内容不能为空',
  181. type: 'warning',
  182. });
  183. } else {
  184. console.log(this.chatUserInfo);
  185. let data = {
  186. _tenant: this._tenant,
  187. client_id: this.chatUserInfo.client_id,
  188. customer_id: this.chatUserInfo.customer_id,
  189. sender_id: this.chatUserInfo.customer_id,
  190. sender_name: this.user.name,
  191. content: this.content,
  192. };
  193. let res = await this.createChat(data);
  194. if (this.$checkRes(res)) {
  195. this.$message({
  196. message: '消息发送成功',
  197. type: 'success',
  198. });
  199. this.content = '';
  200. }
  201. }
  202. },
  203. chatChannel() {
  204. this.$stomp({
  205. [`/exchange/${this._tenant}/customer.${this.chatUserInfo.client_id}.${this.chatUserInfo.customer_id}`]: this.chatMessage,
  206. });
  207. },
  208. chatMessage(message) {
  209. let body = _.get(message, 'body');
  210. if (body) {
  211. body = JSON.parse(body);
  212. this.chatList.push(body);
  213. this.scrollToBottom();
  214. }
  215. },
  216. scrollToBottom: function() {
  217. this.$nextTick(() => {
  218. var container = this.$el.querySelector('.right_2');
  219. container.scrollTop = container.scrollHeight;
  220. });
  221. },
  222. toChannel() {
  223. this.toOnline();
  224. this.$stomp({
  225. [`/exchange/${this._tenant}/customer.${this.customer_id}`]: this.onMessage,
  226. });
  227. },
  228. },
  229. computed: {
  230. ...mapState(['user', '_tenant']),
  231. },
  232. metaInfo() {
  233. return { title: this.$route.meta.title };
  234. },
  235. watch: {
  236. test: {
  237. deep: true,
  238. immediate: true,
  239. handler(val) {},
  240. },
  241. },
  242. };
  243. </script>
  244. <style lang="less" scoped>
  245. .main {
  246. border: 1px solid #cccccc;
  247. .left {
  248. border-right: 1px solid #cccccc;
  249. .left_1 {
  250. height: 40px;
  251. overflow: hidden;
  252. /deep/.el-input--small .el-input__inner {
  253. height: 40px;
  254. line-height: 40px;
  255. }
  256. }
  257. .left_2 {
  258. height: 700px;
  259. overflow-x: hidden;
  260. overflow-y: auto;
  261. .list {
  262. border-bottom: 1px dashed #cccccc;
  263. padding: 8px 5px;
  264. .icon {
  265. text-align: center;
  266. .el-image {
  267. /deep/.el-image__inner {
  268. width: 40px;
  269. height: 40px;
  270. border-radius: 90px;
  271. }
  272. }
  273. }
  274. .info {
  275. .name {
  276. font-size: 14px;
  277. font-weight: bold;
  278. color: #000;
  279. margin: 0 0 5px 0;
  280. }
  281. .date {
  282. font-size: 12px;
  283. color: #666;
  284. }
  285. }
  286. }
  287. .list:hover {
  288. cursor: pointer;
  289. background-color: #f9f9f9;
  290. .info {
  291. .name {
  292. color: #409eff;
  293. }
  294. }
  295. }
  296. .active {
  297. background-color: #f9f9f9;
  298. .info {
  299. .name {
  300. color: #409eff;
  301. }
  302. }
  303. }
  304. }
  305. }
  306. .right {
  307. height: 740px;
  308. overflow: hidden;
  309. .right_1 {
  310. height: 40px;
  311. overflow: hidden;
  312. background-color: #000;
  313. .icon {
  314. text-align: center;
  315. height: 40px;
  316. padding: 5px 0;
  317. .el-image {
  318. /deep/.el-image__inner {
  319. width: 30px;
  320. height: 30px;
  321. border-radius: 90px;
  322. }
  323. }
  324. }
  325. .info {
  326. color: #ffffff;
  327. .name {
  328. margin: 2px 0;
  329. font-size: 14px;
  330. font-weight: bold;
  331. }
  332. .date {
  333. font-size: 12px;
  334. color: #ccc;
  335. }
  336. }
  337. }
  338. .right_2 {
  339. height: 600px;
  340. overflow-x: hidden;
  341. overflow-y: auto;
  342. background-color: #f7f7f7;
  343. .list {
  344. padding: 5px;
  345. margin: 0 0 10px 0;
  346. .list_left {
  347. .icon {
  348. text-align: center;
  349. .el-image {
  350. /deep/.el-image__inner {
  351. width: 40px;
  352. height: 40px;
  353. border-radius: 90px;
  354. }
  355. }
  356. }
  357. .info {
  358. .name {
  359. font-size: 12px;
  360. margin: 0 0 5px 0;
  361. }
  362. .list_leftCont {
  363. background-color: #ffffff;
  364. border-radius: 5px;
  365. padding: 5px;
  366. font-size: 15px;
  367. color: #000000;
  368. line-height: 25px;
  369. overflow-y: unset;
  370. }
  371. }
  372. }
  373. .list_right {
  374. text-align: right;
  375. .info {
  376. .list_leftCont {
  377. background-color: #409eff;
  378. color: #ffffff;
  379. }
  380. }
  381. }
  382. }
  383. }
  384. .right_3 {
  385. height: 100px;
  386. border-top: 1px solid #cccccc;
  387. .btn {
  388. text-align: right;
  389. }
  390. }
  391. }
  392. }
  393. </style>