chat.js 7.0 KB

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