house-middleware-sdk
Version:
58 hosue rn native sdk
431 lines (396 loc) • 17.1 kB
JavaScript
/*
* @method getNativeParams 获取httpHeader、跳转协议附带参数
* @param params "HTTP_HEAD|JUMP_PARAMS|ALL_INITIAL_DATA"
* @param callback 返回结果{...}
*/
import { Platform, NativeModules, Dimensions } from "react-native";
import { writeLog } from './actionLog';
import { getSafeBottom, getSafeTop, getSafeAreaInset } from '../utils/getSafeArea';
export function initNativeParams(callback, isRequest) {
let promises = [getNativeParamsPromise(isRequest)];
if (!isRequest) {
let safePromise = new Promise((resolve, reject) => {
getSafeAreaInset((safeInset) => {
if (safeInset) {
global.SAFE_TOP = safeInset.top;
global.SAFE_BOTTOM = safeInset.bottom;
}
resolve({});
})
})
promises.push(safePromise);
}
Promise.all(promises).then((response => {
if (response && response[0]) {
callback && callback(response[0]);
}
}))
}
function getNativeParamsPromise(isRequest) {
return new Promise((resolve, reject) => {
getNativeParams('ALL_INITIAL_DATA', (res) => {
global.jumpParams = {};
if (res.JUMP_PARAMS && res.JUMP_PARAMS.hasOwnProperty("content")) {
global.jumpParams = res.JUMP_PARAMS;
} else {
global.jumpParams['content'] = res.JUMP_PARAMS;
}
if (!global.jumpParams.content) {
global.jumpParams.content = {};
}
if (global.jumpParams.content.params) {
if (!global.jumpParams.content.params.full_path) {
global.jumpParams.content.params['full_path'] = '1';
}
} else {
global.jumpParams.content['params'] = { 'full_path': '1' };
}
global.full_path = global.jumpParams.content.params.full_path;
if (!global.jumpParams.content.source) {
global.jumpParams.content['source'] = '';
}
const initNativeParams = {};
initNativeParams.jumpParams = global.jumpParams;
initNativeParams.httpHeader = res.HTTP_HEAD;
initNativeParams.publicParams = res.PUBLIC_PARAMS;
resolve(initNativeParams);
}, isRequest);
})
}
// 初始化全局实例变量
function initBaseConstants() {
global.IS_ANDROID = (Platform.OS === "android");
global.WINDOW_WIDTH = Dimensions.get("window").width;
global.WINDOW_HEIGHT = Dimensions.get("window").height;
global.WINDOW_SCALE = Dimensions.get("window").scale;
global.SAFE_TOP = getSafeTop();
global.SAFE_BOTTOM = getSafeBottom();
}
//这个方法的返回值应该逐渐统一并固定 慎用!!!!!!!这个方法不会自动处理jumpParams中的数据结构(content)
function getNativeParams(params, callback, isRequest) {
if (!params) {
console.error('params不能为空');
return;
}
if (typeof params !== 'string') {
console.error('params应该类似 \'HTTP_HEAD|JUMP_PARAMS|ALL_INITIAL_DATA\'');
return;
}
params.split('|')
.map((item) => {
if (item !== 'HTTP_HEAD' && item !== 'JUMP_PARAMS' && item !== 'ALL_INITIAL_DATA') {
console.error('params应该以 \'|\' 进行分割 值应为 \'HTTP_HEAD|JUMP_PARAMS|ALL_INITIAL_DATA\' 中的一个或多个');
}
});
if (!isRequest) {
initBaseConstants();
}
require('../utils/findImpl')
.default(
() => getNativeParamsWbImpl(params, callback, isRequest),
() => getNativeParamsAjkImpl(params, callback, isRequest),
() => getNativeParamsWbImpl(params, callback, isRequest),
() => getNativeParamsWbtzImpl(params, callback, isRequest)
);
}
function getNativeParamsWbImpl(params, callback, isRequest) {
require('@w/wbapp')
.initialParams(
params.split('ALL_INITIAL_DATA').join('ALL'),
(res) => {
// res空埋点
let resParams = 'res self normal'
if (res == '') {
resParams = 'res str \'\''
} else if (res == 'null') {
resParams = 'res str \'null\''
} else if (res == '{}') {
resParams = 'res str \'{}\''
} else if (res == null) {
resParams = 'res self null'
} else if (res == undefined) {
resParams = 'res self undefined'
} else {
if (res && res instanceof Object) {
var keysArr = Object.keys(res);
if (keysArr.length == 0) {
resParams = 'res keysArr 0'
}
}
}
let result = {};
if (resParams == 'res self normal') {
// header空埋点
let headerParams = 'head self normal';
if (res.WB_HTTP_HEAD == '') {
headerParams = 'head str \'\''
}
else if (res.WB_HTTP_HEAD == 'null') {
headerParams = 'head str \'null\''
} else if (res.WB_HTTP_HEAD == '{}') {
headerParams = 'head str \'{}\''
} else if (res.WB_HTTP_HEAD == null) {
headerParams = 'head self null'
} else if (res.WB_HTTP_HEAD == undefined) {
headerParams = 'head self undefined'
} else {
if (res.WB_HTTP_HEAD) {
if (res.WB_HTTP_HEAD instanceof Object) {
var keysArr = Object.keys(res.WB_HTTP_HEAD);
if (keysArr.length == 0) {
headerParams = 'head keysArr 0'
}
} else {
headerParams = 'head not Object'
}
} else {
headerParams = 'head empty'
}
}
if (headerParams !== 'head self normal') {
// header空埋点
if (global.SCENE == 'roomer' && isRequest) {
writeLog(
'new_other',
{
wb: '200000003262000100000010',
ajk: '200000003262000100000010'
}, '1', {
type: headerParams,
error: JSON.stringify(res.WB_HTTP_HEAD)
}
);
}
result['HTTP_HEAD'] = {};
} else {
result['HTTP_HEAD'] = res.WB_HTTP_HEAD;
}
// 协议空埋点
let jumpParams = 'jump self normal';
if (res.WB_JUMPER == '') {
jumpParams = 'jump str \'\''
}
else if (res.WB_JUMPER == 'null') {
jumpParams = 'jump str \'null\''
} else if (res.WB_JUMPER == '{}') {
jumpParams = 'jump str \'{}\''
} else if (res.WB_JUMPER == null) {
jumpParams = 'jump self null'
} else if (res.WB_JUMPER == undefined) {
jumpParams = 'jump self undefined'
} else {
if (res.WB_JUMPER) {
if (res.WB_JUMPER instanceof Object) {
var keysArr = Object.keys(res.WB_JUMPER);
if (keysArr.length == 0) {
jumpParams = 'jump keysArr 0'
}
} else {
jumpParams = 'jump not Object'
}
}
else {
jumpParams = 'jump empty'
}
}
if (jumpParams !== 'jump self normal') {
// 协议空埋点
if (global.SCENE == 'roomer' && isRequest) {
writeLog(
'new_other',
{
wb: '200000003263000100000010',
ajk: '200000003263000100000010'
}, '1', {
error: jumpParams
}
);
}
result['JUMP_PARAMS'] = {};
} else {
result['JUMP_PARAMS'] = res.WB_JUMPER;
}
} else {
// res空埋点
if (global.SCENE == 'roomer' && isRequest) {
writeLog(
'new_other',
{
wb: '200000003262000100000010',
ajk: '200000003262000100000010'
}, '1', {
error: resParams
}
);
}
result['JUMP_PARAMS'] = {};
result['HTTP_HEAD'] = {};
}
// let result = {};
// result['JUMP_PARAMS'] = res.WB_JUMPER;
//
// // 协议空埋点
// writeLog(
// 'new_other',
// {
// wb: '200000003263000100000010',
// ajk: '200000003263000100000010'
// }, '1', {
// error: JSON.stringify(res.WB_JUMPER)
// }
// );
//
// result['HTTP_HEAD'] = res.WB_HTTP_HEAD;
// // header空埋点
// writeLog(
// 'new_other',
// {
// wb: '200000003262000100000010',
// ajk: '200000003262000100000010'
// }, '1', {
// error: JSON.stringify(res.WB_HTTP_HEAD)
// }
// );
callback(result);
});
}
function getNativeParamsAjkImpl(params, callback) {
require('ajkapp')
.getNativeParams(params, res => {
callback(res);
});
}
//TODO:{58同镇 获取Native初始化参数...}
function getNativeParamsWbtzImpl(params, callback, isRequest) {
require('@w/wbapp')
.initialParams(
params.split('ALL_INITIAL_DATA').join('ALL'),
(res) => {
// res空埋点
let resParams = 'res self normal'
if (res == '') {
resParams = 'res str \'\''
} else if (res == 'null') {
resParams = 'res str \'null\''
} else if (res == '{}') {
resParams = 'res str \'{}\''
} else if (res == null) {
resParams = 'res self null'
} else if (res == undefined) {
resParams = 'res self undefined'
} else {
if (res && res instanceof Object) {
var keysArr = Object.keys(res);
if (keysArr.length == 0) {
resParams = 'res keysArr 0'
}
}
}
let result = {};
if (resParams == 'res self normal') {
// header空埋点
let headerParams = 'head self normal';
if (res.WB_HTTP_HEAD == '') {
headerParams = 'head str \'\''
}
else if (res.WB_HTTP_HEAD == 'null') {
headerParams = 'head str \'null\''
} else if (res.WB_HTTP_HEAD == '{}') {
headerParams = 'head str \'{}\''
} else if (res.WB_HTTP_HEAD == null) {
headerParams = 'head self null'
} else if (res.WB_HTTP_HEAD == undefined) {
headerParams = 'head self undefined'
} else {
if (res.WB_HTTP_HEAD) {
if (res.WB_HTTP_HEAD instanceof Object) {
var keysArr = Object.keys(res.WB_HTTP_HEAD);
if (keysArr.length == 0) {
headerParams = 'head keysArr 0'
}
} else {
headerParams = 'head not Object'
}
} else {
headerParams = 'head empty'
}
}
if (headerParams !== 'head self normal') {
// header空埋点
if (global.SCENE == 'roomer' && isRequest) {
writeLog(
'new_other',
{
wbtz: '200000003262000100000010'
}, '1', {
type: headerParams,
error: JSON.stringify(res.WB_HTTP_HEAD)
}
);
}
result['HTTP_HEAD'] = {};
} else {
result['HTTP_HEAD'] = res.WB_HTTP_HEAD;
}
// 协议空埋点
let jumpParams = 'jump self normal';
if (res.WB_JUMPER == '') {
jumpParams = 'jump str \'\''
}
else if (res.WB_JUMPER == 'null') {
jumpParams = 'jump str \'null\''
} else if (res.WB_JUMPER == '{}') {
jumpParams = 'jump str \'{}\''
} else if (res.WB_JUMPER == null) {
jumpParams = 'jump self null'
} else if (res.WB_JUMPER == undefined) {
jumpParams = 'jump self undefined'
} else {
if (res.WB_JUMPER) {
if (res.WB_JUMPER instanceof Object) {
var keysArr = Object.keys(res.WB_JUMPER);
if (keysArr.length == 0) {
jumpParams = 'jump keysArr 0'
}
} else {
jumpParams = 'jump not Object'
}
}
else {
jumpParams = 'jump empty'
}
}
if (jumpParams !== 'jump self normal') {
// 协议空埋点
if (global.SCENE == 'roomer' && isRequest) {
writeLog(
'new_other',
{
wbtz: '200000003262000100000010'
}, '1', {
error: jumpParams
}
);
}
result['JUMP_PARAMS'] = {};
} else {
result['JUMP_PARAMS'] = res.WB_JUMPER;
}
} else {
// res空埋点
if (global.SCENE == 'roomer' && isRequest) {
writeLog(
'new_other',
{
wbtz: '200000003262000100000010'
}, '1', {
error: resParams
}
);
}
result['JUMP_PARAMS'] = {};
result['HTTP_HEAD'] = {};
}
callback(result);
});
}