chat.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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:'',
  37. name:'',
  38. price: 0,
  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. this.setData({
  83. goods:{
  84. pic:good.Pic,
  85. name: good.Title,
  86. price: good.Price
  87. }
  88. })
  89. }).catch((err)=>{
  90. console.log(err);
  91. })
  92. //获取用户信息
  93. getUserInfoListOption.data.uid = [this.data.toUid];
  94. getUserInfoList(getUserInfoListOption).then((res)=> {
  95. console.log(res);
  96. const {users} = res.data.data;
  97. wx.setStorageSync('anotherNickName', users[0].name);
  98. wx.setStorageSync('anotherAvatar', users[0].avatar);
  99. }).catch((err)=>{
  100. console.log(err);
  101. })
  102. //建立ws连接
  103. setWsConnect(setWsConnectOption).then((res)=> {
  104. console.log(res);
  105. })
  106. // 监听服务器的数据返回
  107. wx.onSocketMessage((result) => {
  108. console.log("服务器的数据返回", result);
  109. const res = JSON.parse(result.data);
  110. if(res.msgType == 1){
  111. //获取当前时间
  112. let date = new Date();
  113. let month = date.getMonth() + 1;
  114. let day = date.getDate();
  115. let hour = date.getHours();
  116. let minu = date.getMinutes();
  117. let now1 = month < 10 ? '0' + month : month;
  118. let now2 = day < 10 ? '0' + day : day;
  119. //处理数据
  120. if(res.from == this.data.toUid){
  121. let msg = {
  122. from: res.from,
  123. to:res.to,
  124. message: res.content,
  125. type: 'text',
  126. time: now1 + '-' + now2 + ' ' + hour + ':' + minu,
  127. nickname: wx.getStorageSync('anotherNiakName'),
  128. avatar: wx.getStorageSync('anotherAvatar')
  129. }
  130. console.log(msg);
  131. this.setData({
  132. chatList: list.concat(msg)
  133. }, () => {
  134. that.scrollToBottom();
  135. that.setData({
  136. content: ''
  137. })
  138. })
  139. }
  140. }
  141. })
  142. this.scrollToBottom();
  143. },
  144. /**
  145. * 生命周期函数--监听页面初次渲染完成
  146. */
  147. onReady() {
  148. },
  149. /**
  150. * 生命周期函数--监听页面显示
  151. */
  152. onShow() {
  153. },
  154. /**
  155. * 生命周期函数--监听页面隐藏
  156. */
  157. onHide() {
  158. },
  159. /**
  160. * 生命周期函数--监听页面卸载
  161. */
  162. onUnload() {
  163. wx.closeSocket();
  164. console.log('websocket 已关闭!');
  165. },
  166. /**
  167. * 页面相关事件处理函数--监听用户下拉动作
  168. */
  169. onPullDownRefresh() {
  170. },
  171. /**
  172. * 页面上拉触底事件的处理函数
  173. */
  174. onReachBottom() {
  175. },
  176. /**
  177. * 用户点击右上角分享
  178. */
  179. onShareAppMessage() {
  180. },
  181. // 输入监听
  182. inputClick(e) {
  183. this.setData({
  184. content: e.detail.value
  185. })
  186. },
  187. // 发送监听
  188. sendClick() {
  189. let that = this;
  190. let list = this.data.chatList;
  191. // 获取当前时间
  192. let date = new Date();
  193. let month = date.getMonth() + 1;
  194. let day = date.getDate();
  195. let hour = date.getHours();
  196. let minu = date.getMinutes();
  197. let now1 = month < 10 ? '0' + month : month;
  198. let now2 = day < 10 ? '0' + day : day;
  199. // 组装数据
  200. let msg = {
  201. from: wx.getStorageSync('uid'),
  202. to: this.data.toUid,
  203. nickname: wx.getStorageSync('nickName'),
  204. avatar: wx.getStorageSync('avatarUrl'),
  205. message: this.data.content,
  206. type: 'text',
  207. time: now1 + '-' + now2 + ' ' + hour + ':' + minu
  208. }
  209. console.log(msg);
  210. this.setData({
  211. chatList: list.concat(msg)
  212. }, () => {
  213. that.scrollToBottom();
  214. that.setData({
  215. content: ''
  216. })
  217. })
  218. let socketMsg = socketMsgQueue;
  219. socketMsg.msgType = 1;
  220. socketMsg.content = this.data.content;
  221. socketMsg.to = this.data.toUid;
  222. console.log(this.data.toUid);
  223. let time = new Date().getTime();
  224. console.log(time);
  225. socketMsg.time = time;
  226. console.log(socketMsg);
  227. wx.sendSocketMessage({
  228. data: JSON.stringify(socketMsg),
  229. })
  230. },
  231. // 滑动到最底部
  232. scrollToBottom() {
  233. setTimeout(() => {
  234. wx.pageScrollTo({
  235. scrollTop: 200000,
  236. duration: 3
  237. });
  238. }, 600)
  239. },
  240. })