angular.module('push') .controller('singleChatModeCtrl', function ($scope, $stateParams, ScienceCircleService, UtilService,ConstantService, CommentService, UserService, $ionicScrollDelegate, $timeout, IMChatTempData) { //TODO if($scope.app){ $scope.setStatusBar(0); } $scope.message = { content: "" }; console.log($stateParams); var previoustime = "";//记录上一个消息时间(时间不能相等) $scope.newmsgnum = 0;//非当前会话新消息个数 $scope.userid = UserService.id; var user = UserService.user; var topicid = $stateParams.topicid;//主题id var topicname = $stateParams.topicname;//主题名称 $scope.username = $stateParams.username;//对方用户名 var reciverid = $stateParams.reciverid;//对方userid $scope.isfocus = $stateParams.isfocus;//是否关注对方 1已 2未 $scope.chatmsglist = []; // 点赞、收藏、分享的传参 $scope.clickRecord= { targetid: $stateParams.reciverid, moduleid: ConstantService.TABLE_CODE_30003, userid:UserService.id, clicktype:1 }; //获取聊天消息列表 var getChatMessageList = function () { var msgid = $scope.chatmsglist.length > 0 ? $scope.chatmsglist[0].messageid : 0; ScienceCircleService.getChatMessageList(topicid, msgid).then(function (response) { // console.log(response); $scope.chatmsglist = response; if (response.length > 0) { angular.forEach($scope.chatmsglist, function (value, index) { if (filtertime(value.messagetime)) { value.messagetime = ""; } }) } $timeout(function () { $ionicScrollDelegate.$getByHandle("singlechatcontent").scrollBottom(); }, 0); }, function () { }) }; //初始化聊天消息object var tempobj = { message: "", messageid: 0, messagetime: "", messagetype: 6101, reciverid: reciverid, topicid: topicid, type: 6202, userModel: {username: user.username, photo: user.photo}, userid: $scope.userid }; //发送消息 $scope.sendMessage = function () { if (!UtilService.isDefined($scope.message.content)) { return; } var msgindex = $scope.chatmsglist.length;//当前当前数组长度 var tempmessage = angular.copy(tempobj);//深拷贝空消息 tempmessage.messagetime = UtilService.formatDate().formattime;//消息时间初始化 if (filtertime(tempmessage.messagetime)) { tempmessage.messagetime = ""; } tempmessage.messagetype = 6101;//消息内容类型初始化 tempmessage.type = 6202;//消息类型初始化 tempmessage.message = $scope.message.content;//消息内容初始化 $scope.chatmsglist.push(tempmessage);//聊天列表添加消息 scrollToBottom();//滚动至底部 $scope.message.content = "";//输入框消息内容情况 // 发送消息 ScienceCircleService.sendMessage(reciverid, tempmessage.message, tempmessage.messagetype, tempmessage.type).then(function (response) { // console.log(response); console.log(msgindex); }, function () { console.log(msgindex); }) }; //进入获取数据 $scope.$on('$ionicView.beforeEnter', function () { getChatMessageList(); }); //查看历史消息记录 $scope.getHistoryMessage = function () { var msgid = $scope.chatmsglist.length > 0 ? $scope.chatmsglist[0].messageid : 0; ScienceCircleService.getChatMessageList(topicid, msgid).then(function (response) { // console.log(response); if (response.length > 0) { $scope.chatmsglist = response.concat($scope.chatmsglist); angular.forEach($scope.chatmsglist, function (value, index) { if (filtertime(value.messagetime)) { value.messagetime = ""; } }) } $scope.$broadcast('scroll.refreshComplete'); }, function () { $scope.$broadcast('scroll.refreshComplete'); }) }; //接收单聊信息 $scope.$on("recSingleMessage", function (event, data) { if (topicname == data.topicname) { if (filtertime(data.messagetime)) { data.messagetime = ""; } //消息属于当前会话 $scope.$apply(function () { $scope.chatmsglist.push(data); }); scrollToBottom(); IMChatTempData.clearMsgDataByTopic(topicname); } else { //消息不属于当前会话 $scope.$apply(function () { $scope.newmsgnum++; }); } }); // 滚动至底部 var scrollToBottom = function () { $timeout(function () { $ionicScrollDelegate.$getByHandle("singlechatcontent").scrollBottom(); }, 50); }; // 控制重复点击 var flag = 1; // 关注聊天用户 --已经关注是1,未关注是2 $scope.focusUser = function () { if(flag == 0){ return; } flag = 0; $scope.clickRecord.targetid = reciverid; $scope.clickRecord.moduleid = ConstantService.TABLE_CODE_30043; $scope.clickRecord.clicktype = ConstantService.TRACE_TYPE_2617; CommentService.postClickFavour($scope.clickRecord).then(function (response) { // console.log(response); if (response.code == ConstantService.INTERFACE_STATUS_CODE_3350) { $scope.isfocus = 1; // 关注成功 UtilService.showMess(ConstantService.TRACE_TYPE_STRING_2617); } // 恢复可用 flag = 1; }, function () { // 恢复可用 flag = 1; UtilService.showMess(ConstantService.INTERFACE_MESSAGE_ERROR); }); }; //过滤相邻时间一致的消息(以分钟算) var filtertime = function (time) { var time1 = previoustime != "" ? new Date(previoustime) : ""; if($scope.app){ if (device.platform != "Android") { time = time.replace("-", "/"); time = time.replace("-", "/"); } } var time2 = new Date(time.substr(0, 16)); if (previoustime != "" && time1.getTime() == time2.getTime()) { return true; } else { previoustime = time.substr(0, 16); return false; } }; //键盘弹出最新消息出现在键盘顶部 $scope.myTouch=function () { window.addEventListener('native.keyboardshow',function (e){ scrollToBottom();//滚动至底部 if (device.platform != "Android") { $('.chatActionModleWrap').css({ 'bottom':e.keyboardHeight +4+ 'px' }); $('.singleViewWrap').css({ 'bottom':e.keyboardHeight +44+ 'px' }); } // }else{ // $('.chatActionModleWrap').css({ // 'bottom':e.keyboardHeight +74+ 'px' // }); // $('.singleViewWrap').css({ // 'bottom':e.keyboardHeight +114+ 'px' // }); // } }); }; //收起键盘 window.addEventListener('native.keyboardhide',function (e){ $('.chatActionModleWrap').css({ 'bottom':0 }); $('.singleViewWrap').css({ 'bottom':54+ 'px' }); cordova.plugins.Keyboard.isVisible = true; $timeout(function() { cordova.plugins.Keyboard.isVisible = false; }, 100); }); });