chatForm.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. noMore: false,
  23. input: '',
  24. typing: '',
  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. //首次加载
  61. wx.setNavigationBarTitle({
  62. title: that.data.otherSide.nickName
  63. })
  64. let _this = that
  65. that.getScrollHeight().then((res) => {
  66. var scroll = res - _this.data.scrollHeight
  67. _this.setData({
  68. scrollTop: 5000,
  69. scrollHeight: res,
  70. })
  71. })
  72. } else {
  73. //重新设置scroll,scrollTop = 之前的scrollHeigth - 加入了数据后的scrollHeigth
  74. let _this = that
  75. that.getScrollHeight().then((res) => {
  76. var scroll = res - _this.data.scrollHeight
  77. _this.setData({
  78. scrollTop: scroll,
  79. scrollHeight: res,
  80. })
  81. })
  82. }
  83. } else {
  84. console.log(res)
  85. }
  86. })
  87. },
  88. openListen:function(){
  89. let that = this
  90. websocket.listenChatForm(this.data.id).then(res => {
  91. var newHistory = [{
  92. chatId: res.chatId,
  93. u1ToU2: res.senderId < res.receiverId ? true : false,
  94. messageType: res.messageType,
  95. messageBody: res.messageBody,
  96. sendTime: res.sendTime,
  97. }]
  98. that.addHistoryList(newHistory)
  99. that.openListen()
  100. })
  101. },
  102. toGoods: function(event) {
  103. let goodsId = event.target.dataset.id;
  104. wx.navigateTo({
  105. url: '/pages/goods/goods?id=' + goodsId,
  106. });
  107. },
  108. more: function() {
  109. console.log("到顶加载更多")
  110. if (!this.data.noMore) {
  111. this.getHistory()
  112. }
  113. },
  114. getScrollHeight: function() {
  115. let that = this
  116. return new Promise(function(resolve, reject) {
  117. var query = wx.createSelectorQuery()
  118. //#hei是位于scroll最低端的元素,求它离scroll顶部的距离,得出ScrollHeight
  119. query.select('#hei').boundingClientRect()
  120. query.selectViewport().scrollOffset()
  121. query.exec(function(res) {
  122. console.log("ScrollHeight " + res[0].top)
  123. resolve(res[0].top);
  124. })
  125. });
  126. },
  127. inputChange: function(e) {
  128. console.log(e.detail.value)
  129. this.setData({
  130. input: e.detail.value
  131. })
  132. },
  133. sendMsg: function() {
  134. let that = this
  135. var data = this.createMsg()
  136. var input = this.data.input
  137. this.setData({
  138. typing: '',
  139. input: '',
  140. })
  141. if (input.trim() == '') {
  142. return
  143. }
  144. //通过webSocket发送消息
  145. websocket.sendMessage(data).then(res => {
  146. console.log(res)
  147. var newHistory = [{
  148. chatId: this.data.id,
  149. u1ToU2: wx.getStorageSync('userInfo').openId < this.data.otherSide.openId ? true : false,
  150. messageType: 1,
  151. messageBody: input,
  152. sendTime: util.formatTime(new Date()),
  153. }]
  154. that.addHistoryList(newHistory)
  155. }).catch((res) => {
  156. console.log(res)
  157. util.showErrorToast('发送失败')
  158. });
  159. },
  160. addHistoryList: function(historyList) {
  161. //把新的数据加入目前的对话框
  162. var newHistoryList = this.data.historyList.concat(historyList)
  163. this.setData({
  164. historyList: newHistoryList,
  165. })
  166. //重新设置scroll
  167. let _this = this
  168. this.getScrollHeight().then((res) => {
  169. var scroll = res - _this.data.scrollHeight
  170. _this.setData({
  171. scrollTop: 100000000,
  172. scrollHeight: res,
  173. })
  174. })
  175. },
  176. createMsg: function() {
  177. var msgType;
  178. if (this.data.historyList.length>1) {
  179. msgType = 1
  180. } else {
  181. msgType = 3
  182. }
  183. var data = JSON.stringify({
  184. chatId: this.data.id,
  185. receiverId: this.data.otherSide.openId,
  186. senderId: wx.getStorageSync('userInfo').openId,
  187. goodsId: this.data.goods.id,
  188. messageType: msgType,
  189. messageBody: this.data.input
  190. })
  191. return data
  192. },
  193. buy:function(){
  194. util.showErrorToast('功能开发中')
  195. },
  196. /**
  197. * 生命周期函数--监听页面初次渲染完成
  198. */
  199. onReady: function() {
  200. },
  201. /**
  202. * 生命周期函数--监听页面显示
  203. */
  204. onShow: function() {
  205. },
  206. /**
  207. * 生命周期函数--监听页面隐藏
  208. */
  209. onHide: function() {
  210. websocket.listenBadge()
  211. },
  212. /**
  213. * 生命周期函数--监听页面卸载
  214. */
  215. onUnload: function() {
  216. },
  217. /**
  218. * 页面相关事件处理函数--监听用户下拉动作
  219. */
  220. onPullDownRefresh: function() {
  221. },
  222. /**
  223. * 页面上拉触底事件的处理函数
  224. */
  225. onReachBottom: function() {
  226. },
  227. /**
  228. * 用户点击右上角分享
  229. */
  230. onShareAppMessage: function() {
  231. },
  232. })