emoji.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <view class="uni-column">
  3. <view class="content" id="content" :style="{height:style.contentViewHeight+'px'}">
  4. <scroll-view id="scrollview" scroll-y="true" :style="{height:style.contentViewHeight+'px'}" :scroll-with-animation="true"
  5. :scroll-top="scrollTop">
  6. <page-foot :name="name"></page-foot>
  7. <message-show v-for="(message,index) in messages" :key="index" :message="message" :cid="index"></message-show>
  8. <view id="bottom"></view>
  9. </scroll-view>
  10. </view>
  11. <view class="foot">
  12. <chat-input @send-message="getInputMessage" @show="setScrollH" @foc="setScrollH"></chat-input>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. import chatInput from '@/components/emoji/chatinput.vue';
  18. import messageShow from '@/components/emoji/messageshow.vue';
  19. export default {
  20. components: {
  21. chatInput,
  22. messageShow
  23. },
  24. data() {
  25. return {
  26. name:'xcecd@qq.com',
  27. style: {
  28. pageHeight: 0,
  29. contentViewHeight: 0,
  30. footViewHeight: 90,
  31. mitemHeight: 0,
  32. },
  33. scrollTop: 0,
  34. messages: [{
  35. user: 'home',
  36. type: 'head', //input,content
  37. content: '你好!'
  38. }]
  39. }
  40. },
  41. onLoad: function () {
  42. const res = uni.getSystemInfoSync();
  43. this.style.pageHeight = res.windowHeight;
  44. this.style.contentViewHeight = res.windowHeight - uni.getSystemInfoSync().screenWidth / 750 * (100) ; //像素
  45. //console.log(res)
  46. },
  47. methods: {
  48. getInputMessage: function (message) { //获取子组件的输入数据
  49. // console.log(message);
  50. this.addMessage('customer', message.content, false);
  51. this.toRobot(message.content);
  52. this.setScrollH();
  53. },
  54. addMessage: function (user, content, hasSub, subcontent) {
  55. var that = this;
  56. content =
  57. that.messages.push({
  58. user: user,
  59. content: content,
  60. hasSub: hasSub,
  61. subcontent: subcontent
  62. });
  63. },
  64. scrollToBottom: function () {
  65. var that = this;
  66. this.scrollTop += 133;
  67. //console.log(this.scrollTop)
  68. },
  69. toRobot: function (info) {
  70. // this.addMessage('home', info, false);
  71. var apiUrl = 'http://www.tuling123.com/openapi/api';
  72. uni.request({
  73. url: apiUrl,
  74. data: {
  75. "key": 'acfbca724ea1b5db96d2eef88ce677dc',
  76. "info": info,
  77. "userid": 'uni-test'
  78. },
  79. success: (res) => {
  80. console.log("s", res);
  81. this.addMessage('home', res.data.text, false);
  82. this.scrollToBottom();
  83. console.log('request success:' + res.data.text);
  84. },
  85. fail: (err) => {
  86. console.log('request fail', err);
  87. uni.showModal({
  88. content: err.errMsg,
  89. showCancel: false
  90. })
  91. }
  92. });
  93. },
  94. // 设置高度 用emit辅助
  95. setScrollH: function(){
  96. var query = uni.createSelectorQuery();
  97. let footh = query.select('.foot');
  98. //console.log('fh',footh);
  99. const res = uni.getSystemInfoSync();
  100. this.style.pageHeight = res.windowHeight;
  101. this.$nextTick(function(){
  102. footh.fields({
  103. size: true
  104. }, data => {
  105. footh = data.height;
  106. console.log('fh',data.height);
  107. this.style.contentViewHeight = res.windowHeight -footh ; //像素
  108. }).exec();
  109. console.log('contentViewHeight',this.style.contentViewHeight);
  110. })
  111. }
  112. }
  113. }
  114. </script>
  115. <style>
  116. .uni-column {
  117. display: flex;
  118. flex-direction: column;
  119. }
  120. .content {
  121. display: flex;
  122. flex: 1;
  123. /* margin-bottom: 100upx; */
  124. }
  125. .foot {
  126. position: fixed;
  127. width: 100%;
  128. /* height: 90upx;
  129. min-height: 90upx; */
  130. height: auto;
  131. left: 0upx;
  132. bottom: 0;
  133. overflow: hidden;
  134. }
  135. </style>