chat.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // pages/chat/chat.js
  2. const {getUserInfoList,getChattingRecord,getGoodsInfo,setWsConnect} = require('../../API/appraise')
  3. import messageMap from '../../utils/util'
  4. var socketMsgQueue = {
  5. from: wx.getStorageSync('uid'),
  6. to: '',
  7. msgType: 1,
  8. time: '',
  9. content: ''
  10. }
  11. var getUserInfoListOption = {
  12. data:{
  13. uid:[]
  14. }
  15. }
  16. var getChattingRecordOption = {
  17. uid:wx.getStorageSync('uid'),
  18. target:''
  19. }
  20. var getGoodsInfoOption = {
  21. id:'',
  22. uid:'',
  23. date:''
  24. }
  25. var setWsConnectOption = {
  26. uid: wx.getStorageSync('uid')
  27. }
  28. Page({
  29. /**
  30. * 页面的初始数据
  31. */
  32. data: {
  33. toUid:'',
  34. goodsId:'',
  35. goods: {
  36. pic:'https://img.js.design/assets/img/6229b69ff89eeaf3b7fd1919.jpg',
  37. name:'粉色',
  38. price:20
  39. },
  40. //登录用户的信息
  41. login: {
  42. id: wx.getStorageSync('uid')
  43. },
  44. chatList: [
  45. {
  46. from: '7385700531999608832',
  47. to: '7384540497554968576',
  48. nickname:'daisy',
  49. avatar: 'http://192.168.31.30:8084/pics/1698753512AiD8PaNrvqsDb1493fda534929a5b5d05f80a8cf3d16.jpg',
  50. message: '你好',
  51. type: 'text',
  52. time: '11-8 20:52'
  53. },
  54. {
  55. to: '7385700531999608832',
  56. from: '7384540497554968576',
  57. nickname:'无情的敲代码机器人',
  58. avatar: 'http://192.168.31.30:8084/pics/1699175056BvtgC8vv8gSzd85dedb14362dc3a251c6c85705ec495.jpg',
  59. message: '你好',
  60. type: 'text',
  61. time: '11-8 20:52'
  62. }
  63. ]
  64. },
  65. /**
  66. * 生命周期函数--监听页面加载
  67. */
  68. onLoad(options) {
  69. //获取商品信息
  70. this.data.toUid = options.toUid;
  71. this.data.goodsId = options.goodsId;
  72. getChattingRecordOption.target = this.data.toUid;
  73. let time = new Date().getTime();
  74. getGoodsInfoOption = {
  75. id:this.data.goodsId,
  76. uid:wx.getStorageSync('uid'),
  77. date:time
  78. }
  79. getGoodsInfo(getGoodsInfoOption).then((res)=>{
  80. console.log(res);
  81. const good = res.data.data;
  82. console.log(good);
  83. }).catch((err)=>{
  84. console.log(err);
  85. })
  86. //获取用户信息
  87. getUserInfoListOption.data.uid = [this.data.toUid];
  88. getUserInfoList(getUserInfoListOption).then((res)=> {
  89. console.log(res);
  90. const {users} = res.data.data;
  91. wx.setStorageSync('anotherNickName', users[0].name);
  92. wx.setStorageSync('anotherAvatar', users[0].avatar);
  93. }).catch((err)=>{
  94. console.log(err);
  95. })
  96. //建立ws连接
  97. setWsConnect(setWsConnectOption).then((res)=> {
  98. console.log(res);
  99. })
  100. // 监听服务器的数据返回
  101. wx.onSocketMessage((result) => {
  102. console.log("服务器的数据返回", result);
  103. const res = JSON.parse(result.data);
  104. if(res.msgType == 1){
  105. //获取当前时间
  106. let date = new Date();
  107. let month = date.getMonth() + 1;
  108. let day = date.getDate();
  109. let hour = date.getHours();
  110. let minu = date.getMinutes();
  111. let now1 = month < 10 ? '0' + month : month;
  112. let now2 = day < 10 ? '0' + day : day;
  113. //处理数据
  114. if(res.from == this.data.toUid){
  115. let msg = {
  116. from: res.from,
  117. to:res.to,
  118. message: res.content,
  119. type: 'text',
  120. time: now1 + '-' + now2 + ' ' + hour + ':' + minu,
  121. nickname: wx.getStorageSync('anotherNiakName'),
  122. avatar: wx.getStorageSync('anotherAvatar')
  123. }
  124. console.log(msg);
  125. this.setData({
  126. chatList: list.concat(msg)
  127. }, () => {
  128. that.scrollToBottom();
  129. that.setData({
  130. content: ''
  131. })
  132. })
  133. }
  134. }
  135. })
  136. this.scrollToBottom();
  137. },
  138. /**
  139. * 生命周期函数--监听页面初次渲染完成
  140. */
  141. onReady() {
  142. },
  143. /**
  144. * 生命周期函数--监听页面显示
  145. */
  146. onShow() {
  147. },
  148. /**
  149. * 生命周期函数--监听页面隐藏
  150. */
  151. onHide() {
  152. },
  153. /**
  154. * 生命周期函数--监听页面卸载
  155. */
  156. onUnload() {
  157. },
  158. /**
  159. * 页面相关事件处理函数--监听用户下拉动作
  160. */
  161. onPullDownRefresh() {
  162. },
  163. /**
  164. * 页面上拉触底事件的处理函数
  165. */
  166. onReachBottom() {
  167. },
  168. /**
  169. * 用户点击右上角分享
  170. */
  171. onShareAppMessage() {
  172. },
  173. // 输入监听
  174. inputClick(e) {
  175. this.setData({
  176. content: e.detail.value
  177. })
  178. },
  179. // 发送监听
  180. sendClick() {
  181. let that = this;
  182. let list = this.data.chatList;
  183. // 获取当前时间
  184. let date = new Date();
  185. let month = date.getMonth() + 1;
  186. let day = date.getDate();
  187. let hour = date.getHours();
  188. let minu = date.getMinutes();
  189. let now1 = month < 10 ? '0' + month : month;
  190. let now2 = day < 10 ? '0' + day : day;
  191. // 组装数据
  192. let msg = {
  193. from: wx.getStorageSync('uid'),
  194. to: this.data.toUid,
  195. nickname: wx.getStorageSync('nickName'),
  196. avatar: wx.getStorageSync('avatarUrl'),
  197. message: this.data.content,
  198. type: 'text',
  199. time: now1 + '-' + now2 + ' ' + hour + ':' + minu
  200. }
  201. console.log(msg);
  202. this.setData({
  203. chatList: list.concat(msg)
  204. }, () => {
  205. that.scrollToBottom();
  206. that.setData({
  207. content: ''
  208. })
  209. })
  210. let socketMsg = socketMsgQueue;
  211. socketMsg.msgType = 1;
  212. socketMsg.content = this.data.content;
  213. socketMsg.to = this.data.toUid;
  214. console.log(this.data.toUid);
  215. let time = new Date().getTime();
  216. console.log(time);
  217. socketMsg.time = time;
  218. console.log(socketMsg);
  219. wx.sendSocketMessage({
  220. data: JSON.stringify(socketMsg),
  221. })
  222. },
  223. // 滑动到最底部
  224. scrollToBottom() {
  225. setTimeout(() => {
  226. wx.pageScrollTo({
  227. scrollTop: 200000,
  228. duration: 3
  229. });
  230. }, 600)
  231. },
  232. })