From 4f6ee37163e1ea61ef7e7d1f679c760b05ba1c34 Mon Sep 17 00:00:00 2001 From: fff Date: Tue, 21 Jan 2025 14:44:26 +0800 Subject: [PATCH] =?UTF-8?q?IP=E8=8E=B7=E5=8F=96=E5=9F=8E=E5=B8=82=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/address/city_picker_page.dart | 8 +++--- lib/constant.dart | 2 -- lib/data/mip_data.dart | 29 +++++++++++++++++++++ lib/data/mip_data.g.dart | 30 +++++++++++++++++++++ lib/data/vip_card.dart | 2 +- lib/main_page.dart | 2 ++ lib/retrofit/min_api.g.dart | 2 +- lib/retrofit/retrofit_api.dart | 19 +++++++------- lib/retrofit/retrofit_api.g.dart | 43 +++++++++++++++++++++++++++---- lib/union/union_page.dart | 23 ++++++++--------- lib/union/union_select_city.dart | 30 ++++++++++----------- 11 files changed, 141 insertions(+), 49 deletions(-) create mode 100644 lib/data/mip_data.dart create mode 100644 lib/data/mip_data.g.dart diff --git a/lib/address/city_picker_page.dart b/lib/address/city_picker_page.dart index de7efdbe..8052a1d2 100644 --- a/lib/address/city_picker_page.dart +++ b/lib/address/city_picker_page.dart @@ -57,15 +57,17 @@ class _CityPickerPage extends State { style: TextStyle( fontSize: 12.sp, color: Colors.black, - fontWeight: MyFontWeight.regular), + fontWeight: MyFontWeight.regular, + ), ), ), ), Container( width: 1.w, color: Colors.black, - margin: - EdgeInsets.only(top: 2.h, bottom: 2.h, left: 10.w, right: 16.w), + margin: EdgeInsets.only( + top: 2.h, bottom: 2.h, left: 10.w, right: 16.w, + ), ), Icon( Icons.search, diff --git a/lib/constant.dart b/lib/constant.dart index be733676..4e939e0c 100644 --- a/lib/constant.dart +++ b/lib/constant.dart @@ -11,8 +11,6 @@ const String chatImageHost = "http://skk8mlm5b.hn-bkt.clouddn.com/"; const String socketHost = kDebugMode ? '192.168.10.200' : '47.93.216.24'; const int socketPort = 9090; -const ipBaseUrl = "http://whois.pconline.com.cn"; - /// 小程序接口的请求地址 const localMiniBaseUrl = "http://192.168.10.54:8765/app/"; ///本地 ///线上 diff --git a/lib/data/mip_data.dart b/lib/data/mip_data.dart new file mode 100644 index 00000000..d6f21461 --- /dev/null +++ b/lib/data/mip_data.dart @@ -0,0 +1,29 @@ +import 'package:json_annotation/json_annotation.dart'; + +part 'mip_data.g.dart'; + +@JsonSerializable(explicitToJson: true) +class MIpData { + String? ret; + Mip? data; + + MIpData(); + + factory MIpData.fromJson(Map json) => _$MIpDataFromJson(json); + + Map toJson() => _$MIpDataToJson(this); + +} + +@JsonSerializable(explicitToJson: true) +class Mip { + String? ip; + List? location; + + + Mip({this.ip, this.location}); + + factory Mip.fromJson(Map json) => _$MipFromJson(json); + Map toJson() => _$MipToJson(this); + +} diff --git a/lib/data/mip_data.g.dart b/lib/data/mip_data.g.dart new file mode 100644 index 00000000..fb3b2be1 --- /dev/null +++ b/lib/data/mip_data.g.dart @@ -0,0 +1,30 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'mip_data.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +MIpData _$MIpDataFromJson(Map json) => MIpData() + ..ret = json['ret'] as String? + ..data = json['data'] == null + ? null + : Mip.fromJson(json['data'] as Map); + +Map _$MIpDataToJson(MIpData instance) => { + 'ret': instance.ret, + 'data': instance.data?.toJson(), + }; + +Mip _$MipFromJson(Map json) => Mip( + ip: json['ip'] as String?, + location: (json['location'] as List?) + ?.map((e) => e as String) + .toList(), + ); + +Map _$MipToJson(Mip instance) => { + 'ip': instance.ip, + 'location': instance.location, + }; diff --git a/lib/data/vip_card.dart b/lib/data/vip_card.dart index 66f91830..c3ab13d2 100644 --- a/lib/data/vip_card.dart +++ b/lib/data/vip_card.dart @@ -68,11 +68,11 @@ class VipCard { VipCard(); + @JsonKey(includeToJson: false, includeFromJson: false) Color color = Color(0xFF32A060); factory VipCard.fromJson(Map json) => _$VipCardFromJson(json); Map toJson() => _$VipCardToJson(this); - } diff --git a/lib/main_page.dart b/lib/main_page.dart index 01b9163e..f6ebb67a 100644 --- a/lib/main_page.dart +++ b/lib/main_page.dart @@ -90,6 +90,8 @@ class _MainPage extends State with WidgetsBindingObserver { break; case AppLifecycleState.detached: // APP结束时调用 break; + case AppLifecycleState.hidden: + break; } } diff --git a/lib/retrofit/min_api.g.dart b/lib/retrofit/min_api.g.dart index f16677f8..1353de1c 100644 --- a/lib/retrofit/min_api.g.dart +++ b/lib/retrofit/min_api.g.dart @@ -14,7 +14,7 @@ class _MinApiService implements MinApiService { this.baseUrl, this.errorLogger, }) { - baseUrl ??= serviceMiniBaseUrl; + baseUrl ??= 'http://192.168.10.54:8765/app/'; } final Dio _dio; diff --git a/lib/retrofit/retrofit_api.dart b/lib/retrofit/retrofit_api.dart index bf80f9d4..0ddd21bc 100644 --- a/lib/retrofit/retrofit_api.dart +++ b/lib/retrofit/retrofit_api.dart @@ -81,7 +81,6 @@ abstract class ApiService { BuildContext? context, String? token, bool showLoading = false, - bool isIp = false, bool pay = true, ParseErrorLogger? errorLogger, }) { @@ -91,7 +90,6 @@ abstract class ApiService { } print("软件当前运行的模式 kReleaseMode:" + (kReleaseMode ? "发布模式" : "调试模式")); baseUrl = kReleaseMode ? serviceBaseUrl : localBaseUrl; - if (isIp) baseUrl = ipBaseUrl; dio.options = BaseOptions( connectTimeout: 30000.milliseconds, receiveTimeout: 30000.milliseconds, @@ -188,14 +186,12 @@ abstract class ApiService { @Part(name: "folderId") int folderId, bool isVideo); /// 周边搜索 - @GET( - "https://restapi.amap.com/v3/place/around?key=542b46afa8e4b88fe1eb3c4d0ba0872f&location={lat},{lng}&keywords={keywords}&offset={size}&page={page}&extensions=all") + @GET("https://restapi.amap.com/v3/place/around?key=542b46afa8e4b88fe1eb3c4d0ba0872f&location={lat},{lng}&keywords={keywords}&offset={size}&page={page}&extensions=all") Future searchPoi(@Path("lat") String lat, @Path("lng") String lng, @Path("keywords") String keywords, int size, int page); /// 逆地理编码 - @GET( - "https://restapi.amap.com/v3/geocode/regeo?key=542b46afa8e4b88fe1eb3c4d0ba0872f&location={lat},{lng}&extensions=base") + @GET("https://restapi.amap.com/v3/geocode/regeo?key=542b46afa8e4b88fe1eb3c4d0ba0872f&location={lat},{lng}&extensions=base") Future geocodeRegeo( @Path("lat") String lat, @Path("lng") String lng); @@ -612,12 +608,15 @@ abstract class ApiService { ///兑换积分门店列表 @GET("/store/storeListByCreditId/{creditGoodsId}") - Future> storeListByCreditId( - @Path("creditGoodsId") String creditGoodsId); + Future> storeListByCreditId(@Path("creditGoodsId") String creditGoodsId); + + ///获取APP门店推广渠道 + @GET("http://whois.pconline.com.cn/ipJson.jsp?json=true") + Future getIpInfo(); ///获取APP门店推广渠道 - @GET("/ipJson.jsp") - Future getIpInfo(); + @GET("https://myip.ipip.net/json") + Future getIpInfo1(); // Future getUrlFile(String url); diff --git a/lib/retrofit/retrofit_api.g.dart b/lib/retrofit/retrofit_api.g.dart index aec56b10..f87c5d58 100644 --- a/lib/retrofit/retrofit_api.g.dart +++ b/lib/retrofit/retrofit_api.g.dart @@ -14,7 +14,7 @@ class _ApiService implements ApiService { this.baseUrl, this.errorLogger, }) { - baseUrl ??= serviceBaseUrl; + baseUrl ??= 'http://192.168.10.54:8766/app/'; } final Dio _dio; @@ -3744,7 +3744,7 @@ class _ApiService implements ApiService { } @override - Future getIpInfo() async { + Future getIpInfo() async { final _extra = {}; final queryParameters = {}; final _headers = {}; @@ -3756,7 +3756,7 @@ class _ApiService implements ApiService { ) .compose( _dio.options, - '/ipJson.jsp', + 'http://whois.pconline.com.cn/ipJson.jsp?json=true', queryParameters: queryParameters, data: _data, ) @@ -3766,9 +3766,42 @@ class _ApiService implements ApiService { baseUrl, ))); final _result = await _dio.fetch(_options); - late String? _value; + late String _value; try { - _value = _result.data; + _value = _result.data!; + } on Object catch (e, s) { + errorLogger?.logError(e, s, _options); + rethrow; + } + return _value; + } + + @override + Future getIpInfo1() async { + final _extra = {}; + final queryParameters = {}; + final _headers = {}; + const Map? _data = null; + final _options = _setStreamType(Options( + method: 'GET', + headers: _headers, + extra: _extra, + ) + .compose( + _dio.options, + 'https://myip.ipip.net/json', + queryParameters: queryParameters, + data: _data, + ) + .copyWith( + baseUrl: _combineBaseUrls( + _dio.options.baseUrl, + baseUrl, + ))); + final _result = await _dio.fetch(_options); + late String _value; + try { + _value = _result.data!; } on Object catch (e, s) { errorLogger?.logError(e, s, _options); rethrow; diff --git a/lib/union/union_page.dart b/lib/union/union_page.dart index bb814a9d..0b6b75c0 100644 --- a/lib/union/union_page.dart +++ b/lib/union/union_page.dart @@ -1,5 +1,6 @@ import 'dart:convert'; import 'dart:io'; +import 'dart:typed_data'; import 'package:device_info_plus/device_info_plus.dart'; import 'package:dio/dio.dart'; @@ -10,6 +11,7 @@ import 'package:geolocator/geolocator.dart'; import 'package:get/utils.dart'; import 'package:huixiang/constant.dart'; import 'package:huixiang/data/ip_data.dart'; +import 'package:huixiang/data/mip_data.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/main.dart'; import 'package:huixiang/retrofit/retrofit_api.dart'; @@ -197,18 +199,14 @@ class UnionPageState extends State queryIpInfo() async { S.current.zhengzaijiazai.loading; try { - ApiService apiIpService = ApiService(Dio(), context: context, isIp: true); - String? baseData = await apiIpService.getIpInfo().catchError((onError) { - debugPrint("onError: ${onError}"); - return Future.value(null); + ApiService apiIpService = ApiService(Dio(), context: context); + String ipResult = await apiIpService.getIpInfo1().catchError((onError) { + return ""; }); - if (baseData?.isNotEmpty ?? false) { - debugPrint("baseData: ${baseData}"); - String ipDataStr = baseData! - .replaceAll("if(window.IPCallBack) {IPCallBack(", "") - .replaceAll(");}", ""); - IpData ipData = IpData.fromJson(jsonDecode(ipDataStr)); - areaName = ipData.city?.replaceAll("市", ""); + if (ipResult.isNotEmpty) { + String string = ipResult.replaceAll("\n", ""); + MIpData ipData = MIpData.fromJson(jsonDecode(string)); + areaName = ipData.data?.location?[2]; } } finally { getLocation(showLoading: false); @@ -218,8 +216,9 @@ class UnionPageState extends State loadFinish({bool showLoading = true}) { SmartDialog.dismiss(status: SmartStatus.loading); - if (showLoading) + if (showLoading) { S.current.zhengzaijiazai.loading; + } _allKey = [GlobalKey(), GlobalKey(), GlobalKey(), GlobalKey()]; setState(() {}); } diff --git a/lib/union/union_select_city.dart b/lib/union/union_select_city.dart index ba7a4b9d..38fb4318 100644 --- a/lib/union/union_select_city.dart +++ b/lib/union/union_select_city.dart @@ -3,12 +3,13 @@ import 'dart:convert'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'package:huixiang/data/ip_data.dart'; +import 'package:huixiang/data/mip_data.dart'; +import 'package:huixiang/retrofit/retrofit_api.dart'; import 'package:huixiang/utils/font_weight.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:pull_to_refresh/pull_to_refresh.dart'; -import '../data/ip_data.dart'; -import '../retrofit/retrofit_api.dart'; class UnionSelectCity extends StatefulWidget { final Map? arguments; @@ -23,7 +24,6 @@ class UnionSelectCity extends StatefulWidget { class _UnionSelectCity extends State { RefreshController refreshController = RefreshController(); late Map areaMap; - ApiService? apiIpService; String? ipName; int ipState = 0; List hotCity = []; @@ -60,23 +60,23 @@ class _UnionSelectCity extends State { } queryIpInfo() async { - apiIpService = ApiService( + ApiService? apiIpService = ApiService( Dio(), context: context, - isIp: true ); - String? baseData = await apiIpService?.getIpInfo().catchError((onError) {}); - if (baseData?.isNotEmpty ?? false) { - print("baseData: ${baseData}"); - String ipDataStr = baseData!.replaceAll("if(window.IPCallBack) {IPCallBack(", "").replaceAll(");}", ""); - IpData ipData = IpData.fromJson(jsonDecode(ipDataStr)); - ipName = ipData.city?.replaceAll("市", ""); + String ipResult = await apiIpService.getIpInfo1().catchError((onError) { + return ""; + }); + if (ipResult.isNotEmpty) { + String string = ipResult.replaceAll("\n", ""); + MIpData ipData = MIpData.fromJson(jsonDecode(string)); + ipName = ipData.data?.location?[2]; } - if(ipName != widget.arguments?["cityName"] ){ - ipState = 2; - setState((){}); - } + if (ipName != widget.arguments?["cityName"] && (ipName?.isNotEmpty ?? false)) { + ipState = 2; + setState((){}); } + } @override Widget build(BuildContext context) {