123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- // pages/chat-list/chat-list.js
- // import Times from "../../utils/time";
- const webSocket = require('../../utils/websocket.js');
- const timefunc = require('../../utils/time.js');
- const app = getApp();
- /**
- * 会话列表页面
- */
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- conversations: [],
- myheadurl:'',
- isTouchMove:false,
-
-
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad(options) {
- let that = this;
- var timestamp = Date.parse(new Date());
- timestamp = timestamp / 1000;
- console.log("当前时间戳为:" + timestamp);
-
- //获取自己的头像
- app.postRequest('my/images/headurl', {}, function (res) {
- if (res.code == 201) {
- // console.log(res);
- that.setData({
- myheadurl: res.data.img,
- // optioninfocode: res,
- })
- }
- })
- },
- toChat(e) { //跳转进入聊天页面
- // console.log(e);
- // return ;
- let _that = this;
- for (var index in _that.data.conversations){
- if (_that.data.conversations[index].to_id == e.currentTarget.dataset.item.to_id){
- _that.data.conversations[index].count = 0;
- }
- }
-
- //唯一用户表示
- wx.setStorageSync('ChatListCount', e.currentTarget.dataset.item.to_str);
- // wx.setStorageSync('','');
- wx.setStorageSync('chat_to_id', e.currentTarget.dataset.item.to_id)
- wx.navigateTo({
- url: '../information/chat/chat?to_id=' + e.currentTarget.dataset.item.to_id + '&headUrl=' + e.currentTarget.dataset.item.img + '&myheadurl='+_that.data.myheadurl
- });
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow(e) {
- var _this = this;
- try {
- let value = wx.getStorageSync('chat_to_id')
- if (value) {
- for (var index in _this.data.conversations) {
- if (_this.data.conversations[index].to_id == value) {
- _this.data.conversations[index].count = 0;
- }
- }
- }
- } catch (e) {
- }
- webSocket.sendSocketMessage({
- msg: JSON.stringify({ form_id: getApp().UserId(), type: 'Chat_list' }),
- success: function (res) {
- console.log(res, 'dakai111');
- }, fail: function (res) {
- console.log('发送消息失败')
- }
- })
- webSocket.onSocketMessageCallback = this.onSocketMessageCallback;
- // console.log(Times.dealChatTime(new Date(), 1555257947));
- // _this.setData({
- // conversations: [{
- // friendId:1111,
- // msgUserId: 2222,//消息的所有人id
- // friendHeadUrl: 'http://downza.img.zz314.com/edu/pc/wlgj-1008/2016-06-23/64ec0888b15773e3ba5b5f744b9df16c.jpg',//好友头像
- // conversationId: -1,//会话id,目前未用到
- // friendName: '夏天的风',//好友昵称
- // content:'dsadasdsa',//最新一条消息
- // unread: 10,//未读消息计数
- // timestamp: 1555257947,//最新消息的时间戳
- // timeStr:'2019-04-15 00:05:47'//最新消息的时间
- // }]
- // })
- },
- getConversationsItem(item) {
- let {latestMsg, ...msg} = item;
- return Object.assign(msg, JSON.parse(latestMsg));
- console.log(latestMsg)
- },
- onSocketMessageCallback:function(e){
- let _this = this;
- let data = JSON.parse(e);
- let chatlistcount = wx.getStorageSync('ChatListCount')
- try {
-
-
- } catch (e) {
- // Do something when catch error
- }
- switch(data.type){
- case 'Chat_list':
- let dataAtt = data.data
- console.log(data.data,111111);
- for (var index in dataAtt) {
- //清除缓存
- if (chatlistcount && dataAtt[index].to_str == chatlistcount) {
- wx.removeStorageSync('ChatListCount');
- // console.log(112233);
- dataAtt[index].count = 0;
- }
- dataAtt[index].timeStr = timefunc.TimeFormatting(dataAtt[index].create_at)
- if (dataAtt[index].type == 2) {
- dataAtt[index].content = '[图片]'
- }
-
- dataAtt[index].isTouchMove = false // 处理滑动
- }
- if(data.data){
- _this.setData({
- conversations: _this.sortarr(dataAtt)
- })
- }
-
- break;
- case 'chat_list_message':
- let dataArr = _this.data.conversations;
- let idData = [];
- console.log(data.data,'chat_list_message');
-
- for (var index in dataArr){
- // idData.push(String(dataArr[index].to_id));
- idData.push(String(dataArr[index].to_str)); //统一转换成string
-
- if (dataArr[index].to_str == data.data.to_str)
- {
- dataArr[index].timeStr = timefunc.TimeFormatting(data.data.create_at);
- dataArr[index].count = data.data.count;
- dataArr[index].content = data.data.content;
- dataArr[index].create_at = data.data.create_at;
- }
- // if(dataArr[index].type == 2){
- // dataArr[index].content = '[图片]'
- // }
- }
-
- //新增一列
- if (idData.indexOf(String(data.data.to_str)) == '-1'){
- dataArr.push(data.data);
- }
- _this.setData({
- conversations: _this.sortarr(dataArr)
- })
- break;
- }
- },
- sortarr:function(arr){//冒泡排序
- for(let i = 0;i<arr.length- 1; i++) {
- for (let j = 0; j < arr.length - 1 - i; j++) {
- if (arr[j].create_at < arr[j + 1].create_at) {
- var temp = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = temp;
- }
- }
- }
- return arr;
- },
- ceshi: function (e) {
- console.log(this.data.isTouchMove);
- this.setData({
- isTouchMove: 'true',
- })
-
- console.log(this.data.isTouchMove);
- },
-
- touchstart: function (e) {
- this.setData({
- startX: e.changedTouches[0].clientX,
- startY: e.changedTouches[0].clientY
- })
- },
- touchmove: function (e) {
- let index = e.currentTarget.dataset.index,//当前索引
- startX = this.data.startX,//开始X坐标
- startY = this.data.startY,//开始Y坐标
- touchMoveX = e.changedTouches[0].clientX,//滑动变化坐标
- touchMoveY = e.changedTouches[0].clientY,//滑动变化坐标
- //获取滑动角度
- angle = this.angle({ X: startX, Y: startY }, { X: touchMoveX, Y: touchMoveY });
- console.log(angle)
- this.data.list.forEach(function (v, i) {
- v.isTouchMove = false
- //滑动超过30度角 return
- if (Math.abs(angle) > 30) return;
- if (i == index) {
- if (touchMoveX > startX) //右滑
- v.isTouchMove = false
- else //左滑
- v.isTouchMove = true
- }
- })
- // //更新数据
- this.setData({
- list: this.data.list
- })
- },
- angle: function (start, end) {
- var _X = end.X - start.X,
- _Y = end.Y - start.Y
- //返回角度 /Math.atan()返回数字的反正切值
- return 360 * Math.atan(_Y / _X) / (2 * Math.PI)
- },
- });
|