|
|
'use strict';
|
|
|
|
|
|
import ReactNative from 'react-native';
|
|
|
import ReactNative, {
|
|
|
Dimensions,
|
|
|
Alert,
|
|
|
} from 'react-native';
|
|
|
|
|
|
import QRCodeService from '../../services/QRCodeService';
|
|
|
|
|
|
const {
|
|
|
JUMP_WITH_URL,
|
|
|
SET_SCANTYPE,
|
|
|
JUMP_WITH_OBJECT_NEWFLAG,
|
|
|
PROCESS_SCAN_STRING,
|
|
|
} = require('../../constants/actionTypes').default;
|
|
|
|
|
|
let showTip = false;
|
|
|
|
|
|
if (typeof ScanTypeInfo === "undefined") {
|
|
|
var ScanTypeInfo = {
|
|
|
YHScanType_Default: 0,
|
|
|
YHScanType_Skn: 1, //Skn二维码
|
|
|
YHScanType_Bear: 2, //小熊二维码
|
|
|
YHScanType_Booth: 3, //品牌二维码
|
|
|
YHScanType_Express: 4, //快递单号扫描
|
|
|
YHScanType_Brand: 5,
|
|
|
YHScanType_YohoBuy: 6, //以jumpWithObjectNewFlag传Native
|
|
|
YHScanType_PCLogin: 7,
|
|
|
YHScanType_CommonUrl: 8,
|
|
|
}
|
|
|
}
|
|
|
function showAlert(title = '二维码扫描', message1, message2, confirmInfo = '确定') {
|
|
|
if (!showTip) {
|
|
|
showTip = true;
|
|
|
Alert.alert(title, `${message1}${message2}`, [
|
|
|
{text: `${confirmInfo}`, onPress: () => {
|
|
|
showTip = false;
|
|
|
}}
|
|
|
]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
function scanSkn(scanString) {
|
|
|
let strs= new Array();
|
|
|
strs = scanString.split(":");
|
|
|
if (strs.length < 2) {
|
|
|
showAlert('二维码扫描','商品skn无法识别:', scanString,);
|
|
|
return false;
|
|
|
}
|
|
|
let sknID = strs[1];
|
|
|
if (sknID === '') {
|
|
|
showAlert('二维码扫描','商品skn无法识别:', scanString,);
|
|
|
return;
|
|
|
}
|
|
|
ReactNative.NativeModules.YH_QRCodeHelper.jumpWithUrl(sknID, ScanTypeInfo.YHScanType_Skn);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
function scanBear(scanString) {
|
|
|
let strs= new Array();
|
|
|
strs = scanString.split(":");
|
|
|
if (strs.length < 2) {
|
|
|
showAlert('二维码扫描','小熊二维码无法识别:', scanString,);
|
|
|
return false;
|
|
|
}
|
|
|
let bearID = strs[1];
|
|
|
if (bearID === '') {
|
|
|
showAlert('二维码扫描','小熊二维码无法识别:', scanString,);
|
|
|
return false;
|
|
|
}
|
|
|
ReactNative.NativeModules.YH_QRCodeHelper.jumpWithUrl(bearID, ScanTypeInfo.YHScanType_Bear);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
function scanBooth(scanString) {
|
|
|
let strs= new Array();
|
|
|
strs = scanString.split(":");
|
|
|
if (strs.length < 2) {
|
|
|
showAlert('二维码扫描','品牌店铺号无法识别:', scanString,);
|
|
|
return false;
|
|
|
}
|
|
|
let boothID = strs[1];
|
|
|
if (boothID === '') {
|
|
|
showAlert('二维码扫描','品牌店铺号无法识别:', scanString,);
|
|
|
return false;
|
|
|
}
|
|
|
ReactNative.NativeModules.YH_QRCodeHelper.jumpWithUrl(boothID, ScanTypeInfo.YHScanType_Booth);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
function scanExpress(expressString) {
|
|
|
if (scanString === '') {
|
|
|
showAlert('二维码扫描','快递单号无法识别:', expressString,);
|
|
|
return false;
|
|
|
}
|
|
|
ReactNative.NativeModules.YH_QRCodeHelper.jumpWithUrl(expressString, ScanTypeInfo.YHScanType_Express);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
function scanYohoBuy(scanString) {
|
|
|
let dataString = '';
|
|
|
let isNew = false;
|
|
|
let strs= new Array();
|
|
|
if (scanString.indexOf('yohobuy:') !== -1) {
|
|
|
strs = scanString.split("yohobuy:");
|
|
|
if (strs.length == 1) {
|
|
|
dataString = strs[0];
|
|
|
} else {
|
|
|
dataString = strs[1];
|
|
|
}
|
|
|
}
|
|
|
if (scanString.indexOf('yohobuy=') !== -1 || scanString.indexOf('yohobuy=') !== -1 ) {
|
|
|
isNew = true;
|
|
|
if (scanString.indexOf('yohobuy=') !== -1) {
|
|
|
strs = scanString.split("yohobuy=");
|
|
|
if (strs.length == 1) {
|
|
|
dataString = strs[0];
|
|
|
} else {
|
|
|
dataString = strs[1];
|
|
|
}
|
|
|
} else {
|
|
|
strs = scanString.split("yohobuy=");
|
|
|
if (strs.length == 1) {
|
|
|
dataString = strs[0];
|
|
|
} else {
|
|
|
dataString = strs[1];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
var obj = JSON.parse(dataString); //由JSON字符串转换为JSON对象
|
|
|
if (!obj) {
|
|
|
showAlert('二维码扫描','', dataString,);
|
|
|
return false;
|
|
|
} else {
|
|
|
//回传
|
|
|
ReactNative.NativeModules.YH_QRCodeHelper.jumpWithObjectNewFlag(obj, isNew);
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
function handleUrlForSknId(scanString) {
|
|
|
let sknId = '';
|
|
|
let sknIndex = scanString.indexOf('skn=');
|
|
|
if (sknIndex !== -1 ) {
|
|
|
let evaluateString = '^[0-9]*$';
|
|
|
let sknIdRight = scanString.substr(sknIndex + 'skn='.length);
|
|
|
sknId = sknIdRight;
|
|
|
for (var i = 0; i < sknIdRight.length; i++) {
|
|
|
let tempSknId = sknIdRight.substr(0,i);
|
|
|
if (!tempSknId.match(evaluateString)) {
|
|
|
sknId = sknIdRight.substr(i - 1);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return sknId;
|
|
|
}
|
|
|
|
|
|
function handleUrlForBrandId(brandUrl) {
|
|
|
// brandUrl
|
|
|
let brandId = '';
|
|
|
let brandIndex = brandUrl.indexOf('brand_id=');
|
|
|
if (brandIndex !== -1) {
|
|
|
let evaluateString = '^[0-9]*$';
|
|
|
let brandIdRight = brandUrl.substr(brandIndex + 'brand_id='.length);
|
|
|
brandId = brandIdRight;
|
|
|
for (var i = 0; i < brandIdRight.length; i++) {
|
|
|
let tempBrandId = brandIdRight.substr(0,i);
|
|
|
if (!tempBrandId.match(evaluateString)) {
|
|
|
brandId = brandIdRight.substr(i - 1);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return brandId;
|
|
|
}
|
|
|
|
|
|
function handleUrlForPCLogin(pcloginUrl) {
|
|
|
let pcloginIndex = pcloginUrl.indexOf('yohobuy.com/signin.html?qr=');
|
|
|
let pcLoginCode = '';
|
|
|
if (pcloginIndex !== -1) {
|
|
|
pcLoginCode = pcloginUrl.substr(pcloginIndex + 'yohobuy.com/signin.html?qr='.length);
|
|
|
}
|
|
|
return pcLoginCode;
|
|
|
}
|
|
|
|
|
|
function scanComonURL(urlString) {
|
|
|
let newString = urlString.replace(' ','');
|
|
|
return newString;
|
|
|
}
|
|
|
|
|
|
function scanAddressLink (scanString) {
|
|
|
console.log('3333333333');
|
|
|
//判断yoho 商品二维码'
|
|
|
let sknId = handleUrlForSknId(scanString);
|
|
|
if (sknId !== '') {
|
|
|
ReactNative.NativeModules.YH_QRCodeHelper.jumpWithUrl(sknId, ScanTypeInfo.YHScanType_Skn);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
//判断yoho 品牌链接二维码
|
|
|
let brandId = handleUrlForBrandId(scanString);
|
|
|
if (brandId !== '') {
|
|
|
ReactNative.NativeModules.YH_QRCodeHelper.jumpWithUrl(brandId, ScanTypeInfo.YHScanType_Brand);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
//判断PC登录二维码
|
|
|
let pcLoginCode = handleUrlForPCLogin(scanString);
|
|
|
if (pcLoginCode != '') {
|
|
|
NetInfo.isConnected.fetch().done((isConnected) => {
|
|
|
if (!isConnected) {
|
|
|
if (!showTip) {
|
|
|
showTip = true;
|
|
|
Alert.alert('提示', '无网络连接,刷新试试', [
|
|
|
{text: '取消', onPress: () => {
|
|
|
showTip = false;
|
|
|
}},
|
|
|
{text: '刷新', onPress: () => {
|
|
|
showTip = false;
|
|
|
}}
|
|
|
]);
|
|
|
return false;
|
|
|
};
|
|
|
ReactNative.NativeModules.YH_QRCodeHelper.jumpWithUrl(pcLoginCode, ScanTypeInfo.YHScanType_PCLogin);
|
|
|
return true;
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
//扫描信息全局变量
|
|
|
let scanQRString = scanComonURL(scanString);
|
|
|
ReactNative.NativeModules.YH_QRCodeHelper.jumpWithUrl(scanQRString, ScanTypeInfo.YHScanType_CommonUrl);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
|
|
|
export function jumpWithUrl(url) {
|
|
|
function processScanString(url,scanType) {
|
|
|
let prefixRegex = '^skn:|^booth:|^bear:|^yohobuy:|^yohobuy=|^http://|^https://';
|
|
|
let isHasPrefix = url.match(prefixRegex);//prefixRegex.search(url);
|
|
|
if(isHasPrefix === -1 && scanType !== 4 && !showTip){
|
|
|
showAlert('二维码扫描','无法识别的二维码:', url,);
|
|
|
return false;
|
|
|
};
|
|
|
|
|
|
switch (scanType) {
|
|
|
case ScanTypeInfo.YHScanType_Skn://YHScanType_Skn
|
|
|
scanSkn(url);
|
|
|
break;
|
|
|
case ScanTypeInfo.YHScanType_Bear:
|
|
|
scanBear(url);
|
|
|
break;
|
|
|
case ScanTypeInfo.YHScanType_Booth:
|
|
|
scanBooth(url);
|
|
|
break;
|
|
|
case ScanTypeInfo.YHScanType_Express:
|
|
|
scanExpress(url);
|
|
|
break;
|
|
|
case ScanTypeInfo.YHScanType_Default: {
|
|
|
if (url.indexOf('skn:') !== -1) {
|
|
|
scanSkn(url);
|
|
|
} else if (url.indexOf('booth:') !== -1) {
|
|
|
scanBooth(url);
|
|
|
} else if (url.indexOf('bear:') !== -1) {
|
|
|
scanBear(url);
|
|
|
} else if (url.indexOf('yohobuy:') !== -1 || url.indexOf('yohobuy=') !== -1) {
|
|
|
scanYohoBuy(url);
|
|
|
} else if (url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1) {
|
|
|
scanAddressLink(url);
|
|
|
} else {
|
|
|
showAlert('二维码扫描','无法识别的二维码:', url,);
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
break;
|
|
|
default:
|
|
|
|
|
|
}
|
|
|
};
|
|
|
|
|
|
export function setScanType(scanType) {
|
|
|
return {
|
|
|
type: SET_SCANTYPE,
|
|
|
payload: scanType,
|
|
|
};
|
|
|
}
|
|
|
|
|
|
export function processURL(scanString) {
|
|
|
return (dispatch, getState) => {
|
|
|
|
|
|
const {qrcode} = getState();
|
|
|
if(!scanString) {
|
|
|
return;
|
|
|
}
|
|
|
let useableScanString = processScanString(scanString,qrcode.scanType);
|
|
|
if (useableScanString) {
|
|
|
return;
|
|
|
}
|
|
|
dispatch({
|
|
|
type: PROCESS_SCAN_STRING,
|
|
|
payload: useableScanString
|
|
|
});
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
export function jumpWithUrl(url, JumpType) {
|
|
|
if (!url) {
|
|
|
__DEV__ && console.log('Illegal url');
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
ReactNative.NativeModules.YH_PlustarHelper.jumpWithUrl(url);
|
|
|
ReactNative.NativeModules.YH_QRCodeHelper.jumpWithUrl({url, JumpType});
|
|
|
return {
|
|
|
type: JUMP_WITH_URL,
|
|
|
payload: url
|
|
|
};
|
|
|
}
|
|
|
|
|
|
export function jumpWithObjectNewFlag(jsonObj, isNew) {
|
|
|
if (!url) {
|
|
|
__DEV__ && console.log('Illegal object');
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
ReactNative.NativeModules.YH_QRCodeHelper.jumpWithObjectNewFlag({jsonObj, isNew});
|
|
|
return {
|
|
|
type: JUMP_WITH_OBJECT_NEWFLAG,
|
|
|
payload: url
|
|
|
};
|
|
|
} |
...
|
...
|
|