diff --git a/lib/retrofit/business_api.dart b/lib/retrofit/business_api.dart new file mode 100644 index 00000000..33cfeac0 --- /dev/null +++ b/lib/retrofit/business_api.dart @@ -0,0 +1,110 @@ +import 'dart:convert'; + +import 'package:dio/dio.dart'; +import 'package:flutter/cupertino.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:retrofit/retrofit.dart'; + +part 'business_api.g.dart'; + +///本地 +const localBaseUrl = "http://admin-api.test.yixinhuixiang.com/"; +///测试 +// const localBaseUrl = "http://test-merchant.lotus-wallet.com/"; +///线上 +const serviceBaseUrl = "http://pos.tenant.lotus-wallet.com/"; + +@RestApi(baseUrl: localBaseUrl) +abstract class BusinessApiService { + factory BusinessApiService( + Dio dio, { + String baseUrl, + BuildContext context, + String token, + bool showLoading = false, + String url, + bool showErrorToast = true, + }) { + Map headers = + (token == null || token == "") ? {} : {'token': "Bearer $token"}; + baseUrl = serviceBaseUrl; + if (url != null) baseUrl = url; + dio.options = BaseOptions( + connectTimeout: 60000, + receiveTimeout: 60000, + headers: headers, + responseType: ResponseType.json, + baseUrl: baseUrl, + ); + dio.interceptors.add( + InterceptorsWrapper(onRequest: + (RequestOptions options) { + debugPrint("\n======================= 请求数据 ======================="); + debugPrint("method = ${options.method.toString()}"); + debugPrint("url = ${options.uri.toString()}"); + debugPrint("headers = ${options.headers}"); + if (showLoading && !EasyLoading.isShow) { + //是否显示loading + EasyLoading.show(status: "正在加载..."); + } + if (options.data is FormData) { + debugPrint("params data = FormData"); + } else { + debugPrint("params data = ${jsonEncode(options.data)}"); + } + debugPrint("params queryParameters = ${options.queryParameters}"); + }, onResponse: (Response response) { + if (showLoading && EasyLoading.isShow) EasyLoading.dismiss(); + debugPrint("\n======================= 响应数据开始 ======================="); + debugPrint("code = ${response.statusCode}"); + p(jsonEncode(response.data)); + + // debugPrint(jsonEncode(response.data), wrapWidth: response.data.toString().length * 10); + + 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); + // } + // } + if (showErrorToast && + map["code"] == 404 && + (map["msg"] ?? map["message"]) != null) { + SmartDialog.showToast(map["msg"] ?? map["message"], + alignment: Alignment.center); + } + debugPrint("======================= 响应数据结束 =======================\n"); + }, onError: (DioError e) { + if (EasyLoading.isShow) EasyLoading.dismiss(); + SmartDialog.showToast("网络错误,请切换网络或稍后再试!", alignment: Alignment.center); + debugPrint("\n======================= 错误响应数据 ======================="); + debugPrint("type = ${e.type}"); + debugPrint("message = ${e.message}"); + debugPrint("\n"); + }), + ); + return _BusinessApiService(dio, baseUrl: baseUrl); + } + + void dispose(); + + static void p(String msg) { + //因为String的length是字符数量不是字节数量所以为了防止中文字符过多, + // 把4*1024的MAX字节打印长度改为1000字符数 + int maxStrLength = 900; + //大于1000时 + while (msg.length > maxStrLength) { + debugPrint(msg.substring(0, maxStrLength), wrapWidth: maxStrLength); + msg = msg.substring(maxStrLength); + } + //剩余部分 + debugPrint(msg, wrapWidth: maxStrLength); + } +} diff --git a/lib/retrofit/business_api.g.dart b/lib/retrofit/business_api.g.dart new file mode 100644 index 00000000..61d7889c --- /dev/null +++ b/lib/retrofit/business_api.g.dart @@ -0,0 +1,23 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'business_api.dart'; + +// ************************************************************************** +// RetrofitGenerator +// ************************************************************************** + +class _BusinessApiService implements BusinessApiService { + _BusinessApiService(this._dio, {this.baseUrl}) { + ArgumentError.checkNotNull(_dio, '_dio'); + } + + final Dio _dio; + + String baseUrl; + + @override + void dispose() { + _dio.close(force: true); + } + +}