Browse Source

safety

master
fmk 3 years ago
parent
commit
015bf7ca22
  1. 136
      lib/retrofit/min_api.dart

136
lib/retrofit/min_api.dart

@ -0,0 +1,136 @@
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flare_flutter/flare_testing.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:huixiang/retrofit/data/base_data.dart';
import 'package:huixiang/view_widget/login_tips_dialog.dart';
import 'package:retrofit/retrofit.dart';
import 'data/findMiNiGroupList.dart';
import 'data/miNiDetail.dart';
import 'data/shoppingCart.dart';
part 'min_api.g.dart';
// const base_url = "https://pos.platform.lotus-wallet.com/app/"; ///
// const baseUrl = "https://pos.platform.lotus-wallet.com/app/"; ///
const base_url = "http://192.168.10.236:8765/app/"; ///
const baseUrl = "http://192.168.10.236:8765/app/"; ///
// const base_url = "http://192.168.10.37:8766/app/";
// const baseUrl = "http://192.168.10.37:8766/app/";
///
@RestApi(baseUrl: baseUrl)
abstract class MinApiService {
factory MinApiService(Dio dio, {
String baseUrl,
BuildContext context,
String token,
String tenant,
bool showLoading = true,
bool pay = false,
}) {
Map<String, dynamic> headers =
(token == null || token == "") ? {} : {'token': "Bearer $token"};
if (tenant != null && tenant != "") {
headers["tenant"] = tenant;
}
if (pay) {
headers["Environment"] = "app";
}
dio.options = BaseOptions(
connectTimeout: 60000,
receiveTimeout: 60000,
headers: headers,
responseType: ResponseType.json,
);
dio.interceptors.add(
InterceptorsWrapper(onRequest: (RequestOptions options) {
debugPrint("\n======================= Min_请求数据 =======================");
debugPrint("method = ${options.method.toString()}");
debugPrint("url = ${options.uri.toString()}");
debugPrint("headers = ${options.headers}");
if (showLoading && !EasyLoading.isShow) {
//loading
EasyLoading.show(status: S.of(context).zhengzaijiazai);
}
if (options.data is FormData) {
debugPrint("params data = FormData");
} else {
debugPrint("params data = ${jsonEncode(options.data)}");
}
debugPrint("params queryParameters = ${options.queryParameters}");
}, onResponse: (Response response) {
debugPrint("\n======================= Min_响应数据开始 =======================");
if (showLoading && EasyLoading.isShow) {
EasyLoading.dismiss();
}
debugPrint("code = ${response.statusCode}");
// p(jsonEncode(response.data));
debugPrint(jsonEncode(response.data), wrapWidth: response.data.toString().length);
Map map = response.data;
if (map["code"] != 0) {
EasyLoading.dismiss();
}
if (map["code"] == 40005 || map["code"] == 40001) {
if (!LoginTipsDialog().isShow) {
// print("show: ${LoginTipsDialog().isShow}");
LoginTipsDialog().show(context);
}
} else if (map["code"] != 0 && response.request.baseUrl == baseUrl) {
///poi服务请求不需要toast
SmartDialog.showToast(map["msg"], alignment: Alignment.center);
}
debugPrint("======================= Min_响应数据结束 =======================\n");
}, onError: (DioError e) {
if (EasyLoading.isShow) {
EasyLoading.dismiss();
}
debugPrint("\n======================= Min_错误响应数据 =======================");
debugPrint("type = ${e.type}");
debugPrint("message = ${e.message}");
debugPrint("\n");
}),
);
if (kReleaseMode) {
baseUrl = base_url;
}
return _MinApiService(dio, baseUrl: baseUrl);
}
static void p(String msg) {
int maxStrLength = 900;
while (msg.length > maxStrLength) {
debugPrint(msg.substring(0, maxStrLength));
msg = msg.substring(maxStrLength);
}
debugPrint(msg);
}
///
@POST("product/findMiNiGroupList")
Future<BaseData<List<FindMiNiGroupList>>> findMiNiGroupList(
@Body() Map<String, dynamic> param);
////
@GET("/product/queryMiNiProductDetail?id={id}")
Future<BaseData<MiNiDetail>> miNiDetail(@Path("id") String id);
///
@POST("shoppingcart")
Future<BaseData<ShoppingCart>> shoppingCart(
@Body() Map<String, dynamic> param,Map<String, dynamic> header);
}
Loading…
Cancel
Save