|
@@ -1,6 +1,14 @@
|
|
// pages/chat/chat.js
|
|
// pages/chat/chat.js
|
|
-const {getUserInfoList,getChattingRecord,getGoodsInfo,setWsConnect,getUserInfo} = require('../../API/appraise');
|
|
|
|
-const {formatTime} = require('../../utils/util')
|
|
|
|
|
|
+const {
|
|
|
|
+ getUserInfoList,
|
|
|
|
+ getChattingRecord,
|
|
|
|
+ getGoodsInfo,
|
|
|
|
+ setWsConnect,
|
|
|
|
+ getUserInfo
|
|
|
|
+} = require('../../API/appraise');
|
|
|
|
+const {
|
|
|
|
+ formatTime
|
|
|
|
+} = require('../../utils/util')
|
|
import messageMap from '../../utils/util'
|
|
import messageMap from '../../utils/util'
|
|
var socketMsgQueue = {
|
|
var socketMsgQueue = {
|
|
from: wx.getStorageSync('uid'),
|
|
from: wx.getStorageSync('uid'),
|
|
@@ -10,23 +18,23 @@ var socketMsgQueue = {
|
|
content: ''
|
|
content: ''
|
|
}
|
|
}
|
|
var getUserInfoListOption = {
|
|
var getUserInfoListOption = {
|
|
- data:{
|
|
|
|
- id:[]
|
|
|
|
|
|
+ data: {
|
|
|
|
+ id: []
|
|
}
|
|
}
|
|
}
|
|
}
|
|
var getChattingRecordOption = {
|
|
var getChattingRecordOption = {
|
|
- uid:wx.getStorageSync('uid'),
|
|
|
|
- target:''
|
|
|
|
|
|
+ uid: wx.getStorageSync('uid'),
|
|
|
|
+ target: ''
|
|
}
|
|
}
|
|
var getGoodsInfoOption = {
|
|
var getGoodsInfoOption = {
|
|
- id:'',
|
|
|
|
- uid:'',
|
|
|
|
- date:''
|
|
|
|
|
|
+ id: '',
|
|
|
|
+ uid: '',
|
|
|
|
+ date: ''
|
|
}
|
|
}
|
|
|
|
|
|
var getChattingRecordOption = {
|
|
var getChattingRecordOption = {
|
|
- uid:'',
|
|
|
|
- target:''
|
|
|
|
|
|
+ uid: '',
|
|
|
|
+ target: ''
|
|
}
|
|
}
|
|
var setWsConnectOption = {
|
|
var setWsConnectOption = {
|
|
uid: wx.getStorageSync('uid')
|
|
uid: wx.getStorageSync('uid')
|
|
@@ -37,11 +45,11 @@ Page({
|
|
* 页面的初始数据
|
|
* 页面的初始数据
|
|
*/
|
|
*/
|
|
data: {
|
|
data: {
|
|
- toUid:'',
|
|
|
|
- goodsId:'',
|
|
|
|
|
|
+ toUid: '',
|
|
|
|
+ goodsId: '',
|
|
goods: {
|
|
goods: {
|
|
- pic:'',
|
|
|
|
- name:'',
|
|
|
|
|
|
+ pic: '',
|
|
|
|
+ name: '',
|
|
price: 0,
|
|
price: 0,
|
|
},
|
|
},
|
|
//登录用户的信息
|
|
//登录用户的信息
|
|
@@ -68,7 +76,7 @@ Page({
|
|
// time: '11-8 20:52'
|
|
// time: '11-8 20:52'
|
|
// }
|
|
// }
|
|
]
|
|
]
|
|
-
|
|
|
|
|
|
+
|
|
|
|
|
|
},
|
|
},
|
|
|
|
|
|
@@ -78,47 +86,49 @@ Page({
|
|
onLoad(options) {
|
|
onLoad(options) {
|
|
//获取商品信息
|
|
//获取商品信息
|
|
this.data.toUid = options.toUid;
|
|
this.data.toUid = options.toUid;
|
|
- this.data.goodsId = options.goodsId;
|
|
|
|
- this.data.goods.pic = options.pic;
|
|
|
|
|
|
+ this.data.goodsId = options.goodsId;
|
|
|
|
+ this.data.goods.pic = options.pic;
|
|
getChattingRecordOption.target = this.data.toUid;
|
|
getChattingRecordOption.target = this.data.toUid;
|
|
let time = new Date().getTime();
|
|
let time = new Date().getTime();
|
|
getGoodsInfoOption = {
|
|
getGoodsInfoOption = {
|
|
- id:this.data.goodsId,
|
|
|
|
- uid:wx.getStorageSync('uid'),
|
|
|
|
- date:time
|
|
|
|
|
|
+ id: this.data.goodsId,
|
|
|
|
+ uid: wx.getStorageSync('uid'),
|
|
|
|
+ date: time
|
|
}
|
|
}
|
|
let targetId = '';
|
|
let targetId = '';
|
|
- getGoodsInfo(getGoodsInfoOption).then((res)=>{
|
|
|
|
|
|
+ getGoodsInfo(getGoodsInfoOption).then((res) => {
|
|
// console.log(res);
|
|
// console.log(res);
|
|
- const good = res.data.data;
|
|
|
|
- if(good.Pic == null) {
|
|
|
|
- good.Pic = this.data.goods.pic
|
|
|
|
- }
|
|
|
|
|
|
+ const good = res.data.data;
|
|
|
|
+ if (good.Pic == null) {
|
|
|
|
+ good.Pic = this.data.goods.pic
|
|
|
|
+ }
|
|
this.setData({
|
|
this.setData({
|
|
- goods:{
|
|
|
|
|
|
+ goods: {
|
|
name: good.Title,
|
|
name: good.Title,
|
|
- price: good.Price,
|
|
|
|
- pic: good.Pic
|
|
|
|
- }
|
|
|
|
- })
|
|
|
|
- }).catch((err)=>{
|
|
|
|
|
|
+ price: good.Price,
|
|
|
|
+ pic: good.Pic
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ }).catch((err) => {
|
|
console.log(err);
|
|
console.log(err);
|
|
})
|
|
})
|
|
//获取对方用户信息
|
|
//获取对方用户信息
|
|
getUserInfoListOption.data.id = [this.data.toUid];
|
|
getUserInfoListOption.data.id = [this.data.toUid];
|
|
- getUserInfoList(getUserInfoListOption).then((res)=> {
|
|
|
|
|
|
+ getUserInfoList(getUserInfoListOption).then((res) => {
|
|
console.log(res);
|
|
console.log(res);
|
|
- const {users} = res.data.data;
|
|
|
|
|
|
+ const {
|
|
|
|
+ users
|
|
|
|
+ } = res.data.data;
|
|
targetId = users[0].uid;
|
|
targetId = users[0].uid;
|
|
getChattingRecordOption.target = targetId;
|
|
getChattingRecordOption.target = targetId;
|
|
wx.setStorageSync('anotherNickName', users[0].name);
|
|
wx.setStorageSync('anotherNickName', users[0].name);
|
|
wx.setStorageSync('anotherAvatar', users[0].avatar);
|
|
wx.setStorageSync('anotherAvatar', users[0].avatar);
|
|
- }).catch((err)=>{
|
|
|
|
|
|
+ }).catch((err) => {
|
|
console.log(err);
|
|
console.log(err);
|
|
})
|
|
})
|
|
|
|
|
|
//获取用户本人信息
|
|
//获取用户本人信息
|
|
- getUserInfo().then((res)=>{
|
|
|
|
|
|
+ getUserInfo().then((res) => {
|
|
console.log(res);
|
|
console.log(res);
|
|
let data = res.data.data;
|
|
let data = res.data.data;
|
|
wx.setStorageSync('avatar', data.avatar);
|
|
wx.setStorageSync('avatar', data.avatar);
|
|
@@ -130,23 +140,22 @@ Page({
|
|
//获取聊天记录
|
|
//获取聊天记录
|
|
getChattingRecordOption.uid = wx.getStorageSync('uid');
|
|
getChattingRecordOption.uid = wx.getStorageSync('uid');
|
|
console.log(getChattingRecordOption);
|
|
console.log(getChattingRecordOption);
|
|
- getChattingRecord(getChattingRecordOption).then((res)=>{
|
|
|
|
|
|
+ getChattingRecord(getChattingRecordOption).then((res) => {
|
|
let data = res.data.data;
|
|
let data = res.data.data;
|
|
console.log(data);
|
|
console.log(data);
|
|
|
|
+ data.sort(sortBy('time', 1));
|
|
let chatList1 = [];
|
|
let chatList1 = [];
|
|
- for(let i = 0;i<data.length;i++)
|
|
|
|
- {
|
|
|
|
|
|
+ for (let i = 0; i < data.length; i++) {
|
|
let message = {};
|
|
let message = {};
|
|
message['from'] = data[i].from;
|
|
message['from'] = data[i].from;
|
|
message['to'] = data[i].to;
|
|
message['to'] = data[i].to;
|
|
message['message'] = data[i].content;
|
|
message['message'] = data[i].content;
|
|
- message['type'] = data[i].msgType == 1 ? 'text':'pic';
|
|
|
|
|
|
+ message['type'] = data[i].msgType == 1 ? 'text' : 'pic';
|
|
message['time'] = formatTime(data[i].time);
|
|
message['time'] = formatTime(data[i].time);
|
|
- if(data[i].from === wx.getStorageSync('uid'))
|
|
|
|
- {
|
|
|
|
|
|
+ if (data[i].from === wx.getStorageSync('uid')) {
|
|
message['nickname'] = wx.getStorageSync('nickname');
|
|
message['nickname'] = wx.getStorageSync('nickname');
|
|
message['avatar'] = wx.getStorageSync('avatar');
|
|
message['avatar'] = wx.getStorageSync('avatar');
|
|
- }else {
|
|
|
|
|
|
+ } else {
|
|
message['nickname'] = wx.getStorageSync('anotherNickName');
|
|
message['nickname'] = wx.getStorageSync('anotherNickName');
|
|
message['avatar'] = wx.getStorageSync('anotherAvatar');
|
|
message['avatar'] = wx.getStorageSync('anotherAvatar');
|
|
}
|
|
}
|
|
@@ -155,49 +164,68 @@ Page({
|
|
console.log(chatList1);
|
|
console.log(chatList1);
|
|
}
|
|
}
|
|
this.setData({
|
|
this.setData({
|
|
- chatList:chatList1,
|
|
|
|
|
|
+ chatList: chatList1,
|
|
})
|
|
})
|
|
- }).catch((err)=>{
|
|
|
|
|
|
+ }).catch((err) => {
|
|
console.log(err);
|
|
console.log(err);
|
|
})
|
|
})
|
|
|
|
|
|
|
|
+
|
|
|
|
+ function sortBy(attr, rev) {
|
|
|
|
+ if (rev == undefined) {
|
|
|
|
+ rev = 1
|
|
|
|
+ } else {
|
|
|
|
+ (rev) ? 1: -1;
|
|
|
|
+ }
|
|
|
|
+ return function (a, b) {
|
|
|
|
+ a = a[attr];
|
|
|
|
+ b = b[attr];
|
|
|
|
+ if (a < b) {
|
|
|
|
+ return rev * -1
|
|
|
|
+ }
|
|
|
|
+ if (a > b) {
|
|
|
|
+ return rev * 1
|
|
|
|
+ }
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
//建立ws连接
|
|
//建立ws连接
|
|
- setWsConnect(setWsConnectOption).then((res)=> {
|
|
|
|
|
|
+ setWsConnect(setWsConnectOption).then((res) => {
|
|
console.log(res);
|
|
console.log(res);
|
|
})
|
|
})
|
|
// 监听服务器的数据返回
|
|
// 监听服务器的数据返回
|
|
wx.onSocketMessage((result) => {
|
|
wx.onSocketMessage((result) => {
|
|
console.log("服务器的数据返回", result);
|
|
console.log("服务器的数据返回", result);
|
|
const res = JSON.parse(result.data);
|
|
const res = JSON.parse(result.data);
|
|
- if(res.msgType == 1){
|
|
|
|
- //获取当前时间
|
|
|
|
- let date = new Date();
|
|
|
|
- console.log(date);
|
|
|
|
- //处理数据
|
|
|
|
- if(res.from == this.data.toUid){
|
|
|
|
- let msg = {
|
|
|
|
- from: res.from,
|
|
|
|
- to:res.to,
|
|
|
|
- message: res.content,
|
|
|
|
- type: 'text',
|
|
|
|
- time: formatTime(date),
|
|
|
|
- nickname: wx.getStorageSync('anotherNiakName'),
|
|
|
|
- avatar: wx.getStorageSync('anotherAvatar')
|
|
|
|
- }
|
|
|
|
- console.log(msg);
|
|
|
|
- this.setData({
|
|
|
|
- chatList: list.concat(msg)
|
|
|
|
- }, () => {
|
|
|
|
- that.scrollToBottom();
|
|
|
|
- that.setData({
|
|
|
|
- content: ''
|
|
|
|
|
|
+ if (res.msgType == 1) {
|
|
|
|
+ //获取当前时间
|
|
|
|
+ let date = new Date();
|
|
|
|
+ console.log(date);
|
|
|
|
+ //处理数据
|
|
|
|
+ if (res.from == this.data.toUid) {
|
|
|
|
+ let msg = {
|
|
|
|
+ from: res.from,
|
|
|
|
+ to: res.to,
|
|
|
|
+ message: res.content,
|
|
|
|
+ type: 'text',
|
|
|
|
+ time: formatTime(date),
|
|
|
|
+ nickname: wx.getStorageSync('anotherNiakName'),
|
|
|
|
+ avatar: wx.getStorageSync('anotherAvatar')
|
|
|
|
+ }
|
|
|
|
+ console.log(msg);
|
|
|
|
+ this.setData({
|
|
|
|
+ chatList: list.concat(msg)
|
|
|
|
+ }, () => {
|
|
|
|
+ that.scrollToBottom();
|
|
|
|
+ that.setData({
|
|
|
|
+ content: ''
|
|
|
|
+ })
|
|
})
|
|
})
|
|
- })
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|
|
- })
|
|
|
|
-
|
|
|
|
|
|
+ })
|
|
|
|
+
|
|
this.scrollToBottom();
|
|
this.scrollToBottom();
|
|
},
|
|
},
|
|
|
|
|
|
@@ -205,7 +233,7 @@ Page({
|
|
* 生命周期函数--监听页面初次渲染完成
|
|
* 生命周期函数--监听页面初次渲染完成
|
|
*/
|
|
*/
|
|
onReady() {
|
|
onReady() {
|
|
-
|
|
|
|
|
|
+
|
|
},
|
|
},
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -250,42 +278,42 @@ Page({
|
|
onShareAppMessage() {
|
|
onShareAppMessage() {
|
|
|
|
|
|
},
|
|
},
|
|
- // 输入监听
|
|
|
|
- inputClick(e) {
|
|
|
|
- this.setData({
|
|
|
|
- content: e.detail.value
|
|
|
|
- })
|
|
|
|
- },
|
|
|
|
- // 发送监听
|
|
|
|
- sendClick() {
|
|
|
|
- let that = this;
|
|
|
|
- let list = this.data.chatList;
|
|
|
|
- // 获取当前时间
|
|
|
|
- let date = new Date();
|
|
|
|
- let month = date.getMonth() + 1;
|
|
|
|
- let day = date.getDate();
|
|
|
|
- let hour = date.getHours();
|
|
|
|
- let minu = date.getMinutes();
|
|
|
|
- let now1 = month < 10 ? '0' + month : month;
|
|
|
|
|
|
+ // 输入监听
|
|
|
|
+ inputClick(e) {
|
|
|
|
+ this.setData({
|
|
|
|
+ content: e.detail.value
|
|
|
|
+ })
|
|
|
|
+ },
|
|
|
|
+ // 发送监听
|
|
|
|
+ sendClick() {
|
|
|
|
+ let that = this;
|
|
|
|
+ let list = this.data.chatList;
|
|
|
|
+ // 获取当前时间
|
|
|
|
+ let date = new Date();
|
|
|
|
+ let month = date.getMonth() + 1;
|
|
|
|
+ let day = date.getDate();
|
|
|
|
+ let hour = date.getHours();
|
|
|
|
+ let minu = date.getMinutes();
|
|
|
|
+ let now1 = month < 10 ? '0' + month : month;
|
|
let now2 = day < 10 ? '0' + day : day;
|
|
let now2 = day < 10 ? '0' + day : day;
|
|
// 组装数据
|
|
// 组装数据
|
|
let msg = {
|
|
let msg = {
|
|
from: wx.getStorageSync('uid'),
|
|
from: wx.getStorageSync('uid'),
|
|
to: this.data.toUid,
|
|
to: this.data.toUid,
|
|
- nickname: wx.getStorageSync('nickName'),
|
|
|
|
- avatar: wx.getStorageSync('avatarUrl'),
|
|
|
|
- message: this.data.content,
|
|
|
|
- type: 'text',
|
|
|
|
- time: now1 + '-' + now2 + ' ' + hour + ':' + minu
|
|
|
|
|
|
+ nickname: wx.getStorageSync('nickName'),
|
|
|
|
+ avatar: wx.getStorageSync('avatarUrl'),
|
|
|
|
+ message: this.data.content,
|
|
|
|
+ type: 'text',
|
|
|
|
+ time: now1 + '-' + now2 + ' ' + hour + ':' + minu
|
|
}
|
|
}
|
|
console.log(msg);
|
|
console.log(msg);
|
|
- this.setData({
|
|
|
|
- chatList: list.concat(msg)
|
|
|
|
- }, () => {
|
|
|
|
- that.scrollToBottom();
|
|
|
|
- that.setData({
|
|
|
|
- content: ''
|
|
|
|
- })
|
|
|
|
|
|
+ this.setData({
|
|
|
|
+ chatList: list.concat(msg)
|
|
|
|
+ }, () => {
|
|
|
|
+ that.scrollToBottom();
|
|
|
|
+ that.setData({
|
|
|
|
+ content: ''
|
|
|
|
+ })
|
|
})
|
|
})
|
|
let socketMsg = socketMsgQueue;
|
|
let socketMsg = socketMsgQueue;
|
|
socketMsg.msgType = 1;
|
|
socketMsg.msgType = 1;
|
|
@@ -297,19 +325,19 @@ Page({
|
|
socketMsg.time = time;
|
|
socketMsg.time = time;
|
|
console.log(socketMsg);
|
|
console.log(socketMsg);
|
|
wx.sendSocketMessage({
|
|
wx.sendSocketMessage({
|
|
- data: JSON.stringify(socketMsg),
|
|
|
|
|
|
+ data: JSON.stringify(socketMsg),
|
|
})
|
|
})
|
|
},
|
|
},
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- // 滑动到最底部
|
|
|
|
- scrollToBottom() {
|
|
|
|
- setTimeout(() => {
|
|
|
|
- wx.pageScrollTo({
|
|
|
|
- scrollTop: 200000,
|
|
|
|
- duration: 3
|
|
|
|
- });
|
|
|
|
- }, 600)
|
|
|
|
- },
|
|
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ // 滑动到最底部
|
|
|
|
+ scrollToBottom() {
|
|
|
|
+ setTimeout(() => {
|
|
|
|
+ wx.pageScrollTo({
|
|
|
|
+ scrollTop: 200000,
|
|
|
|
+ duration: 3
|
|
|
|
+ });
|
|
|
|
+ }, 600)
|
|
|
|
+ },
|
|
})
|
|
})
|