chat-list.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // pages/chat-list/chat-list.js
  2. // import Times from "../../utils/time";
  3. const webSocket = require('../../utils/websocket.js');
  4. const timefunc = require('../../utils/time.js');
  5. const app = getApp();
  6. /**
  7. * 会话列表页面
  8. */
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. conversations: [],
  15. myheadurl:'',
  16. isTouchMove:false,
  17. },
  18. /**
  19. * 生命周期函数--监听页面加载
  20. */
  21. onLoad(options) {
  22. let that = this;
  23. var timestamp = Date.parse(new Date());
  24. timestamp = timestamp / 1000;
  25. console.log("当前时间戳为:" + timestamp);
  26. //获取自己的头像
  27. app.postRequest('my/images/headurl', {}, function (res) {
  28. if (res.code == 201) {
  29. // console.log(res);
  30. that.setData({
  31. myheadurl: res.data.img,
  32. // optioninfocode: res,
  33. })
  34. }
  35. })
  36. },
  37. toChat(e) { //跳转进入聊天页面
  38. // console.log(e);
  39. // return ;
  40. let _that = this;
  41. for (var index in _that.data.conversations){
  42. if (_that.data.conversations[index].to_id == e.currentTarget.dataset.item.to_id){
  43. _that.data.conversations[index].count = 0;
  44. }
  45. }
  46. //唯一用户表示
  47. wx.setStorageSync('ChatListCount', e.currentTarget.dataset.item.to_str);
  48. // wx.setStorageSync('','');
  49. wx.setStorageSync('chat_to_id', e.currentTarget.dataset.item.to_id)
  50. wx.navigateTo({
  51. url: '../information/chat/chat?to_id=' + e.currentTarget.dataset.item.to_id + '&headUrl=' + e.currentTarget.dataset.item.img + '&myheadurl='+_that.data.myheadurl
  52. });
  53. },
  54. /**
  55. * 生命周期函数--监听页面显示
  56. */
  57. onShow(e) {
  58. var _this = this;
  59. try {
  60. let value = wx.getStorageSync('chat_to_id')
  61. if (value) {
  62. for (var index in _this.data.conversations) {
  63. if (_this.data.conversations[index].to_id == value) {
  64. _this.data.conversations[index].count = 0;
  65. }
  66. }
  67. }
  68. } catch (e) {
  69. }
  70. webSocket.sendSocketMessage({
  71. msg: JSON.stringify({ form_id: getApp().UserId(), type: 'Chat_list' }),
  72. success: function (res) {
  73. console.log(res, 'dakai111');
  74. }, fail: function (res) {
  75. console.log('发送消息失败')
  76. }
  77. })
  78. webSocket.onSocketMessageCallback = this.onSocketMessageCallback;
  79. // console.log(Times.dealChatTime(new Date(), 1555257947));
  80. // _this.setData({
  81. // conversations: [{
  82. // friendId:1111,
  83. // msgUserId: 2222,//消息的所有人id
  84. // friendHeadUrl: 'http://downza.img.zz314.com/edu/pc/wlgj-1008/2016-06-23/64ec0888b15773e3ba5b5f744b9df16c.jpg',//好友头像
  85. // conversationId: -1,//会话id,目前未用到
  86. // friendName: '夏天的风',//好友昵称
  87. // content:'dsadasdsa',//最新一条消息
  88. // unread: 10,//未读消息计数
  89. // timestamp: 1555257947,//最新消息的时间戳
  90. // timeStr:'2019-04-15 00:05:47'//最新消息的时间
  91. // }]
  92. // })
  93. },
  94. getConversationsItem(item) {
  95. let {latestMsg, ...msg} = item;
  96. return Object.assign(msg, JSON.parse(latestMsg));
  97. console.log(latestMsg)
  98. },
  99. onSocketMessageCallback:function(e){
  100. let _this = this;
  101. let data = JSON.parse(e);
  102. let chatlistcount = wx.getStorageSync('ChatListCount')
  103. try {
  104. } catch (e) {
  105. // Do something when catch error
  106. }
  107. switch(data.type){
  108. case 'Chat_list':
  109. let dataAtt = data.data
  110. console.log(data.data,111111);
  111. for (var index in dataAtt) {
  112. //清除缓存
  113. if (chatlistcount && dataAtt[index].to_str == chatlistcount) {
  114. wx.removeStorageSync('ChatListCount');
  115. // console.log(112233);
  116. dataAtt[index].count = 0;
  117. }
  118. dataAtt[index].timeStr = timefunc.TimeFormatting(dataAtt[index].create_at)
  119. if (dataAtt[index].type == 2) {
  120. dataAtt[index].content = '[图片]'
  121. }
  122. dataAtt[index].isTouchMove = false // 处理滑动
  123. }
  124. if(data.data){
  125. _this.setData({
  126. conversations: _this.sortarr(dataAtt)
  127. })
  128. }
  129. break;
  130. case 'chat_list_message':
  131. let dataArr = _this.data.conversations;
  132. let idData = [];
  133. console.log(data.data,'chat_list_message');
  134. for (var index in dataArr){
  135. // idData.push(String(dataArr[index].to_id));
  136. idData.push(String(dataArr[index].to_str)); //统一转换成string
  137. if (dataArr[index].to_str == data.data.to_str)
  138. {
  139. dataArr[index].timeStr = timefunc.TimeFormatting(data.data.create_at);
  140. dataArr[index].count = data.data.count;
  141. dataArr[index].content = data.data.content;
  142. dataArr[index].create_at = data.data.create_at;
  143. }
  144. // if(dataArr[index].type == 2){
  145. // dataArr[index].content = '[图片]'
  146. // }
  147. }
  148. //新增一列
  149. if (idData.indexOf(String(data.data.to_str)) == '-1'){
  150. dataArr.push(data.data);
  151. }
  152. _this.setData({
  153. conversations: _this.sortarr(dataArr)
  154. })
  155. break;
  156. }
  157. },
  158. sortarr:function(arr){//冒泡排序
  159. for(let i = 0;i<arr.length- 1; i++) {
  160. for (let j = 0; j < arr.length - 1 - i; j++) {
  161. if (arr[j].create_at < arr[j + 1].create_at) {
  162. var temp = arr[j];
  163. arr[j] = arr[j + 1];
  164. arr[j + 1] = temp;
  165. }
  166. }
  167. }
  168. return arr;
  169. },
  170. ceshi: function (e) {
  171. console.log(this.data.isTouchMove);
  172. this.setData({
  173. isTouchMove: 'true',
  174. })
  175. console.log(this.data.isTouchMove);
  176. },
  177. touchstart: function (e) {
  178. this.setData({
  179. startX: e.changedTouches[0].clientX,
  180. startY: e.changedTouches[0].clientY
  181. })
  182. },
  183. touchmove: function (e) {
  184. let index = e.currentTarget.dataset.index,//当前索引
  185. startX = this.data.startX,//开始X坐标
  186. startY = this.data.startY,//开始Y坐标
  187. touchMoveX = e.changedTouches[0].clientX,//滑动变化坐标
  188. touchMoveY = e.changedTouches[0].clientY,//滑动变化坐标
  189. //获取滑动角度
  190. angle = this.angle({ X: startX, Y: startY }, { X: touchMoveX, Y: touchMoveY });
  191. console.log(angle)
  192. this.data.list.forEach(function (v, i) {
  193. v.isTouchMove = false
  194. //滑动超过30度角 return
  195. if (Math.abs(angle) > 30) return;
  196. if (i == index) {
  197. if (touchMoveX > startX) //右滑
  198. v.isTouchMove = false
  199. else //左滑
  200. v.isTouchMove = true
  201. }
  202. })
  203. // //更新数据
  204. this.setData({
  205. list: this.data.list
  206. })
  207. },
  208. angle: function (start, end) {
  209. var _X = end.X - start.X,
  210. _Y = end.Y - start.Y
  211. //返回角度 /Math.atan()返回数字的反正切值
  212. return 360 * Math.atan(_Y / _X) / (2 * Math.PI)
  213. },
  214. });