Commit 67e19b13 authored by 汪忙忙's avatar 汪忙忙

增加风驰台接口请求类型

parent ed15fb7d
...@@ -6,8 +6,9 @@ cloud.init({ ...@@ -6,8 +6,9 @@ cloud.init({
}); });
App({ App({
requestType: 'yapi', // cloud: 云函数, yapi: yapi 接口, ams: ams接口, requestType: 'tornadoAPI', // cloud: 云函数, yapi: yapi 接口, ams: ams接口,tornadoAPI: 风驰台接口
cloudName: 'CommonToC', // 主云函数项目名 cloudName: 'CommonToC', // 主云函数项目名
tornadoAPI: 'https://tornado.duibadev.com.cn/tbServer/api?db=db3000000027115325&proxyIp=172.16.75.54', // 新增:当requestType: 'tornadoAPI'时,找服务端提供地址
cloud, cloud,
tbcc, tbcc,
onLaunch(options) { onLaunch(options) {
...@@ -22,6 +23,6 @@ App({ ...@@ -22,6 +23,6 @@ App({
*/ */
handleQuery(query) { handleQuery(query) {
const { activityId } = query; const { activityId } = query;
this.activityId = activityId || 'testId'; this.activityId = activityId || '5f8550a3964f97be3647a635';
} }
}); });
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
"main": "", "main": "",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@tbmp/mp-cloud-sdk": "*" "@tbmp/mp-cloud-sdk": "*",
"crypto": "^1.0.1"
} }
} }
var Buffer = require("buffer").Buffer
var crypto = require("crypto")
const request = ({ cloud, cloudName, requestType = 'cloud', mockUrl }) => { const request = ({ cloud, cloudName, requestType = 'cloud', mockUrl }) => {
console.log(2)
if (!cloud) { if (!cloud) {
console.error('请传入cloud'); console.error('请传入cloud');
return false; return false;
...@@ -8,13 +12,14 @@ const request = ({ cloud, cloudName, requestType = 'cloud', mockUrl }) => { ...@@ -8,13 +12,14 @@ const request = ({ cloud, cloudName, requestType = 'cloud', mockUrl }) => {
return false; return false;
} }
return async (handle, method = 'POST', params, ext = {}) => { return async (handle, method = 'POST', params, ext = {}) => {
const { activityId } = getApp(); const { activityId, tornadoAPI, nickName='', tbcc } = getApp();
// 默认注入activityId // 默认注入activityId
params = { activityId, ...params }; params = { activityId, ...params };
if (!params.activityId) { if (!params.activityId) {
console.error(`${handle}:请传入activityId`); console.error(`${handle}:请传入activityId`);
return false; return false;
} }
console.log(params, handle)
const _cloudName = ext.cloudName || cloudName; const _cloudName = ext.cloudName || cloudName;
const { isShowLoading } = ext; const { isShowLoading } = ext;
const hideMyLoading = () => { const hideMyLoading = () => {
...@@ -27,29 +32,94 @@ const request = ({ cloud, cloudName, requestType = 'cloud', mockUrl }) => { ...@@ -27,29 +32,94 @@ const request = ({ cloud, cloudName, requestType = 'cloud', mockUrl }) => {
} }
const requestMock = () => { const requestMock = () => {
const mockUrlPrefix = { // 若请求的是风驰台的api
ams: 'https://ams.dui88.com/server/index.php?g=Web&c=Mock&o=simple&projectID=218&uri=', if (requestType === 'tornadoAPI') {
yapi: 'https://docs.dui88.com/mock/140/' // 处理涉及的三个参数
}; let matchRes = tornadoAPI.match(/db=(\w*)&proxyIp=([\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3})/)
const requestPrefix = mockUrl || mockUrlPrefix[requestType]; let db = matchRes[1]
return new Promise((resolve, reject) => { let proxyIp = matchRes[2]
my.request({ let data_ = {
url: requestPrefix + _cloudName + '.' + handle, handler: handle,
method, data: params
data: params, }
dataType: 'json' console.log(`data_`, data_)
}).then(({ data: res }) => { let params_ = {
hideMyLoading(); db,
if (res && res.success) { proxyIp,
resolve(res); data: data_
} else { }
reject(res); console.log(`params_`, params_)
// nickName 转 md5 作为唯一的openId
if (!nickName) {
tbcc.tb.getAuthUserInfo().catch(err => {
console.log('未授权成功', err);
}).then(res => {
cb(res)
})
}
function cb({nickName}) {
if (!db || !proxyIp) {
my.confirm({
title: '风驰台地址错误',
content: '请确认风驰台的地址是否正确,未查找到db和proxyIp',
confirmButtonText: '确定',
cancelButtonText: '取消'
});
return false
} }
}).catch(() => { params_.openId = md5(nickName)
hideMyLoading(); return new Promise((resolve, reject) => {
reject(); my.request({
}); url: tornadoAPI,
}) method: 'post',
data: params_,
dataType: 'json'
}).then(({ data: res }) => {
console.log(`调用风驰台返回结果`, res)
hideMyLoading();
if (res && res.success) {
resolve(res);
} else {
reject(res);
}
}).catch((e) => {
hideMyLoading();
console.log(e)
reject();
});
})
function md5(data) {
var buf = new Buffer(data);
var str = buf.toString("binary");
return crypto.createHash("md5").update(str).digest("hex").slice(0, 30);
}
}
} else {
const mockUrlPrefix = {
ams: 'https://ams.dui88.com/server/index.php?g=Web&c=Mock&o=simple&projectID=218&uri=',
yapi: 'https://docs.dui88.com/mock/140/'
};
const requestPrefix = mockUrl || mockUrlPrefix[requestType];
return new Promise((resolve, reject) => {
my.request({
url: requestPrefix + _cloudName + '.' + handle,
method,
data: params,
dataType: 'json'
}).then(({ data: res }) => {
hideMyLoading();
if (res && res.success) {
resolve(res);
} else {
reject(res);
}
}).catch(() => {
hideMyLoading();
reject();
});
})
}
}; };
const requestCloud = () => { const requestCloud = () => {
...@@ -70,7 +140,7 @@ const request = ({ cloud, cloudName, requestType = 'cloud', mockUrl }) => { ...@@ -70,7 +140,7 @@ const request = ({ cloud, cloudName, requestType = 'cloud', mockUrl }) => {
}); });
}; };
return ['ams', 'yapi'].includes(requestType) ? requestMock() : requestCloud(); return ['ams', 'yapi', 'tornadoAPI'].includes(requestType) ? requestMock() : requestCloud();
}; };
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment