123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- // pages/chat/chat.js
- const {getUserInfoList,getChattingRecord,getGoodsInfo,setWsConnect} = require('../../API/appraise')
- import messageMap from '../../utils/util'
- var socketMsgQueue = {
- from: wx.getStorageSync('uid'),
- to: '',
- msgType: 1,
- time: '',
- content: ''
- }
- var getUserInfoListOption = {
- data:{
- uid:[]
- }
- }
- var getChattingRecordOption = {
- uid:wx.getStorageSync('uid'),
- target:''
- }
- var getGoodsInfoOption = {
- id:'',
- uid:'',
- date:''
- }
- var setWsConnectOption = {
- uid: wx.getStorageSync('uid')
- }
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- toUid:'',
- goodsId:'',
- goods: {
- pic:'',
- name:'',
- price: 0,
- },
- //登录用户的信息
- login: {
- id: wx.getStorageSync('uid')
- },
- chatList: [
- // {
- // from: '7385700531999608832',
- // to: '7384540497554968576',
- // nickname:'daisy',
- // avatar: 'http://192.168.31.30:8084/pics/1698753512AiD8PaNrvqsDb1493fda534929a5b5d05f80a8cf3d16.jpg',
- // message: '你好',
- // type: 'text',
- // time: '11-8 20:52'
- // },
- // {
- // to: '7385700531999608832',
- // from: '7384540497554968576',
- // nickname:'无情的敲代码机器人',
- // avatar: 'http://192.168.31.30:8084/pics/1699175056BvtgC8vv8gSzd85dedb14362dc3a251c6c85705ec495.jpg',
- // message: '你好',
- // type: 'text',
- // time: '11-8 20:52'
- // }
- ]
-
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad(options) {
- //获取商品信息
- this.data.toUid = options.toUid;
- this.data.goodsId = options.goodsId;
- getChattingRecordOption.target = this.data.toUid;
- let time = new Date().getTime();
- getGoodsInfoOption = {
- id:this.data.goodsId,
- uid:wx.getStorageSync('uid'),
- date:time
- }
- getGoodsInfo(getGoodsInfoOption).then((res)=>{
- console.log(res);
- const good = res.data.data;
- this.setData({
- goods:{
- pic:good.Pic,
- name: good.Title,
- price: good.Price
- }
- })
- }).catch((err)=>{
- console.log(err);
- })
- //获取用户信息
- getUserInfoListOption.data.uid = [this.data.toUid];
- getUserInfoList(getUserInfoListOption).then((res)=> {
- console.log(res);
- const {users} = res.data.data;
- wx.setStorageSync('anotherNickName', users[0].name);
- wx.setStorageSync('anotherAvatar', users[0].avatar);
- }).catch((err)=>{
- console.log(err);
- })
- //建立ws连接
- 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();
- 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;
- //处理数据
- if(res.from == this.data.toUid){
- let msg = {
- from: res.from,
- to:res.to,
- message: res.content,
- type: 'text',
- time: now1 + '-' + now2 + ' ' + hour + ':' + minu,
- nickname: wx.getStorageSync('anotherNiakName'),
- avatar: wx.getStorageSync('anotherAvatar')
- }
- console.log(msg);
- this.setData({
- chatList: list.concat(msg)
- }, () => {
- that.scrollToBottom();
- that.setData({
- content: ''
- })
- })
- }
-
- }
- })
-
- this.scrollToBottom();
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady() {
-
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide() {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload() {
- wx.closeSocket();
- console.log('websocket 已关闭!');
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh() {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom() {
- },
- /**
- * 用户点击右上角分享
- */
- 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;
- 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
- }
- console.log(msg);
- this.setData({
- chatList: list.concat(msg)
- }, () => {
- that.scrollToBottom();
- that.setData({
- content: ''
- })
- })
- let socketMsg = socketMsgQueue;
- socketMsg.msgType = 1;
- socketMsg.content = this.data.content;
- socketMsg.to = this.data.toUid;
- console.log(this.data.toUid);
- let time = new Date().getTime();
- console.log(time);
- socketMsg.time = time;
- console.log(socketMsg);
- wx.sendSocketMessage({
- data: JSON.stringify(socketMsg),
- })
- },
-
-
-
- // 滑动到最底部
- scrollToBottom() {
- setTimeout(() => {
- wx.pageScrollTo({
- scrollTop: 200000,
- duration: 3
- });
- }, 600)
- },
- })
|