# 微信小程序 IM 接入

**模块**：`xuqm-group-wechat-mini-program-sdk`

---

## 登录

```ts
import { XuqmMiniProgramSDK } from 'xuqm-group-wechat-mini-program-sdk'

const sdk = new XuqmMiniProgramSDK()
sdk.init({ appKey: 'your_app_key' })

// 使用 UserSig 登录
await sdk.login('user_001', 'your_user_sig_jwt')
```

---

## 监听消息

```ts
sdk.on('connected', () => {
  console.log('IM 已连接')
})

sdk.on('message', (msg) => {
  console.log('收到消息:', msg.msgType, msg.content)
})

sdk.on('read', (msg) => {
  console.log('已读回执:', msg.id)
})

sdk.on('revoke', (data) => {
  console.log('消息被撤回:', data.msgId)
})

sdk.on('disconnected', (reason) => {
  console.log('断开连接:', reason)
})

sdk.on('error', (error) => {
  console.error('IM 错误:', error)
})
```

---

## 发送消息

```ts
const msg = await sdk.send({
  toId: 'user_002',
  chatType: 'SINGLE',
  msgType: 'TEXT',
  content: 'Hello!',
})
```

### 发送文本消息（快捷方法）

```ts
await sdk.sendTextMessage('user_002', 'SINGLE', 'Hello!')
```

---

## 历史消息

```ts
const history = await sdk.fetchHistory('user_002')
const groupHistory = await sdk.fetchGroupHistory('group_xxx')
```

---

## 会话列表

```ts
const conversations = await sdk.listConversations()
await sdk.markRead('user_002')
await sdk.setConversationPinned('user_002', 'SINGLE', true)
await sdk.setConversationMuted('user_002', 'SINGLE', true)
```

---

## 群聊

```ts
const groups = await sdk.listGroups()
const group = await sdk.getGroupInfo('group_xxx')
const members = await sdk.listGroupMembers('group_xxx')
```

---

## 好友管理

```ts
const friends = await sdk.listFriends()
await sdk.addFriend('user_002')
await sdk.removeFriend('user_002')
```

---

## 离线消息同步

```ts
const count = await sdk.offlineMessageCount()
const messages = await sdk.syncOfflineMessages()
```

---

## 消息类型

| MsgType | 说明 |
|---------|------|
| `TEXT` | 纯文本 |
| `IMAGE` | 图片 |
| `VIDEO` | 视频 |
| `AUDIO` | 语音 |
| `FILE` | 文件 |
| `LOCATION` | 位置 |
| `CUSTOM` | 自定义 |
| `NOTIFY` | 系统通知 |
| `RICH_TEXT` | 富文本 |
| `CALL_AUDIO` | 语音通话信令 |
| `CALL_VIDEO` | 视频通话信令 |
| `QUOTE` | 引用 |
| `MERGE` | 合并转发 |
| `FORWARD` | 转发 |
| `REVOKED` | 撤回 |
