_onSocket.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. import _data from './_data';
  2. import _get from './_get';
  3. import _mixins from './_mixins';
  4. import _action from './_action';
  5. import _page from "./common";
  6. import SDK from "./tendenceImSdk";
  7. // #ifdef APP-PLUS
  8. const call = uni.requireNativePlugin('TencentCloud-TUICallKit');
  9. // let call = uni.requireNativePlugin('TUICallingUniPlugin-TUICallingModule')
  10. // #endif
  11. export default {
  12. closeVoiceRoom() {
  13. uni.$emit('closeVoiceRoom', {})
  14. },
  15. getVoiceRoom(params) {
  16. let route = _page.getRoute();
  17. if (route.indexOf('chat/message') > -1) {
  18. _get.checkVoiceRoomState(params, function(ret) {
  19. //加入
  20. uni.$emit('memberJoinRoom', ret)
  21. })
  22. }
  23. },
  24. clientLink(data) {
  25. _data.localData('client_id', data.client_id);
  26. },
  27. cancleVedio(data) {
  28. // #ifdef APP-PLUS
  29. call.logout();
  30. // #endif
  31. uni.$emit('close_call', data);
  32. },
  33. vedioData(data) {
  34. //判断是否在线发送视频邀请
  35. if (!_data.data('isOnlie')) {
  36. _page.pushVedioLocalMsg(data);
  37. } else {
  38. console.log(data);
  39. // uni.navigateTo({
  40. // url: '/pages/chat/videoCall?'+_page.pageParam(data)
  41. // });
  42. }
  43. return true;
  44. },
  45. /** 验证token */
  46. checkToken(res) {
  47. if (res.err) {
  48. _action.checkFail();
  49. } else {
  50. /** 获取基础数据 */
  51. _get.base();
  52. }
  53. },
  54. /** 下线 */
  55. offline(res) {
  56. // uni.showModal({
  57. // content: '你的账号在另一客户端登陆,如果不是你本人操作,请修改你的密码',
  58. // success() {
  59. // _action.checkFail();
  60. // },
  61. // });
  62. _action.checkFail();
  63. // uni.navigateTo({
  64. // url:'/pages/in/login'
  65. // })
  66. },
  67. /** 获得会话列表 */
  68. getChatList() {
  69. _get.getChatList();
  70. },
  71. /** 获得好友列表 */
  72. getFriendList() {
  73. _get.getFriendList({
  74. up: 1
  75. });
  76. },
  77. /** 新好友提醒 */
  78. newFriend(data) {
  79. _action.playVoice('/static/voice/friend.mp3');
  80. let num = _data.data('new_friend_tips_num') + (data.num * 1);
  81. _data.data('new_friend_tips_num', num);
  82. _action.setStatusTips();
  83. },
  84. /** 点赞提醒 */
  85. circleLike(data) {
  86. _action.playVoice('/static/voice/circle.mp3');
  87. //通知朋友圈
  88. let circle_data = _data.localData('circle_data');
  89. for (let i = 0, j = circle_data.length; i < j; i++) {
  90. if (circle_data[i].post_id == data.id) {
  91. console.log('circleLike1...')
  92. circle_data[i].like = data.likes;
  93. circle_data[i].islike = data.action;
  94. _data.localData('circle_data', circle_data);
  95. uni.$emit('data_circle_data', circle_data);
  96. break;
  97. }
  98. }
  99. let circle_data_user = _data.localData('circle_data_user');
  100. for (let i = 0, j = circle_data_user.length; i < j; i++) {
  101. if (circle_data_user[i].post_id == data.id) {
  102. console.log('circleLike2...')
  103. circle_data_user[i].like = data.likes;
  104. circle_data_user[i].islike = data.action;
  105. _data.localData('circle_data_user', circle_data_user);
  106. uni.$emit('data_circle_data_user', circle_data_user);
  107. break;
  108. }
  109. }
  110. },
  111. addChatList() { //田百合花
  112. },
  113. timeData(data) { //田百合花
  114. uni.$emit('newReadTime', data.data.msg.time)
  115. // uni.setStorageSync(data.list_id+'_time',data.data.msg.time);
  116. },
  117. /** 接收新消息 */
  118. chatData(data) {
  119. console.log("删除成员", data);
  120. if (!_data.localData('token')) return false;
  121. let chat_data = _data.localData(data.list_id),
  122. brate = _data.localData('DISTURB:' + data.list_id),
  123. msg_reader_num = 0;
  124. /** 如果不是自己的消息,在这条会话界面,震动提示,没有在这条会话界面,震动加声音提示 */
  125. if (_data.data('user_info').id != data.data.msg.user_info.uid) {
  126. console.log('到这里');
  127. // #ifdef APP-PLUS
  128. _page.pushLocalMsg(data);
  129. if (brate == 0 || brate == undefined || brate == null) {
  130. uni.vibrateLong();
  131. }
  132. // #endif
  133. //在列表则更新未读状态
  134. if (_data.localData('message_list_id') == data.list_id) {
  135. _action.updataNoReader(data.list_id);
  136. } else {
  137. //否则声音提示
  138. if (brate == 0 || brate == undefined || brate == null) {
  139. _action.playVoice('/static/voice/chat.mp3');
  140. }
  141. msg_reader_num = 1;
  142. }
  143. }
  144. /** 更新对话列表数据 */
  145. let action_list_data = {};
  146. for (let i = 0, local_chat_list = _data.localData('chat_list'), j = local_chat_list.length; i < j; i++) {
  147. if (local_chat_list[i].list_id == data.list_id) {
  148. switch (data.data.msg.type * 1) {
  149. case 0:
  150. local_chat_list[i].last_msg = data.data.msg.content.text;
  151. break;
  152. case 1:
  153. /** 语音 */
  154. local_chat_list[i].last_msg = '[语音]';
  155. break;
  156. case 2:
  157. /** 图片 */
  158. local_chat_list[i].last_msg = '[图片]';
  159. break;
  160. case 3:
  161. /** 视频 */
  162. local_chat_list[i].last_msg = '[视频]';
  163. break;
  164. case 4:
  165. /** 文件 */
  166. local_chat_list[i].last_msg = '[文件]';
  167. break;
  168. case 5:
  169. /** 红包 */
  170. local_chat_list[i].last_msg = '[红包]';
  171. break;
  172. case 6:
  173. /** 在线视频 */
  174. local_chat_list[i].last_msg = '[在线视频]';
  175. break;
  176. case 7:
  177. /** 在线语音 */
  178. local_chat_list[i].last_msg = '[在线语音]';
  179. break;
  180. case 8:
  181. /** 名片 */
  182. local_chat_list[i].last_msg = '[名片]';
  183. break;
  184. case 9:
  185. /** 名片 */
  186. local_chat_list[i].last_msg = '[戳一戳]';
  187. uni.vibrateLong();
  188. break;
  189. default:
  190. /** 未知消息类型 */
  191. local_chat_list[i].last_msg = '[未知]';
  192. break;
  193. }
  194. // todo屏蔽 @群朋友 如果屏蔽消息,则不提示
  195. // if(local_chat_list[i].is_disturb == 1){
  196. // // 如果屏蔽,但是@本人和所有人all,则提示消息
  197. // if (data.data.msg.content && data.data.msg.content.user_id){
  198. // if(data.data.msg.content.user_id.indexOf(_data.data('user_info').id)>-1 || data.data.msg.content.user_id.indexOf('all')>-1){
  199. // local_chat_list[i].no_reader_num += msg_reader_num;
  200. // }
  201. // }
  202. // }else{
  203. // local_chat_list[i].no_reader_num += msg_reader_num;
  204. // }
  205. local_chat_list[i].no_reader_num += msg_reader_num;
  206. local_chat_list[i].time = data.data.msg.time;
  207. local_chat_list.sort((x, y) => {
  208. if (x.top > y.top) {
  209. return -1;
  210. }
  211. if (x.time > y.time) {
  212. return -1;
  213. }
  214. })
  215. _data.localData('chat_list', local_chat_list);
  216. uni.$emit('data_chat_list', local_chat_list);
  217. action_list_data = local_chat_list[i];
  218. break;
  219. }
  220. }
  221. if (!Object.keys(action_list_data).length) {
  222. //如果没有则新增
  223. action_list_data.list_id = data.list_id;
  224. action_list_data.chat_id = data.data.msg.id;
  225. action_list_data.no_reader_num = 1;
  226. action_list_data.photo_path = data.data.msg.user_info.face;
  227. action_list_data.show_name = data.data.msg.user_info.name;
  228. action_list_data.time = data.data.msg.time;
  229. action_list_data.last_msg = data.data.msg.content.text;
  230. action_list_data.top = 0;
  231. action_list_data.top_time = 0;
  232. action_list_data.type = data.data.type;
  233. let chat_list = _data.localData('chat_list');
  234. chat_list.splice(0, 0, action_list_data);
  235. //排序首选根据top排序,然后根据time排序
  236. chat_list = chat_list.slice(0, 30);
  237. chat_list.sort((x, y) => {
  238. if (x.top == y.top) {
  239. y.time - x.time;
  240. } else {
  241. return y.top - x.top;
  242. }
  243. })
  244. _data.localData('chat_list', chat_list);
  245. uni.$emit('data_chat_list', chat_list);
  246. }
  247. /** 在有这条对话的缓存数据情况下 */
  248. if (chat_data) {
  249. chat_data.list.push(data.data);
  250. chat_data.list = chat_data.list.slice(-15);
  251. _data.localData(data.list_id, chat_data);
  252. /** 如果在与对方的对话界面,发送数据到页面显示 */
  253. if (_data.localData('message_list_id') == data.list_id) {
  254. /** 保持页面15条数据,提升性能 */
  255. uni.$emit('data_chat_data_push', chat_data.list);
  256. }
  257. }
  258. _action.setStatusTips();
  259. },
  260. /** 接收好友朋友圈动态提示 */
  261. circleTips(data) {
  262. _action.playVoice('/static/voice/circle.mp3');
  263. _data.data('no_reader_circle', 1);
  264. _action.setStatusTips();
  265. },
  266. /** 接收朋友圈好友回复/赞通知 */
  267. cricleChatTips(data) {
  268. _action.playVoice('/static/voice/circle.mp3');
  269. let num = _data.data('no_reader_circle_chat_num');
  270. num++;
  271. _data.data('no_reader_circle_chat_num', num);
  272. _action.setStatusTips();
  273. },
  274. /** 撤回消息 */
  275. deleteChat(data) {
  276. let chat_data = _data.localData(data.list_id);
  277. for (let i = 0, j = chat_data.list.length; i < j; i++) {
  278. if (chat_data.list[i].msg.id == data.data.msg.id) {
  279. chat_data.list[i] = data.data;
  280. _data.localData(data.list_id, chat_data);
  281. uni.$emit('data_chat_data_delete', chat_data.list);
  282. break;
  283. }
  284. }
  285. },
  286. // 系统删除一条会话
  287. adminDeleteChat(data) {
  288. let chat_data = _data.localData(data.list_id);
  289. for (let i = 0, j = chat_data.list.length; i < j; i++) {
  290. if (chat_data.list[i].msg.id == data.data.msg.id) {
  291. // chat_data.list[i] = data.data;
  292. chat_data.list.splice(i, 1);
  293. _data.localData(data.list_id, chat_data);
  294. uni.$emit('data_chat_data_delete', chat_data.list);
  295. break;
  296. }
  297. }
  298. },
  299. /** 加群申请 */
  300. chatGroupApply(data) {
  301. let local_data = _data.localData('group_apply_list');
  302. if (!local_data) {
  303. local_data = [];
  304. }
  305. local_data.push(data);
  306. _data.localData('group_apply_list', local_data);
  307. uni.$emit('data_group_apply_data', local_data);
  308. let num = _data.data('new_group_tips_num');
  309. num++;
  310. _data.data('new_group_tips_num', num);
  311. _action.playVoice('/static/voice/friend.mp3');
  312. _action.setStatusTips();
  313. },
  314. /** 通知群管理已处理 */
  315. groupChatApplyAllow(id) {
  316. let local_data = _data.localData('group_apply_list');
  317. for (let value of local_data) {
  318. if (value.id == id) {
  319. value.status = 1;
  320. value.text = '已接受';
  321. let num = _data.data('new_group_tips_num');
  322. num--;
  323. if (num < 0) {
  324. num = 0;
  325. }
  326. _data.data('new_group_tips_num', num);
  327. }
  328. break;
  329. }
  330. _data.localData('group_apply_list', local_data);
  331. uni.$emit('data_group_apply_data', local_data);
  332. _action.setStatusTips();
  333. },
  334. // 清空双方聊天记录(对话)
  335. chat_del(data) {
  336. _data.localData(data.list_id, null);
  337. uni.$emit('chat_del', data.list_id);
  338. },
  339. // 删除群聊
  340. group_del(data) {
  341. /** 删除对话列表缓存数据 */
  342. let show_name = '';
  343. for (let i = 0, local_chat_list = _data.localData('chat_list'), j = local_chat_list.length; i < j; i++) {
  344. if (local_chat_list[i].list_id == data.list_id) {
  345. let del_group = local_chat_list.splice(i, 1);
  346. // console.log("删除群的属性", del_group);
  347. show_name = del_group[0].show_name;
  348. _data.localData('chat_list', local_chat_list);
  349. uni.$emit('data_chat_list', local_chat_list);
  350. break;
  351. }
  352. }
  353. /** 删除对话缓存数据 */
  354. _data.localData(data.list_id, null);
  355. uni.showModal({
  356. title: show_name + ' 群聊已经被解散了!',
  357. showCancel: false,
  358. success: function(res) {
  359. uni.switchTab({
  360. url: '/pages/chat/index'
  361. })
  362. }
  363. });
  364. },
  365. //清除群聊某些消息
  366. groupDeleteChat(data) {
  367. console.log("清空群聊消息", data);
  368. uni.$emit('group_delete_chat', data.list_id);
  369. },
  370. // todo清除移除会员的记录
  371. removeMember(data) {
  372. console.log("清空成员消息", data);
  373. uni.$emit('remove_member', data.list_id);
  374. },
  375. /** 解散群 */
  376. removeGroup(data) {
  377. /** 删除对话列表缓存数据 */
  378. for (let i = 0, local_chat_list = _data.localData('chat_list'), j = local_chat_list.length; i < j; i++) {
  379. if (local_chat_list[i].list_id == data.list_id) {
  380. local_chat_list.splice(i, 1);
  381. _data.localData('chat_list', local_chat_list);
  382. uni.$emit('data_chat_list', local_chat_list);
  383. break;
  384. }
  385. }
  386. /** 删除对话缓存数据 */
  387. _data.localData(data.list_id, null);
  388. uni.showModal({
  389. title: data.group_name + ' 群聊已经被群主解散了!',
  390. });
  391. },
  392. //付款消息
  393. payAmount(data) {
  394. _action.playVoice('/static/voice/chat.mp3');
  395. let local_data = _data.data('data_pay_tips');
  396. local_data = parseInt(local_data);
  397. if (local_data <= 0) {
  398. local_data = 0
  399. }
  400. local_data++;
  401. _data.data('data_pay_tips', local_data);
  402. uni.$emit('pay_amount_after', data)
  403. uni.$emit('data_user_info', data);
  404. _data.data('user_info', data);
  405. _action.setStatusTips();
  406. },
  407. collectAmount(data) {
  408. _action.playVoice('/static/voice/chat.mp3');
  409. let user_info = data.user_info;
  410. user_info.amount = data.amount;
  411. uni.navigateTo({
  412. url: '/pages/pay/collect_money?' + _page.pageParam(user_info)
  413. })
  414. },
  415. //在线支付成功通知
  416. onlinePaySuccess(data) {
  417. uni.$emit('data_user_info', data);
  418. _data.data('user_info', data);
  419. },
  420. cricleComment(data) {
  421. let comment = data.comment;
  422. let circle = _data.localData('circle_data');
  423. for (let i = 0, j = circle.length; i < j; i++) {
  424. if (circle[i].post_id == data.circle_id) {
  425. circle[i].comments.push(data.comment)
  426. _data.localData('circle_data', circle);
  427. uni.$emit('data_circle_data', circle);
  428. break;
  429. }
  430. }
  431. let circle_user = _data.localData('circle_data_user');
  432. for (let i = 0, j = circle_user.length; i < j; i++) {
  433. console.log('circle_user[i]:', circle_user[i])
  434. if (circle_user[i].post_id == data.circle_id) {
  435. circle_user[i].comments.push(data.comment)
  436. _data.localData('circle_data_user', circle_user);
  437. uni.$emit('data_circle_data_user', circle_user);
  438. break;
  439. }
  440. }
  441. },
  442. //购买成功通知
  443. payCircleOrder(data) {
  444. console.log('payCircleOrder....')
  445. _action.playVoice('/static/voice/circle.mp3');
  446. //通知朋友圈
  447. let circle_data = _data.localData('circle_data');
  448. for (let i = 0, j = circle_data.length; i < j; i++) {
  449. if (circle_data[i].post_id == data.circle_id) {
  450. console.log('circle_data11[i]', circle_data[i])
  451. circle_data[i].can_pay_times = data.can_pay_times;
  452. _data.localData('circle_data', circle_data);
  453. uni.$emit('data_circle_data', circle_data);
  454. break;
  455. }
  456. }
  457. let circle_data_user = _data.localData('circle_data_user');
  458. for (let i = 0, j = circle_data_user.length; i < j; i++) {
  459. if (circle_data_user[i].post_id == data.circle_id) {
  460. circle_data_user[i].can_pay_times = data.can_pay_times;
  461. _data.localData('circle_data_user', circle_data_user);
  462. uni.$emit('data_circle_data_user', circle_data_user);
  463. break;
  464. }
  465. }
  466. },
  467. memberIsOnline() { //消息提醒
  468. _action.playVoice('/static/voice/chat.mp3');
  469. },
  470. setMessagePageTitle(data) { //设置聊天页面的title
  471. console.log("设置标题:", data)
  472. let chat_data = _data.localData(data.list_id);
  473. chat_data.show_name = data.show_name;
  474. _data.localData(data.list_id, chat_data);
  475. },
  476. zhenDong(data) {
  477. let user_id = _data.data('user_info').id;
  478. console.log("USER_ID", user_id);
  479. if (user_id != data['user_id']) {
  480. // #ifdef APP-PLUS
  481. uni.vibrateLong();
  482. // #endif
  483. }
  484. }
  485. }