From e3ab58e507a71757b1c7222e08f119f5aab77ae6 Mon Sep 17 00:00:00 2001 From: VK <370725567@qq.com> Date: Thu, 21 Nov 2024 17:20:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7uni-map-common=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uni_modules/uni-map-common/changelog.md | 2 + uni_modules/uni-map-common/package.json | 4 +- .../common/uni-map-common/package.json | 4 +- .../cloudfunctions/uni-map-co/index.obj.js | 58 ++++++++++++++++--- .../cloudfunctions/uni-map-co/package.json | 17 +++--- 5 files changed, 65 insertions(+), 20 deletions(-) diff --git a/uni_modules/uni-map-common/changelog.md b/uni_modules/uni-map-common/changelog.md index 21749635..fce0a0fb 100644 --- a/uni_modules/uni-map-common/changelog.md +++ b/uni_modules/uni-map-common/changelog.md @@ -1,3 +1,5 @@ +## 1.1.4(2024-11-21) +新增 参数payload的接收和验证示例 ## 1.1.3(2024-11-11) 优化 chooseLocation 函数供 uni-app x 的 uni.chooseLocation 使用 ## 1.1.2(2024-10-21) diff --git a/uni_modules/uni-map-common/package.json b/uni_modules/uni-map-common/package.json index daf00bb1..4648c428 100644 --- a/uni_modules/uni-map-common/package.json +++ b/uni_modules/uni-map-common/package.json @@ -1,7 +1,7 @@ { "id": "uni-map-common", "displayName": "uni-map-common地图服务端API", - "version": "1.1.3", + "version": "1.1.4", "description": "聚合了多家地图供应商的服务端API,几行代码即可快速接入地图API", "keywords": [ "uni-map-common", @@ -32,7 +32,7 @@ "npmurl": "" }, "uni_modules": { - "dependencies": [], + "dependencies": ["uni-config-center"], "encrypt": [], "platforms": { "cloud": { diff --git a/uni_modules/uni-map-common/uniCloud/cloudfunctions/common/uni-map-common/package.json b/uni_modules/uni-map-common/uniCloud/cloudfunctions/common/uni-map-common/package.json index dfbb59e8..75cd3d92 100644 --- a/uni_modules/uni-map-common/uniCloud/cloudfunctions/common/uni-map-common/package.json +++ b/uni_modules/uni-map-common/uniCloud/cloudfunctions/common/uni-map-common/package.json @@ -13,7 +13,7 @@ "uni-config-center": "file:..\/..\/..\/..\/..\/uni-config-center\/uniCloud\/cloudfunctions\/common\/uni-config-center" }, "origin-plugin-dev-name": "uni-map-common", - "origin-plugin-version": "1.1.3", + "origin-plugin-version": "1.1.4", "plugin-dev-name": "uni-map-common", - "plugin-version": "1.1.3" + "plugin-version": "1.1.4" } \ No newline at end of file diff --git a/uni_modules/uni-map-common/uniCloud/cloudfunctions/uni-map-co/index.obj.js b/uni_modules/uni-map-common/uniCloud/cloudfunctions/uni-map-co/index.obj.js index 0211e7c1..b41bfa66 100644 --- a/uni_modules/uni-map-common/uniCloud/cloudfunctions/uni-map-co/index.obj.js +++ b/uni_modules/uni-map-common/uniCloud/cloudfunctions/uni-map-co/index.obj.js @@ -21,6 +21,16 @@ const $ = _.aggregate; const opendbPoiDB = db.collection("opendb-poi"); +class MyError extends Error { + constructor(errMsg, errCode = -1) { + super(errMsg); + this.err = { + errCode, + errMsg + } + } +} + module.exports = { _before: function() { // 如果配置中心不存在地图配置,则使用本地地图配置 @@ -33,7 +43,6 @@ module.exports = { provider = defaultProvider, needOriginalResult = false } = params[0] || {}; - console.log('provider: ', provider) const key = UniMapConfig.key[provider] || LocalMapConfig.key[provider]; if (!key) { throw { errCode: -1, errMsg: `请在uni-config-center/uni-map/config.js中或LocalMapConfig中配置地图供应商${provider}对应的key` }; @@ -45,10 +54,39 @@ module.exports = { needOriginalResult }); this.uniMap = uniMap; + // // 在这里可以做一些统一的前置处理,比如权限校验、参数校验等 + // let { + // payload, // payload参数为前端传递的参数,可以在前端调用uni.chooseLocation时传递 + // } = this.getParams()[0] || {}; + // if (!payload) { + // throw new MyError("payload参数不能为空", -1); + // } + // // 如果业务在uniCloud上,则直接在这里写判断逻辑即可 + // if (true) { + // throw new MyError("权限不足", -1); + // } + + // // 如果业务不在uniCloud上,可通过 uniCloud.request 调用自己的服务进行校验 + // const requestRes = await uniCloud.request({ + // method: 'POST', + // url: '你自己的接口地址', + // data: payload, + // }); + // // 约定errCode不为0代表校验失败,errMsg为失败原因 + // if (requestRes.data.errCode !== 0) { + // throw new MyError(requestRes.data.errMsg, requestRes.data.errCode); + // } + }, - _after: function(error, res) { - if (error) { - throw error; // 如果方法抛出错误,也直接抛出不处理 + _after: function(err, res) { + if (err) { + if (err.err) { + return err.err; + } + if (err.errCode) { + return err; + } + throw err; // 如果方法抛出错误,也直接抛出不处理 } console.log("result", res.result); return res; @@ -58,15 +96,17 @@ module.exports = { let res = {}; let { action, - data, + data, needOriginalResult } = parame; - // 初始化实例 // 获取uniMap实例 const uniMap = this.uniMap; // 调用API let result = await uniMap[action](data); res.result = needOriginalResult ? result.originalResult : result; + // 模拟错误 + // res.errCode = 121; + // res.errMsg = '此key每日调用量已达到上限' return res; }, // 经纬度坐标转地址 @@ -448,7 +488,7 @@ module.exports = { /** * 生成在指定经纬度圆内的随机坐标 - + const latitude = 39.908823; // 指定纬度 const longitude = 116.39747; // 指定经度 const radiusInKm = 10; // 指定圆的半径(单位:千米) @@ -484,7 +524,7 @@ function getRandomCoordinateWithinRadius(longitude, latitude, radiusInKm) { /** * 计算坐标B在坐标A的方向,0代表正西方 90 代表正北方 - + const latitude = 39.908823; // 指定纬度 const longitude = 116.39747; // 指定经度 const radiusInKm = 10; // 指定圆的半径(单位:千米) @@ -515,4 +555,4 @@ function calculateDirectionAngle(coordA, coordB) { angleDegrees = (angleDegrees > 180) ? angleDegrees - 180 : angleDegrees + 180; angleDegrees -= 90; // 以正西方为0°表示,因此需要-90 return angleDegrees; -} \ No newline at end of file +} diff --git a/uni_modules/uni-map-common/uniCloud/cloudfunctions/uni-map-co/package.json b/uni_modules/uni-map-common/uniCloud/cloudfunctions/uni-map-co/package.json index 5d4ecb92..1d234415 100644 --- a/uni_modules/uni-map-common/uniCloud/cloudfunctions/uni-map-co/package.json +++ b/uni_modules/uni-map-common/uniCloud/cloudfunctions/uni-map-co/package.json @@ -1,9 +1,12 @@ { - "name": "uni-map-co", - "dependencies": { - "uni-config-center": "file:../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center", - "uni-map-common": "file:../common/uni-map-common", - "uni-id-common": "file:../../../../uni-id-common/uniCloud/cloudfunctions/common/uni-id-common" - }, - "extensions": {} + "name": "uni-map-co", + "dependencies": { + "uni-config-center": "file:..\/..\/..\/..\/uni-config-center\/uniCloud\/cloudfunctions\/common\/uni-config-center", + "uni-map-common": "file:..\/common\/uni-map-common" + }, + "extensions": {}, + "origin-plugin-dev-name": "uni-map-common", + "origin-plugin-version": "1.1.4", + "plugin-dev-name": "uni-map-common", + "plugin-version": "1.1.4" } \ No newline at end of file -- GitLab