sessionItem.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <template>
  2. <div
  3. class="pub-chat-item"
  4. :class="{'current': item.session_id == curSession, 'top':item.is_pin==1}"
  5. @click="changeSessionId(item)"
  6. @contextmenu.prevent="onToolBtn"
  7. >
  8. <div class="avatar-wrap">
  9. <i class="mute mute-point" v-if="item.unread>0 && item.is_mute==1"></i>
  10. <i class="mute mute-num" v-else-if="item.unread>0 && item.is_mute==0">{{item.unread}}</i>
  11. <img v-if="item.cover_photo" class="user-avatar" :src="item.cover_photo" alt>
  12. <div
  13. v-else
  14. class="user-avatar"
  15. :class="`avatar_bg${bgColorNum(item.session_id)}`"
  16. :data-name="item.name && item.name.slice(0,2).toUpperCase()"
  17. ></div>
  18. <i class="icon-auth" v-if="item.is_auth==1"></i>
  19. </div>
  20. <div class="info">
  21. <h3>
  22. <i class="icon-group" v-if="item.is_group==1"></i>
  23. <span>{{item.name}}</span>
  24. </h3>
  25. <p><span v-if="item.unread>0 && item.is_mute==1">[{{item.unread}}{{$t('public.item')}}]</span>
  26. <template v-if="item.last_msg">
  27. <template v-if="item.is_group">
  28. {{item.last_msg.nick_name}}:{{item.last_msg.msg_type === 0? item.cont:item.last_msg.msg_type=== -1? $t('chat.revokeMsg'):`[${$t(`chat.msgType[${item.last_msg.msg_type - 1}]`)}]`}}
  29. </template>
  30. <template v-else>
  31. {{item.last_msg.msg_type === 0? item.cont:item.last_msg.msg_type=== -1?`${userId == item.last_msg.from ? $t('public.you'):item.last_msg.nick_name}${$t('chat.revokeMsg')}` :`[${$t(`chat.msgType[${item.last_msg.msg_type - 1}]`)}]`}}
  32. </template>
  33. </template>
  34. </p>
  35. <span class="time">{{ handleUpdate(item.update_time_int)}}</span>
  36. <i class="icon-mute" v-if="item.is_mute==1"></i>
  37. </div>
  38. <ul
  39. class="pub-pop-toolbar ext-session"
  40. v-if="item.session_id"
  41. v-show="showToolbar"
  42. :style="{left:toolBarLeft,top:toolBarTop}"
  43. >
  44. <li
  45. @click.stop="changePin(item.session_id,item.is_pin)"
  46. >{{item.is_pin==1?`${$t('public.cancel')}${$t('chat.sticky')}`:$t('chat.sticky')}}</li>
  47. <li
  48. @click.stop="changeMute(item.session_id,item.is_mute)"
  49. >{{item.is_mute==1?$t('group.cancelDisturb'):$t('group.doNotDisturb')}}</li>
  50. <li @click.stop="delSession(item.session_id)">{{$t('chat.deleteSession')}}</li>
  51. </ul>
  52. </div>
  53. </template>
  54. <script>
  55. import { mapState } from 'vuex'
  56. import API from '@/api'
  57. import { confirmPopup, formatMsgTime, getMeechatType, getAvatarBgColor } from '@/util/util'
  58. export default {
  59. name: 'msgItem',
  60. props: {
  61. item: {
  62. type: Object
  63. }
  64. },
  65. data () {
  66. return {
  67. toolBarLeft: 0,
  68. toolBarTop: 0,
  69. showToolbar: false,
  70. meechatType: getMeechatType()// meechat版本
  71. }
  72. },
  73. computed: {
  74. ...mapState(['curSession', 'userId']),
  75. userInfo () {
  76. return this.$store.state.userInfo
  77. }
  78. },
  79. methods: {
  80. changeSessionId (item) {
  81. let path =
  82. item.is_group == 1
  83. ? `/group/${item.session_id}`
  84. : `/pm/${item.session_id}`
  85. this.$router.push({ path })
  86. },
  87. bgColorNum (str) {
  88. return getAvatarBgColor(str, this.userId)
  89. },
  90. hideToolbar (event) {
  91. if (this.showToolbar !== false) {
  92. this.showToolbar = false
  93. document.body.removeEventListener('click', this.hideToolbar, false)
  94. document.body.removeEventListener('contextmenu', this.hideToolbar, false)
  95. }
  96. },
  97. getPagePos (target) {
  98. let current = target
  99. let actualX = 0
  100. let actualY = 0
  101. while (!/pub-chat-item/ig.test(current.getAttribute('class'))) {
  102. current = current.offsetParent
  103. }
  104. while (current != null) {
  105. actualX += current.offsetLeft
  106. actualY += current.offsetTop
  107. current = current.offsetParent
  108. }
  109. return {
  110. actualX, actualY
  111. }
  112. },
  113. onToolBtn (event) {
  114. if (this.showToolbar) {
  115. this.hideToolbar(event)
  116. return
  117. }
  118. let parentWidth = this.meechatType == 'h5' ? window.innerWidth : 276
  119. let popWidth = 100
  120. let { actualX } = this.getPagePos(event.target)
  121. let { pageX } = event
  122. let x = pageX - actualX
  123. let maxX = parentWidth - popWidth - 20
  124. this.toolBarLeft = x < maxX ? `${x}px` : `${maxX}px`
  125. setTimeout(() => {
  126. document.body.addEventListener('click', this.hideToolbar, false)
  127. document.body.addEventListener(
  128. 'contextmenu',
  129. this.hideToolbar,
  130. false
  131. )
  132. }, 0)
  133. this.showToolbar = true
  134. },
  135. /**
  136. * @des 置顶聊天相关操作
  137. * @param {Number}
  138. * @param {String} val {1: 取消, 0: 置顶}
  139. */
  140. async changePin (sessionId, val) {
  141. let opt = val == 0 ? 'setPin' : 'cancelPin'
  142. let optRes = val == 0 ? 'updateSessionListByPin' : 'cancelSessionListByPin'
  143. let res = await API.session[opt]({
  144. session_id: sessionId
  145. })
  146. let data = res.data.data
  147. this.$store.commit('updatePin', val)
  148. this.hideToolbar()
  149. // 更新侧边栏的顺序
  150. this.$store.commit(optRes, {
  151. session_id: sessionId,
  152. is_pin: data.is_pin,
  153. pin_time_int: data.pin_time_int
  154. })
  155. },
  156. /**
  157. * @des 消息免打扰
  158. * @param {String} val 消息免打扰{1: 取消免打扰, 0: 免打扰}
  159. */
  160. async changeMute (sessionId, val) {
  161. let opt = val == 0 ? 'setMute' : 'cancelMute'
  162. let optRes = val == 0 ? 'updateSessionListByMute' : 'cancelSessionListByMute'
  163. await API.session[opt]({
  164. session_id: sessionId
  165. })
  166. this.hideToolbar()
  167. this.$store.commit('updateMute', val)
  168. // 更新侧边栏的顺序
  169. this.$store.commit(optRes, sessionId)
  170. },
  171. // 删除会话
  172. delSession (sessionId) {
  173. confirmPopup(this.$t('chat.closeSessionMsg')).then(() => {
  174. // API.session
  175. // .deleteSession({
  176. // session_id: sessionId
  177. // })
  178. // .then(() => {
  179. // this.$store.commit('removeSessionListById', sessionId)
  180. // this.$showTips(this.$t('chat.quitSessionSucc'))
  181. // this.hideToolbar()
  182. // // 如果正在打开该会话,重置路由
  183. // if (this.curSession == sessionId) {
  184. // this.$store.commit('changeSessionId', 0)
  185. // this.$router.push({ path: '/' })
  186. // }
  187. // })
  188. this.$store.commit('removeSessionListById', sessionId)
  189. this.hideToolbar()
  190. // 如果正在打开该会话,重置路由
  191. if (this.curSession == sessionId) {
  192. this.$store.commit('changeSessionId', 0)
  193. this.$router.push({ path: '/' })
  194. }
  195. })
  196. },
  197. handleUpdate (val) {
  198. return formatMsgTime(val, 1, this)
  199. }
  200. }
  201. }
  202. </script>
  203. <style lang="scss">
  204. .pub-chat-item {
  205. padding: 10px 13px 10px 20px;
  206. height: 40px;
  207. cursor: pointer;
  208. position: relative;
  209. @include webkitbox(1);
  210. &:hover {
  211. background: #383b41;
  212. }
  213. &:before{
  214. content: "";
  215. position: absolute;
  216. top: 10px;
  217. right: 2px;
  218. display: none;
  219. @include triangle-topright(4px,#1c93ec)
  220. }
  221. &.top{
  222. &:before{
  223. display: block;
  224. }
  225. }
  226. &.current {
  227. background: #3f424c;
  228. cursor: default;
  229. }
  230. .ext {
  231. position: absolute;
  232. background-color: #f12f28;
  233. color: #fff;
  234. text-align: center;
  235. }
  236. .info {
  237. @include flex(1);
  238. position: relative;
  239. padding-right: 27px;
  240. margin-left: 10px;
  241. h3 {
  242. font-size: 13px;
  243. color: #fff;
  244. line-height: 20px;
  245. @include ellipsis;
  246. span {
  247. display: inline-block;
  248. max-width: 130px;
  249. @include ellipsis;
  250. vertical-align: middle;
  251. }
  252. .icon-group {
  253. display: inline-block;
  254. vertical-align: middle;
  255. width: 15px;
  256. height: 12px;
  257. margin-right: 4px;
  258. background: url("../../assets/icon-chat-group.png") center center;
  259. }
  260. }
  261. p {
  262. font-size: 12px;
  263. line-height: 20px;
  264. color: #7c8ca5;
  265. @include ellipsis;
  266. }
  267. .time{
  268. position: absolute;
  269. top: 0;
  270. right: 0;
  271. font-size: 12px;
  272. line-height: 20px;
  273. color: #7c8ca5;
  274. }
  275. }
  276. .icon-mute {
  277. position: absolute;
  278. right: 0;
  279. bottom: 2px;
  280. width: 12px;
  281. height: 13px;
  282. margin-left: 4px;
  283. background: url("../../assets/icon-chat-mute.png");
  284. }
  285. .avatar-wrap {
  286. position: relative;
  287. .mute {
  288. position: absolute;
  289. top: -7px;
  290. right: -7px;
  291. background: #ff0000;
  292. color: #fff;
  293. font-size: 12px;
  294. padding: 2px 5px;
  295. border-radius: 12px;
  296. z-index: 2;
  297. }
  298. .mute-point{
  299. width: 12px;
  300. height: 12px;
  301. padding: 0;
  302. border-radius: 50%;
  303. top: -6px;
  304. right: -6px;
  305. }
  306. .icon-auth {
  307. position: absolute;
  308. bottom: 0;
  309. right: 0;
  310. width: 14px;
  311. height: 13px;
  312. background: url("../../assets/icon-chat-auth.png");
  313. }
  314. }
  315. img.user-avatar {
  316. display: block;
  317. }
  318. }
  319. .h5-wrap{
  320. .pub-chat-item{
  321. padding: px2rem(24) px2rem(32);
  322. height: px2rem(90);
  323. &:before{
  324. top: 0;
  325. right: 0;
  326. }
  327. &.current {
  328. background-color: #fff;
  329. }
  330. &:hover{
  331. background-color: #f8f8f8;
  332. }
  333. .time{
  334. font-size: px2rem(24);
  335. color: #999999;
  336. }
  337. .info {
  338. position: relative;
  339. margin-left: px2rem(28);
  340. h3 {
  341. font-size: px2rem(32);
  342. color: #020202;
  343. margin-bottom: px2rem(14);
  344. line-height: px2rem(40);
  345. .icon-mute{
  346. width: px2rem(24);
  347. height: px2rem(26);
  348. background-size: 100%;
  349. background: url(../../assets/icon-chat-mute.png) 0 center /100% no-repeat;
  350. }
  351. .icon-group{
  352. width: px2rem(32);
  353. height: px2rem(28);
  354. background: url(../../assets/h5/icon-chat-group.png) 0 center /100% no-repeat;
  355. }
  356. span{
  357. max-width: px2rem(390);
  358. }
  359. }
  360. p {
  361. font-size: px2rem(24);
  362. color: #9b9b9b;
  363. line-height: px2rem(40);
  364. }
  365. }
  366. .ext {
  367. right: -px2rem(15);
  368. top: -px2rem(15);
  369. padding: 0 px2rem(8);
  370. height: px2rem(30);
  371. border-radius: px2rem(15);
  372. line-height: px2rem(30);
  373. font-size: px2rem(24);
  374. }
  375. &::after {
  376. content: "";
  377. pointer-events: none;
  378. box-sizing: border-box;
  379. position: absolute;
  380. width: 200%;
  381. height: 200%;
  382. left: px2rem(24+90+28);
  383. top: 0;
  384. border-bottom: 1px solid #d8d8d8;
  385. transform: scale(0.5);
  386. transform-origin: 0 0;
  387. }
  388. }
  389. }
  390. </style>