微搭低代碼使用的是文檔型數(shù)據(jù)庫(kù),有時(shí)候會(huì)有數(shù)據(jù)集成的需求,將低代碼的數(shù)據(jù)庫(kù)中的數(shù)據(jù)抽取到第三方系統(tǒng)中。本篇就介紹一下本地抽取數(shù)據(jù)以及云函數(shù)中抽取數(shù)據(jù)
1 本地抽取數(shù)據(jù)
本地抽取數(shù)據(jù)我們以nodejs作為后端服務(wù),本機(jī)要求先安裝好nodejs。
然后在電腦上創(chuàng)建一個(gè)文件夾,并且創(chuàng)建一個(gè)index.js,使用vscode打開(kāi)我們的工程目錄
index的代碼如下
const EnvId = ''; // 環(huán)境 ID,例如 lowcode-2gay8jgh25
const SecretId = '';
const SecretKey = '';
const Koa = require('koa');
const axios = require('axios');
const app = new Koa();
// 域名
const domain = `https://${EnvId}.ap-shanghai.tcb-api.tencentcloudapi.com`;
app.use(async (ctx) => {
// 換取 AccessToken
console.log(domain)
console.log(`Basic ${Buffer.from(`${SecretId}:${SecretKey}`).toString('base64')}`)
const params = {
grant_type: 'client_credentials'
};
const response=await axios.post(`${domain}/auth/v1/token/clientCredential`, params, {
headers: {
'Authorization': `Basic ${Buffer.from(`${SecretId}:${SecretKey}`).toString('base64')}`
}
})
console.log("response",response.data)
const { data } = response;
const { access_token } = data;
console.log(access_token)
// 請(qǐng)求某個(gè)服務(wù)端 API
const response2 = await axios.get(`${domain}/weda/odata/v1/prod/sys_user`, {
headers: {
"Authorization": `Bearer ${access_token}`
}
});
ctx.body = response2.data;
});
app.listen(3000);
這里涉及到參數(shù)初始化的問(wèn)題,首先獲取我們的資源ID,打開(kāi)控制臺(tái),找到資源ID
然后獲取我們的密鑰
然后安裝好axios和koa
npm install koa
npm install axios
安裝好之后我們?cè)诮K端里輸入
node index.js
啟動(dòng)服務(wù),在postman里訪問(wèn)我們的接口,接口地址http://localhost:3000
2 云函數(shù)中抽取數(shù)據(jù)
云函數(shù),可以使用騰訊云的SCF,我們以微搭的云函數(shù)作為示例
代碼如下:
'use strict';
const axios = require('axios');
const EnvId = ''; // 環(huán)境 ID,例如 lowcode-2gay8jgh25
const SecretId = '';
const SecretKey = '';
const domain = `https://${EnvId}.ap-shanghai.tcb-api.tencentcloudapi.com`;
exports.main = async (event, context) => {
const params = {
grant_type: 'client_credentials'
};
const response=await axios.post(`${domain}/auth/v1/token/clientCredential`, params, {
headers: {
'Authorization': `Basic ${Buffer.from(`${SecretId}:${SecretKey}`).toString('base64')}`
}
})
console.log("response",response.data)
const { data } = response;
const { access_token } = data;
console.log(access_token)
// 請(qǐng)求某個(gè)服務(wù)端 API
const response2 = await axios.get(`${domain}/weda/odata/v1/prod/sys_user`, {
headers: {
"Authorization": `Bearer ${access_token}`
}
});
console.log(response2.data)
return response2.data
};
我們?cè)谠粕祥_(kāi)發(fā),需要編寫(xiě)package.json,代碼如下
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {},
"author": "",
"license": "ISC",
"dependencies": {
"@cloudbase/node-sdk": "latest",
"axios":"latest"
}
}
點(diǎn)擊保存并安裝依賴,測(cè)試即可
總結(jié)
我們本篇介紹了集成微搭低代碼后端API的兩種方式,實(shí)際應(yīng)用中根據(jù)需求自行選擇即可。
聯(lián)系客服