index.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <template>
  2. <view class="chat-room">
  3. <scroll-view id="scrollview" class="room" :scroll-top="scrollTop" scroll-y="true" >
  4. <view class="chat-container">
  5. <view class="item" v-for="(item,index) in chats" :key="index"
  6. :class="item.type==='mine'?'lf':''">
  7. <view class="avatar">
  8. <!-- #ifdef MP-WEIXIN -->
  9. <open-data type="userAvatarUrl" class="avatar-tag" v-if="item.type==='mine'"></open-data>
  10. <image src="/static/images/avatar.png" class="avatar-tag" mode="widthFix" v-if="item.type!=='mine'"></image>
  11. <text class="username">{{item.user}}</text>
  12. <!-- #endif -->
  13. <!-- #ifndef MP-WEIXIN -->
  14. <image src="/static/images/avatar.png" class="avatar-tag" mode="widthFix"></image>
  15. <text class="username">{{item.user}}</text>
  16. <!-- #endif -->
  17. </view>
  18. <view class="msgbox">
  19. <text class="msg">{{item.msg}}</text>
  20. </view>
  21. </view>
  22. </view>
  23. </scroll-view>
  24. <view class="send">
  25. <input
  26. class="sendInput"
  27. type="text"
  28. v-model="sendMsg"
  29. cursor-spacing="20"
  30. @confirm="sendMessage"
  31. confirm-type="发送"
  32. >
  33. </input>
  34. <button type="default" size="mini" @click="sendMessage" class="sendBt">发送</button>
  35. </view>
  36. </view>
  37. </template>
  38. <script>
  39. export default {
  40. name:"IM-chat",
  41. props:{
  42. chats:{
  43. type:[Array],
  44. default:()=>[]
  45. }
  46. },
  47. watch:{
  48. chats(){
  49. if(this.chats.length>1){
  50. this.scrollToBottom();
  51. }
  52. }
  53. },
  54. data() {
  55. return {
  56. scrollTop: 0,
  57. sendMsg:''
  58. }
  59. },
  60. methods: {
  61. sendMessage(){
  62. this.$emit('send',this.sendMsg);
  63. this.sendMsg="";
  64. },
  65. scrollToBottom: function () {
  66. const query = uni.createSelectorQuery().in(this);
  67. query.selectAll('.chat-container').boundingClientRect(data => {
  68. this.scrollTop=data[0].height;
  69. }).exec();
  70. }
  71. }
  72. }
  73. </script>
  74. <style lang="scss" scoped>
  75. @import url('./index.css')
  76. </style>