session.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <template>
  2. <div
  3. class="cl-chat-session"
  4. :class="{
  5. 'is-position': browser.isMini,
  6. 'is-show': sessionVisible
  7. }"
  8. >
  9. <!-- 关键字搜索 -->
  10. <div class="cl-chat-session__search">
  11. <el-input
  12. v-model="keyWord"
  13. placeholder="搜索"
  14. prefix-icon="el-icon-search"
  15. size="small"
  16. clearable
  17. @clear="onSearch"
  18. @keyup.enter.native="onSearch"
  19. ></el-input>
  20. </div>
  21. <!-- 会话列表 -->
  22. <ul class="cl-chat-session__list scroller1" v-loading="loading">
  23. <li
  24. class="cl-chat-session__item"
  25. v-for="(item, index) in list"
  26. :key="index"
  27. :class="{
  28. 'is-active': session ? item.id == session.id : false
  29. }"
  30. @click="toDetail(item)"
  31. @contextmenu.stop.prevent="openCM($event, item.id, index)"
  32. >
  33. <!-- 头像 -->
  34. <div class="avatar">
  35. <el-badge
  36. :value="item.serviceUnreadCount"
  37. :hidden="item.serviceUnreadCount === 0"
  38. :max="99"
  39. >
  40. <img :src="item.headimgurl" alt="" />
  41. </el-badge>
  42. </div>
  43. <!-- 昵称,内容 -->
  44. <div class="det">
  45. <p class="name">{{ item.nickname }}</p>
  46. <p class="content">{{ item.lastMessage }}</p>
  47. </div>
  48. </li>
  49. </ul>
  50. </div>
  51. </template>
  52. <script>
  53. import { mapGetters, mapMutations } from "vuex";
  54. import { isEmpty } from "cl-admin/utils";
  55. import { ContextMenu } from "cl-admin-crud";
  56. import { parseContent } from "../utils";
  57. import eventBus from "../utils/event-bus";
  58. export default {
  59. data() {
  60. return {
  61. loading: false,
  62. pagination: {
  63. page: 1,
  64. size: 100,
  65. total: 0
  66. },
  67. keyWord: ""
  68. };
  69. },
  70. computed: {
  71. ...mapGetters(["sessionList", "session", "browser", "sessionVisible"]),
  72. // 列表数据
  73. list() {
  74. return this.sessionList
  75. .map(e => {
  76. const { _text } = parseContent(e);
  77. e.lastMessage = _text;
  78. return e;
  79. })
  80. .sort((a, b) => {
  81. return a.updateTime < b.updateTime ? 1 : -1;
  82. });
  83. }
  84. },
  85. created() {
  86. // 监听列表刷新
  87. eventBus.$on("session.refresh", this.refresh);
  88. // PC 端下首次请求读取第一个消息
  89. this.refresh().then(res => {
  90. if (!isEmpty(res.list) && !this.browser.isMini) {
  91. this.SET_SESSION(res.list[0]);
  92. }
  93. });
  94. },
  95. methods: {
  96. ...mapMutations(["SET_SESSION_LIST", "SET_SESSION", "CLEAR_SESSION", "CLOSE_SESSION"]),
  97. // 右键菜单
  98. openCM(e, id, index) {
  99. ContextMenu.open(e, {
  100. list: [
  101. {
  102. label: "删除",
  103. icon: "el-icon-delete",
  104. callback: (_, done) => {
  105. this.$service.im.session.delete({
  106. ids: id
  107. });
  108. this.list.splice(index, 1);
  109. if (id == this.session.id) {
  110. this.toDetail();
  111. }
  112. done();
  113. }
  114. }
  115. ]
  116. });
  117. },
  118. // 刷新列表
  119. refresh(params) {
  120. this.loading = true;
  121. return new Promise((resolve, reject) => {
  122. this.$service.im.session
  123. .page({
  124. ...this.pagination,
  125. keyWord: this.keyWord,
  126. params,
  127. order: "updateTime",
  128. sort: "desc"
  129. })
  130. .then(res => {
  131. this.SET_SESSION_LIST(res.list);
  132. this.pagination = res.pagination;
  133. resolve(res);
  134. })
  135. .catch(err => {
  136. this.$message.error(err);
  137. reject(err);
  138. })
  139. .done(() => {
  140. this.loading = false;
  141. });
  142. });
  143. },
  144. // 搜索关键字
  145. onSearch() {
  146. this.refresh({ page: 1 });
  147. },
  148. // 会话详情
  149. toDetail(item) {
  150. if (item) {
  151. // 点击关闭弹窗
  152. if (this.browser.isMini) this.CLOSE_SESSION();
  153. // 设置为当前会话
  154. if (!this.session || this.session.id != item.id) {
  155. this.SET_SESSION(item);
  156. }
  157. } else {
  158. this.CLEAR_SESSION();
  159. }
  160. }
  161. }
  162. };
  163. </script>
  164. <style lang="scss" scoped>
  165. .cl-chat-session {
  166. height: 100%;
  167. width: 0;
  168. transition: width 0.2s ease-in-out;
  169. border-radius: 5px;
  170. background-color: #fff;
  171. overflow: hidden;
  172. &.is-show {
  173. width: 250px;
  174. max-width: 100%;
  175. margin-right: 5px;
  176. }
  177. &.is-position {
  178. position: absolute;
  179. left: 5px;
  180. top: 51px;
  181. height: calc(100% - 56px);
  182. z-index: 3000;
  183. &.is-show {
  184. width: calc(100% - 10px);
  185. }
  186. }
  187. &__search {
  188. padding: 10px;
  189. }
  190. &__list {
  191. height: calc(100% - 52px);
  192. overflow: auto;
  193. li {
  194. display: flex;
  195. list-style: none;
  196. padding: 10px;
  197. border-left: 5px solid #fff;
  198. .avatar {
  199. margin-right: 12px;
  200. img {
  201. display: block;
  202. height: 40px;
  203. width: 40px;
  204. border-radius: 3px;
  205. background-color: #eee;
  206. }
  207. .el-badge {
  208. &__content {
  209. height: 14px;
  210. line-height: 14px;
  211. padding: 0 4px;
  212. background-color: #fa5151;
  213. border: 0;
  214. }
  215. }
  216. }
  217. .det {
  218. flex: 1;
  219. .name {
  220. font-size: 13px;
  221. margin-top: 1px;
  222. }
  223. .content {
  224. font-size: 12px;
  225. margin-top: 5px;
  226. color: #666;
  227. }
  228. .name,
  229. .content {
  230. overflow: hidden;
  231. text-overflow: ellipsis;
  232. display: -webkit-box;
  233. -webkit-box-orient: vertical;
  234. -webkit-line-clamp: 1;
  235. }
  236. }
  237. &.is-active {
  238. background-color: #eee;
  239. border-color: $color-primary;
  240. }
  241. &:hover {
  242. background-color: #eee;
  243. cursor: pointer;
  244. }
  245. }
  246. }
  247. &__empty {
  248. text-align: center;
  249. margin-top: 10px;
  250. }
  251. }
  252. </style>