chatForm.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. // that.culScroll();
  62. // setTimeout(function () {
  63. // that.setData({
  64. // scrollTop: 5000,
  65. // scrollHeight: that.data.newScrollHeight
  66. // })
  67. // }, 100);
  68. let _this = that
  69. that.getScrollHeight().then((res) => {
  70. var scroll = res - _this.data.scrollHeight
  71. _this.setData({
  72. scrollTop: 5000,
  73. scrollHeight: res,
  74. })
  75. })
  76. } else {
  77. console.log("加载了么")
  78. // that.queryMultipleNodes();
  79. // setTimeout(function() {
  80. // var scroll = that.data.newScrollHeight - that.data.scrollHeight
  81. // console.log("加载了么: scrollTop " + scroll)
  82. // console.log("加载了么: newScrollHeight " + that.data.newScrollHeight)
  83. // console.log("加载了么: scrollHeight " + that.data.scrollHeight)
  84. // that.setData({
  85. // scrollTop: scroll,
  86. // scrollHeight: that.data.newScrollHeight,
  87. // })
  88. // }, 100);
  89. let _this=that
  90. that.getScrollHeight().then((res) => {
  91. var scroll = res - _this.data.scrollHeight
  92. _this.setData({
  93. scrollTop: scroll,
  94. scrollHeight: res,
  95. })
  96. })
  97. }
  98. } else {
  99. console.log(res)
  100. }
  101. })
  102. },
  103. culScroll: function() {
  104. let that = this
  105. var query = wx.createSelectorQuery()
  106. query.select('#hei').boundingClientRect()
  107. query.selectViewport().scrollOffset()
  108. query.exec(function(res) {
  109. console.log("同步设置newScrollHeight" + res[0].top)
  110. that.setData({
  111. newScrollHeight: res[0].top
  112. })
  113. })
  114. },
  115. toGoods: function(event) {
  116. let goodsId = event.target.dataset.id;
  117. wx.navigateTo({
  118. url: '/pages/goods/goods?id=' + goodsId,
  119. });
  120. },
  121. more: function() {
  122. console.log("到顶加载更多")
  123. if (!this.data.noMore) {
  124. this.getHistory()
  125. }
  126. },
  127. /**
  128. * 生命周期函数--监听页面初次渲染完成
  129. */
  130. onReady: function() {
  131. },
  132. /**
  133. * 生命周期函数--监听页面显示
  134. */
  135. onShow: function() {
  136. },
  137. /**
  138. * 生命周期函数--监听页面隐藏
  139. */
  140. onHide: function() {
  141. },
  142. /**
  143. * 生命周期函数--监听页面卸载
  144. */
  145. onUnload: function() {
  146. },
  147. /**
  148. * 页面相关事件处理函数--监听用户下拉动作
  149. */
  150. onPullDownRefresh: function() {
  151. },
  152. /**
  153. * 页面上拉触底事件的处理函数
  154. */
  155. onReachBottom: function() {
  156. },
  157. /**
  158. * 用户点击右上角分享
  159. */
  160. onShareAppMessage: function() {
  161. },
  162. getScrollHeight: function() {
  163. let that = this
  164. return new Promise(function(resolve, reject) {
  165. var query = wx.createSelectorQuery()
  166. query.select('#hei').boundingClientRect()
  167. query.selectViewport().scrollOffset()
  168. query.exec(function(res) {
  169. console.log("异步设置newScrollHeight" + res[0].top)
  170. resolve(res[0].top);
  171. })
  172. });
  173. }
  174. })