- 添加 expiresAt 和 refreshUserSig 参数支持自动续签 - 修改 PushSDK 初始化方式,自动完成设备注册和厂商初始化 - 调整过期续签策略,从提前 15 分钟改为提前 5 分钟触发 - 重构 RN SDK 文档结构,简化安装和使用方式 - 更新统一登录流程,支持 profile 信息传递 - 添加 IM 数据库自动隔离功能 - 修复 Android 群消息聚合问题 - 补充自动化测试验证和错误处理机制
122 行
2.9 KiB
JavaScript
122 行
2.9 KiB
JavaScript
const app = getApp()
|
|
|
|
Page({
|
|
data: {
|
|
userId: '',
|
|
connected: false,
|
|
activeTab: 'conv',
|
|
conversations: [],
|
|
friends: [],
|
|
groups: [],
|
|
messages: [],
|
|
currentTarget: '',
|
|
currentChatType: 'SINGLE',
|
|
inputText: '',
|
|
},
|
|
|
|
onLoad() {
|
|
const sdk = app.globalData.sdk
|
|
const userId = app.globalData.userId
|
|
this.setData({ userId })
|
|
|
|
sdk.on('connected', () => this.setData({ connected: true }))
|
|
sdk.on('disconnected', () => this.setData({ connected: false }))
|
|
sdk.on('message', (msg) => this.handleMessage(msg))
|
|
sdk.on('revoke', ({ msgId }) => this.handleRevoke(msgId))
|
|
|
|
this.loadConversations()
|
|
this.loadFriends()
|
|
this.loadGroups()
|
|
},
|
|
|
|
onUnload() {
|
|
app.globalData.sdk?.disconnect()
|
|
},
|
|
|
|
handleMessage(msg) {
|
|
const messages = [...this.data.messages, msg]
|
|
this.setData({ messages })
|
|
},
|
|
|
|
handleRevoke(msgId) {
|
|
const messages = this.data.messages.map(m =>
|
|
m.id === msgId ? { ...m, revoked: true, content: '' } : m
|
|
)
|
|
this.setData({ messages })
|
|
},
|
|
|
|
async loadConversations() {
|
|
try {
|
|
const conversations = await app.globalData.sdk.listConversations()
|
|
this.setData({ conversations })
|
|
} catch (err) {
|
|
console.error('loadConversations failed', err)
|
|
}
|
|
},
|
|
|
|
async loadFriends() {
|
|
try {
|
|
const friends = await app.globalData.sdk.listFriends()
|
|
this.setData({ friends })
|
|
} catch (err) {
|
|
console.error('loadFriends failed', err)
|
|
}
|
|
},
|
|
|
|
switchTab(e) {
|
|
this.setData({ activeTab: e.currentTarget.dataset.tab })
|
|
},
|
|
|
|
async loadGroups() {
|
|
try {
|
|
const groups = await app.globalData.sdk.listGroups()
|
|
this.setData({ groups })
|
|
} catch (err) {
|
|
console.error('loadGroups failed', err)
|
|
}
|
|
},
|
|
|
|
startGroupChat(e) {
|
|
const id = e.currentTarget.dataset.id
|
|
this.setData({ currentTarget: id, currentChatType: 'GROUP', messages: [] })
|
|
this.loadHistory(id, 'GROUP')
|
|
},
|
|
|
|
selectConv(e) {
|
|
const { id, type } = e.currentTarget.dataset
|
|
this.setData({ currentTarget: id, currentChatType: type, messages: [] })
|
|
this.loadHistory(id, type)
|
|
},
|
|
|
|
startChat(e) {
|
|
const id = e.currentTarget.dataset.id
|
|
this.setData({ currentTarget: id, currentChatType: 'SINGLE', messages: [] })
|
|
this.loadHistory(id, 'SINGLE')
|
|
},
|
|
|
|
async loadHistory(targetId, chatType) {
|
|
try {
|
|
const result = await app.globalData.sdk.fetchHistory(targetId, { page: 0, size: 50 })
|
|
this.setData({ messages: result.content || [] })
|
|
} catch (err) {
|
|
console.error('loadHistory failed', err)
|
|
}
|
|
},
|
|
|
|
onInput(e) {
|
|
this.setData({ inputText: e.detail.value })
|
|
},
|
|
|
|
sendText() {
|
|
const { inputText, currentTarget, currentChatType } = this.data
|
|
if (!inputText.trim() || !currentTarget) return
|
|
app.globalData.sdk.send({
|
|
toId: currentTarget,
|
|
chatType: currentChatType,
|
|
msgType: 'TEXT',
|
|
content: inputText.trim(),
|
|
})
|
|
this.setData({ inputText: '' })
|
|
},
|
|
})
|