Preserve H5 flows while adding Taobao identity, native capture, reports and isolated local development. Fix canceled province selection and expired-session logout. Record passing local regressions, two pre-existing H5 assertion failures and pending platform validation.
45 lines
2.1 KiB
JavaScript
45 lines
2.1 KiB
JavaScript
var config = require('../config');
|
|
var cloudReady;
|
|
|
|
function unwrap(result) {
|
|
if (typeof result === 'string') result = JSON.parse(result);
|
|
if (result && typeof result.success === 'boolean') {
|
|
if (!result.success) throw new Error(result.errorMessage || '服务暂不可用');
|
|
return typeof result.data === 'string' ? JSON.parse(result.data) : result.data;
|
|
}
|
|
return result;
|
|
}
|
|
function initCloud() {
|
|
if (!cloudReady) {
|
|
var sdk = require('@tbmp/mp-cloud-sdk'), cloud = sdk.default || sdk;
|
|
cloudReady = Promise.resolve(cloud.init({ env: config.cloudEnv })).then(function (ok) {
|
|
if (!ok) throw new Error('淘宝云服务初始化失败');
|
|
return cloud;
|
|
}).catch(function (err) { cloudReady = null; throw err; });
|
|
}
|
|
return cloudReady;
|
|
}
|
|
function request(path, body, token) {
|
|
var headers = { 'Content-Type': 'application/json;charset=UTF-8' };
|
|
if (token) headers.Authorization = 'Bearer ' + token;
|
|
if (config.transport === 'direct') {
|
|
if (!/^http:\/\/(localhost|127\.0\.0\.1)(:\d+)?$/.test(config.baseUrl)) return Promise.reject(new Error('直连接口仅用于本机联调'));
|
|
return new Promise(function (resolve, reject) {
|
|
my.request({ url: config.baseUrl + config.basePath + path, method: 'POST', headers: headers, data: JSON.stringify(body),
|
|
dataType: 'json', timeout: 10000,
|
|
success: function (res) {
|
|
try {
|
|
if ((res.status || res.statusCode) !== 200) throw new Error('服务暂不可用');
|
|
resolve(unwrap(res.data));
|
|
} catch (e) { reject(e); }
|
|
}, fail: function () { reject(new Error('网络异常,请稍后重试')); } });
|
|
});
|
|
}
|
|
if (!config.cloudAppId || !/^https:\/\//.test(config.baseUrl)) return Promise.reject(new Error('请先配置淘宝云应用和 HTTPS 服务地址'));
|
|
return initCloud().then(function (cloud) {
|
|
return cloud.application.httpRequest({ path: config.basePath + path, method: 'POST', headers: headers,
|
|
params: {}, body: body, exts: { cloudAppId: config.cloudAppId, domain: config.baseUrl, timeout: 10000 } });
|
|
}).then(unwrap);
|
|
}
|
|
module.exports = { request: request, unwrap: unwrap };
|