index.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <template>
  2. <div id="index">
  3. <el-row>
  4. <el-col :span="24" class="main animate__animated animate__backInRight" v-loading="loading">
  5. <div class="w_1200">
  6. <el-container>
  7. <el-aside width="400px" class="aside">
  8. <el-col :span="24" class="one">
  9. <el-col :span="24" class="one_1">
  10. <el-col :span="4" class="left">
  11. <a-avatar :size="60" style="background-color: #409eff">
  12. {{ user.nick_name }}
  13. </a-avatar>
  14. </el-col>
  15. <el-col :span="20" class="right">
  16. {{ user.nick_name || '游客' }}
  17. </el-col>
  18. </el-col>
  19. <el-col :span="24" class="one_2">
  20. <a-input placeholder="查找联系人">
  21. <template #prefix><SearchOutlined /></template>
  22. </a-input>
  23. </el-col>
  24. </el-col>
  25. <el-col :span="24" class="two">
  26. <el-col
  27. :span="24"
  28. class="list"
  29. v-for="(item, index) in chatList"
  30. :key="index"
  31. @click="toView(item)"
  32. >
  33. <div class="line" v-if="item._id == id"></div>
  34. <el-col :span="4" class="left">
  35. <a-badge :dot="item.is_read == '0'">
  36. <a-avatar :size="60" style="background-color: #409eff">
  37. {{ item.nick_name }}
  38. </a-avatar>
  39. </a-badge>
  40. </el-col>
  41. <el-col :span="20" class="right">
  42. <el-col :span="24" class="right_1">
  43. <div class="name">{{ item.nick_name }}</div>
  44. <div class="time">{{ item.send_time }}</div>
  45. </el-col>
  46. <el-col :span="24" class="right_2">{{ item.content }}</el-col>
  47. </el-col>
  48. </el-col>
  49. </el-col>
  50. </el-aside>
  51. <el-main style="background: #eee; padding: 0">
  52. <Chat></Chat>
  53. </el-main>
  54. </el-container>
  55. </div>
  56. </el-col>
  57. </el-row>
  58. <el-dialog v-model="dialog" title="智能客服">
  59. <el-col :span="24" class="dialog">
  60. <div class="content">
  61. <div class="title">智能客服为您服务</div>
  62. <div class="list">
  63. <el-image class="image" :src="kf" fit="fill" />
  64. <div class="message">Hi,遇到问题随时找智能客服哟~ 有什么需要我帮忙的吗?</div>
  65. </div>
  66. </div>
  67. <div class="foot">
  68. <textarea class="input" placeholder="输入消息"></textarea>
  69. <div class="button">
  70. <div class="send">发 送</div>
  71. </div>
  72. </div>
  73. </el-col>
  74. </el-dialog>
  75. </div>
  76. </template>
  77. <script setup>
  78. // 基础
  79. import moment from 'moment'
  80. import Chat from './parts/chat.vue'
  81. import { SearchOutlined } from '@ant-design/icons-vue'
  82. import { sendWebsocket, closeWebsocket } from '@/utils/websocket'
  83. // 接口
  84. import { ChatStore } from '@/store/api/platform/chat'
  85. import { UsersStore } from '@/store/api/user/user'
  86. const store = UsersStore()
  87. const chatstore = ChatStore()
  88. import { UserStore } from '@/store/user'
  89. const userStore = UserStore()
  90. const user = computed(() => userStore.user)
  91. // 图片引入
  92. import kf from '/images/kf.png'
  93. // 路由
  94. const route = useRoute()
  95. // 加载中
  96. const loading = ref(false)
  97. // 是否弹框客服
  98. const dialog = ref(false)
  99. // 聊天者id
  100. const id = ref(route.query.id || '')
  101. // 聊天室
  102. const chatList = ref([])
  103. // 列表
  104. const list = ref([])
  105. let skip = 0
  106. let limit = inject('limit')
  107. // 搜索
  108. const searchForm = ref({})
  109. // 个人信息
  110. const info = ref({})
  111. // 消息
  112. const textarea = ref('')
  113. // 请求
  114. onMounted(async () => {
  115. loading.value = true
  116. await search()
  117. sendWebsocket('ws://localhost:9700', wsMessage, wsError, succeed)
  118. loading.value = false
  119. })
  120. onBeforeUnmount(() => {
  121. closeWebsocket()
  122. })
  123. // ws连接成功,后台返回的ws数据,组件要拿数据渲染页面等操作
  124. const wsMessage = async (data) => {
  125. const dataJson = data
  126. if (dataJson && dataJson._id) {
  127. if (
  128. (dataJson.sender_id == user.value.id && dataJson.receiver_id == id.value) ||
  129. (dataJson.sender_id == id.value && dataJson.receiver_id == user.value.id)
  130. ) {
  131. await searchList()
  132. list.value.push(dataJson)
  133. }
  134. }
  135. }
  136. // ws连接失败,组件要执行的代码
  137. const wsError = async () => {
  138. sendWebsocket('ws://localhost:9700', wsMessage, wsError, succeed)
  139. }
  140. // ws连接成功,组件要执行的代码
  141. const succeed = async () => {
  142. console.log('ws连接成功')
  143. }
  144. const search = async () => {
  145. searchList()
  146. if (id.value) {
  147. let res = await store.fetch(id.value)
  148. if (res.errcode == '0') info.value = res.data
  149. searchChat({ skip, limit })
  150. }
  151. }
  152. const searchList = async () => {
  153. const res = await chatstore.chat()
  154. if (res.errcode == '0') chatList.value = res.data
  155. }
  156. const toView = async (item) => {
  157. id.value = item._id
  158. info.value = item
  159. await chatstore.read({ sender_id: user.value.id, receiver_id: id.value })
  160. await searchList()
  161. await searchChat({ skip, limit })
  162. }
  163. const searchChat = async (query = { skip: 0, limit }) => {
  164. const data = {
  165. skip: query.skip,
  166. limit: query.limit,
  167. user: info.value._id,
  168. ...searchForm.value
  169. }
  170. const res = await chatstore.query(data)
  171. if (res.errcode == '0' && res.data.length > 0) list.value = res.data
  172. else chatList.value.unshift(info.value)
  173. }
  174. const toSend = async () => {
  175. const data = {
  176. sender_id: user.value.id,
  177. receiver_id: id.value,
  178. type: '0',
  179. content: textarea.value,
  180. send_time: moment().format('YYYY-MM-DD HH:mm:ss')
  181. }
  182. const res = await chatstore.create(data)
  183. if (res.errcode === 0) {
  184. textarea.value = ''
  185. await searchChat({ skip, limit })
  186. }
  187. }
  188. // provide
  189. provide('id', id)
  190. provide('list', list)
  191. provide('info', info)
  192. provide('textarea', textarea)
  193. provide('toSend', toSend)
  194. </script>
  195. <style scoped lang="scss">
  196. .main {
  197. margin: 10px 0;
  198. .aside {
  199. border-radius: 5px 0 0 5px;
  200. background: #33353a;
  201. .one {
  202. .one_1 {
  203. display: flex;
  204. align-items: center;
  205. background: #292b2e;
  206. padding: 10px;
  207. .left {
  208. image {
  209. width: 50px;
  210. height: 50px;
  211. border-radius: 50%;
  212. }
  213. }
  214. .right {
  215. margin: 0 0 0 10px;
  216. white-space: nowrap;
  217. color: #fff;
  218. font-size: $global-font-size-18;
  219. }
  220. }
  221. .one_2 {
  222. padding: 10px;
  223. border-bottom: 1px solid #404247;
  224. background-color: hsla(0, 0%, 75.3%, 0.2);
  225. :deep(.ant-input-affix-wrapper) {
  226. color: #ccc !important;
  227. background-color: transparent;
  228. }
  229. :deep(.ant-input) {
  230. height: 32px;
  231. position: relative;
  232. caret-color: #fff;
  233. color: #fff;
  234. font-size: $global-font-size-16;
  235. padding: 6px 0;
  236. border: none;
  237. outline: none;
  238. background-color: transparent;
  239. &::placeholder {
  240. color: #ccc !important;
  241. }
  242. }
  243. }
  244. }
  245. .two {
  246. overflow-y: auto;
  247. height: 57vh;
  248. .list {
  249. position: relative;
  250. display: flex;
  251. padding: 10px;
  252. border-bottom: 1px solid #2c2e31;
  253. .right {
  254. margin: 5px 0 0 10px;
  255. .right_1 {
  256. display: flex;
  257. justify-content: space-between;
  258. padding: 0 10px 10px 0;
  259. .name {
  260. color: #fff;
  261. font-size: $global-font-size-16;
  262. }
  263. .time {
  264. color: #51555e;
  265. vertical-align: top;
  266. word-break: keep-all;
  267. font-size: 10px;
  268. }
  269. }
  270. .right_2 {
  271. color: #999;
  272. }
  273. }
  274. .line {
  275. position: absolute;
  276. background: #ff8f2c;
  277. width: 2px;
  278. left: 0;
  279. top: 0;
  280. height: 85px;
  281. }
  282. }
  283. .list:hover {
  284. background-color: #3a3c42;
  285. }
  286. }
  287. .two::-webkit-scrollbar {
  288. display: none;
  289. }
  290. }
  291. }
  292. :deep(.el-dialog__body) {
  293. padding: 0 !important;
  294. }
  295. .dialog {
  296. .content {
  297. padding: 20px;
  298. .title {
  299. padding-top: 23px;
  300. padding-bottom: 26px;
  301. display: flex;
  302. flex-direction: row;
  303. align-items: center;
  304. justify-content: center;
  305. font-size: $global-font-size-16;
  306. color: rgba(0, 0, 0, 0.45);
  307. }
  308. .list {
  309. display: flex;
  310. align-items: center;
  311. .image {
  312. width: 60px;
  313. height: 60px;
  314. border-radius: 60px;
  315. margin: 0 10px 0 0;
  316. }
  317. .message {
  318. position: relative;
  319. max-width: 330px;
  320. border-radius: 4px;
  321. font-size: $global-font-size-16;
  322. line-height: $global-font-size-24;
  323. box-sizing: border-box;
  324. color: rgba(0, 0, 0, 0.65);
  325. padding: 16px 11px 16px 16px;
  326. background: #ffffff;
  327. box-shadow: 0px 2px 10px 1px rgba(0, 0, 0, 0.12);
  328. }
  329. }
  330. }
  331. .foot {
  332. height: 140px;
  333. background: rgba(0, 0, 0, 0.04);
  334. border: 1px solid rgba(0, 0, 0, 0.15);
  335. padding: 13px 20px;
  336. display: flex;
  337. flex-direction: column;
  338. align-items: stretch;
  339. .input {
  340. flex: 1;
  341. height: 0;
  342. resize: none;
  343. border: none;
  344. outline: none;
  345. background: transparent;
  346. font-size: $global-font-size-16;
  347. line-height: $global-font-size-24;
  348. }
  349. .button {
  350. margin-top: 8px;
  351. flex-shrink: 0;
  352. display: flex;
  353. flex-direction: row;
  354. justify-content: flex-end;
  355. .send {
  356. cursor: pointer;
  357. color: #fff;
  358. background: #2f54eb;
  359. border-radius: 4px;
  360. width: 64px;
  361. height: 32px;
  362. font-size: $global-font-size-16;
  363. display: flex;
  364. align-items: center;
  365. justify-content: center;
  366. }
  367. }
  368. }
  369. }
  370. </style>