sessionItem.vue 11 KB

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