model.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 '../flooks/User';
  5. const IM = (now) => ({
  6. SDKAppID: 1400375593,
  7. userID: 0,
  8. userSig: null,
  9. messages: [],
  10. chatList: [],
  11. sysNoticeList: [],
  12. chatInfo: {},
  13. tim: null,
  14. getSysNotice() {
  15. request.get('/email/my').then((res) => {
  16. now({ sysNoticeList: res });
  17. });
  18. },
  19. getChat() {
  20. request.get('/chat/my').then((res) => {
  21. if (res) {
  22. now({ chatList: res });
  23. }
  24. });
  25. },
  26. initChat() {
  27. const { getSysNotice, getChat, getUserSig } = now();
  28. getUserSig();
  29. getSysNotice();
  30. getChat();
  31. },
  32. getUserSig() {
  33. const { id } = now(User);
  34. request
  35. .get('/chat/getUserSig', {
  36. params: {
  37. userId: id,
  38. },
  39. })
  40. .then((res) => {
  41. now({
  42. userSig: res,
  43. userID: id,
  44. });
  45. });
  46. },
  47. updateChatInfo(info) {
  48. now({ chatInfo: info });
  49. },
  50. sendMessage(content, receiveUserId) {
  51. const { id } = now(User);
  52. request
  53. .post('/chat/save', {
  54. data: {
  55. sendUserId: id,
  56. receiveUserId,
  57. content,
  58. },
  59. })
  60. .then((res) => {
  61. const { timSend } = now();
  62. timSend(content, receiveUserId);
  63. });
  64. },
  65. setTim(tim) {
  66. now({ tim });
  67. },
  68. timSend(text, toUserId) {
  69. const { tim } = now();
  70. if (tim) tim.injectJavaScript(`window.sendMessage(${text},${toUserId})`);
  71. },
  72. });
  73. export default IM;