singleChatModeCtrl.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. angular.module('push')
  2. .controller('singleChatModeCtrl', function ($scope, $stateParams, ScienceCircleService, UtilService,ConstantService, CommentService,
  3. UserService, $ionicScrollDelegate, $timeout, IMChatTempData) {
  4. //TODO
  5. if($scope.app){
  6. $scope.setStatusBar(0);
  7. }
  8. $scope.message = {
  9. content: ""
  10. };
  11. console.log($stateParams);
  12. var previoustime = "";//记录上一个消息时间(时间不能相等)
  13. $scope.newmsgnum = 0;//非当前会话新消息个数
  14. $scope.userid = UserService.id;
  15. var user = UserService.user;
  16. var topicid = $stateParams.topicid;//主题id
  17. var topicname = $stateParams.topicname;//主题名称
  18. $scope.username = $stateParams.username;//对方用户名
  19. var reciverid = $stateParams.reciverid;//对方userid
  20. $scope.isfocus = $stateParams.isfocus;//是否关注对方 1已 2未
  21. $scope.chatmsglist = [];
  22. // 点赞、收藏、分享的传参
  23. $scope.clickRecord=
  24. {
  25. targetid: $stateParams.reciverid,
  26. moduleid: ConstantService.TABLE_CODE_30003,
  27. userid:UserService.id,
  28. clicktype:1
  29. };
  30. //获取聊天消息列表
  31. var getChatMessageList = function () {
  32. var msgid = $scope.chatmsglist.length > 0 ? $scope.chatmsglist[0].messageid : 0;
  33. ScienceCircleService.getChatMessageList(topicid, msgid).then(function (response) {
  34. // console.log(response);
  35. $scope.chatmsglist = response;
  36. if (response.length > 0) {
  37. angular.forEach($scope.chatmsglist, function (value, index) {
  38. if (filtertime(value.messagetime)) {
  39. value.messagetime = "";
  40. }
  41. })
  42. }
  43. $timeout(function () {
  44. $ionicScrollDelegate.$getByHandle("singlechatcontent").scrollBottom();
  45. }, 0);
  46. }, function () {
  47. })
  48. };
  49. //初始化聊天消息object
  50. var tempobj = {
  51. message: "",
  52. messageid: 0,
  53. messagetime: "",
  54. messagetype: 6101,
  55. reciverid: reciverid,
  56. topicid: topicid,
  57. type: 6202,
  58. userModel: {username: user.username, photo: user.photo},
  59. userid: $scope.userid
  60. };
  61. //发送消息
  62. $scope.sendMessage = function () {
  63. if (!UtilService.isDefined($scope.message.content)) {
  64. return;
  65. }
  66. var msgindex = $scope.chatmsglist.length;//当前当前数组长度
  67. var tempmessage = angular.copy(tempobj);//深拷贝空消息
  68. tempmessage.messagetime = UtilService.formatDate().formattime;//消息时间初始化
  69. if (filtertime(tempmessage.messagetime)) {
  70. tempmessage.messagetime = "";
  71. }
  72. tempmessage.messagetype = 6101;//消息内容类型初始化
  73. tempmessage.type = 6202;//消息类型初始化
  74. tempmessage.message = $scope.message.content;//消息内容初始化
  75. $scope.chatmsglist.push(tempmessage);//聊天列表添加消息
  76. scrollToBottom();//滚动至底部
  77. $scope.message.content = "";//输入框消息内容情况
  78. // 发送消息
  79. ScienceCircleService.sendMessage(reciverid, tempmessage.message, tempmessage.messagetype, tempmessage.type).then(function (response) {
  80. // console.log(response);
  81. console.log(msgindex);
  82. }, function () {
  83. console.log(msgindex);
  84. })
  85. };
  86. //进入获取数据
  87. $scope.$on('$ionicView.beforeEnter', function () {
  88. getChatMessageList();
  89. });
  90. //查看历史消息记录
  91. $scope.getHistoryMessage = function () {
  92. var msgid = $scope.chatmsglist.length > 0 ? $scope.chatmsglist[0].messageid : 0;
  93. ScienceCircleService.getChatMessageList(topicid, msgid).then(function (response) {
  94. // console.log(response);
  95. if (response.length > 0) {
  96. $scope.chatmsglist = response.concat($scope.chatmsglist);
  97. angular.forEach($scope.chatmsglist, function (value, index) {
  98. if (filtertime(value.messagetime)) {
  99. value.messagetime = "";
  100. }
  101. })
  102. }
  103. $scope.$broadcast('scroll.refreshComplete');
  104. }, function () {
  105. $scope.$broadcast('scroll.refreshComplete');
  106. })
  107. };
  108. //接收单聊信息
  109. $scope.$on("recSingleMessage", function (event, data) {
  110. if (topicname == data.topicname) {
  111. if (filtertime(data.messagetime)) {
  112. data.messagetime = "";
  113. }
  114. //消息属于当前会话
  115. $scope.$apply(function () {
  116. $scope.chatmsglist.push(data);
  117. });
  118. scrollToBottom();
  119. IMChatTempData.clearMsgDataByTopic(topicname);
  120. } else {
  121. //消息不属于当前会话
  122. $scope.$apply(function () {
  123. $scope.newmsgnum++;
  124. });
  125. }
  126. });
  127. // 滚动至底部
  128. var scrollToBottom = function () {
  129. $timeout(function () {
  130. $ionicScrollDelegate.$getByHandle("singlechatcontent").scrollBottom();
  131. }, 50);
  132. };
  133. // 控制重复点击
  134. var flag = 1;
  135. // 关注聊天用户 --已经关注是1,未关注是2
  136. $scope.focusUser = function () {
  137. if(flag == 0){
  138. return;
  139. }
  140. flag = 0;
  141. $scope.clickRecord.targetid = reciverid;
  142. $scope.clickRecord.moduleid = ConstantService.TABLE_CODE_30043;
  143. $scope.clickRecord.clicktype = ConstantService.TRACE_TYPE_2617;
  144. CommentService.postClickFavour($scope.clickRecord).then(function (response) {
  145. // console.log(response);
  146. if (response.code == ConstantService.INTERFACE_STATUS_CODE_3350) {
  147. $scope.isfocus = 1;
  148. // 关注成功
  149. UtilService.showMess(ConstantService.TRACE_TYPE_STRING_2617);
  150. }
  151. // 恢复可用
  152. flag = 1;
  153. }, function () {
  154. // 恢复可用
  155. flag = 1;
  156. UtilService.showMess(ConstantService.INTERFACE_MESSAGE_ERROR);
  157. });
  158. };
  159. //过滤相邻时间一致的消息(以分钟算)
  160. var filtertime = function (time) {
  161. var time1 = previoustime != "" ? new Date(previoustime) : "";
  162. if($scope.app){
  163. if (device.platform != "Android") {
  164. time = time.replace("-", "/");
  165. time = time.replace("-", "/");
  166. }
  167. }
  168. var time2 = new Date(time.substr(0, 16));
  169. if (previoustime != "" && time1.getTime() == time2.getTime()) {
  170. return true;
  171. } else {
  172. previoustime = time.substr(0, 16);
  173. return false;
  174. }
  175. };
  176. //键盘弹出最新消息出现在键盘顶部
  177. $scope.myTouch=function () {
  178. window.addEventListener('native.keyboardshow',function (e){
  179. scrollToBottom();//滚动至底部
  180. if (device.platform != "Android") {
  181. $('.chatActionModleWrap').css({
  182. 'bottom':e.keyboardHeight +4+ 'px'
  183. });
  184. $('.singleViewWrap').css({
  185. 'bottom':e.keyboardHeight +44+ 'px'
  186. });
  187. }
  188. // }else{
  189. // $('.chatActionModleWrap').css({
  190. // 'bottom':e.keyboardHeight +74+ 'px'
  191. // });
  192. // $('.singleViewWrap').css({
  193. // 'bottom':e.keyboardHeight +114+ 'px'
  194. // });
  195. // }
  196. });
  197. };
  198. //收起键盘
  199. window.addEventListener('native.keyboardhide',function (e){
  200. $('.chatActionModleWrap').css({
  201. 'bottom':0
  202. });
  203. $('.singleViewWrap').css({
  204. 'bottom':54+ 'px'
  205. });
  206. cordova.plugins.Keyboard.isVisible = true;
  207. $timeout(function() {
  208. cordova.plugins.Keyboard.isVisible = false;
  209. }, 100);
  210. });
  211. });