Реализовано получение токена кассы

This commit is contained in:
Ivan Murashov
2017-07-18 09:43:00 +03:00
parent 79a16fb81f
commit fc07eca469
4 changed files with 54 additions and 57 deletions

View File

@@ -18,44 +18,41 @@ import com.google.android.gms.iid.InstanceID;
public class MainActivity extends FlutterActivity { public class MainActivity extends FlutterActivity {
private static final String SCANNER_CHANNEL = "com.dinnect.checker"; private static final String INSTANCE_ID_CHANNEL = "com.dinnect.checker/instance_id";
// private static final String INSTANCE_ID_CHANNEL = "com.dinnect.checker/instance_id";
private String mInstanceID = null;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this); GeneratedPluginRegistrant.registerWith(this);
// new MethodChannel(getFlutterView(), INSTANCE_ID_CHANNEL).setMethodCallHandler( new MethodChannel(getFlutterView(), INSTANCE_ID_CHANNEL).setMethodCallHandler(
// new MethodCallHandler() {
// @Override
// public void onMethodCall(MethodCall call, Result result) {
//
// }
// });
new MethodChannel(getFlutterView(), SCANNER_CHANNEL).setMethodCallHandler(
new MethodCallHandler() { new MethodCallHandler() {
@Override @Override
public void onMethodCall(MethodCall call, Result result) { public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("getInstanceID")) {
InstanceID instanceID = InstanceID.getInstance(MainActivity.this);
String id = instanceID.getId();
if (id != null) {
result.success(id);
} else {
result.error("UNAVAILABLE", "Can't get instanceID.", null);
}
} else {
result.notImplemented();
}
} }
}); });
} }
public void startScanner() { public void startScanner() {
startActivity(new Intent(MainActivity.this, CameraActivity.class));
} }
public String getInstanceID() { public void getInstanceID() {
if (mInstanceID == null) {
InstanceID instanceID = InstanceID.getInstance(this);
mInstanceID = instanceID.getId();
}
Log.d()
return mInstanceID;
} }
} }

View File

@@ -3,7 +3,7 @@ import 'package:flutter/services.dart';
import 'splash.dart'; import 'splash.dart';
const String _name = 'Ivan Murashov'; const String _name = 'Ivan Murashov';
const String intUrl = 'https://pos-api.dinect.com/20130701/'; const String intUrl = 'https://pos-api-int.dinect.com/20130701/';
const String intToken = '9fec83cdca38c357e6b65dbb17514cdd36bf2a08'; const String intToken = '9fec83cdca38c357e6b65dbb17514cdd36bf2a08';
final httpClient = createHttpClient(); final httpClient = createHttpClient();
@@ -28,6 +28,3 @@ abstract class BaseState<T> extends State<StatefulWidget> {
} }
} }
//DMUrl := 'http://pos-api-int.dinect.com/20130701/';
//DMAToken := '9fec83cdca38c357e6b65dbb17514cdd36bf2a08';

View File

