更新记录
1.0.6(2024-09-04)
支持多APP
1.0.5(2024-09-04)
无
1.0.1(2024-02-01)
无
查看更多平台兼容性
阿里云 | 腾讯云 | 支付宝云 |
---|---|---|
√ | √ | √ |
Vue2 | Vue3 |
---|---|
√ | × |
App | 快应用 | 微信小程序 | 支付宝小程序 | 百度小程序 | 字节小程序 | QQ小程序 |
---|---|---|---|---|---|---|
HBuilderX 3.99 app-vue | × | × | × | × | × | × |
钉钉小程序 | 快手小程序 | 飞书小程序 | 京东小程序 | 鸿蒙元服务 |
---|---|---|---|---|
× | × | × | × | × |
H5-Safari | Android Browser | 微信浏览器(Android) | QQ浏览器(Android) | Chrome | IE | Edge | Firefox | PC-Safari |
---|---|---|---|---|---|---|---|---|
× | × | × | × | × | × | × | × | × |
云函数类插件通用教程
使用云函数类插件的前提是:使用HBuilderX 2.9+
准备
uniCloud
导入插件后需创建/关联 uniCloud
uniCloud 的使用方法 可详读uniCloud文档
manifest.json 配置 开通uni-push2.0
*webp配置->uniPush2.0
DCLOUD 开发者中心 https://dev.dcloud.net.cn/pages/app/push2/info
添加 uni-id 配置
uni-id的云端配置文件在 uniCloud/cloudfunctions/common/uni-config-center/uni-id/config.json中
配置项:
passwordSecret为用于加密密码入库的密钥
tokenSecret为生成token需要的密钥
tokenExpiresIntoken有效期,以秒为单位
passwordErrorLimit密码错误重试次数,分ip记录密码错误次数,达到重试次数之后等待passwordErrorRetryTime时间之后才可以重试
passwordErrorRetryTime单位为秒
如果使用sendSmsCode接口发送短信需要前往uniCloud控制台开通并充值,配置config.json的service字段,字段说明见下方示例
另外可以按照客户端平台进行不同的配置,参考下面示例
{
"passwordSecret": [{
"type": "hmac-sha256",
"version": 1
}],
"passwordStrength": "medium",
"tokenSecret": "tokenSecret-demo",
"requestAuthSecret": "",
"tokenExpiresIn": 7200,
"tokenExpiresThreshold": 3600,
"maxTokenLength": 10,
"passwordErrorLimit": 6,
"passwordErrorRetryTime": 3600,
"autoSetInviteCode": false,
"forceInviteCode": false,
"idCardCertifyLimit": 1,
"realNameCertifyLimit": 5,
"sensitiveInfoEncryptSecret": "",
"frvNeedAlivePhoto": false,
"userRegisterDefaultRole": [],
"app": {
"tokenExpiresIn": 2592000,
"tokenExpiresThreshold": 864000,
"oauth": {
"weixin": {
"appid": "",
"appsecret": ""
},
"qq": {
"appid": "",
"appsecret": ""
},
"apple": {
"bundleId": ""
}
}
},
"web": {
"tokenExpiresIn": 7200,
"tokenExpiresThreshold": 3600,
"oauth": {
"weixin-h5": {
"appid": "",
"appsecret": ""
},
"weixin-web": {
"appid": "",
"appsecret": ""
}
}
},
"mp-weixin": {
"tokenExpiresIn": 259200,
"tokenExpiresThreshold": 86400,
"oauth": {
"weixin": {
"appid": "",
"appsecret": ""
}
}
},
"mp-qq": {
"tokenExpiresIn": 259200,
"tokenExpiresThreshold": 86400,
"oauth": {
"qq": {
"appid": "",
"appsecret": ""
}
}
},
"mp-alipay": {
"tokenExpiresIn": 259200,
"tokenExpiresThreshold": 86400,
"oauth": {
"alipay": {
"appid": "",
"privateKey": "",
"keyType": "PKCS8"
}
}
},
"service": {
"sms": {
"name": "",
"codeExpiresIn": 180,
"scene": {
"bind-mobile-by-sms": {
"templateId": "",
"codeExpiresIn": 240
}
}
},
"univerify": {
"appid": ""
}
}
}
YQB-IM 配置
uni_modules -> yqb-im-f -> uniCloud -> cloudfunctions -> yqb-im -> config.js
module.exports = {
debug: true, // 调试模式时,将返回 stack 错误堆栈
baseDir: __dirname, // 必选,应用根目录
middleware: [], // 自定义中间件
pushInfo: [
{
appid: '',// uni-app 应用标识
// 参商离线推送配置
channel: {
"HW": "NORMAL",
"XM": "",
"OP": "",
"VV": "IM"
},
// 个推密钥ID 可在 uniCloud->数据库->opendb-tempdata 查看
pushTokenId: ""
}
]
};
客户端
在APP.vue 文件引入 yqbImUtils 和样式文件 且初始化 IM
<style lang="scss"> @import "@/uni_modules/yqb-im/static/css/app.scss"; /*每个页面公共css */ </style>yqbImUtils.init() 示例如下
<script> import yqbImUtils from '@/uni_modules/yqb-im/common/utils.js' export default { onLaunch: function () { yqbImUtils.init() console.log('App Launch') }, onShow: function () { console.log('App Show') }, onHide: function () { console.log('App Hide') } } </script>
<!-- 使用的页面引入 -->
```vue
import yqbIm from '@/uni_modules/yqb-im/lib/main.js'
IM 对象
{
state: {
conversation: {
dataList: [], 会话数据列表
hasMore: true 是否还有更多会话数据
},
currentConversationId: false, 正在对话的会话id
会话分组
groupingList: {
0: { _id: 0, name: '全部消息', num: 0 },
1: { _id: 1, name: '单聊', num: 0 },
2: { _id: 2, name: '群聊', num: 0 },
3: { _id: 3, name: '未读消息', num: 0 },
},
usersInfo: {}, 存储所有出现过的用户信息,包括群好友信息
groupInfo: {}, 存储所有出现过的群信息
isWidescreen: false, 是否为pc宽屏
systemInfo: {} 系统信息
},
methods: {
toChat, 前往会话页面
conversation: {
get, 前往会话页面
revokeMsg, 撤回消息
removeMsg, 删除消息
loadMore, 会话列表
hide, 隐藏会话
setTop, 会话置顶
userInfoChage, 用户数据更改
setRemind, 设置免打扰
clearChatLog, 清除聊天记录
performTasks, 执行离线任务
},
group: {
inviteMember, 邀请加入群聊
removeMember, 删除群成员
loadMember, 加载群成员信息
dissolveGroupChat, 解散群聊
createConversation, 创建群聊
editGroupInfo, 修改群信息
},
grouping: {
setGrouping, 设置会话分组
get, 获取分组
statistics, 统计每个分组下的会话数量
create, 添加分组
update, 更新/修改 分组信息
delete 删除分组
}
}
}
发送文件需自行引入文件选择器
小程序端 需勾选 manifest.json->微信小程序配置->uniPush 且在微信后台配置 socket 合法域名
wss://wshzn.gepush.com wss://wshzn.getui.net
推送结构
{
"user_id": [${user_id}],
"payload": {
type: "yqb-im",
data: msgData,
title, // "收到im消息,在线时显示的标题",
content, // "在线时显示的副标题",
},
title: title, // "收到im消息,离线时显示的标题",
content: title,//"离线时显示的内容"
}
payload.type 现有类型
yqb-im // 常规会话
yqb-im-group-exit // 用户退群
yqb-im-revoke-msg // 消息撤回
yqb-im-user-info-change // 用户数据变化
前往会话页面
yqbIm.toChat
请求参数
参数 | 类型 | 描述 |
---|---|---|
conversation_id |
String | 会话ID |
user_id |
String | 用户ID |
group_id |
String | 群组ID |
使用示例
yqbIm.toChat(conversation_id)
yqbIm.toChat({
conversation_id: ""
})
yqbIm.toChat({
user_id: ""
})
yqbIm.toChat({
group_id: ""
})
单聊
发起会话 / 获取会话对象
yqbIm.conversation.get
请求参数
参数 | 类型 | 描述 |
---|---|---|
*user_id |
String | 目标用户ID |
使用示例
yqbIm.conversation.get(conversation_id)
yqbIm.conversation.get({
conversation_id: ""
})
yqbIm.conversation.get({
user_id: ""
})
yqbIm.conversation.get({
group_id: ""
})
撤回消息
yqbIm.conversation.revokeMsg
请求参数
参数 | 类型 | 描述 |
---|---|---|
*_id |
String | 消息ID |
*conversation_id |
String | 会话ID |
user_id |
String | 操作用户ID |
create_time |
Date | 操作时间 |
使用示例
await yqbIm.conversation.revokeMsg({
_id: "",
conversation_id: "",
user_id: "",
create_time: ""
})
删除消息
yqbIm.conversation.removeMsg
请求参数
参数 | 类型 | 描述 |
---|---|---|
*_id |
String | 消息ID |
*conversation_id |
String | 会话ID |
user_id |
String | 操作用户ID |
create_time |
Date | 操作时间 |
使用示例
await yqbIm.conversation.removeMsg({
_id: "",
conversation_id: "",
user_id: "",
create_time: ""
})
获取会话列表
yqbIm.conversation.loadMore
请求参数
参数 | 类型 | 描述 |
---|
使用示例
yqbIm.conversation.loadMore()
群聊
邀请加入群聊
yqbIm.group.inviteMember
请求参数
参数 | 类型 | 描述 |
---|---|---|
*user_ids |
String | 受邀请成员 |
*group_id |
String | 群ID |
使用示例
await yqbIm.group.inviteMember({
user_ids: [],
group_id: ''
})
删除群成员
yqbIm.group.removeMember
请求参数
参数 | 类型 | 描述 |
---|---|---|
*user_id |
String | 成员ID |
*group_id |
String | 群ID |
使用示例
await yqbIm.group.removeMember({
user_id: [],
group_id: ''
})
加载群成员信息
yqbIm.group.loadMember
请求参数
参数 | 类型 | 描述 |
---|---|---|
*group_id |
String | 群ID |
type |
Int : 0 | 0:常规模式 , 1:加载头像 |
is_all_staff |
Boolean - false | 是否 加载组织信息 |
使用示例
await yqbIm.group.loadMember({
group_id: '',
type: 0 ,
})
解散群聊
yqbIm.group.dissolveGroupChat
请求参数
参数 | 类型 | 描述 |
---|---|---|
*group_id |
String | 群ID |
使用示例
await yqbIm.group.dissolveGroupChat({
group_id: '',
})
创建群聊
yqbIm.group.createConversation
请求参数
参数 | 类型 | 描述 |
---|---|---|
*userId |
String | 群主ID |
*memberId |
Array | 用户ID |
groupName |
String | 群名称 不传则以 成员昵称拼接 |
organizationId |
String | 群类型为 2 时 必传 |
groupGroupingId |
String | 分组ID |
groupType |
String | 1:普通群 2:全员群 默认普通群 |
使用示例
await yqbIm.group.createConversation({
userId:'',
memberId: [],
})
unicloud 项目结构
// 项目结构
// 控制器
├── package.json
├── index.js
├── config.js
├── controller // 控制器
| ├── im
| ├── conversation
| ├── group
| ├── msg
| ├── statistics // 消息统计
| ├── init // 云服务初始化类
| ├── msg // 消息对外接口
| ├── user // 用户数据同步
├── service // 逻辑层
| ├── msg // 消息收发处理接口
基础API
发送消息
/msg/sendMsg
请求参数
参数 | 类型 | 描述 |
---|---|---|
from_uid | String | app:小秘书 approval:0A审批助手 attendance:考勤助手 |
time:日历助手 work:工作通知 order:订单通知 | ||
conversation_id | String/Array | 会话ID |
to_uid | String/Array | 用户ID |
group_id | String/Array | 群组ID |
push | Boolean | true:创建推送通知、fasle:不创建推送通知 |
version | Int | 消息模板版本号 |
type | String | 消息类型 |
title | String | |
content | String | |
appId | String | 推送指向 App |
is_top | Boolean | 是否置顶消息 |
body | Obj | 消息内容 |
mapping | Obj | 用于查找对应系统用户 |
请求示例
{
* "appId": "__UNI__B2E3757", // 推送指向 APP_ID
"push": true,
// app:小秘书 approval:0A审批助手 attendance:考勤助手 calendar:日历助手 work:工作通知 order:订单通知 ...
// 可通过 系统用户创建接口创建 自定义系统用户
* "from_uid": "app",
"mapp ing" : {}, // 使用自定义系统用户时 mapping 必传
"version": "100", // 版本号
"type": "...", // 消息类型 - 【text,image,video,sound】固定有类型 模板消息填入模板类型
"to_uid": "64d2f5368b0da41af05698d4",
"title": "...",
"content": "...",
"body": {
* "template":"...", //模板类型 模板消息必填项 /
"version":"100", // 版本号
"extend": { // 模板消息 内置数据
...
}
}
创建用户
/msg/createSystemUser
请求参数
参数 | 类型 | 描述 |
---|---|---|
name |
String | 名称 |
avatarFile |
String | 用户头像 |
type |
String | 系统用户类型 |
mapping |
Object | 映射数据 |
请求示例
{
"name": "测试系统用户",
"avatarFile": ...,
"type": ...,
"mapping": {
...
}
}
项目结构
// 项目结构
// 控制器
├── package.json
├── index.js
├── config.js
├── controller // 控制器
| ├── im
| ├── conversation
| ├── group
| ├── msg
| ├── statistics // 消息统计
| ├── init // 云服务初始化类
| ├── msg // 消息对外接口
| ├── user // 用户数据同步
├── service // 逻辑层
| ├── msg // 消息收发处理接口
基础API
发送消息
/msg/sendMsg
请求参数
参数 | 类型 | 描述 |
---|---|---|
from_uid | String | app:小秘书 approval:0A审批助手 attendance:考勤助手 |
time:日历助手 work:工作通知 order:订单通知 | ||
conversation_id | String/Array | 会话ID |
to_uid | String/Array | 用户ID |
group_id | String/Array | 群组ID |
push | Boolean | true:创建推送通知、fasle:不创建推送通知 |
version | Int | 消息模板版本号 |
type | String | 消息类型 |
title | String | |
content | String | |
appId | String | 推送指向 App |
is_top | Boolean | 是否置顶消息 |
body | Obj | 消息内容 |
mapping | Obj | 用于查找对应系统用户 |
请求示例
{
* "appId": "__UNI__B2E3757", // 推送指向 APP_ID
"push": true,
// app:小秘书 approval:0A审批助手 attendance:考勤助手 calendar:日历助手 work:工作通知 order:订单通知 ...
// 可通过 系统用户创建接口创建 自定义系统用户
* "from_uid": "app",
"mapp ing" : {}, // 使用自定义系统用户时 mapping 必传
"version": "100", // 版本号
"type": "...", // 消息类型 - 【text,image,video,sound】固定有类型 模板消息填入模板类型
"to_uid": "64d2f5368b0da41af05698d4",
"title": "...",
"content": "...",
"body": {
* "template":"...", //模板类型 模板消息必填项 /
"version":"100", // 版本号
"extend": { // 模板消息 内置数据
...
}
}
创建用户
/msg/createSystemUser
请求参数
参数 | 类型 | 描述 |
---|---|---|
name |
String | 名称 |
avatarFile |
String | 用户头像 |
type |
String | 系统用户类型 |
mapping |
Object | 映射数据 |
请求示例
{
"name": "测试系统用户",
"avatarFile": ...,
"type": ...,
"mapping": {
...
}
}
uniCloud
项目结构
// 项目结构
// 控制器
├── package.json
├── index.js
├── config.js
├── controller // 控制器
| ├── im
| ├── conversation
| ├── group
| ├── msg
| ├── statistics // 消息统计
| ├── init // 云服务初始化类
| ├── msg // 消息对外接口
| ├── user // 用户数据同步
├── service // 逻辑层
| ├── msg // 消息收发处理接口
基础API
发送消息
/msg/sendMsg
请求参数
参数 | 类型 | 描述 |
---|---|---|
from_uid | String | app:小秘书 approval:0A审批助手 attendance:考勤助手 |
time:日历助手 work:工作通知 order:订单通知 | ||
conversation_id | String/Array | 会话ID |
to_uid | String/Array | 用户ID |
group_id | String/Array | 群组ID |
push | Boolean | true:创建推送通知、fasle:不创建推送通知 |
version | Int | 消息模板版本号 |
type | String | 消息类型 |
title | String | |
content | String | |
appId | String | 推送指向 App |
is_top | Boolean | 是否置顶消息 |
body | Obj | 消息内容 |
mapping | Obj | 用于查找对应系统用户 |
请求示例
{
* "appId": "__UNI__B2E3757", // 推送指向 APP_ID
"push": true,
// app:小秘书 approval:0A审批助手 attendance:考勤助手 calendar:日历助手 work:工作通知 order:订单通知 ...
// 可通过 系统用户创建接口创建 自定义系统用户
* "from_uid": "app",
"mapp ing" : {}, // 使用自定义系统用户时 mapping 必传
"version": "100", // 版本号
"type": "...", // 消息类型 - 【text,image,video,sound】固定有类型 模板消息填入模板类型
"to_uid": "64d2f5368b0da41af05698d4",
"title": "...",
"content": "...",
"body": {
* "template":"...", //模板类型 模板消息必填项 /
"version":"100", // 版本号
"extend": { // 模板消息 内置数据
...
}
}
创建用户
/msg/createSystemUser
请求参数
参数 | 类型 | 描述 |
---|---|---|
name |
String | 名称 |
avatarFile |
String | 用户头像 |
type |
String | 系统用户类型 |
mapping |
Object | 映射数据 |
请求示例
{
"name": "测试系统用户",
"avatarFile": ...,
"type": ...,
"mapping": {
...
}
}