import TIM from 'tim-js-sdk'; import COS from 'cos-js-sdk-v5'; import request from '../utils/RequestUtils'; import User from '../stores/User'; import * as Notifications from 'expo-notifications'; const IM = (now) => ({ SDKAppID: 1400401634, userID: 0, userSig: null, messages: [], chatList: [], sysNoticeList: [], chatInfo: {}, tim: null, getSysNotice() { request.get('/email/my').then((res) => { now({ sysNoticeList: res }); }); }, getChat() { request.get('/chat/my').then((res) => { if (res) { now({ chatList: res }); } }); }, initChat() { const { getSysNotice, getChat, getUserSig } = now(); getUserSig(); getSysNotice(); getChat(); }, getUserSig() { const { id } = now(User); request .get('/chat/getUserSig', { params: { userId: id, }, }) .then((res) => { now({ userSig: res, userID: id, }); }); }, updateChatInfo(info) { now({ chatInfo: info }); }, sendMessage(content, receiveUserId) { const { id } = now(User); request .post('/chat/save', { data: { sendUserId: id, receiveUserId, content, }, }) .then((res) => { const { timSend } = now(); timSend(content, receiveUserId); }); }, setTim(tim) { now({ tim }); }, timSend(text, toUserId) { const { tim } = now(); if (tim) tim.injectJavaScript(`window.sendMessage(${text},${toUserId})`); }, async sendPushNotification(title, body) { await Notifications.scheduleNotificationAsync({ content: { title: title || '您有一条新的信息 📬', body: body || '请打开app查看', }, trigger: { seconds: 2 }, }); }, }); export default IM;