eason 1 سال پیش
والد
کامیت
e143999297
3فایلهای تغییر یافته به همراه163 افزوده شده و 121 حذف شده
  1. 1 1
      fore-end/API/appraise.js
  2. 144 116
      fore-end/pages/chat/chat.js
  3. 18 4
      fore-end/pages/details/details.js

+ 1 - 1
fore-end/API/appraise.js

@@ -171,7 +171,7 @@ function addUserRelation(params) {
 	return request.post(
 		API.addUserRelationUrl,
 		{
-			data:params.data,
+			data:params,
 		}
 	)
 }

+ 144 - 116
fore-end/pages/chat/chat.js

@@ -1,6 +1,14 @@
 // 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'
 var socketMsgQueue = {
 	from: wx.getStorageSync('uid'),
@@ -10,23 +18,23 @@ var socketMsgQueue = {
 	content: ''
 }
 var getUserInfoListOption = {
-	data:{
-		id:[]
+	data: {
+		id: []
 	}
 }
 var getChattingRecordOption = {
-	uid:wx.getStorageSync('uid'),
-	target:''
+	uid: wx.getStorageSync('uid'),
+	target: ''
 }
 var getGoodsInfoOption = {
-	id:'',
-	uid:'',
-	date:''
+	id: '',
+	uid: '',
+	date: ''
 }
 
 var getChattingRecordOption = {
-	uid:'',
-	target:''
+	uid: '',
+	target: ''
 }
 var setWsConnectOption = {
 	uid: wx.getStorageSync('uid')
@@ -37,11 +45,11 @@ Page({
 	 * 页面的初始数据
 	 */
 	data: {
-		toUid:'',
-		goodsId:'',
+		toUid: '',
+		goodsId: '',
 		goods: {
-			pic:'',
-			name:'',
+			pic: '',
+			name: '',
 			price: 0,
 		},
 		//登录用户的信息
@@ -68,7 +76,7 @@ Page({
 			// 	time: '11-8 20:52'
 			// }
 		]
-		
+
 
 	},
 
@@ -78,47 +86,49 @@ Page({
 	onLoad(options) {
 		//获取商品信息
 		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;
 		let time = new Date().getTime();
 		getGoodsInfoOption = {
-			id:this.data.goodsId,
-			uid:wx.getStorageSync('uid'),
-			date:time
+			id: this.data.goodsId,
+			uid: wx.getStorageSync('uid'),
+			date: time
 		}
 		let targetId = '';
-		getGoodsInfo(getGoodsInfoOption).then((res)=>{
+		getGoodsInfo(getGoodsInfoOption).then((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({
-				goods:{
+				goods: {
 					name: good.Title,
-                    price: good.Price,
-                    pic: good.Pic
-                }
-            })
-		}).catch((err)=>{
+					price: good.Price,
+					pic: good.Pic
+				}
+			})
+		}).catch((err) => {
 			console.log(err);
 		})
 		//获取对方用户信息
 		getUserInfoListOption.data.id = [this.data.toUid];
-		getUserInfoList(getUserInfoListOption).then((res)=> {
+		getUserInfoList(getUserInfoListOption).then((res) => {
 			console.log(res);
-			const {users} = res.data.data;
+			const {
+				users
+			} = res.data.data;
 			targetId = users[0].uid;
 			getChattingRecordOption.target = targetId;
 			wx.setStorageSync('anotherNickName', users[0].name);
 			wx.setStorageSync('anotherAvatar', users[0].avatar);
-		}).catch((err)=>{
+		}).catch((err) => {
 			console.log(err);
 		})
 
 		//获取用户本人信息
-		getUserInfo().then((res)=>{
+		getUserInfo().then((res) => {
 			console.log(res);
 			let data = res.data.data;
 			wx.setStorageSync('avatar', data.avatar);
@@ -130,23 +140,22 @@ Page({
 		//获取聊天记录
 		getChattingRecordOption.uid = wx.getStorageSync('uid');
 		console.log(getChattingRecordOption);
-		getChattingRecord(getChattingRecordOption).then((res)=>{
+		getChattingRecord(getChattingRecordOption).then((res) => {
 			let data = res.data.data;
 			console.log(data);
+			data.sort(sortBy('time', 1));
 			let chatList1 = [];
-			for(let i = 0;i<data.length;i++)
-			{
+			for (let i = 0; i < data.length; i++) {
 				let message = {};
 				message['from'] = data[i].from;
 				message['to'] = data[i].to;
 				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);
-				if(data[i].from === wx.getStorageSync('uid'))
-				{
+				if (data[i].from === wx.getStorageSync('uid')) {
 					message['nickname'] = wx.getStorageSync('nickname');
 					message['avatar'] = wx.getStorageSync('avatar');
-				}else {
+				} else {
 					message['nickname'] = wx.getStorageSync('anotherNickName');
 					message['avatar'] = wx.getStorageSync('anotherAvatar');
 				}
@@ -155,49 +164,68 @@ Page({
 				console.log(chatList1);
 			}
 			this.setData({
-				chatList:chatList1,
+				chatList: chatList1,
 			})
-		}).catch((err)=>{
+		}).catch((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连接
-		setWsConnect(setWsConnectOption).then((res)=> {
+		setWsConnect(setWsConnectOption).then((res) => {
 			console.log(res);
 		})
 		// 监听服务器的数据返回
 		wx.onSocketMessage((result) => {
 			console.log("服务器的数据返回", result);
 			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();
 	},
 
@@ -205,7 +233,7 @@ Page({
 	 * 生命周期函数--监听页面初次渲染完成
 	 */
 	onReady() {
-		
+
 	},
 
 	/**
@@ -250,42 +278,42 @@ Page({
 	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 msg = {
 			from: wx.getStorageSync('uid'),
 			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);
-        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;
 		socketMsg.msgType = 1;
@@ -297,19 +325,19 @@ Page({
 		socketMsg.time = time;
 		console.log(socketMsg);
 		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)
+	},
 })

+ 18 - 4
fore-end/pages/details/details.js

@@ -1,5 +1,5 @@
 // pages/details/details.js
-const { getGoodsDetail, getUserInfoList } = require('../../API/appraise')
+const { getGoodsDetail, getUserInfoList, addUserRelation } = require('../../API/appraise')
 const { formatTime } = require('../../utils/util')
 
 Page({
@@ -46,9 +46,23 @@ Page({
         this.getUserInfo();
     },
     toChat() {
-        wx.navigateTo({
-          url: '/pages/chat/chat?toUid=' + this.data.uid + '&goodsId=' + this.data.id + '&pic=' + this.data.pic,
-        })
+		let _this = this.data;
+		let $id = parseInt(_this.id);
+		let addUserRelationOption = {
+			goodsId:$id,
+			owner:wx.getStorageSync('uid'),
+			target:_this.uid,
+			type:1
+		}
+		console.log(addUserRelationOption);
+		addUserRelation(addUserRelationOption).then((res) => {
+			console.log(res);
+			wx.navigateTo({
+				url: '/pages/chat/chat?toUid=' + this.data.uid + '&goodsId=' + this.data.id + '&pic=' + this.data.pic,
+			  })
+		}).catch((err) => {
+			console.log(err);
+		})
     },
     GoodsDetail() {
         getGoodsDetail(this.data).then((res) => {