message.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // pages/message/message.js
  2. const {getUserRelation,addUserRelation, getUserInfoList, getGoodsInfoList} = require('../../API/appraise')
  3. import messageMap from '../../utils/util'
  4. var getRelationOption = {
  5. data:{
  6. }
  7. }
  8. var addRelationOption = {
  9. data:{
  10. "goodsId":9,
  11. "owner": '7386090454720188416',
  12. "target": '7384540497554968576',
  13. "type":1
  14. }
  15. }
  16. var getUserInfoListOption = {
  17. data:{
  18. uid:[]
  19. }
  20. }
  21. var getGoodsInfoListOption = {
  22. data:{
  23. id:[]
  24. }
  25. }
  26. var key = "commodityPicture";
  27. let key1 = "avatarUrl";
  28. let key2 = "merchantName";
  29. let key3 = "id";
  30. function sendSocketMessage(msg){
  31. wx.sendSocketMessage({
  32. data: msg,
  33. })
  34. }
  35. Page({
  36. /**
  37. * 页面的初始数据
  38. */
  39. data: {
  40. message:[
  41. // {
  42. // // id:1,
  43. // // item:{
  44. // // avatarUrl: "http://192.168.31.30:8084/pics/1698753512AiD8PaNrvqsDb1493fda534929a5b5d05f80a8cf3d16.jpg",
  45. // // commodityPicture: "https://img.js.design/assets/img/6229b69ff89eeaf3b7fd1919.jpg",
  46. // // merchantName: "daisy"
  47. // // }
  48. // }
  49. ]
  50. },
  51. /**
  52. * 生命周期函数--监听页面加载
  53. */
  54. onLoad(options) {
  55. },
  56. /**
  57. * 生命周期函数--监听页面初次渲染完成
  58. */
  59. onReady() {
  60. var message = this.data.message;
  61. // addUserRelation(addRelationOption).then((res) => {
  62. // console.log(res);
  63. // }).catch((err) => {
  64. // console.log(err);
  65. // })
  66. getRelationOption.data = {
  67. uid: wx.getStorageSync('uid'),
  68. }
  69. getUserRelation(getRelationOption).then((res) => {
  70. console.log(res);
  71. const fUid = res.data.data.fUid;
  72. let fUidLength = fUid.length;
  73. let target = [];
  74. for(let i=0;i<fUidLength;i++)
  75. {
  76. target[i] = fUid[i].target;
  77. }
  78. var goodsId = [];
  79. for(let i=0;i<fUidLength;i++)
  80. {
  81. goodsId[i] = fUid[i].goodsId;
  82. }
  83. console.log(target);
  84. console.log(goodsId);
  85. getUserInfoListOption.data.uid = target;
  86. console.log(getUserInfoListOption);
  87. getUserInfoList(getUserInfoListOption).then((res) => {
  88. console.log(res);
  89. const users = res.data.data.users;
  90. for(let i=0;i<users.length;i++)
  91. {
  92. message.push({item:{}});
  93. message[i][key3] = i+1;
  94. message[i].item[key1] = users[i].avatar;
  95. message[i].item[key2] = users[i].name;
  96. message[i].item['targetId'] = users[i].uid;
  97. }
  98. console.log(message);
  99. getGoodsInfoListOption.data.id = goodsId;
  100. getGoodsInfoList(getGoodsInfoListOption).then((res => {
  101. console.log(res);
  102. const goods = res.data.data.goods;
  103. for(let i=0;i<goods.length;i++)
  104. {
  105. message[i].item[key] = goods[i].Pic;
  106. message[i].item['goodsId'] = goods[i].Id;
  107. }
  108. this.setData({
  109. message:message
  110. })
  111. })).catch((err) => {
  112. console.log(err);
  113. })
  114. }).catch((err) => {
  115. console.log(err);
  116. })
  117. }).catch((err) => {
  118. console.log(err);
  119. })
  120. },
  121. /**
  122. * 生命周期函数--监听页面显示
  123. */
  124. onShow() {
  125. },
  126. /**
  127. * 生命周期函数--监听页面隐藏
  128. */
  129. onHide() {
  130. },
  131. /**
  132. * 生命周期函数--监听页面卸载
  133. */
  134. onUnload() {
  135. },
  136. /**
  137. * 页面相关事件处理函数--监听用户下拉动作
  138. */
  139. onPullDownRefresh() {
  140. },
  141. /**
  142. * 页面上拉触底事件的处理函数
  143. */
  144. onReachBottom() {
  145. },
  146. /**
  147. * 用户点击右上角分享
  148. */
  149. onShareAppMessage() {
  150. },
  151. toChat(e) {
  152. wx.connectSocket({
  153. url: 'ws://192.168.31.30:8084'+'/chat'+'?uid='+wx.getStorageSync('uid'),
  154. })
  155. // 监听
  156. wx.onSocketOpen((result) => {
  157. console.log("websocket连接已打开");
  158. })
  159. // 失败监控
  160. wx.onSocketError((result) => {
  161. console.log("websocket连接失败", result);
  162. })
  163. // 监听服务器的数据返回
  164. wx.onSocketMessage((result) => {
  165. console.log("服务器的数据返回", result);
  166. })
  167. console.log(e.target.dataset.item);
  168. let item = e.target.dataset.item;
  169. wx.navigateTo({
  170. url: '/pages/chat/chat?toUid='+item.targetId+'?goodsId='+item.goodsId+'?nickName',
  171. })
  172. }
  173. })