Authored by 张丽霞

扫码后处理逻辑,review by redding

... ... @@ -26,6 +26,10 @@ import {
setPlatform,
} from './reducers/app/appActions';
import {
setScanType,
} from './reducers/qrcode/qrcodeActions';
function getInitialState() {
const _initState = {
app: (new appInitialState()),
... ... @@ -41,6 +45,8 @@ export default function native(platform) {
render() {
const store = configureStore(getInitialState());
store.dispatch(setPlatform(platform));
let scanType = this.props.scanType;
store.dispatch(setScanType(scanType));
return (
<Provider store={store}>
... ...
... ... @@ -11,29 +11,27 @@ import ReactNative, {
TouchableOpacity,
Image,
Animated,
NetInfo,
} from 'react-native';
import Camera from 'react-native-camera';
const yh_QRCodeHelperEvt = new NativeEventEmitter(ReactNative.NativeModules.YH_QRCodeHelper)
yh_QRCodeHelperEvt.addListener('scanAlertDismiss', () => camera._addOnBarCodeReadListener())
let camera;
let scanTypeValue, animationFromTopToBottom;
let animationStartHeight = Dimensions.get('window').height * 0.10;
let animationEndHeight = Dimensions.get('window').height * 0.44;
export default class QRCode extends Component {
startAnimation() {
Animated.sequence([
Animated.timing(this.state.destX, {
toValue: animationEndHeight,
duration: 2500,
}),
Animated.timing(this.state.destX, {
toValue: animationStartHeight,
duration: 2500,
})
]).start(() => this.startAnimation());
export default class QRCode extends React.Component {
startAnimation() {
Animated.timing(this.state.destX, {
toValue: animationFromTopToBottom ? animationEndHeight : animationStartHeight,
duration: 2500,
}).start(() => {
this.startAnimation();
animationFromTopToBottom = !animationFromTopToBottom;
});
};
componentDidMount() {
... ... @@ -43,28 +41,42 @@ export default class QRCode extends Component {
constructor(props) {
super(props);
camera = null;
scanTypeValue = 0;
animationFromTopToBottom = true;
this.state = {
destX: new Animated.Value(animationStartHeight),
};
}
_onBarCodeRead(e) {
console.log(e);
let type = scanTypeValue;
if (e.bounds.origin.x >= 50 && e.bounds.origin.x <= 250 && e.bounds.origin.y >= 70 && e.bounds.origin.y < 270 && e.bounds.size.width < 200 && e.bounds.size.height < 200) {
ReactNative.NativeModules.YH_QRCodeHelper.jumpWithUrl(e.data);
camera._removeOnBarCodeReadListener();
this.props._onBarCodeRead && this.props._onBarCodeRead(e.data);
}
}
render() {
render() {
let {scanType} = this.props;
scanTypeValue = scanType;
return (
<View style={styles.container}>
<Camera
ref={(cam) => {
camera = cam;
}}
onBarCodeRead={this._onBarCodeRead}
onBarCodeRead={(e) => {
console.log(e);
let type = scanTypeValue;
if (e.bounds.origin.x >= 50 && e.bounds.origin.x <= 250 && e.bounds.origin.y >= 70 && e.bounds.origin.y < 270 && e.bounds.size.width < 200 && e.bounds.size.height < 200) {
camera._removeOnBarCodeReadListener();
this.props.onBarCodeRead && this.props.onBarCodeRead(e.data);
}
}}
style={styles.camera}
captureAudio={false}
>
... ...
... ... @@ -4,5 +4,7 @@ export default keyMirror({
SET_PLATFORM: null,
JUMP_WITH_URL: null,
SET_SCANTYPE: null,
JUMP_WITH_OBJECT_NEWFLAG: null,
PROCESS_SCAN_STRING: null,
});
... ...
... ... @@ -16,7 +16,6 @@ import {connect} from 'react-redux';
import {Map} from 'immutable';
import * as qrcodeActions from '../reducers/qrcode/qrcodeActions';
import QRCode from '../components/qrcode/QRCode';
const actions = [
qrcodeActions,
];
... ... @@ -44,6 +43,7 @@ class QRCodeContainer extends Component {
constructor(props) {
super(props);
this._onBarCodeRead = this._onBarCodeRead.bind(this);
}
componentDidMount() {
... ... @@ -54,13 +54,23 @@ class QRCodeContainer extends Component {
}
_onBarCodeRead(scanString) {
if (!scanString) {
__DEV__ && console.log('Illegal scanString');
return;
}
this.props.actions.processURL(scanString);
}
render() {
let {qrcode} = this.props;
let {scanType} = qrcode;
return (
<View style={styles.container}>
<QRCode/>
<QRCode
scanType={scanType}
onBarCodeRead={this._onBarCodeRead}
/>
</View>
);
}
... ...
'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;
}
});
}
export function jumpWithUrl(url) {
//扫描信息全局变量
let scanQRString = scanComonURL(scanString);
ReactNative.NativeModules.YH_QRCodeHelper.jumpWithUrl(scanQRString, ScanTypeInfo.YHScanType_CommonUrl);
return true;
}
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
};
}
... ...
... ... @@ -4,21 +4,8 @@ import {Record, List, Map} from 'immutable';
let InitialState = Record({
activeTab: 0,
gender: -1,
0: new (Record({
isFetching: false,
error: null,
head: List(),
foot: List(),
list: List(),
})),
1: new (Record({
isFetching: false,
error: null,
head: List(),
foot: List(),
list: List(),
})),
scanType: 0,
jumpScanStringFaild: 0,
segment: null,
});
... ...
... ... @@ -5,13 +5,21 @@ import Immutable, {Map} from 'immutable';
const {
JUMP_WITH_URL,
SET_SCANTYPE,
JUMP_WITH_OBJECT_NEWFLAG,
PROCESS_SCAN_STRING,
} = require('../../constants/actionTypes').default;
const initialState = new InitialState;
export default function qrcodeReducer(state=initialState, action) {
switch(action.type) {
case SET_SCANTYPE: {
return state.set('scanType',action.payload);
}
case PROCESS_SCAN_STRING: {
return state.set('jumpScanStringFaild',action.payload);
}
}
return state;
... ...