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

增加风驰台接口请求类型

parent ed15fb7d
......@@ -6,8 +6,9 @@ cloud.init({
});
App({
requestType: 'yapi', // cloud: 云函数, yapi: yapi 接口, ams: ams接口,
requestType: 'tornadoAPI', // cloud: 云函数, yapi: yapi 接口, ams: ams接口,tornadoAPI: 风驰台接口
cloudName: 'CommonToC', // 主云函数项目名
tornadoAPI: 'https://tornado.duibadev.com.cn/tbServer/api?db=db3000000027115325&proxyIp=172.16.75.54', // 新增:当requestType: 'tornadoAPI'时,找服务端提供地址
cloud,
tbcc,
onLaunch(options) {
......@@ -22,6 +23,6 @@ App({
*/
handleQuery(query) {
const { activityId } = query;
this.activityId = activityId || 'testId';
this.activityId = activityId || '5f8550a3964f97be3647a635';
}
});
......@@ -4,6 +4,7 @@
"main": "",
"license": "MIT",
"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 }) => {
console.log(2)
if (!cloud) {
console.error('请传入cloud');
return false;
......@@ -8,13 +12,14 @@ const request = ({ cloud, cloudName, requestType = 'cloud', mockUrl }) => {
return false;
}
return async (handle, method = 'POST', params, ext = {}) => {
const { activityId } = getApp();
const { activityId, tornadoAPI, nickName='', tbcc } = getApp();
// 默认注入activityId
params = { activityId, ...params };
if (!params.activityId) {
console.error(`${handle}:请传入activityId`);
return false;
}
console.log(params, handle)
const _cloudName = ext.cloudName || cloudName;
const { isShowLoading } = ext;
const hideMyLoading = () => {
......@@ -27,29 +32,94 @@ const request = ({ cloud, cloudName, requestType = 'cloud', mockUrl }) => {
}
const requestMock = () => {
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);
// 若请求的是风驰台的api
if (requestType === 'tornadoAPI') {
// 处理涉及的三个参数
let matchRes = tornadoAPI.match(/db=(\w*)&proxyIp=([\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3})/)
let db = matchRes[1]
let proxyIp = matchRes[2]
let data_ = {
handler: handle,
data: params
}
console.log(`data_`, data_)
let params_ = {
db,
proxyIp,
data: data_
}
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(() => {
hideMyLoading();
reject();
});
})
params_.openId = md5(nickName)
return new Promise((resolve, 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 = () => {
......@@ -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