123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- // pages/chat/chat.js
- const {getUserInfoList,getChattingRecord} = 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:''
- }
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- toUid:'',
- goodsId:'',
- goods: {
- pic:'',
- name:'',
- price:''
- },
- //登录用户的信息
- 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'
- },
- {
- to: '7385700531999608832',
- from: '7384540497554968576',
- nickname:'无情的敲代码机器人',
- avatar: 'http://192.168.31.30:8084/pics/1699175056BvtgC8vv8gSzd85dedb14362dc3a251c6c85705ec495.jpg',
- message: '你好',
- type: 'text'
- }
- ]
-
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad(options) {
- this.data.toUid = options.toUid;
- this.data.goodsId = options.goodsId;
- getChattingRecordOption.target = this.data.toUid;
- getChattingRecord(getChattingRecordOption).then((res)=>{
- console.log(res);
- const receiveMessage = res.data.data;
-
- }).catch((err)=>{
- console.log(err);
- })
- this.scrollToBottom();
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady() {
- getUserInfoListOption.data.uid = [wx.getStorageSync('uid'),this.data.toUid];
- getUserInfoList(getUserInfoListOption).then((res)=> {
- console.log(res);
- }).catch((err)=>{
- console.log(err);
- })
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide() {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload() {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh() {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom() {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage() {
- },
- // 输入监听
- inputClick(e) {
- this.setData({
- content: e.detail.value
- })
- },
- // 发送监听
- sendClick() {
- var that = this;
- var list = this.data.chatList;
- // 获取当前时间
- var date = new Date();
- var month = date.getMonth() + 1;
- var day = date.getDate();
- var hour = date.getHours();
- var minu = date.getMinutes();
- var now1 = month < 10 ? '0' + month : month;
- var now2 = day < 10 ? '0' + day : day;
- // 组装数据
- var msg = {
- from: wx.getStorageSync('uid'),
- to: this.data.toUid,
- nickname: wx.getStorageSync('nickName'),
- avatar: wx.getStorageSync('avatarUrl'),
- message: this.data.content,
- type: 'text',
- date: now1 + '-' + now2 + ' ' + hour + ':' + minu
- }
- console.log(msg);
- this.setData({
- chatList: list.concat(msg)
- }, () => {
- that.scrollToBottom();
- that.setData({
- content: ''
- })
- })
- var socketMsg = socketMsgQueue;
- socketMsg.msgType = 1;
- socketMsg.content = this.data.content;
- socketMsg.to = this.data.toUid;
- console.log(this.data.toUid);
- var 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)
- },
- })
|