const app = getApp(); import { ApiModel } from "../../utils/api"; const _Http = new ApiModel(); Page({ /** * 页面的初始数据 */ data: { iosX: false, sendText: "", //输入框内容 headerTitle: "对话框", socketMsgQueue: [], lineCount: 1, //输入框行数 toView: 'item0', keyboardHeight: 0, //键盘高度 functionToChoose: "", //功能选项 memePopup: false, //表情弹出 pageNumber: 1, pageTotal: 1, triggered: false, //下拉状态 unreadCount: 0, //对方发送未读信息 joinHands: false, //是否显示合作按钮 ownerid: 0, //供需id }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { app.initSocket(); //链接websocket 用于测试 const that = this; this.setData({ timdialogid: options.id, userid: wx.getStorageSync('userData').userid }); //详情 _Http.basic({ "accesstoken": wx.getStorageSync('userData').token, "classname": "system.im.imdialog.imdialog", "method": "query_imdialogMain", "content": { "timdialogid": this.data.timdialogid } }).then(res => { console.log("详情", res) if (res.msg != '成功') return wx.showToast({ title: res.data, icon: "none" }); let title = res.data[0].fimdialogname.split('-'), headerTitle = (title[0] == res.data[0].fname) ? title[1] : title[0], joinHands = false; if (res.data[0].fimdialogtype == '供需') { if (res.data[0].fimuserid == this.data.userid) { _Http.basic({ "accesstoken": wx.getStorageSync('userData').token, "classname": "customer.supplyanddemand.supplyanddemand", "method": "updatesupplyanddemandstatus", "content": { "tsupplyanddemandid": res.data[0].ownerid, "fstatus": "对接中", "timdialogid": res.data[0].timdialogid } }, false).then(res => { console.log("对接中", res) }) } else { joinHands = true } } this.setData({ headerTitle, ownerid: res.data[0].ownerid, initiator: res.data[0].fimuserid, joinHands }) }) //群历史信息 this.query_imdialogMessage(); let iosX = (getApp().globalData.safeAreaBottom == 0) ? false : true; this.setData({ iosX }) //获取表情包 this.getMeme() }, /* 确认合作 */ confirmTheCooperation() { _Http.basic({ "accesstoken": wx.getStorageSync('userData').token, "classname": "customer.supplyanddemand.supplyanddemand", "method": "updatesupplyanddemandstatus", "content": { "tsupplyanddemandid": this.data.ownerid, "fstatus": "已解决", "tenterprise_userid": this.data.initiator, "timdialogid": this.data.timdialogid } }).then(res => { console.log("确认合作", res) }) }, /* 拒绝合作 */ refuseToCooperate() { _Http.basic({ "accesstoken": wx.getStorageSync('userData').token, "classname": "customer.supplyanddemand.supplyanddemand", "method": "updatesupplyanddemandstatus", "content": { "tsupplyanddemandid": this.data.ownerid, "fstatus": "待对接", "timdialogid": this.data.timdialogid } }).then(res => { console.log("拒绝合作", res) }) }, //历史信息查询 query_imdialogMessage() { _Http.basic({ "accesstoken": wx.getStorageSync('userData').token, "classname": "system.im.imdialog.imdialog", "method": "query_imdialogMessage", "content": { "pageNumber": this.data.pageNumber, "pageSize": 20, "timdialogid": this.data.timdialogid } }, false).then(res => { if (res.msg != '成功') return wx.showToast({ title: res.data, icon: "none" }) if (res.pageNumber != 1) { let list = res.data; list = list.reverse(); list = list.concat(this.data.socketMsgQueue); this.setData({ socketMsgQueue: list, toView: `item${res.data.length - 1}`, triggered: false }) } else { let list = res.data; list = list.reverse(); this.setData({ socketMsgQueue: list, toView: `item${res.data.length - 1}`, pageTotal: res.pageTotal }) setTimeout(() => { this.setData({ toView: 'bottom' }) }, 500) } }) }, scrolltoupper() { if (this.data.pageNumber < this.data.pageTotal) { this.setData({ pageNumber: this.data.pageNumber + 1, triggered: true }) this.query_imdialogMessage(); } else { this.setData({ triggered: true }) } }, //键盘弹出高度 keyboardheightchange(e) { let { height } = e.detail; if (this.data.iosX && height > 150) height = height - 17; this.setData({ keyboardHeight: height }) }, /* 输入框数据绑定 */ sendInput(e) { this.setData({ sendText: e.detail.value }) }, /* 发送信息 */ sendMsg(type, data) { const content = this.data.sendText.trim(), that = this; if (type === 'file') { console.log("发送", data) app.globalData.SocketTask.send({ data: JSON.stringify({ "classname": "message.message", "method": "sendFileMessage", "content": { "timdialogid": this.data.timdialogid, "tattachmentid": data[0].tattachmentid } }), success(res) {} }) } else { if (content == '') return; app.globalData.SocketTask.send({ data: JSON.stringify({ "classname": "message.message", "method": "sendTextMessage", "content": { "timdialogid": that.data.timdialogid, "fmessage": content } }), success(res) { that.setData({ sendText: '' }) } }) } }, sendMeme(e) { const { item } = e.currentTarget.dataset; let data = []; data.push(item); this.sendMsg('file', data) }, //功能选择 selectionFunction(e) { const { name } = e.target.dataset, that = this; if (name == undefined || name == "") return; if (name == '表情') { let keyboardHeight = (this.data.iosX == true) ? 220 : 200; this.setData({ keyboardHeight, memePopup: true }) that.toBotton() let time1 = setInterval(() => { this.setData({ keyboardHeight }) if (this.data.keyboardHeight == keyboardHeight && this.data.memePopup) clearInterval(time1); }, 100); } else if (name == '图片') { this.endMemePopup() wx.chooseMedia({ count: 1, mediaType: ['image'], sourceType: ['album', 'camera'], maxDuration: 30, camera: 'back', success(res) { let data = { file: res.tempFiles[0].tempFilePath }; that.afterRead(data) } }) } else if (name == '历史') { wx.navigateTo({ url: './history?id=' + that.data.timdialogid, }) } }, textareaFocus() { this.setData({ memePopup: false, //表情弹出 }) this.toBotton() }, //关闭表情 endMemePopup(e) { this.setData({ keyboardHeight: 0, //键盘高度 memePopup: false, //表情弹出 }) }, /* 文本域行数变化 */ /* linechange(e) { console.log(e) const { lineCount } = e.detail; if (lineCount == this.data.lineCount) return; this.setData({ lineCount }) }, */ //获取表情包 getMeme() { _Http.basic({ "accesstoken": wx.getStorageSync('userData').token, "classname": "system.system.docManage", "method": "queryDoc", "content": { "getdatafromdbanyway": true, "ownertable": "system", "ownerid": 0 } }, false).then(res => { if (res.msg != '成功') return setTimeout(() => { this.getMeme(); }, 3000); this.setData({ memeList: res.data }) }) }, afterRead({ file }) { var that = this var index = file.lastIndexOf("."); var ext = file.substr(index + 1); var timestamp = Date.parse(new Date()); wx.getFileSystemManager().readFile({ filePath: file, // encoding:'utf-8', success: result => { //返回临时文件路径 const fileData = result.data wx.request({ url: 'https://www.buwanjia.com/bwj/rest/webclientrest/', //仅为示例,并非真实的接口地址 data: { "accesstoken": wx.getStorageSync('userData').token, "classname": "system.system.docManage", "method": "getFileName", "content": { "filename": 'wx' + timestamp, "filetype": ext, "ownertable": "timdialog", "ownerid": that.data.timdialogid, "ftype": "default", "HttpMethod": 'put' } }, method: 'post', header: { 'content-type': 'application/json' // 默认值 }, success(res) { console.log(res) that.uploadFile(res, fileData) } }) }, fail: console.error }) }, uploadFile(res, data) { var that = this wx.request({ url: res.data.data.obsuploadurl, method: "PUT", data: data, header: { 'content-type': 'application/octet-stream' // 默认值 }, success() { wx.request({ url: 'https://www.buwanjia.com/bwj/rest/webclientrest/', //仅为示例,并非真实的接口地址 data: { "accesstoken": wx.getStorageSync('userData').token, "classname": "system.system.docManage", "method": "uploadSuccess", "content": { "obsfilename": res.data.data.obsfilename } }, method: 'post', header: { 'content-type': 'application/json' // 默认值 }, success(res) { let file = res.data.data; console.log(res) that.sendMsg('file', file) } }) } }) }, toBotton() { this.setData({ toView: 'bottom' }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { var that = this app.globalData.callback = function (res) { //res 接收websocket onMessage事件返回的数据 let objs = JSON.parse(res.data), unreadCount = that.data.unreadCount; /* triggered */ that.data.socketMsgQueue.push(objs) console.log("发送消息", objs) that.setData({ socketMsgQueue: that.data.socketMsgQueue }) that.toBotton() /* if (wx.getStorageSync('userData').userid == objs.message.sendfrom.userid) { } else { that.setData({ socketMsgQueue: that.data.socketMsgQueue }) } */ /* , toView: `item${that.data.socketMsgQueue.length - 1}` */ } }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { //重置未读信息 _Http.basic({ "accesstoken": wx.getStorageSync('userData').token, "classname": "system.im.imdialog.imdialog", "method": "restUnReadMsgCount", "content": { "timdialogid": this.data.timdialogid } }).then(res => { console.log(res) }) }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })