chat.js 6.7 KB

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