chatForm.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. var util = require('../../../utils/util.js');
  2. var api = require('../../../config/api.js');
  3. var websocket = require('../../../services/websocket.js');
  4. // 参考:
  5. // https://blog.csdn.net/qq_35713752/article/details/78688311
  6. // https://blog.csdn.net/qq_35713752/article/details/80811397
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. id: 0,
  13. historyList: [],
  14. otherSide: {},
  15. goods: {},
  16. isU1: false,
  17. myAvatar: '',
  18. scrollTop: 0,
  19. offsetTime: null,
  20. size: 10,
  21. scrollHeight: 0,
  22. newScrollHeight: 0,
  23. noMore: false,
  24. input: '',
  25. },
  26. /**
  27. * 生命周期函数--监听页面加载
  28. */
  29. onLoad: function(options) {
  30. let now = new Date();
  31. this.setData({
  32. id: options.id,
  33. myAvatar: wx.getStorageSync('userInfo').avatarUrl,
  34. offsetTime: now.toISOString()
  35. })
  36. this.getHistory();
  37. this.openListen();
  38. },
  39. getHistory: function() {
  40. let that = this;
  41. util.request(api.ChatForm + '/' + this.data.id, {
  42. offsetTime: this.data.offsetTime,
  43. size: this.data.size
  44. }).then(function(res) {
  45. if (res.errno === 0) {
  46. console.log(res.data);
  47. that.setData({
  48. otherSide: res.data.otherSide,
  49. historyList: res.data.historyList.concat(that.data.historyList),
  50. goods: res.data.goods,
  51. isU1: res.data.isU1,
  52. offsetTime: res.data.offsetTime+"",
  53. });
  54. if (res.data.historyList.length < that.data.size) {
  55. that.setData({
  56. noMore: true
  57. })
  58. }
  59. if (that.data.historyList.length < 11) {
  60. wx.setNavigationBarTitle({
  61. title: that.data.otherSide.nickName
  62. })
  63. let _this = that
  64. that.getScrollHeight().then((res) => {
  65. var scroll = res - _this.data.scrollHeight
  66. _this.setData({
  67. scrollTop: 5000,
  68. scrollHeight: res,
  69. })
  70. })
  71. } else {
  72. //重新设置scroll,scrollTop = 之前的scrollHeigth - 加入了数据后的scrollHeigth
  73. let _this = that
  74. that.getScrollHeight().then((res) => {
  75. var scroll = res - _this.data.scrollHeight
  76. _this.setData({
  77. scrollTop: scroll,
  78. scrollHeight: res,
  79. })
  80. })
  81. }
  82. } else {
  83. console.log(res)
  84. }
  85. })
  86. },
  87. openListen:function(){
  88. let that = this
  89. websocket.listenChatForm(this.data.id).then(res => {
  90. var newHistory = [{
  91. chatId: res.chatId,
  92. u1ToU2: res.senderId < res.receiverId ? true : false,
  93. messageType: res.messageType,
  94. messageBody: res.messageBody,
  95. sendTime: res.sendTime,
  96. }]
  97. that.addHistoryList(newHistory)
  98. that.openListen()
  99. })
  100. },
  101. toGoods: function(event) {
  102. let goodsId = event.target.dataset.id;
  103. wx.navigateTo({
  104. url: '/pages/goods/goods?id=' + goodsId,
  105. });
  106. },
  107. more: function() {
  108. console.log("到顶加载更多")
  109. if (!this.data.noMore) {
  110. this.getHistory()
  111. }
  112. },
  113. getScrollHeight: function() {
  114. let that = this
  115. return new Promise(function(resolve, reject) {
  116. var query = wx.createSelectorQuery()
  117. //#hei是位于scroll最低端的元素,求它离scroll顶部的距离,得出ScrollHeight
  118. query.select('#hei').boundingClientRect()
  119. query.selectViewport().scrollOffset()
  120. query.exec(function(res) {
  121. console.log("异步设置ScrollHeight" + res[0].top)
  122. resolve(res[0].top);
  123. })
  124. });
  125. },
  126. inputChange: function(e) {
  127. console.log(e.detail.value)
  128. this.setData({
  129. input: e.detail.value
  130. })
  131. },
  132. sendMsg: function() {
  133. if (this.data.input.trim() == '') {
  134. this.setData({
  135. input: '',
  136. })
  137. return
  138. }
  139. let that = this
  140. var data = this.createMsg()
  141. //通过webSocket发送消息
  142. websocket.sendMessage(data).then(res => {
  143. console.log(res)
  144. var newHistory = [{
  145. chatId: this.data.id,
  146. u1ToU2: wx.getStorageSync('userInfo').openId < this.data.otherSide.openId ? true : false,
  147. messageType: 1,
  148. messageBody: this.data.input,
  149. sendTime: util.formatTime(new Date()),
  150. }]
  151. that.addHistoryList(newHistory)
  152. }).catch((res) => {
  153. console.log(res)
  154. util.showErrorToast('发送失败')
  155. });
  156. },
  157. addHistoryList: function(historyList) {
  158. //把新的数据加入目前的对话框
  159. var newHistoryList = this.data.historyList.concat(historyList)
  160. this.setData({
  161. input: '',
  162. historyList: newHistoryList,
  163. })
  164. //重新设置scroll
  165. let _this = this
  166. this.getScrollHeight().then((res) => {
  167. var scroll = res - _this.data.scrollHeight
  168. _this.setData({
  169. scrollTop: 100000000,
  170. scrollHeight: res,
  171. })
  172. })
  173. },
  174. createMsg: function() {
  175. var msgType;
  176. if (this.data.historyList.length>1) {
  177. msgType = 1
  178. } else {
  179. msgType = 2
  180. }
  181. var data = JSON.stringify({
  182. chatId: this.data.id,
  183. receiverId: this.data.otherSide.openId,
  184. senderId: wx.getStorageSync('userInfo').openId,
  185. goodsId: this.data.goods.id,
  186. messageType: msgType,
  187. messageBody: this.data.input
  188. })
  189. return data
  190. },
  191. /**
  192. * 生命周期函数--监听页面初次渲染完成
  193. */
  194. onReady: function() {
  195. },
  196. /**
  197. * 生命周期函数--监听页面显示
  198. */
  199. onShow: function() {
  200. },
  201. /**
  202. * 生命周期函数--监听页面隐藏
  203. */
  204. onHide: function() {
  205. },
  206. /**
  207. * 生命周期函数--监听页面卸载
  208. */
  209. onUnload: function() {
  210. websocket.stopListenForm(this.data.id)
  211. websocket.listenBadge()
  212. },
  213. /**
  214. * 页面相关事件处理函数--监听用户下拉动作
  215. */
  216. onPullDownRefresh: function() {
  217. },
  218. /**
  219. * 页面上拉触底事件的处理函数
  220. */
  221. onReachBottom: function() {
  222. },
  223. /**
  224. * 用户点击右上角分享
  225. */
  226. onShareAppMessage: function() {
  227. },
  228. })