chat.js 7.3 KB

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