@@ -1,6 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'dart:async';
import 'main.dart'; import 'main.dart';
/// Экран регистрации магазина и кассы. /// Экран регистрации магазина и кассы.
@@ -10,10 +9,11 @@ class RegistrationScreen extends StatefulWidget {
class _RegistrationScreenState extends BaseState<RegistrationScreen> { class _RegistrationScreenState extends BaseState<RegistrationScreen> {
static const platform = const MethodChannel('com.dinnect.checker'); static const String _merchantIDHint = 'ID магазина';
static const String _posIDHint = 'Номер кассы';
String _merchantID = ""; String _merchantID = "";
String _posNumber = ""; String _posID = "";
@override Widget build(BuildContext context) { @override Widget build(BuildContext context) {
return new Scaffold(appBar: _getAppBar(), body: _getScreen()); return new Scaffold(appBar: _getAppBar(), body: _getScreen());
@@ -35,24 +35,15 @@ class _RegistrationScreenState extends BaseState<RegistrationScreen> {
]); ]);
} }
/// На экране будет отображаться Column, с отцентрированными виджетами.
Widget _getScreen() { Widget _getScreen() {
return new Center(child: new Column(children: _getChildren())); return new Center(child: new Column(children: _getChildren()));
} }
Future<Null> startScanner() async {
try {
platform.invokeMethod('startScanner');
} on PlatformException {
}
}
List<Widget> _getChildren() { List<Widget> _getChildren() {
return<Widget>[ return<Widget>[
_getLogo(), _getLogo(),
_getDecoratedInputField("ID магазина", 0.0), _getDecoratedInputField(_merchantIDHint, 0.0),
_getDecoratedInputField("Номер кассы", 36.0), _getDecoratedInputField(_posIDHint, 36.0),
_getButton() _getButton()
]; ];
} }
@@ -78,13 +69,16 @@ class _RegistrationScreenState extends BaseState<RegistrationScreen> {
return new TextField(decoration: new InputDecoration(hintText: hint, return new TextField(decoration: new InputDecoration(hintText: hint,
hideDivider: true, hideDivider: true,
hintStyle: new TextStyle(color: const Color(0xffa5a5a5), hintStyle: new TextStyle(color: const Color(0xffa5a5a5),
fontSize: 16.0)), onChanged: (text) => _handleUserInput(text) ); fontSize: 16.0)), onChanged: (text) => _handleUserInput(hint, text));
} }
void _handleUserInput(String text) { void _handleUserInput(String hint, String text) {
if (text.length > 0) { if (text.length > 0) {
setState(() { //new setState(() {
_merchantID = text; if (hint == _merchantIDHint)
_merchantID = text;
else if (hint == _posIDHint)
_posID = text;
}); });
} }
} }
@@ -101,31 +95,41 @@ class _RegistrationScreenState extends BaseState<RegistrationScreen> {
child: new Container(height: 64.0, padding: new EdgeInsets.all(8.0), child: new Container(height: 64.0, padding: new EdgeInsets.all(8.0),
child: new RaisedButton(child: new Text('ЗАРЕГИСТРИРОВАТЬ', child: new RaisedButton(child: new Text('ЗАРЕГИСТРИРОВАТЬ',
style: new TextStyle(color: Colors.white)), style: new TextStyle(color: Colors.white)),
onPressed: _merchantID.length == 0 onPressed: _isFieldsAreFilled() ? () => _register(_merchantID) : null,
? null
: () => _register(_merchantID),
disabledColor: const Color(0xffbfbfbf)))); disabledColor: const Color(0xffbfbfbf))));
} }
void _register(String merchantShop) { _isFieldsAreFilled() {
_registerToken(merchantShop); print(_merchantID.length);
print(_posID.length);
return _merchantID.length == 5 && _posID.length > 0;
} }
Future<Null> _registerToken(String merchantShop) async { _register(String merchantShop) async {
const platform = const MethodChannel('com.dinnect.checker/instance_id');
String url = intUrl + 'tokens/?_dmapptoken=' + intToken;
String pos = await platform.invokeMethod('getInstanceID'); String pos = await platform.invokeMethod('getInstanceID');
print(pos);
String userAgent = 'dm-checker-test v1.0.1'; String userAgent = 'dm-checker-test v1.0.1';
var body = { var body = {
'merchant_shop' : merchantShop, 'merchant_shop': merchantShop,
'pos' : pos, 'pos': pos,
'description' : userAgent + '-' + pos 'description': userAgent + '-' + pos
}; };
httpClient.post(intUrl, body: body).then((response) { print(url);
print(response);
for (var value in body.values) {
print(value);
}
httpClient.post(url, body: body).then((response) {
print(response.body);
}).catchError((error) { }).catchError((error) {
print(error); print(error.toString());
}); });
} }
} }

View File

@@ -2,7 +2,6 @@ import 'dart:async';
import 'registration.dart'; import 'registration.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class SplashScreen extends StatelessWidget { class SplashScreen extends StatelessWidget {