sessionItem.vue 11 KB

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