diff --git a/changelog.md b/changelog.md index c41e9353b21c1560a0d40fb1db807652c8acb041..c62f241de7efa27e66fda304b5bbd4a83d6833e0 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,5 @@ +## 3.4.40(2024-11-18) +uni-im-co 新增 httpApi 方法,方便第三方服务器调用其 url 化后的任意方法 ## 3.4.39(2024-11-15) - 新增 客户端错误日志收集功能 - 修复 由3.4.37更新引起的解散群功能出错的问题 diff --git a/package.json b/package.json index 1e6e48ea60da93fd57a3c0e185a38e9ec72a8e55..44eb1cbbaed9e4fa3267ad190fd7a9a0a2c69f73 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "id": "uni-im", "displayName": "uni-im", - "version": "3.4.39", + "version": "3.4.40", "description": "uni-im是云端一体的、全平台的、免费的、开源即时通讯系统", "keywords": [ "im,即时通讯,客服,聊天" @@ -33,6 +33,7 @@ "uni_modules": { "dependencies": [ "uni-id-pages", + "uni-cloud-s2s", "uni-search-bar", "uni-segmented-control", "Sansnn-uQRCode", diff --git a/uniCloud/cloudfunctions/uni-im-co/httpApi.js b/uniCloud/cloudfunctions/uni-im-co/httpApi.js new file mode 100644 index 0000000000000000000000000000000000000000..56d893aa0c2cc6ac281522271c4ee8daf29925ce --- /dev/null +++ b/uniCloud/cloudfunctions/uni-im-co/httpApi.js @@ -0,0 +1,54 @@ +const {verifyHttpInfo} = require('uni-cloud-s2s') +module.exports = async function httpApi(modules){ + // console.log('this.getClientInfo()',this.getClientInfo()) + // 校验是否有权限调用 + checkPermission.call(this) + // 获取 HTTP 请求参数 + const [param,options] = getHttpParams.call(this) + // 获取模拟操作的用户的信息 + const {userInfo,clientInfo} = options + this.current_uid = userInfo._id + // 角色信息 + this.current_user_role = userInfo.role || [] + // 设置客户端信息,例如设置客户端appid + if (clientInfo){ + this.clientInfo = Object.assign(this.getClientInfo(),clientInfo) + } + + const funcName = Object.keys(param)[0] + const funcParam = Object.values(param)[0] + // 执行要调用的方法 + // console.log('funcName:',funcName) + // console.log('funcParam:',funcParam) + return await modules[funcName].call(this,funcParam) +} + +function checkPermission(){ + if(this.getClientInfo().source != 'http'){ + throw { + errCode: 'permission-denied', + errMsg: '仅限制 HTTP 调用' + } + } + // 校验 HTTP 请求是否合法 + verifyHttpInfo(this.getHttpInfo()) +} + +function getHttpParams(){ + // 获取 HTTP 请求相关信息 + const httpInfo = this.getHttpInfo(); + // 获取请求参数 + let params = httpInfo.body + // 尝试地将参数转换为 JSON 对象 + try { + params = JSON.parse(params) + } catch (_) {} + + if (!Array.isArray(params) || (params.length != 2) || !params.every(item => Object.prototype.toString.call(item) === '[object Object]')) { + throw { + errCode: 'params-error', + errMsg: '参数错误:必须是长度为2的数组,且每一项都是对象,如:[{"方法名":"参数"},{"userInfo":{"_id":"执行操作的用户id"},"clientInfo":{"appid":"模拟的客户端appid"}}]' + } + } + return params +} \ No newline at end of file diff --git a/uniCloud/cloudfunctions/uni-im-co/index.obj.js b/uniCloud/cloudfunctions/uni-im-co/index.obj.js index aa1d65c8c254a6e6f9f8c907258f74e318c5abf1..5daa60ce92cf09efbd058f96801c14e1baa6a3ef 100644 --- a/uniCloud/cloudfunctions/uni-im-co/index.obj.js +++ b/uniCloud/cloudfunctions/uni-im-co/index.obj.js @@ -1,3 +1,4 @@ +const httpApi = require('./httpApi.js') const conversation = require('./conversation.js') const msg = require('./msg.js') const push = require('./push.js') @@ -5,6 +6,15 @@ const friend = require('./friend.js') const group = require('./group.js') const filtered_conversation = require('./filtered-conversation.js') const tools = require('./tools.js') +const modules = Object.assign( + conversation, + msg, + push, + friend, + group, + filtered_conversation, + tools +) module.exports = { async _before() { @@ -23,9 +33,9 @@ module.exports = { this.uniIdCommon = uniIdCommon.createInstance({ clientInfo: this.clientInfo }) - + // 除非特定方法允许未登录用户调用,其它都需要验证用户的身份 - const allowedMethodsWithouLogin = [] + const allowedMethodsWithouLogin = ["httpApi"] if (!allowedMethodsWithouLogin.includes(this.getMethodName())) { if (this.getClientInfo().source == 'function') { // 云函数互调时,免校验 token 直接使用传来的用户 id @@ -66,7 +76,7 @@ module.exports = { this._promises.push(promise) } }, - + async _after(error, result) { // 请求完成时间 // console.error('请求完成时间', Date.now() - this.requestStartTime, 'ms') @@ -100,12 +110,8 @@ module.exports = { return result || { errCode: 0 } }, - - ...conversation, - ...msg, - ...push, - ...friend, - ...group, - ...filtered_conversation, - ...tools, + ...modules, + httpApi(){ + return httpApi.call(this,modules) + } } diff --git a/uniCloud/cloudfunctions/uni-im-co/package.json b/uniCloud/cloudfunctions/uni-im-co/package.json index c5f098ec8072263219828119655c87616cc1d8d6..2158df6723429ec426ca899ff18c141a8895c4ab 100644 --- a/uniCloud/cloudfunctions/uni-im-co/package.json +++ b/uniCloud/cloudfunctions/uni-im-co/package.json @@ -1,26 +1,27 @@ -{ - "name": "uni-im-co", - "dependencies": { - "uni-config-center": "file:../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center", - "uni-id-common": "file:../../../../uni-id-common/uniCloud/cloudfunctions/common/uni-id-common", - "uni-im-ext": "file:../common/uni-im-ext", - "uni-im-utils": "file:../common/uni-im-utils" - }, - "extensions": { - "uni-cloud-jql": {}, - "uni-cloud-push": {} - }, - "version": "1.0.0", - "main": "index.obj.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "keywords": [], - "author": "", - "license": "ISC", - "description": "", - "cloudfunction-config": { - "path": "/uni-im-co", - "runtime": "Nodejs12" - } +{ + "name": "uni-im-co", + "dependencies": { + "uni-cloud-s2s": "file:../../../../uni-cloud-s2s/uniCloud/cloudfunctions/common/uni-cloud-s2s", + "uni-config-center": "file:../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center", + "uni-id-common": "file:../../../../uni-id-common/uniCloud/cloudfunctions/common/uni-id-common", + "uni-im-ext": "file:../common/uni-im-ext", + "uni-im-utils": "file:../common/uni-im-utils" + }, + "extensions": { + "uni-cloud-jql": {}, + "uni-cloud-push": {} + }, + "version": "1.0.0", + "main": "index.obj.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "description": "", + "cloudfunction-config": { + "path": "/uni-im-co", + "runtime": "Nodejs12" + } } \ No newline at end of file