const that = getApp() var sessionTimer; var recordTimeInterval = 0; var formidSubmitTimer; const recorderManager = wx.getRecorderManager() //创建录音 const innerAudioContext = wx.createInnerAudioContext() //播放录音 const util = require('../zoosutils/util.js') // 引入SDK核心类 var QQMapWX = require('../zoosutils/qqmap-wx-jssdk.js'); // 实例化API核心类 var qqmapsdk = new QQMapWX({ key: 'LGOBZ-LUVE6-2P3ST-MYLJK-UV2SZ-MXFUN' // 必填 }); //轨迹 var ZOOSTRSDK = require('../zoosutils/common.js'); Page({ // 以下为用户填写参数 appid: "wxb4ce09b25dab3bf9", //填写appid secretkey: "e9980f351a30693d2b4ee99b6dc6580f", siteid: "PLT47075192", //填写siteid // 以上为用户填写参数 msgtick: 0, bbb: "1", ifgetfous: "0", //是否获取过焦点 0没获取过 1获取过 globalUserData: { serviceurl: "https://wkbservice.yiaitao.cn/", //"https://wkbservice.yiaitao.cn/",//, openid: "", username: "", userheadurl: "", avatarUrl: "", gender: "", province: "", city: "", country: "", language: "", userPhone: "", }, //设备信息 system: "", model: "", //微信版本号 wechatVersionNumber: "", isAtTalkPage: true, //当前是否在对话页面 scene: "", companyinfo: {}, serverconncet: false, //正常开启对话是否成功 sence: "", //企业信息缓存(小程序打开,访问服务器可返回) bisLoginAuth: false, //用户信息是否已经获取完成 getsubscribemsgcontentList: [], //获取订阅模版消息(客服发送的关键词)["111", "222", "333"] nowsendmsgcount: 1, // 当前发送消息数量 data: { receivecus: "", //准备接待当前访客客服名称(对接SDK传入) showModalStatus: false, consumerheadurl: "../zoosimg/user.png", scrollTop: 0, msgcontent: "", //放消息内容 msg: [], nowtalksid: "", //一次对话标识 btnSendOrAddMenu: 1, //菜单或者发送按钮 0:发送 1:菜单 menuheight: "110rpx", menrtr: "220rpx", menrtn: '110rpx', funhight: "55rpx", //用于计算底侧功能功能高度 textareahight: "48rpx", //用于计算底侧textarea栏高度 menushow: "none", isshowemoji: "none", //表情默认隐藏 talkviewheight: "", //对话页面高度 isvoice: 1, //1: 文字 2:语音 keyboardOrvoice: "voice", //显示语音按钮或者键盘 voiceBtnStyle: false, //语音按钮样式 currentTab: 0, //预设当前项的值 scrollLeft: 0, //tab标题的滚动条位置 textareacursor: 0, //失去焦点时候获取光标位置 txtfocus: false, //文本输入框获取焦点 isSendVoice: true, //是否发送语音 scrollViewHeight: "", //聊天窗口高度 swiperheight: "350rpx", indicatorDots: true, vertical: false, autoplay: true, circular: true, interval: 2000, duration: 500, previousMargin: 0, nextMargin: 0, //解决部分机型没有发送按钮(小米,红米)暂时先判断是否是安卓还是ios bisexist: false, //是否存在 mark2 timedifference: 0, ifreachessixty: false, //改变量判断是否时间超过60s 自动发送(用于阻止第二次调用停止语音方法,也用于自动发送后禁止上划取消) islastemjandtext: 0, //最后输入表情还是文字 0 文字 1表情 // wxemojliList: [{ // wxemojiid: "[10000+A]", // wxemojicontent: "😊" // }, { // wxemojiid: "[10001+A]", // wxemojicontent: "😍" // }, { // wxemojiid: "[10002+A]", // wxemojicontent: "😉" // }, { // wxemojiid: "[10003+A]", // wxemojicontent: "😃" // }, { // wxemojiid: "[10004+A]", // wxemojicontent: "😁" // }, { // wxemojiid: "[10005+A]", // wxemojicontent: "😝" // }, { // wxemojiid: "[10006+A]", // wxemojicontent: "😘" // }, { // wxemojiid: "[10007+A]", // wxemojicontent: "😚" // }, { // wxemojiid: "[10008+A]", // wxemojicontent: "😷" // }, { // wxemojiid: "[10009+A]", // wxemojicontent: "😏" // }, { // wxemojiid: "[10010+A]", // wxemojicontent: "😣" // }, { // wxemojiid: "[10011+A]", // wxemojicontent: "😥" // }, { // wxemojiid: "[10012+A]", // wxemojicontent: "😜" // }, { // wxemojiid: "[10013+A]", // wxemojicontent: "😲" // }, { // wxemojiid: "[10014+A]", // wxemojicontent: "👽" // }, { // wxemojiid: "[10015+A]", // wxemojicontent: "😭" // }, { // wxemojiid: "[10016+A]", // wxemojicontent: "😡" // }, { // wxemojiid: "[10017+A]", // wxemojicontent: "😌" // }, ], wxemojliList: [{ wxemojiid: "【微笑】", wxemojicontent: "1.gif", wxemojimarhing: "_&1.gif-/a_&" //emoji标识 }, { wxemojiid: "【撇嘴】", wxemojicontent: "2.gif", wxemojimarhing: "_&2.gif-/a_&" }, { wxemojiid: "【色】", wxemojicontent: "3.gif", wxemojimarhing: "_&3.gif-/a_&" }, { wxemojiid: "【发呆】", wxemojicontent: "6.gif", wxemojimarhing: "_&6.gif-/a_&" }, { wxemojiid: "【得意】", wxemojicontent: "5.gif", wxemojimarhing: "_&5.gif-/a_&" }, { wxemojiid: "【流泪】", wxemojicontent: "10.gif", wxemojimarhing: "_&10.gif-/a_&" }, { wxemojiid: "【害羞】", wxemojicontent: "7.gif", wxemojimarhing: "_&7.gif-/a_&" }, { wxemojiid: "【闭嘴】", wxemojicontent: "8.gif", wxemojimarhing: "_&8.gif-/a_&" }, { wxemojiid: "【睡】", wxemojicontent: "9.gif", wxemojimarhing: "_&9.gif-/a_&" }, { wxemojiid: "【尴尬】", wxemojicontent: "11.gif", wxemojimarhing: "_&11.gif-/a_&" }, { wxemojiid: "【生气】", wxemojicontent: "12.gif", wxemojimarhing: "_&12.gif-/a_&" }, { wxemojiid: "【调皮】", wxemojicontent: "13.gif", wxemojimarhing: "_&13.gif-/a_&" }, { wxemojiid: "【呲牙】", wxemojicontent: "14.gif", wxemojimarhing: "_&14.gif-/a_&" }, { wxemojiid: "【惊讶】", wxemojicontent: "15.gif", wxemojimarhing: "_&15.gif-/a_&" }, { wxemojiid: "【难过】", wxemojicontent: "16.gif", wxemojimarhing: "_&16.gif-/a_&" }, { wxemojiid: "【囧】", wxemojicontent: "18.gif", wxemojimarhing: "_&18.gif-/a_&" }, { wxemojiid: "【抓狂】", wxemojicontent: "19.gif", wxemojimarhing: "_&19.gif-/a_&" }, { wxemojiid: "【吐】", wxemojicontent: "20.gif", wxemojimarhing: "_&20.gif-/a_&" }, { wxemojiid: "【偷笑】", wxemojicontent: "21.gif", wxemojimarhing: "_&21.gif-/a_&" }, { wxemojiid: "【愉快】", wxemojicontent: "22.gif", wxemojimarhing: "_&22.gif-/a_&" }, { wxemojiid: "【白眼】", wxemojicontent: "23.gif", wxemojimarhing: "_&23.gif-/a_&" }, { wxemojiid: "【傲慢】", wxemojicontent: "24.gif", wxemojimarhing: "_&24.gif-/a_&" }, { wxemojiid: "【困】", wxemojicontent: "26.gif", wxemojimarhing: "_&26.gif-/a_&" }, { wxemojiid: "【惊恐】", wxemojicontent: "27.gif", wxemojimarhing: "_&27.gif-/a_&" }, { wxemojiid: "【流汗】", wxemojicontent: "28.gif", wxemojimarhing: "_&28.gif-/a_&" }, { wxemojiid: "【憨笑】", wxemojicontent: "29.gif", wxemojimarhing: "_&29.gif-/a_&" }, { wxemojiid: "【悠闲】", wxemojicontent: "30.gif", wxemojimarhing: "_&30.gif-/a_&" }, { wxemojiid: "【奋斗】", wxemojicontent: "31.gif", wxemojimarhing: "_&31.gif-/a_&" }, { wxemojiid: "【咒骂】", wxemojicontent: "32.gif", wxemojimarhing: "_&32.gif-/a_&" }, { wxemojiid: "【疑问】", wxemojicontent: "33.gif", wxemojimarhing: "_&33.gif-/a_&" }, { wxemojiid: "【嘘】", wxemojicontent: "34.gif", wxemojimarhing: "_&34.gif-/a_&" }, { wxemojiid: "【晕】", wxemojicontent: "35.gif", wxemojimarhing: "_&35.gif-/a_&" }, { wxemojiid: "【衰】", wxemojicontent: "37.gif", wxemojimarhing: "_&37.gif-/a_&" }, { wxemojiid: "【骷髅】", wxemojicontent: "38.gif", wxemojimarhing: "_&38.gif-/a_&" }, { wxemojiid: "【敲打】", wxemojicontent: "39.gif", wxemojimarhing: "_&39.gif-/a_&" }, { wxemojiid: "【再见】", wxemojicontent: "40.gif", wxemojimarhing: "_&40.gif-/a_&" }, { wxemojiid: "【擦汗】", wxemojicontent: "41.gif", wxemojimarhing: "_&41.gif-/a_&" }, { wxemojiid: "【抠鼻】", wxemojicontent: "42.gif", wxemojimarhing: "_&42.gif-/a_&" }, { wxemojiid: "【鼓掌】", wxemojicontent: "43.gif", wxemojimarhing: "_&43.gif-/a_&" }, { wxemojiid: "【坏笑】", wxemojicontent: "45.gif", wxemojimarhing: "_&45.gif-/a_&" }, { wxemojiid: "【左哼哼】", wxemojicontent: "46.gif", wxemojimarhing: "_&46.gif-/a_&" }, { wxemojiid: "【右哼哼】", wxemojicontent: "47.gif", wxemojimarhing: "_&47.gif-/a_&" }, { wxemojiid: "【哈欠】", wxemojicontent: "48.gif", wxemojimarhing: "_&48.gif-/a_&" }, { wxemojiid: "【鄙视】", wxemojicontent: "49.gif", wxemojimarhing: "_&49.gif-/a_&" }, { wxemojiid: "【委屈】", wxemojicontent: "50.gif", wxemojimarhing: "_&50.gif-/a_&" }, { wxemojiid: "【快哭了】", wxemojicontent: "51.gif", wxemojimarhing: "_&51.gif-/a_&" }, { wxemojiid: "【阴险】", wxemojicontent: "52.gif", wxemojimarhing: "_&52.gif-/a_&" }, { wxemojiid: "【亲亲】", wxemojicontent: "53.gif", wxemojimarhing: "_&53.gif-/a_&" } ], //自定义表情集合(用来判断msgcontent是否存在表情) companyMenu: "none", isShowPhoneAuth: "none", //复制相关 copyContentMsgid: "", //复制内容msgid copyCotent: "", //复制内容 copyButtonPosition: 0, ifShowGetPhoneAuthBtn: false, //请联系我button是否获取电话信息 //下拉相关 ifscrollToupper: true, //是否触发下拉事件 ifloading: true, //是否显示下拉数据 loadingcontent: "", ifshowhistorymsg: "none", recordBtnstate: "0", //录音按钮状态 0按 1不按 selPlayVoiceMsgid: "", //当前播放语音的msgid soundrecord_state: 0, //是否显示录音弹窗 0 不显示 1显示 soundrecord_tiptext: "", //弹窗标题 curphone: "", //临时贮存电话号码 msgTextAndImg: "", //待拆分的消息(文字图片) scrollheigth: "", anewOpenChatid: 0, //重新开启对话标识 0 未重新开启 1重新开启 anewOpenChatContent: "", //记录发送失败内容(重新开启对话) anewOpenChatType: "", //记录发送失败类型(重新开启对话) anewOpenChatMsgId: "", anewOpenChatNum: 0, //重新开启对话次数 }, onLoad(query) { var that = this; wx.setNavigationBarTitle({ title: '在线客服' }); this.setData({ receivecus: query.receivecus, }) wx.getSystemInfo({ success: function(res) { that.system = res.platform; that.model = res.model; that.wechatVersionNumber = res.version; console.log(that.model); var padtop = 0; var padright = 0; var txthight = 0; var btnsendshow = 0; //解决部分机型没有发送按钮(小米,红米)暂时先判断是否是安卓还是ios var copytxtMode = 0; //控制复制方式 if (that.system == "android") { padtop = 13; padright = 20; txthight = 40; btnsendshow = 0; } else if (that.system == "ios") { padtop = 4; padright = 20; txthight = 62; btnsendshow = 1; } else { padtop = 13; padright = 20; txthight = 40; btnsendshow = 1; } // 1 版本号大于 0版本号等于 -1版本号小于 (6.7.0) if (that.compareVersion(that.wechatVersionNumber, "6.7.0") < 0) { copytxtMode = 1; } that.setData({ padtop: padtop, padright: padright, txthight: txthight, btnsendshow: btnsendshow, copytxtMode: copytxtMode }) }, }); //获取secretkey if (that.secretkey.length == 0) { wx.request({ //url: "http://localhost:62825/api/UserDockingService.ashx?action=getsecretkey", url: that.globalUserData.serviceurl + "/api/UserDockingService.ashx?action=getsecretkey", header: { 'content-type': 'application/json' // 默认值 }, data: { siteid: that.siteid, }, method: "POST", success: function(e) { console.log("secretkeysecretkey" + e); //成功回调 if (e.data.success) { that.secretkey = e.data.secretkey; that.login(); } else { that.errorMsg("缺少密钥!"); return; } }, error: function(res) { that.errorMsg("获取密钥错误!"); return; }, }) } else { that.login(); } }, login: function() { var scrollheigth = ""; var that = this; console.log("vvvvv" + that.secretkey) wx.login({ success: res => { console.log("11"); // 发送 res.code 到后台换取 openId, sessionKey, unionId if (res.code) { console.log("11:" + res.code); var code = res.code; wx.request({ //url: "http://localhost:62825/api/UserDockingService.ashx?action=getopenid", url: that.globalUserData.serviceurl + "/api/UserDockingService.ashx?action=getopenid", header: { 'content-type': 'application/json' // 默认值 }, data: { code: code, // scene: "", appid: that.appid, appsecret: that.secretkey }, method: "POST", success: function(e) { console.log("444"); //成功回调 if (e.data.success) { console.log("22"); that.globalUserData.openid = e.data.data; that.globalUserData.session_key = e.data .session_key; that.loadinghide(); } else { //保证对话 请求失败,即随机生成 that.globalUserData.openid = that.createCode(10); } }, error: function(res) { //保证对话 请求失败,即随机生成 that.globalUserData.openid = that.createCode(10); }, complete: function() { that.openChat(); } }) } } }) //控件 var item = wx.createSelectorQuery(); item.select('#boxview').boundingClientRect() //里面需要绑定 view组件的id item.exec(function(res) { //res为绑定元素信息的数组 that.data.scrollheigth = res[0].top; if (that.system == "android") { scrollheigth = (res[0].top); //这样可以动态获取高度 } else if (that.system == "ios") { scrollheigth = (res[0].top); //这样可以动态获取高度 } else { scrollheigth = (res[0].top); //这样可以动态获取高度 } console.log("--------------------" + scrollheigth); that.setData({ scrollViewHeight: scrollheigth - 9 }) // that.loadingshow(); // setTimeout(function () { // that.onCompanyMessage(); // }, 500) }) }, onShow() { var that = this; //录音开始回调方法 recorderManager.onStart(function(res) { if (that.data.recordBtnstate == "0") { recorderManager.stop(); } }) //录音结束回调用该方法 recorderManager.onStop(function(res) { // wx.hideToast(); clearInterval(recordTimeInterval); that.setData({ soundrecord_state: 0, soundrecord_tiptext: "" }) var msg = that.data.msg; var timedifference = that.data.timedifference; var voiceSrc = res.tempFilePath; var isSendVoice = that.data.isSendVoice console.log(res); if (timedifference < 1) { wx.showToast({ title: '说话时间太短!', image: "../zoosimg/errorshow.png", duration: 800 }) } else if (!that.data.isSendVoice) { console.log("isSendVoice为:" + that.data.isSendVoice); } else { that.uploadfileToServer("2", voiceSrc, ".mp3"); } // that.setData({ // // timedifference: newtimeDifference, // voiceBtnStyle: false, // timedifference: timedifference, // }) }); //录音异常回调用该方法 recorderManager.onError(function(res) { that.setData({ soundrecord_state: 0, }) if (res.errMsg == "operateRecorder:fail auth deny" || res.errMsg == "operateRecorder:fail authorize no response" || res.errMsg == "operateRecorder:fail:auth denied") { // wx.authorize({ // scope: 'scope.record', // fail() { // console.log("第一次录音授权失败"); wx.showModal({ title: '提示', content: '您未授权录音,功能将无法使用', showCancel: true, confirmText: "授权", confirmColor: "#52a2d8", success: function(res) { if (res.confirm) { //确认则打开设置页面(重点) wx.openSetting({ success: (res) => { console.log(res.authSetting); if (!res.authSetting['scope.record']) { //未设置录音授权 console.log("未设置录音授权"); wx.showModal({ title: '提示', content: '您未授权录音,功能将无法使用', showCancel: false, success: function(res) {}, }) } else { //第二次才成功授权 console.log("设置录音授权成功"); wx.showModal({ title: '提示', content: '授权成功!', showCancel: false, success: function(res) {}, }) // that.setData({ // status: 2, // }) } }, fail: function() { console.log("授权设置录音失败"); } }) } else if (res.cancel) { console.log("cancel"); } }, fail: function() { console.log("openfail"); } }) // } // }) } }); }, //页面隐藏操作 onHide: function() { var that = this; wx.hideToast(); clearInterval(recordTimeInterval); that.setData({ voiceBtnStyle: false, isSendVoice: false, }) }, powerDrawer: function(e) { // wx.openUrl({ url: "https://wkbservice.yiaitao.cn/PublicityPage/LGN50496114/index.html", fail: res => { console.log(res) } }); //button 获取formid if (e.detail.formId == 'the formId is a mock one' || e.detail.formId == undefined) {} else { // var item = { // formid: e.detail.formId, // formidcreatetime: util.formatTime(new Date()) // } // this.data.formidArray.push(item); this.data.formid = e.detail.formId; this.data.formidcreatetime = util.formatTime(new Date()); this.formidUpload(); } var currentStatu = e.currentTarget.dataset.statu; this.util(currentStatu) }, util: function(currentStatu) { /* 动画部分 */ // 第1步:创建动画实例 var animation = wx.createAnimation({ duration: 200, //动画时长 timingFunction: "linear", //线性 delay: 0 //0则不延迟 }); // 第2步:这个动画实例赋给当前的动画实例 this.animation = animation; // 第3步:执行第一组动画 animation.opacity(0).rotateX(-100).step(); // 第4步:导出动画对象赋给数据对象储存 this.setData({ animationData: animation.export() }) // 第5步:设置定时器到指定时候后,执行第二组动画 setTimeout(function() { // 执行第二组动画 animation.opacity(1).rotateX(0).step(); // 给数据对象储存的第一组动画,更替为执行完第二组动画的动画对象 this.setData({ animationData: animation }) //关闭 if (currentStatu == "close") { this.setData({ showModalStatus: false }); } }.bind(this), 200) // 显示 if (currentStatu == "open") { this.setData({ showModalStatus: true }); } }, //下拉加载 onPullDownRefresh: function() { wx.showNavigationBarLoading() //在标题栏中显示加载 //模拟加载 setTimeout(function() { // complete wx.hideNavigationBarLoading() //完成停止加载 wx.stopPullDownRefresh() //停止下拉刷新 }, 1500); }, //开启一个对话 openChat: function() { var that = this; var newcode = "LRWT_" + that.createCode(19); console.log("that.globalUserData.openid;:::" + that.globalUserData.openid); // that.setData({ // nowtalksid: newcode // }) var data = { openid: that.globalUserData.openid + "1", username: wx.getStorageSync('userInfo').username || '', avatarurl: wx.getStorageSync('userInfo').avatar || '', gender: wx.getStorageSync('userInfo').gender || '', province: that.globalUserData.province, city: that.globalUserData.city, country: that.globalUserData.country, language: that.globalUserData.language, cid: "LRWT_" + that.globalUserData.openid, siteid: that.siteid, sid: newcode, visitcount: "1", ots: "0", source: that.sence == null ? "测试" : that.sence, mobile: wx.getStorageSync('userInfo').mobile || '', // receivecus: that.data.receivecus || '', //接待客服 receivecus: '客服01', //接待客服 trail: JSON.stringify(ZOOSTRSDK.CONFIG.trailJarr), //轨迹 } console.log("客服连接中", data); wx.request({ //url: "http://localhost:62825/api/Chat.ashx?action=open", //url: "https://wkbservice.yiaitao.cn/api/Chat.ashx?action=open", url: that.globalUserData.serviceurl + "/api/UserDockingService.ashx?action=open", header: { 'content-type': 'thatlication/json' // 默认值 }, data: data, method: "POST", success: function(e) { //成功回调 var threeArray = []; if (e.data.success) { console.log("链接客服成功!"); that.serverconncet = true; ZOOSTRSDK.CONFIG.trailJarr = []; // var cusheadurl = ''; // if (that.globalUserData.avatarUrl.length > 0) { // cusheadurl = that.globalUserData.avatarUrl; // } var resultmsg = e.data.msgs; for (var j = 0; j < resultmsg.length; j++) { for (var i = 0; i < that.data.msg.length; i++) { if (that.data.msg[i].msgid == resultmsg[j].msgid) { that.data.msg.splice(i, 1); } } } for (var k = 0; k < resultmsg.length; k++) { if (resultmsg[k].content != null) { resultmsg[k].content = that.replacecontent(resultmsg[k].content, /\\/g, "/"); var nowdate = util.formatTime(new Date()).substring(0, 10); if (resultmsg[k].msgtype == 2) { var msgparsecontent = JSON.parse(resultmsg[k].content); var msgcontent = msgparsecontent.mcontent; var timedifference = msgparsecontent.timedifference; var voicewidth = Number(timedifference); //计算语音宽度 if (voicewidth >= 60) { voicewidth = 60; } var item = { content: msgcontent, msgid: resultmsg[k].msgid, msgtype: resultmsg[k].msgtype, source: resultmsg[k].source, tick: resultmsg[k].tick, time: resultmsg[k].time.substring(0, 10) == nowdate ? resultmsg[k].time.substring(11, resultmsg[k].time .length) : resultmsg[k].time, timedifference: timedifference, voicewidth: voicewidth } resultmsg[k] = item; } else if (resultmsg[k].msgtype == 0) { var item = {}; var viewContent = that.splitMsgcontent(resultmsg[k].content); var item = { viewContent: viewContent, content: resultmsg[k].content, msgid: resultmsg[k].msgid, msgtype: resultmsg[k].msgtype, source: resultmsg[k].source, tick: resultmsg[k].tick, time: resultmsg[k].time.substring(0, 10) == nowdate ? resultmsg[k].time.substring(11, resultmsg[k].time .length) : resultmsg[k].time, timedifference: timedifference, voicewidth: voicewidth } resultmsg[k] = item; } that.data.msg.push(resultmsg[k]); } } that.setData({ msgArray: that.data.msg.sort(that.compare("tick")), // consumerheadurl: cusheadurl, scrollTop: 2147483647 + "px", msgcontent: "", nowtalksid: e.data.sid, }); that.loadinghide(); that.sync(); //重新开启对话后发送失败消息 if (that.data.anewOpenChatid == 1) { if (that.data.anewOpenChatNum == 3) { that.data.anewOpenChatNum = that.data.anewOpenChatNum++; that.data.msgtype = that.data.anewOpenChatType; that.data.msgcontent = that.data.anewOpenChatContent; that.sendMsg(that.data.anewOpenChatType, 0); } else { that.errorMsg("重新开启对话失败!" + e.data.error); return; } } // that.formidSync(); } else { if (e.data.error.indexOf( 'HTTP Error 404. The requested resource is not found.') == 1) { that.errorMsg('HTTP Error 404!'); console.log(e.data.error); } else { that.errorMsg("开启对话失败!" + e.data.error); console.log(e.data.error); } } }, error: function(e) { console.log("获取企业信息异常!"); that.errorMsg("链接客服失败!"); return }, }); }, ok: function() { console.log("meun!"); }, //发送消息 sendMsgBtn: function(e) { var that = this; that.setData({ companyMenu: "none", }) if (that.data.islastemjandtext == 0) { that.bbb = "0"; } console.log("that.bbb:" + that.bbb); console.log("发消息"); var noConnect = that.checkConnectOk(); if (noConnect.length > 0) { that.errorMsg(noConnect); return; } if (that.data.msgcontent == "" || that.data.msgcontent == null || that.data.msgcontent == undefined) { that.errorMsg("没有发送内容!"); return; } // that.splitMsgcontent(); //拆分文字,图片 that.sendMsg(0, 0); }, //心跳 sync: function() { var that = this; if (that.data.nowtalksid.length == 0) { return; } that.initInterval(); sessionTimer = setInterval(function() { // 心跳 wx.request({ url: that.globalUserData.serviceurl + "/api/UserDockingService.ashx?action=sync", header: { 'content-type': 'thatlication/json' // 默认值 }, data: { siteid: that.siteid, sid: that.data.nowtalksid, tick: that.msgtick, }, method: "POST", success: function(e) { //成功回调 if (e.data.success) { if (e.data.list.length > 0) { var resultmsg = e.data.list; for (var j = 0; j < resultmsg.length; j++) { for (var i = 0; i < that.data.msg.length; i++) { if (that.data.msg[i].msgid == resultmsg[j].msgid) { that.data.msg.splice(i, 1); } } } for (var k = 0; k < resultmsg.length; k++) { if (resultmsg[k].content != null) { resultmsg[k].content = that.replacecontent( resultmsg[k].content, /\\/g, "/"); var nowdate = util.formatTime(new Date()).substring( 0, 10); if (resultmsg[k].msgtype == 2) { var msgparsecontent = JSON.parse(resultmsg[k] .content); var msgcontent = msgparsecontent.mcontent; var timedifference = msgparsecontent .timedifference; var voicewidth = Number( timedifference); //计算语音宽度 if (voicewidth >= 60) { voicewidth = 60; } var item = {}; //客服消息 item = { content: msgcontent, msgid: resultmsg[k].msgid, msgtype: resultmsg[k].msgtype, source: resultmsg[k].source, tick: resultmsg[k].tick, time: resultmsg[k].time.substring(0, 10) == nowdate ? resultmsg[k].time .substring(11, resultmsg[k].time .length) : resultmsg[k].time, timedifference: timedifference, voicewidth: voicewidth, getuser: "0", } resultmsg[k] = item; } else if (resultmsg[k].msgtype == 0) { var item = {}; var viewContent = that.splitMsgcontent( resultmsg[k].content); item = { viewContent: viewContent, content: resultmsg[k].content, msgid: resultmsg[k].msgid, msgtype: resultmsg[k].msgtype, source: resultmsg[k].source, tick: resultmsg[k].tick, time: resultmsg[k].time.substring(0, 10) == nowdate ? resultmsg[k].time .substring(11, resultmsg[k].time .length) : resultmsg[k].time, timedifference: timedifference, getuser: "0", } resultmsg[k] = item; } else if (resultmsg[k].msgtype == 1) { var item = {}; var viewContent = that.splitMsgcontent( resultmsg[k].content); item = { viewContent: viewContent, content: resultmsg[k].content, msgid: resultmsg[k].msgid, msgtype: resultmsg[k].msgtype, source: resultmsg[k].source, tick: resultmsg[k].tick, time: resultmsg[k].time.substring(0, 10) == nowdate ? resultmsg[k].time .substring(11, resultmsg[k].time .length) : resultmsg[k].time, timedifference: timedifference, getuser: "0", } resultmsg[k] = item; } else if (resultmsg[k].msgtype == 3) { //定位消息 var msgparsecontent = JSON.parse(resultmsg[k] .content); var name = msgparsecontent.name; var address = msgparsecontent.address; var latitude = msgparsecontent.latitude; var longitude = msgparsecontent.longitude; var item = {}; //客服消息 item = { name: name, address: address, latitude: latitude, longitude: longitude, msgid: resultmsg[k].msgid, msgtype: resultmsg[k].msgtype, source: resultmsg[k].source, tick: resultmsg[k].tick, time: resultmsg[k].time.substring(0, 10) == nowdate ? resultmsg[k].time .substring(11, resultmsg[k].time .length) : resultmsg[k].time, getuser: "0", } resultmsg[k] = item; } that.data.msg.push(resultmsg[k]); } if (resultmsg[k].tick > that.msgtick) { that.msgtick = resultmsg[k].tick; } } that.setData({ msgArray: that.data.msg.sort(that.compare( "tick")), // scrollTop: 2147483647 + "px", }); that.scrollToTheBottom(); // that.setData({ // scrollTop: 2147483647 + "px", // }) } } else { if (e.data.error == "no chat" && that.isAtTalkPage) { // that.openChat(); // console.log("已重新开启对话!"); } else { console.log(e.data.error); } return false; } }, error: function(e) { console.log("聊天消息获取异常!"); return }, }); }, 1000) }, //上传图片至服务器 uploadfileToServer: function(msgtype, fileurl, fileex) { var that = this; wx.uploadFile({ url: that.globalUserData.serviceurl + "/api/File.ashx", filePath: fileurl, name: 'file', header: { 'lr_siteid': that.siteid, 'lr_fileex': fileex, 'lr_filetype': '1', //格式 0:语音 1:图片 }, formData: [{ name: 'param1', value: 'value1' }, { name: 'lr_siteid', value: that.siteid }, { name: 'lr_fileex', value: fileex }, { name: 'lr_filetype', value: '1' } ], // method: "POST", success: function(e) { //成功回调 var result = JSON.parse(e.data); if (result.success) { that.data.msgcontent = that.globalUserData.serviceurl + result.url, that.sendMsg(msgtype, 0); } else { that.errorMsg("开启对话失败!" + result.error); console.log(result.error); } }, error: function(e) { console.log("获取企业信息异常!"); that.errorMsg("链接客服失败!"); return }, }); }, //发送消息 msgtype:0,文字 1,图片 2,语音 3,定位 sendMsg: function(msgtype, msgsource) { // that.errorMsg("4543534543"); var that = this; var msgid = ""; if (that.data.anewOpenChatid != 1) { msgid = that.createCode(32); //生成msgid 32位字符串 } else { msgid = that.data.anewOpenChatMsgId; } //如果为0 需要禁止发送按钮 防止连击 if (msgtype == 0) { that.setData({ disabled: "false" }) setTimeout(function() { that.setData({ disabled: "", }); // that.data.formid = ""; // that.data.formidcreatetime = ""; }, 1000) }; var mcontent; var viewContent; //处理文字消息 拆分表情 //如果msgtype是2则为语音消息 需要传参数timedifference if (msgtype == 2) { var timeDifference = Number((Number(that.data.timedifference) / 2).toFixed(0)); var msgItem = { mcontent: that.data.msgcontent, timedifference: timeDifference } mcontent = JSON.stringify(msgItem); //计算语音宽度 var voicewidth = timeDifference; if (voicewidth >= 60) { voicewidth = 60; } } else if (msgtype == 0) { mcontent = that.data.msgcontent; viewContent = that.splitMsgcontent(that.data.msgcontent); } else if (msgtype == 3) { var name = that.data.msgcontent.name; var address = that.data.msgcontent.address; var latitude = that.data.msgcontent.latitude; var longitude = that.data.msgcontent.longitude; mcontent = that.data.msgcontent; console.log("ppppppp:" + name) } else { mcontent = that.data.msgcontent; }; var nowtime = util.formatTime(new Date()); if (that.data.anewOpenChatid != 1) { //对话超时重启对话,该消息已经向缓存添加过 //现改为直接向页面赋值 var item = { viewContent: viewContent, content: mcontent, msgid: msgid, msgtype: msgtype, source: msgsource, tick: "999999999999999999", //生成最大tick 保证该消息在最后一条 time: nowtime.substring(11, nowtime.length), timedifference: timeDifference, voicewidth: voicewidth, msgsendstate: "0", //信息发送状态 0 正在发送 }; that.data.msg.push(item); that.setData({ msgArray: that.data.msg, msgcontent: "", // scrollTop: 2147483647 + "px", textareacursor: 0, btnSendOrAddMenu: 1, funhight: "55rpx", textareahight: "48rpx", menushow: "none", isshowemoji: "none", menuheight: "110rpx", }); that.scrollToTheBottom(); } else { mcontent = that.data.anewOpenChatContent; that.data.anewOpenChatid = 0; } wx.request({ //url: "https://wkbservice.yiaitao.cn/api/Chat.ashx?action=send", // url: "http://localhost:62825/api/Chat.ashx?action=send", url: that.globalUserData.serviceurl + "/api/UserDockingService.ashx?action=send", header: { 'content-type': 'thatlication/json' // 默认值 }, data: { siteid: that.siteid, sid: that.data.nowtalksid, msg: mcontent, source: msgsource, msgtype: msgtype, // formid: formid, // formidcreatetime: formidcreatetime, msgid: msgid, }, method: "POST", success: function(e) { //成功回调 if (e.data.success) {} else { if (e.data.error == "no chat" && that.isAtTalkPage) { // that.setData({ // msgArray: that.arrarRemove(that.data.msg, msgid) // }); that.data.anewOpenChatid = 1; that.data.anewOpenChatType = msgtype; that.data.anewOpenChatContent = mcontent; that.data.anewOpenChatMsgId = msgid; that.openChat(); console.log("已重新开启对话!"); } else { console.log(e.data.error); return false; } } }, error: function(e) { console.log("发送消息失败" + e.data.error); return }, complete: function() { }, }); }, //语音按钮显示 voiceShowBtn: function(e) { var that = this; var isShowVioce = "1"; var boardOrvoice = "voice"; if (this.data.isvoice == "1") { isShowVioce = "2"; boardOrvoice = "keyboard"; if (!that.data.isfirstUseRe) { wx.authorize({ scope: 'scope.record', success(res) { // wx.startRecord() }, error(res) { console.log(res); } }); that.data.isfirstUseRe = true; } } this.setData({ isvoice: isShowVioce, menuheight: "110rpx", menushow: "none", isshowemoji: "none", keyboardOrvoice: boardOrvoice, funhight: "55rpx", textareahight: "50rpx", companyMenu: "none", copyContentMsgid: "", }) }, //长按开始: 录音开始 longtapStart: function(e) { var that = this; // wx.showToast({ // title: '正在录音', // duration: 999999, // image: '../zoosimg/luyin-ing.png' // }) that.data.timedifference = 0; that.data.isSendVoice = true; this.setData({ // startseconds: startSeconds, voiceBtnStyle: true, startpointY: e.touches[0].pageY, //当前触摸点样y坐标抽 soundrecord_state: 1, soundrecord_tiptext: "正在录音", recordBtnstate: "1", }) recorderManager.start({ format: "mp3", // duration: 3000, }) // 录音计时器 recordTimeInterval = setInterval(function() { that.data.timedifference = that.data.timedifference + 1; // console.log(that.data.timedifference); if (that.data.timedifference == 120) { that.data.ifreachessixty = true; recorderManager.stop(); } }, 500) }, //触摸结束:录音结束 touchEnd: function(e) { var that = this; clearInterval(recordTimeInterval); that.setData({ voiceBtnStyle: false, // timedifference: that.data.timedifference, recordBtnstate: "0", }) if (!that.data.ifreachessixty) { wx.hideToast(); recorderManager.stop(); } else { that.data.ifreachessixty = false; console.log("到达60s,以自动发出") } }, //手指触摸后移动:取消发送语音 cancelmove: function(e) { var statrtY = this.data.startpointY; var curpointY = e.touches[0].pageY; if (statrtY - curpointY > 80) { // recorderManager.stop(); wx.showToast({ title: '取消发送', duration: 999999 }) this.setData({ isSendVoice: false, }) } }, // 播放录音 audioPlay: function(e) { var that = this; var src = e.currentTarget.dataset.src; var msgid = e.currentTarget.dataset.id; if (src == '') { that.errorMsg("播放语音失败"); return; } //判断当前是否有语音正在播放 if (innerAudioContext.paused) { innerAudioContext.src = src; innerAudioContext.play(); // that.data.selPlayVoiceMsgid = msgid; that.setData({ selPlayVoiceMsgid: msgid }) } else { innerAudioContext.stop(); if (msgid != that.data.selPlayVoiceMsgid) { innerAudioContext.src = src; innerAudioContext.play(); // that.data.selPlayVoiceMsgid = msgid; that.setData({ selPlayVoiceMsgid: msgid }) } } //监听音频自然播放至结束的事件 innerAudioContext.onEnded(function() { that.setData({ selPlayVoiceMsgid: "" }) }) //监听音频的异常事件 innerAudioContext.onError((res) => { console.log(res.errMsg); }) }, //添加表情 sendEmoji: function(e) { console.log(e); var that = this; that.data.islastemjandtext = 1; that.insertEmoji(that.data.msgcontent, e.target.dataset.id) }, insertEmoji: function(originalString, flgString) { var newstr = ""; var cursor = 0; if (originalString.length != 0) { cursor = this.data.textareacursor; } if (originalString.length > 0) { if (cursor != 0) { for (var i = 0; i < originalString.length; i++) { if (i + 1 == cursor) { var tmpstart = originalString.substring(0, cursor); newstr += tmpstart + flgString; cursor = cursor + flgString.length; break; } } } else { newstr = flgString + originalString; cursor = cursor + flgString.length; } } else { newstr = flgString; cursor = cursor + flgString.length; } this.setData({ msgcontent: newstr, textareacursor: cursor, btnSendOrAddMenu: 0, }) }, //展示菜单 menuhide: function() { this.setData({ menuheight: "110rpx", menushow: "none" }) }, // 相机 data-type = 2 or data-type = 1:相册 takeOrAlbumBtn: function(e) { var that = this; var typeoption = e.currentTarget.dataset.type == 1 ? ["album"] : ["camera"]; wx.chooseImage({ count: 1, sizeType: ['original', 'compressed'], sourceType: typeoption, //camera:相机 ,album:相册 success(res) { // tempFilePath可以作为img标签的src属性显示图片 const tempFilePaths = res.tempFilePaths that.uploadfileToServer("1", tempFilePaths[0], '.png'); }, error: function(e) { console.log(e.msg); that.errorMsg(e.msg); }, fail: function(e) { console.log(e.msg); that.errorMsg(e.msg); }, }) }, allmenuhide: function(e) { console.log(e); var that = this; this.setData({ isshowemoji: "none", menuheight: "110rpx", menushow: "none", companyMenu: "none", companypop: "none", contactUspop: "none", funhight: "55rpx", textareahight: "48rpx", copyContentMsgid: "", }) }, //点击banner clickbanner: function(e) { var that = this; that.data.msgcontent = e.currentTarget.dataset.content; that.sendMsg(0, 1); }, // 点击客服头像 companyMenuShow: function(e) { var that = this; var left = e.detail.x; var right = e.detail.y; if (280 + right > this.data.scrollViewHeight.replace("px", "")) { right = this.data.scrollViewHeight.replace("px", "") - 280; } this.setData({ companyMenu: "block", companymenuleft: left + "px", //企业信息left companymenuright: right + "px", //企业信息right }) this.headclick = 1; }, closeCompanypop: function() { this.setData({ companypop: "none" }) }, closeContactUspop: function() { this.setData({ contactUspop: "none" }) }, //展示表情 emojimenu: function() { var menuhei; var showemoji; var txtgetfocus = false; var funhight; var textareahight; if (this.data.isshowemoji == "none") { menuhei = "570rpx" showemoji = "block"; funhight = "510rpx" textareahight = "500rpx" } else { menuhei = "110rpx" showemoji = "none"; funhight = "55rpx" textareahight = "48rpx" } this.setData({ menushow: "none", menuheight: menuhei, isshowemoji: showemoji, // txtfocus: txtgetfocus, isvoice: 1, keyboardOrvoice: "voice", funhight: funhight, textareahight: textareahight, companyMenu: "none", copyContentMsgid: "", }) }, //展示菜单 menuBtn: function() { var menuhei; var menu; var txtgetfocus = false; var funhight; var textareahight; if (this.data.menushow == 'none') { menuhei = "570rpx" menu = "flex"; funhight = "510rpx" textareahight = "500rpx" } else { menuhei = "110rpx" menu = "none"; funhight = "55rpx" textareahight = "48rpx" } this.setData({ isshowemoji: "none", menuheight: menuhei, menushow: menu, txtfocus: txtgetfocus, isvoice: 1, keyboardOrvoice: "voice", funhight: funhight, textareahight: textareahight, companyMenu: "none", copyContentMsgid: "", }) }, talkviewbottomf: function() { this.setData({ isshowemoji: "none", menushow: "none", companyMenu: "none", copyContentMsgid: "", }) this.ifgetfous = "1", this.checkoutmsg(); }, //消息编辑框失去焦点 msgtxtbulr: function(e) { var that = this; var curcursor = e.detail.cursor; //失去焦点时候获取光标位置 if (that.data.isshowemoji == "none") { that.setData({ textareacursor: curcursor, isshowemoji: "none", menuheight: "110rpx", }) } else { that.setData({ textareacursor: curcursor, isshowemoji: "block", }) } if (that.data.menushow == "none") { that.setData({ textareacursor: curcursor, menushow: "none", }) } else { that.setData({ textareacursor: curcursor, menushow: "flex", menuheight: "570rpx", }) }; if (that.data.menushow == "none" && that.data.isshowemoji == "none") { that.setData({ funhight: "55rpx", textareahight: "48rpx" }) } that.setData({ copyContentMsgid: "", }) if (that.data.msgcontent.length > 0) { that.ifgetfous = "0" } }, //输入法事件 checkoutmsg: function(e) { var that = this; that.data.islastemjandtext = 0; if (that.system == "android") { if (that.model == "Redmi Note 3" || that.model == "Mi Note 3" || that.model == "Redmi Note 4") {} else { console.log("BBB:" + that.bbb); if (that.bbb == "0" && that.ifgetfous == "1") { that.bbb = "1"; if (e != undefined) { that.ifgetfous = "0" } that.setData({ msgcontent: "", }) return; } } } if (e != undefined) { var sOrM = 1; if (e.detail.value.replace(/\s+/g, '').length > 0) { sOrM = 0; } that.setData({ btnSendOrAddMenu: sOrM, msgcontent: e.detail.value, }) } }, // 滚动切换标签样式 switchTab: function(e) { this.setData({ currentTab: e.detail.current }); this.checkCor(); }, // 点击标题切换当前页时改变样式 swichNav: function(e) { var cur = e.target.dataset.current; if (this.data.currentTaB == cur) { return false; } else { this.setData({ currentTab: cur }) } }, //判断当前滚动超过一屏时,设置tab标题滚动条。 checkCor: function() { if (this.data.currentTab > 4) { this.setData({ scrollLeft: 300 }) } else { this.setData({ scrollLeft: 0 }) } }, //排序 compare: function(property) { return function(a, b) { var value1 = a[property]; var value2 = b[property]; return value1 - value2; } }, //session 失效后,重新获取 getsessionksy: function() { wx.login({ success: res => { // 发送 res.code 到后台换取 openId, sessionKey, unionId if (res.code) { var code = res.code; wx.request({ url: that.globalUserData.serviceurl + "/api/UserDockingService.ashx?action=getopenid", // url: "http://localhost:62825/api/thatletService.ashx?action=getopenid", header: { 'content-type': 'thatlication/json' // 默认值 }, data: { code: code, // scene: that.sence, }, method: "POST", success: function(e) { //成功回调 if (e.data.success) { that.globalUserData.session_key = e.data .session_key; that.setData({ ifShowGetPhoneAuthBtn: true }); } } }) } } }) }, // 获取用户电话号码End viewLargerImage: function(e) { var src = e.currentTarget.dataset.src; //获取data-src //图片预览 wx.previewImage({ current: src, // 当前显示图片的url urls: [src], // 需要预览的图片utl链接列表 success: function(res) { console.log("查看大图成功!"); }, error: function(res) { that.errorMsg(res); } }) }, //复制文本 copyTxt: function(e) { var that = this; that.data.copyContentMsgid = e.currentTarget.dataset.id; //当前复制内容msgid that.data.copyCotent = e.currentTarget.dataset.text; //当前复制内容 var query = wx.createSelectorQuery(); query.select('#copytext' + that.data.copyContentMsgid).boundingClientRect(); query.exec(function(res) { that.data.copyButtonPosition = Number(res[0].width).toFixed(0) - 30; that.setData({ copyButtonPosition: that.data.copyButtonPosition, copyContentMsgid: that.data.copyContentMsgid, }) }) }, //开始复制 startCopyContet: function() { var that = this; wx.setClipboardData({ data: that.data.copyCotent, success: function(res) { // wx.hideToast(); console.log("复制成功") } }) that.setData({ copyContentMsgid: "", }) }, //版本号比较 compareVersion: function(v1, v2) { v1 = v1.split('.') v2 = v2.split('.') const len = Math.max(v1.length, v2.length) while (v1.length < len) { v1.push('0') } while (v2.length < len) { v2.push('0') } for (let i = 0; i < len; i++) { const num1 = parseInt(v1[i]) const num2 = parseInt(v2[i]) if (num1 > num2) { return 1 } else if (num1 < num2) { return -1 } } return 0 }, //用户点击右上角分享 onSharethatMessage: function(res) { var that = this; var title = that.data.tiny.length == 0 ? "忠仕微客宝" : that.data.tiny; if (res.from === 'button') {} //判断哪种方式分享,menu右上角分享,button 页面内分享 return { title: title, path: '/pages/tripartite/tripartite?from_openid=' + that.globalUserData.openid + '&from_siteid=' + that.globalUserData.siteid, } }, //复制客服人员信息(微信) copyUserInfor: function(e) { var that = this; //button 获取formid if (e.detail.formId == 'the formId is a mock one' || e.detail.formId == undefined) {} else { // var item = { // formid: e.detail.formId, // formidcreatetime: util.formatTime(new Date()) // } // that.data.formidArray.push(item); that.data.formid = e.detail.formId; that.data.formidcreatetime = util.formatTime(new Date()); that.formidUpload(); } var cooytext = that.data.cusWechat; if (cooytext.length == 0) { that.errorMsg("该客服没有设置微信号!"); return; } wx.setClipboardData({ data: cooytext, success: function(res) { wx.hideToast(); that.setData({ addfriendguide: "block" }) //that.data.msgcontent="微信号复制成功:请返回微信--添加朋友--粘贴即可添加客服微信" // that.sendMsgPara("0", "2", "微信号复制成功:请返回微信--添加朋友--粘贴即可添加客服微信"); }, fail: function(res) { that.errorMsg(res.errMsg); } }) }, //访客发送消息(参数带msgcontent) sendMsgPara: function(msgtype, msgsource, msgcontent) { var that = this; var msgid = that.createCode(32); //生成msgid 32位字符串 var nowtime = util.formatTime(new Date()); //向直接向页面赋值 var item = { viewContent: that.splitMsgcontent(msgcontent), content: msgcontent, msgid: msgid, msgtype: msgtype, source: msgsource, tick: "99999999999999999", //生成最大tick 保证该消息在最后一条 time: nowtime.substring(11, nowtime.length), timedifference: "", voicewidth: "", msgsendstate: "0", //信息发送状态 0 正在发送 }; that.data.msg.push(item); that.setData({ msgArray: that.data.msg, // msgcontent: "", // scrollTop: 2147483647 + "px", // textareacursor: 0, // btnSendOrAddMenu: 1, funhight: "55rpx", textareahight: "48rpx", menushow: "none", isshowemoji: "none", menuheight: "110rpx", }); that.scrollToTheBottom(); // that.setData({ // scrollTop: 2147483647 + "px", // }); wx.request({ url: that.globalUserData.serviceurl + "/api/UserDockingService.ashx?action=send", // url:"http://localhost:62825/api/Chat.ashx?action=send", header: { 'content-type': 'thatlication/json' // 默认值 }, data: { siteid: that.siteid, sid: that.data.nowtalksid, msg: msgcontent, source: msgsource, msgtype: msgtype, // formid: "", // formidcreatetime: "", msgid: msgid, }, method: "POST", success: function(e) { //成功回调 if (e.data.success) {} else { if (e.data.error == "no chat" && that.isAtTalkPage) { that.openChat(); console.log("已重新开启对话!"); } else { console.log(e.data.error); return false; } } }, error: function(e) { console.log("发送消息失败" + e.data.error); return }, complete: function() {}, }); }, //数组移除消息 arrarRemove: function(array, msgid) { for (var i = 0; i < array.length; i++) { if (array[i].msgid == msgid) { array.splice(i, 1); } } return array; }, //消息滚动顶部事件 scrollToupper: function(e) { var that = this; //阻止事件多次触发 if (!that.data.ifscrollToupper) { return; } //是否存在下拉数据 if (that.data.loadingcontent == "没有更多了") { setTimeout(function() { that.setData({ ifloading: true, }); }, 1000) return; } //获取当前页面消息最小的tick值 var mintick = that.data.msg[0].tick; for (var i = 0; i < that.data.msg.length; i++) { if (that.data.msg[i].tick < mintick) { mintick = that.data.msg[i].tick } } //加载数据显示 that.setData({ ifloading: false, loadingcontent: "正在加载..." }); setTimeout(function() { wx.request({ url: that.globalUserData.serviceurl + "/api/Chat.ashx?action=getmsgbycidandtick", //url: "http://localhost:62825/api/Chat.ashx?action=getmsgbycidandtick", header: { 'content-type': 'thatlication/json' // 默认值 }, data: { siteid: that.siteid, cid: "LRWT_" + that.globalUserData.openid, tick: mintick, size: "10" }, method: "POST", success: function(e) { if (e.data.success) { if (e.data.list.length != 0) { for (var i = 0; i < e.data.list.length; i++) { var item = {}; if (e.data.list[i].msgtype == "0") { var viewContent = that.splitMsgcontent(e.data.list[ i].content); item = { viewContent: viewContent, content: e.data.list[i].content, msgid: e.data.list[i].msgid, msgtype: e.data.list[i].msgtype, source: e.data.list[i].source, tick: e.data.list[i].tick, time: e.data.list[i].time, } that.data.msg.push(item); } else if (e.data.list[i].msgtype == "1") { var newcontent = that.replacecontent(e.data.list[i] .content, /\\/g, "/"); item = { content: newcontent, msgid: e.data.list[i].msgid, msgtype: e.data.list[i].msgtype, source: e.data.list[i].source, tick: e.data.list[i].tick, time: e.data.list[i].time, }; that.data.msg.push(item); } else { that.data.msg.push(e.data.list[i]); } } that.setData({ msgArray: that.data.msg.sort(that.compare( "tick")), }); } else { that.setData({ loadingcontent: "没有更多" }); } // that.setData({ // ifloading: true, // scrollTop: 10 // }); setTimeout(function() { that.setData({ ifloading: true, scrollTop: 10 }); }, 500) } else { that.setData({ loadingcontent: "下拉获取历史消息失败", }, setTimeout(function() { that.setData({ ifloading: true, scrollTop: 10, }) }, 500)); } }, error: function(res) { wx.showLoading({ title: '下拉获取历史消息失败', }) that.setData({ ifloading: true, scrollTop: 10, }); console.log(res.error); }, complete: function() { that.setData({ ifscrollToupper: true }); } }) }, 2000) that.setData({ ifscrollToupper: false }); }, //滚动条滚动底部方法 scrollToTheBottom: function() { var that = this; setTimeout(function() { that.setData({ scrollTop: 2147483647 + "px", }) }, 200) }, //拆分文字,图片 splitMsgcontent: function(msgcontent) { var that = this; var textAndEmojiJson = []; var strTextAndEmojiList = []; var emojilist = that.data.wxemojliList; var newMsgContent = msgcontent; for (var i = 0; i < emojilist.length; i++) { if (msgcontent.indexOf(emojilist[i].wxemojiid) != -1) { newMsgContent = newMsgContent.replace(new RegExp(emojilist[i].wxemojiid, 'g'), emojilist[i] .wxemojimarhing); //newMsgContent = that.data.msgcontent.replaceAll(emojilist[i].wxemojiid, emojilist[i].wxemojimarhing); } } strTextAndEmojiList = newMsgContent.split("_&"); //去除数组为空的元素 if (strTextAndEmojiList.length != 0) { for (var j = 0; j < strTextAndEmojiList.length; j++) { if (strTextAndEmojiList[j] == "") { strTextAndEmojiList.splice(j, 1); j = j - 1; } else { var item = {}; if (strTextAndEmojiList[j].indexOf(".gif-/a") != -1) { item = { bisemoji: 0, //0 表情 1文字 content: strTextAndEmojiList[j].substring(0, strTextAndEmojiList[j].length - 3) } } else { item = { bisemoji: 1, //0 表情 1文字 content: strTextAndEmojiList[j] } } textAndEmojiJson.push(item); } } } // that.setData({ // msgNewContent: textAndEmojiJson // }) return textAndEmojiJson; }, //发送当位置 choosePotsion: function() { var that = this; wx.chooseLocation({ success: function(res) { console.log("1" + JSON.stringify(res)); var name = res.name; var address = res.address; var latitude = res.latitude; var longitude = res.longitude; var jsonposi = { name: name, address: address, latitude: latitude, longitude: longitude, }; that.data.msgcontent = jsonposi; that.sendMsg(3, 0); //%%% }, fail: function() { wx.getSetting({ success(res) { if (!res.authSetting['scope.userLocation']) { // 禁止获取地理位置后重新调用授权后,再次获取 console.log("2" + res) wx.authorize({ scope: 'scope.userLocation', success() { wx.chooseLocation({ success: function(res) { // 禁止获取地理位置后重新调用授权后,获取位置成功 console.log("3" + res) }, }) }, fail() { // 禁止获取地理位置后重新调用授权后,获取位置失败 console.log("f2") } }) } } }) } }) }, //在地图展示位置 getLocation: function(event) { var that = this; console.log(event.currentTarget.dataset.gid); if (event.currentTarget.dataset.gid == "") { return; } that.loadingshow(); //调用地址解析接口 qqmapsdk.geocoder({ //获取表单传入地址 address: event.currentTarget.dataset.gid, //地址参数,例:固定地址,address: '北京市海淀区彩和坊路海淀西大街74号' success: function(res) { //成功后的回调 that.loadinghide(); console.log(res); var res = res.result; wx.openLocation({ longitude: res.location.lng, latitude: res.location.lat, name: event.currentTarget.dataset.gid, }) }, fail: function(error) { console.error(error); that.errorMsg("打开地图失败!") }, complete: function(res) { console.log(res); } }) }, //生成随机数 createCode: function(length) { var code; //首先默认code为空字符串 code = ''; //设置长度,这里看需求,我这里设置了 var codeLength = length; //设置随机字符 var random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); //循环codeLength 我设置的4就是循环4次 for (var i = 0; i < codeLength; i++) { //设置随机数范围,这设置为0 ~ 36 var index = Math.floor(Math.random() * 9); //字符串拼接 将每次随机的字符 进行拼接 code += random[index]; } return code; }, /** * 正确消息 */ showMsg: function(title) { wx.showToast({ title: title, image: "/images/duihao.png" }); }, /** * 错误消息 */ errorMsg: function(title) { wx.showToast({ title: title, image: "/images/error.png" }); }, loadingshow: function() { wx.showLoading({ title: '加载中', }) }, loadinghide: function() { setTimeout(function() { wx.hideLoading() }, 0) }, replacecontent: function(originalcontent, character, tocharacter) { var content = ""; content = originalcontent.replace(character, tocharacter); return content; }, initInterval: function() { var das; }, //是否正常获取站点,以及正常开启对话 checkConnectOk() { var conncetmsg = ""; if (!this.serverconncet) { conncetmsg = "开启对话失败!" } return conncetmsg; }, })