我创建了一个移动应用程序来扫描QR-CODE,其中嵌入了各种信息。其中一个信息是日期。
以前版本的QR-CODE与日期格式有所变化。之前的日期格式为mm / dd,新版本为yy-MM-dd。
我能够让它扫描新版本的标签或旧标签,但不能同时扫描两者。我需要让它识别机器人类型。我不确定是否有办法使用if语句识别旧格式并将其转换为新格式。
以下是使用新标签的工作代码示例。
(function(){
'use strict';
var app = angular.module('Controllers', []);
var baseUrl = 'https://apps.laticrete.com/LSCWebApiService/lscapi/';
app.controller('BarcodeCtrl', ['$scope', '$state', '$stateParams', '$http', 'alertsManager', '$timeout', 'localStorageService', '$cordovaBarcodeScanner', '$cordovaGeolocation', '$filter',
function($scope, $state, $stateParams, $http, alertsManager, $timeout, localStorageService, $cordovaBarcodeScanner, $cordovaGeolocation, $filter) {
var SessionId = localStorageService.get('SessionId');
// Get GeoLocation
$cordovaGeolocation
.getCurrentPosition()
.then(function(position) {
$scope.lat = position.coords.latitude;
$scope.long = position.coords.longitude;
});
document.addEventListener("deviceready", function() {
$scope.scanMaterial = function() {
$cordovaBarcodeScanner
.scan()
.then(function(result) {
var codeArray = result.text.split(',');
$scope.SKU = codeArray[1].replace("I:", "").trim();
$scope.ControlNumber = codeArray[0].replace("W:", "").trim();
//$scope.ManufactureDate = codeArray[2].replace("MFG:", "").trim();
$scope.ManufactureDate = codeArray[2].replace("MFG:", "20").replace(/(\d{4})(\d{2})(\d{2})/, "$1-$2-$3").trim();
$scope.BatchCode = codeArray[3].replace("B:", "").trim();
var dataObj = {
SessionId: SessionId,
JobId: $stateParams.JobId,
ManufactureDate: $scope.ManufactureDate,
BatchCode: $scope.BatchCode,
SKU: $scope.SKU,
ControlNumber: $scope.ControlNumber,
CreatedClient: new Date(),
Latitude: $scope.lat,
Longitude: $scope.long,
Product: { Id : 1}
};
$http.post(baseUrl + 'Material/PostNewMaterial', dataObj)
.success(function() {
alertsManager.addAlert('Success: You have successfully added material.', 'alert-success');
$state.go('^');
}).error(function(dataObj) {
alertsManager.addAlert('Failure Meesage: ' + JSON.stringify({dataObj:dataObj}), 'alert-danger');
});
$timeout(function(){
alertsManager.clearAlerts();
}, 5000);
}, function(error) {
console.log("An error has happened " + error);
});
};
}, false);
}]);
})();
我要求帮助的部分代码是$ scope.ManufactureDate
提前谢谢你。
答案 0 :(得分:1)
检查代码是否与其中一种格式匹配,如果匹配,则相应地解析。
if (codeArray[2].match(/MFG:\d+-\d+-\d+/) !== null) {
/* The format is of 'MFG:000-000-000', \d+ matches 1..n numbers */
...
}
或者你可以将它们组合成一个正则表达式:
let matches = codeArray[2].match(/MFG:(20(\d{2})-\d{2}-\d{2})|(\d{2}/\d{2}))/);