sessionItem.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <template>
  2. <div class="chat-item"
  3. :class="{'current': item.session_id == curSession, 'moblie': isMoblie}"
  4. @click="changeSessionId(item)"
  5. @contextmenu.prevent="onToolBtn">
  6. <div class="avatar-wrap">
  7. <i class="ext" v-if="item.unread>0">{{item.unread}}</i>
  8. <img v-if="item.cover_photo" class="user-avatar" :src="item.cover_photo" alt="">
  9. <div v-else class="user-avatar"
  10. :class="`avatar_bg${bgColorNum(item.session_id)}`"
  11. :data-name="item.name && item.name.slice(0,2).toUpperCase()">
  12. </div>
  13. </div>
  14. <div class="info">
  15. <h3>{{item.name}}</h3>
  16. <p>{{item.cont}}</p>
  17. </div>
  18. <ul
  19. class="pub-pop-toolbar ext-session"
  20. v-if="item.session_id"
  21. v-show="showToolbar"
  22. :style="{left:toolBarX,top:toolBarY,bottom:toolBarBottom}"
  23. >
  24. <li class="split-line" @click.stop="changePin(item.session_id,item.is_pin)">{{item.is_pin==1?"取消置顶":"置顶"}}</li>
  25. <li class="split-line" @click.stop="changeMute(item.session_id,item.is_mute)">{{item.is_mute==1?"取消免打扰":"消息免打扰"}}</li>
  26. <li @click.stop="delSession(item.session_id)">删除会话</li>
  27. </ul>
  28. </div>
  29. </template>
  30. <script>
  31. import { mapState, mapMutations } from 'vuex'
  32. import API from '@/api'
  33. import { confirmPopup } from '@/util/util'
  34. export default {
  35. name: 'msgItem',
  36. props: {
  37. item: {
  38. type: Object
  39. },
  40. isMoblie: {
  41. type: Boolean,
  42. default: false
  43. }
  44. },
  45. data () {
  46. return {
  47. toolBarX: 0,
  48. toolBarY: 0,
  49. toolBarBottom: 0,
  50. showToolbar: false
  51. }
  52. },
  53. computed: {
  54. ...mapState(['curSession', 'userId']),
  55. ...mapMutations(['setSessionItemUnread']),
  56. userInfo () {
  57. return this.$store.state.userInfo
  58. }
  59. },
  60. methods: {
  61. changeSessionId (item) {
  62. let path = item.is_group == 1 ? `/group/${item.session_id}` : `/pm/${item.session_id}`
  63. this.$router.push({ path })
  64. this.$store.commit('setSessionItemUnread', {
  65. session_id: item.session_id,
  66. unread: 0,
  67. curSession: this.curSession
  68. })
  69. },
  70. bgColorNum (str) {
  71. if (str.match('-')) {
  72. let num = 0
  73. str.split('-').forEach(e => {
  74. if (e !== this.userId) {
  75. num = e % 9
  76. }
  77. })
  78. return num
  79. } else {
  80. return str % 9
  81. }
  82. },
  83. hideToolbar (event) {
  84. if (this.showToolbar !== false) {
  85. this.showToolbar = false
  86. document.body.removeEventListener('click', this.hideToolbar, false)
  87. document.body.removeEventListener('contextmenu', this.hideToolbar, false)
  88. }
  89. },
  90. onToolBtn (event) {
  91. if (this.showToolbar) {
  92. this.hideToolbar(event)
  93. return
  94. }
  95. let winWidth = window.innerWidth
  96. let winHeight = window.innerHeight
  97. let clientX, clientY
  98. if (event instanceof MouseEvent) {
  99. clientX = event.clientX
  100. clientY = event.clientY
  101. this.toolBarX = event.layerX > 170 ? 170 : event.layerX + 20
  102. this.toolBarY = event.layerY
  103. setTimeout(() => {
  104. document.body.addEventListener('click', this.hideToolbar, false)
  105. document.body.addEventListener('contextmenu', this.hideToolbar, false)
  106. }, 0)
  107. }
  108. if (clientX > winWidth * 0.5) {
  109. this.toolBarX = this.toolBarX - 120
  110. }
  111. this.toolBarX += 'px'
  112. if (clientY > winHeight / 2) {
  113. this.toolBarBottom = this.toolBarY + 'px'
  114. this.toolBarY = 'auto'
  115. } else {
  116. this.toolBarBottom = 'auto'
  117. this.toolBarY += 'px'
  118. }
  119. this.showToolbar = true
  120. },
  121. /**
  122. * @des 置顶聊天相关操作
  123. * @param {Number}
  124. * @param {String} val {1: 取消, 0: 置顶}
  125. */
  126. changePin (sessionId, val) {
  127. if (val == 0) {
  128. API.session.setPin({
  129. session_id: sessionId
  130. }).then(() => {
  131. if (this.curSession == sessionId) this.$store.commit('updatePin', '1')
  132. // 更新侧边栏的顺序
  133. this.$store.commit('updateSessionListByPin', sessionId)
  134. this.hideToolbar()
  135. })
  136. } else {
  137. API.session.cancelPin({
  138. session_id: sessionId
  139. }).then(() => {
  140. if (this.curSession == sessionId) this.$store.commit('updatePin', '0')
  141. // 更新侧边栏的顺序
  142. this.$store.commit('cancelSessionListByPin', sessionId)
  143. this.hideToolbar()
  144. })
  145. }
  146. },
  147. /**
  148. * @des 消息免打扰
  149. * @param {String} val 消息免打扰{1: 取消免打扰, 0: 免打扰}
  150. */
  151. changeMute (sessionId, val) {
  152. if (val == 0) {
  153. API.session.setMute({
  154. session_id: sessionId
  155. }).then(() => {
  156. if (this.curSession == sessionId) this.$store.commit('updateMute', '1')
  157. this.$store.commit('updateSessionListByMute', sessionId)
  158. this.hideToolbar()
  159. })
  160. } else {
  161. API.session.cancelMute({
  162. session_id: sessionId
  163. }).then(() => {
  164. if (this.curSession == sessionId) this.$store.commit('updateMute', '0')
  165. this.$store.commit('cancelSessionListByMute', sessionId)
  166. this.hideToolbar()
  167. })
  168. }
  169. },
  170. // 删除会话
  171. delSession (sessionId) {
  172. confirmPopup('确认删除当前会话?').then(() => {
  173. API.session.deleteSession({
  174. session_id: sessionId
  175. }).then(() => {
  176. this.$store.commit('removeSessionListById', sessionId)
  177. this.$showTips('已退出该会话')
  178. this.hideToolbar()
  179. // 如果正在打开该会话,重置路由
  180. if (this.curSession == sessionId) {
  181. this.$store.commit('changeSessionId', 0)
  182. this.$router.push({ path: '/' })
  183. }
  184. })
  185. })
  186. }
  187. }
  188. }
  189. </script>
  190. <style lang="scss" scoped>
  191. .chat-item{
  192. padding: 10px 13px 10px 20px;
  193. height: 40px;
  194. cursor: pointer;
  195. position: relative;
  196. @include webkitbox(1);
  197. &:hover{
  198. background: #383b41;
  199. }
  200. &.current{
  201. background: #3f424c;
  202. cursor: default;
  203. }
  204. .ext{
  205. position: absolute;
  206. background-color: #f12f28;
  207. color: #fff;
  208. text-align: center;
  209. }
  210. .info{
  211. @include flex(1);
  212. margin-right: 20px;
  213. margin-left: 10px;
  214. h3{
  215. font-size: 13px;
  216. color: #fff;
  217. line-height: 20px;
  218. @include ellipsis;
  219. }
  220. p{
  221. font-size: 12px;
  222. line-height: 20px;
  223. color: #7c8ca5;
  224. @include ellipsis;
  225. }
  226. }
  227. .avatar-wrap{
  228. position: relative;
  229. .ext{
  230. position: absolute;
  231. top: -7px;
  232. right: -7px;
  233. background: #ff0000;
  234. color: #fff;
  235. font-size: 12px;
  236. padding: 2px 5px;
  237. border-radius: 12px;
  238. z-index: 99;
  239. }
  240. }
  241. img.user-avatar{
  242. display: block;
  243. background: #ccc;
  244. }
  245. &.moblie{
  246. padding: px2rem(24) px2rem(32);
  247. height: px2rem(90);
  248. &.current{
  249. background-color: #fff;
  250. }
  251. .info{
  252. margin-left: px2rem(28);
  253. h3{
  254. font-size: px2rem(32);
  255. color: #020202;
  256. margin-bottom: px2rem(14);
  257. }
  258. p{
  259. font-size: px2rem(24);
  260. color: #9b9b9b;
  261. }
  262. }
  263. .ext{
  264. right: -px2rem(15);
  265. top: -px2rem(15);
  266. padding: 0 px2rem(8);
  267. height: px2rem(30);
  268. border-radius: px2rem(15);
  269. line-height: px2rem(30);
  270. font-size: px2rem(24);
  271. }
  272. &::after{
  273. content: "";
  274. pointer-events: none;
  275. box-sizing: border-box;
  276. position: absolute;
  277. width: 200%;
  278. height: 200%;
  279. left: px2rem(24+90+28);
  280. top: 0;
  281. border-bottom:1px solid #d8d8d8;
  282. transform:scale(0.5);
  283. transform-origin: 0 0;
  284. }
  285. }
  286. }
  287. </style>