model.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import TIM from 'tim-js-sdk';
  2. import COS from 'cos-js-sdk-v5';
  3. import request from '../utils/RequestUtils';
  4. import User from '../stores/User';
  5. import * as Notifications from 'expo-notifications';
  6. const IM = (now) => ({
  7. SDKAppID: 1400401634,
  8. userID: 0,
  9. userSig: null,
  10. messages: [],
  11. chatList: [],
  12. sysNoticeList: [],
  13. chatInfo: {},
  14. tim: null,
  15. getSysNotice() {
  16. request.get('/email/my').then((res) => {
  17. now({ sysNoticeList: res });
  18. });
  19. },
  20. getChat() {
  21. request.get('/chat/my').then((res) => {
  22. if (res) {
  23. now({ chatList: res });
  24. }
  25. });
  26. },
  27. initChat() {
  28. const { getSysNotice, getChat, getUserSig } = now();
  29. getUserSig();
  30. getSysNotice();
  31. getChat();
  32. },
  33. getUserSig() {
  34. const { id } = now(User);
  35. request
  36. .get('/chat/getUserSig', {
  37. params: {
  38. userId: id,
  39. },
  40. })
  41. .then((res) => {
  42. now({
  43. userSig: res,
  44. userID: id,
  45. });
  46. });
  47. },
  48. updateChatInfo(info) {
  49. now({ chatInfo: info });
  50. },
  51. sendMessage(content, receiveUserId) {
  52. const { id } = now(User);
  53. request
  54. .post('/chat/save', {
  55. data: {
  56. sendUserId: id,
  57. receiveUserId,
  58. content,
  59. },
  60. })
  61. .then((res) => {
  62. const { timSend } = now();
  63. timSend(content, receiveUserId);
  64. });
  65. },
  66. setTim(tim) {
  67. now({ tim });
  68. },
  69. timSend(text, toUserId) {
  70. const { tim } = now();
  71. if (tim) tim.injectJavaScript(`window.sendMessage(${text},${toUserId})`);
  72. },
  73. async sendPushNotification(title, body) {
  74. await Notifications.scheduleNotificationAsync({
  75. content: {
  76. title: title || '您有一条新的信息 📬',
  77. body: body || '请打开app查看',
  78. },
  79. trigger: { seconds: 2 },
  80. });
  81. },
  82. });
  83. export default IM;