chatForm.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. var util = require('../../../utils/util.js');
  2. var api = require('../../../config/api.js');
  3. // pages/chat/chatForm/chatForm.js
  4. // https://blog.csdn.net/qq_35713752/article/details/78688311
  5. // https://blog.csdn.net/qq_35713752/article/details/80811397
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. id: 0,
  12. historyList: [],
  13. otherSide: {},
  14. goods: {},
  15. isU1: false,
  16. myAvatar: '',
  17. scrollTop: 0,
  18. offsetTime: null,
  19. size: 10,
  20. scrollHeight: 0,
  21. newScrollHeight: 0,
  22. noMore: false,
  23. },
  24. /**
  25. * 生命周期函数--监听页面加载
  26. */
  27. onLoad: function(options) {
  28. let now = new Date();
  29. this.setData({
  30. id: options.id,
  31. myAvatar: wx.getStorageSync('userInfo').avatarUrl,
  32. offsetTime: now.toISOString()
  33. })
  34. this.getHistory();
  35. },
  36. getHistory: function() {
  37. let that = this;
  38. util.request(api.ChatForm + '/' + this.data.id, {
  39. offsetTime: this.data.offsetTime,
  40. size: this.data.size
  41. }).then(function(res) {
  42. if (res.errno === 0) {
  43. console.log(res.data);
  44. that.setData({
  45. otherSide: res.data.otherSide,
  46. historyList: res.data.historyList.concat(that.data.historyList),
  47. goods: res.data.goods,
  48. isU1: res.data.isU1,
  49. offsetTime: res.data.offsetTime,
  50. });
  51. if (res.data.historyList.length < that.data.size) {
  52. that.setData({
  53. noMore: true
  54. })
  55. }
  56. console.log(that.data.historyList.length)
  57. if (that.data.historyList.length < 11) {
  58. wx.setNavigationBarTitle({
  59. title: that.data.otherSide.nickName
  60. })
  61. let _this = that
  62. that.getScrollHeight().then((res) => {
  63. var scroll = res - _this.data.scrollHeight
  64. _this.setData({
  65. scrollTop: 5000,
  66. scrollHeight: res,
  67. })
  68. })
  69. } else {
  70. //重新设置scroll,scrollTop = 之前的scrollHeigth - 加入了数据后的scrollHeigth
  71. let _this=that
  72. that.getScrollHeight().then((res) => {
  73. var scroll = res - _this.data.scrollHeight
  74. _this.setData({
  75. scrollTop: scroll,
  76. scrollHeight: res,
  77. })
  78. })
  79. }
  80. } else {
  81. console.log(res)
  82. }
  83. })
  84. },
  85. toGoods: function(event) {
  86. let goodsId = event.target.dataset.id;
  87. wx.navigateTo({
  88. url: '/pages/goods/goods?id=' + goodsId,
  89. });
  90. },
  91. more: function() {
  92. console.log("到顶加载更多")
  93. if (!this.data.noMore) {
  94. this.getHistory()
  95. }
  96. },
  97. getScrollHeight: function () {
  98. let that = this
  99. return new Promise(function (resolve, reject) {
  100. var query = wx.createSelectorQuery()
  101. //#hei是位于scroll最低端的元素,求它离scroll顶部的距离,得出ScrollHeight
  102. query.select('#hei').boundingClientRect()
  103. query.selectViewport().scrollOffset()
  104. query.exec(function (res) {
  105. console.log("异步设置ScrollHeight" + res[0].top)
  106. resolve(res[0].top);
  107. })
  108. });
  109. },
  110. /**
  111. * 生命周期函数--监听页面初次渲染完成
  112. */
  113. onReady: function() {
  114. },
  115. /**
  116. * 生命周期函数--监听页面显示
  117. */
  118. onShow: function() {
  119. },
  120. /**
  121. * 生命周期函数--监听页面隐藏
  122. */
  123. onHide: function() {
  124. },
  125. /**
  126. * 生命周期函数--监听页面卸载
  127. */
  128. onUnload: function() {
  129. },
  130. /**
  131. * 页面相关事件处理函数--监听用户下拉动作
  132. */
  133. onPullDownRefresh: function() {
  134. },
  135. /**
  136. * 页面上拉触底事件的处理函数
  137. */
  138. onReachBottom: function() {
  139. },
  140. /**
  141. * 用户点击右上角分享
  142. */
  143. onShareAppMessage: function() {
  144. },
  145. })