登录接口调试
This commit is contained in:
45
lib/common/encrypt/pwd.dart
Normal file
45
lib/common/encrypt/pwd.dart
Normal file
@@ -0,0 +1,45 @@
|
||||
|
||||
import 'package:cashier_reserve/common/print/print.dart';
|
||||
import 'package:encrypt/encrypt.dart';
|
||||
import 'package:pointycastle/asymmetric/api.dart';
|
||||
|
||||
class PwdEncrypt {
|
||||
static const _rsaPubKey =
|
||||
"MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANL378k3RiZHWx5AfJqdH9xRNBmD9wGD2iRe41HdTNF8RUhNnHit5NpMNtGL0NPTSSpPjjI1kJfVorRvaQerUgkCAwEAAQ==";
|
||||
|
||||
static String encrypt(String pwd) {
|
||||
return _encryptString(publicKeyString: _rsaPubKey, plainText: pwd);
|
||||
}
|
||||
|
||||
/// 利用公钥进行加密
|
||||
static String _encryptString(
|
||||
{required String publicKeyString, required String plainText}) {
|
||||
String key = '-----BEGIN PUBLIC KEY-----\n\r';
|
||||
int length = 0;
|
||||
const baseLen = 64;
|
||||
while (length < publicKeyString.length) {
|
||||
bool over = length + baseLen > publicKeyString.length;
|
||||
key += publicKeyString.substring(
|
||||
length, over ? publicKeyString.length : length + baseLen);
|
||||
key += '\n\r';
|
||||
length += baseLen;
|
||||
}
|
||||
key += '-----END PUBLIC KEY-----';
|
||||
|
||||
yjPrint(key);
|
||||
|
||||
final publicKey = _parsePublicKeyFromPem(key);
|
||||
|
||||
final encrypt = Encrypter(RSA(publicKey: publicKey));
|
||||
|
||||
final encryptedText = encrypt.encrypt(plainText);
|
||||
|
||||
return encryptedText.base64;
|
||||
}
|
||||
|
||||
/// 通过PEM字符串解析公钥字符串
|
||||
static RSAPublicKey _parsePublicKeyFromPem(String pemString) {
|
||||
final key = RSAKeyParser().parse(pemString);
|
||||
return key as RSAPublicKey;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:cashier_reserve/common/push/push.dart';
|
||||
import 'package:cashier_reserve/datas/login/login_result.dart';
|
||||
import 'package:cashier_reserve/login/login_view.dart';
|
||||
|
||||
import '../base/ui.dart';
|
||||
@@ -40,14 +41,6 @@ class AppManager {
|
||||
}
|
||||
|
||||
YJPush.presentWidget(globalContext!, const LoginView());
|
||||
|
||||
// showDialog(
|
||||
// context: globalContext!,
|
||||
// barrierDismissible: false,
|
||||
// builder: (BuildContext context) {
|
||||
// return WillPopScope(
|
||||
// onWillPop: () => Future.value(false), child: const LoginView());
|
||||
// });
|
||||
}
|
||||
|
||||
static void disposeLoginWidget() {
|
||||
@@ -61,4 +54,15 @@ class AppManager {
|
||||
static String getUserToken() {
|
||||
return HiveManager.getUserToken();
|
||||
}
|
||||
|
||||
static void loginSuccess(LoginResult? r) {
|
||||
HiveManager.setUserToken(r?.token ?? '');
|
||||
HiveManager.setShopId((r?.shopId ?? '').toString());
|
||||
HiveManager.setShopName(r?.shopName ?? '');
|
||||
HiveManager.setShopLogo(r?.logo ?? '');
|
||||
HiveManager.setUserInfo(r?.user?.toString() ?? '');
|
||||
|
||||
disposeLoginWidget();
|
||||
Navigator.of(globalContext!).pop();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,51 @@ class HiveManager {
|
||||
_userInfoBox = Hive.box();
|
||||
}
|
||||
|
||||
static void clearLoginInfo() {
|
||||
_userInfoBox?.delete('token');
|
||||
_userInfoBox?.delete('shopId');
|
||||
_userInfoBox?.delete('shopName');
|
||||
_userInfoBox?.delete('shopLogo');
|
||||
_userInfoBox?.delete('userInfo');
|
||||
}
|
||||
|
||||
static void setUserToken(String token) {
|
||||
_userInfoBox?.put('token', token);
|
||||
}
|
||||
|
||||
static String getUserToken() {
|
||||
return _userInfoBox?.get('token') ?? '';
|
||||
}
|
||||
|
||||
static void setShopId(String shopId) {
|
||||
_userInfoBox?.put('shopId', shopId);
|
||||
}
|
||||
|
||||
static String getShopId() {
|
||||
return _userInfoBox?.get('shopId') ?? '';
|
||||
}
|
||||
|
||||
static void setShopName(String shopName) {
|
||||
_userInfoBox?.put('shopName', shopName);
|
||||
}
|
||||
|
||||
static String getShopName() {
|
||||
return _userInfoBox?.get('shopName') ?? '';
|
||||
}
|
||||
|
||||
static void setShopLogo(String shopLogo) {
|
||||
_userInfoBox?.put('shopLogo', shopLogo);
|
||||
}
|
||||
|
||||
static String getShopLogo() {
|
||||
return _userInfoBox?.get('shopLogo') ?? '';
|
||||
}
|
||||
|
||||
static void setUserInfo(String userInfo) {
|
||||
_userInfoBox?.put('userInfo', userInfo);
|
||||
}
|
||||
|
||||
static String getUserInfo() {
|
||||
return _userInfoBox?.get('userInfo') ?? '';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:cashier_reserve/common/manager/app_manager.dart';
|
||||
import 'package:cashier_reserve/common/manager/hive_manager.dart';
|
||||
import 'package:cashier_reserve/common/print/print.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
@@ -20,6 +21,11 @@ class RequestManager {
|
||||
|
||||
/// GET
|
||||
static Future<dynamic> get(String url, {bool catchError = true}) {
|
||||
if (url.contains("?")) {
|
||||
url += "&shopId=${HiveManager.getShopId()}";
|
||||
} else {
|
||||
url += "?shopId=${HiveManager.getShopId()}";
|
||||
}
|
||||
return _doRequest("GET", url, catchError: catchError);
|
||||
}
|
||||
|
||||
@@ -31,12 +37,22 @@ class RequestManager {
|
||||
/// POST
|
||||
static Future<dynamic> post(String url, Map<String, dynamic>? body,
|
||||
{bool catchError = true}) {
|
||||
if (body != null) {
|
||||
body["shopId"] = HiveManager.getShopId();
|
||||
} else {
|
||||
body = {"shopId": HiveManager.getShopId()};
|
||||
}
|
||||
return _doRequest("POST", url, body: body, catchError: catchError);
|
||||
}
|
||||
|
||||
/// PUT
|
||||
static Future<dynamic> put(String url, Map<String, dynamic>? body,
|
||||
{bool catchError = true}) {
|
||||
if (body != null) {
|
||||
body["shopId"] = HiveManager.getShopId();
|
||||
} else {
|
||||
body = {"shopId": HiveManager.getShopId()};
|
||||
}
|
||||
return _doRequest("PUT", url, body: body, catchError: catchError);
|
||||
}
|
||||
|
||||
@@ -49,7 +65,8 @@ class RequestManager {
|
||||
data: body,
|
||||
options: Options(
|
||||
method: method,
|
||||
headers: {"authorization": "Bearer ${AppManager.getUserToken()}"}));
|
||||
headers: {"authorization": "Bearer ${AppManager.getUserToken()}"}
|
||||
));
|
||||
yjPrint("[RequestManager resp]: $method 【$url】body === $resp");
|
||||
if (catchError) {
|
||||
if (resp.statusCode == kNeedLoginCode) {
|
||||
@@ -65,6 +82,17 @@ class RequestManager {
|
||||
} catch (e) {
|
||||
yjPrint("[RequestManager error]: $method 【$url】error === $e");
|
||||
// _alertError("网络错误", "请检查您的网络连接!");
|
||||
if (e is DioException) {
|
||||
DioException de = e;
|
||||
if (de.response?.statusCode == kNeedLoginCode) {
|
||||
AppManager.gotoLogin();
|
||||
return null;
|
||||
}
|
||||
if (de.response?.data is Map) {
|
||||
final data = de.response!.data as Map;
|
||||
Utils.toast(data["message"], AppManager.globalContext!);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user