Skip to content

Commit

Permalink
手机版请求重写增加重定向
Browse files Browse the repository at this point in the history
  • Loading branch information
wanghongenpin committed Sep 7, 2023
1 parent b8525e7 commit 36e32d2
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 51 deletions.
2 changes: 1 addition & 1 deletion lib/network/util/request_rewrite.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class RequestRewriteRule {
factory RequestRewriteRule.formJson(Map<String, dynamic> map) {
return RequestRewriteRule(map['enabled'] == true, map['path'], map['domain'],
name: map['name'],
type: RuleType.fromName(map['type']),
type: map['type'] == null ? RuleType.body : RuleType.fromName(map['type']),
queryParam: map['queryParam'],
requestBody: map['requestBody'],
responseBody: map['responseBody'],
Expand Down
140 changes: 93 additions & 47 deletions lib/ui/mobile/setting/request_rewrite.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,39 @@ class _MobileRequestRewriteState extends State<MobileRequestRewrite> {
}

///请求重写规则添加对话框
class RewriteRule extends StatelessWidget {
class RewriteRule extends StatefulWidget {
final int currentIndex;
final RequestRewriteRule? rule;

const RewriteRule({super.key, required this.rule, this.currentIndex = -1});

@override
State<StatefulWidget> createState() {
return _RewriteRuleState();
}
}

class _RewriteRuleState extends State<RewriteRule> {
late ValueNotifier<bool> enableNotifier;
late RequestRewriteRule rule;

@override
void initState() {
super.initState();
rule = widget.rule ?? RequestRewriteRule(true, "", null);
enableNotifier = ValueNotifier(rule.enabled == true);
}

@override
void dispose() {
enableNotifier.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
GlobalKey formKey = GlobalKey<FormState>();

ValueNotifier<bool> enableNotifier = ValueNotifier(rule == null || rule!.enabled);
String? domain = rule?.domain;
String? path = rule?.path;
String? requestBody = rule?.requestBody;
String? responseBody = rule?.responseBody;

return Scaffold(
appBar: AppBar(
title: const Text("请求重写规则", style: TextStyle(fontSize: 16)),
Expand All @@ -136,19 +153,15 @@ class RewriteRule extends StatelessWidget {
onPressed: () {
if ((formKey.currentState as FormState).validate()) {
(formKey.currentState as FormState).save();
enableNotifier.dispose();
var requestRewriteRule = RequestRewriteRule(
enableNotifier.value, path!, domain?.trim().isEmpty == true ? null : domain?.trim(),
requestBody: requestBody, responseBody: responseBody);

if (currentIndex >= 0) {
RequestRewrites.instance.rules[currentIndex] = requestRewriteRule;
rule.updatePathReg();
if (widget.currentIndex >= 0) {
RequestRewrites.instance.rules[widget.currentIndex] = rule;
} else {
RequestRewrites.instance.addRule(requestRewriteRule);
RequestRewrites.instance.addRule(rule);
}

FlutterToastr.show("添加请求重写规则成功", context);
Navigator.of(context).pop(requestRewriteRule);
Navigator.of(context).pop(rule);
}
})
],
Expand All @@ -167,10 +180,15 @@ class RewriteRule extends StatelessWidget {
value: enable,
onChanged: (value) => enableNotifier.value = value);
}),
TextFormField(
decoration: const InputDecoration(labelText: '名称'),
initialValue: rule.name,
onSaved: (val) => rule.name = val,
),
TextFormField(
decoration: const InputDecoration(labelText: '域名(可选)', hintText: 'baidu.com 不需要填写HTTP'),
initialValue: domain,
onSaved: (val) => domain = val),
initialValue: rule.domain,
onSaved: (val) => rule.domain = val?.trim()),
TextFormField(
decoration: const InputDecoration(labelText: 'Path', hintText: '/api/v1/*'),
validator: (val) {
Expand All @@ -179,27 +197,64 @@ class RewriteRule extends StatelessWidget {
}
return null;
},
minLines: 1,
maxLines: 3,
initialValue: path,
onSaved: (val) => path = val),
TextFormField(
initialValue: requestBody,
decoration: const InputDecoration(labelText: '请求体替换为:'),
minLines: 1,
maxLines: 10,
onSaved: (val) => requestBody = val),
TextFormField(
initialValue: responseBody,
minLines: 3,
maxLines: 15,
decoration: const InputDecoration(labelText: '响应体替换为:', hintText: '{"code":"200","data":{}}'),
onSaved: (val) => responseBody = val)
initialValue: rule.path,
onSaved: (val) => rule.path = val!.trim()),
DropdownButtonFormField<RuleType>(
decoration: const InputDecoration(labelText: '行为'),
value: rule.type,
items: RuleType.values
.map((e) =>
DropdownMenuItem(value: e, child: Text(e.name, style: const TextStyle(fontSize: 14))))
.toList(),
onChanged: (val) {
setState(() {
rule.type = val!;
});
}),
...rewriteWidgets()
]))),
);
}

List<Widget> rewriteWidgets() {
if (rule.type == RuleType.redirect) {
return [
TextFormField(
decoration: const InputDecoration(labelText: '重定向到:', hintText: 'http://www.example.com/api'),
initialValue: rule.redirectUrl,
onSaved: (val) => rule.redirectUrl = val,
validator: (val) {
if (val == null || val.trim().isEmpty) {
return '重定向URL不能为空';
}
return null;
}),
];
}

return [
TextFormField(
initialValue: rule.queryParam,
decoration: const InputDecoration(labelText: 'URL参数替换为:'),
maxLines: 1,
onSaved: (val) => rule.queryParam = val),
TextFormField(
initialValue: rule.requestBody,
decoration: const InputDecoration(labelText: '请求体替换为:'),
minLines: 1,
maxLines: 10,
onSaved: (val) => rule.requestBody = val),
TextFormField(
initialValue: rule.responseBody,
minLines: 3,
maxLines: 15,
decoration: const InputDecoration(labelText: '响应体替换为:', hintText: '{"code":"200","data":{}}'),
onSaved: (val) => rule.responseBody = val)
];
}
}

///请求重写规则列表
class RequestRuleList extends StatefulWidget {
final RequestRewrites requestRewrites;

Expand Down Expand Up @@ -243,31 +298,22 @@ class _RequestRuleListState extends State<RequestRuleList> {
dataRowMaxHeight: 100,
border: TableBorder.symmetric(outside: BorderSide(width: 1, color: Theme.of(context).highlightColor)),
columns: const <DataColumn>[
DataColumn(label: Text('名称')),
DataColumn(label: Text('启用')),
DataColumn(label: Text('URL')),
DataColumn(label: Text('请求体')),
DataColumn(label: Text('响应体')),
DataColumn(label: Text('行为')),
],
rows: List.generate(
widget.requestRewrites.rules.length,
(index) => DataRow(
cells: [
cell(Text(widget.requestRewrites.rules[index].name ?? "")),
cell(Text(widget.requestRewrites.rules[index].enabled ? "是" : "否")),
cell(ConstrainedBox(
constraints: const BoxConstraints(minWidth: 60, maxWidth: 150),
child: Text(
'${widget.requestRewrites.rules[index].domain ?? ''}${widget.requestRewrites.rules[index].path}'))),
cell(Container(
constraints: const BoxConstraints(maxWidth: 150),
child: SelectableText.rich(
TextSpan(text: widget.requestRewrites.rules[index].requestBody),
style: const TextStyle(fontSize: 12)))),
cell(Container(
constraints: const BoxConstraints(maxWidth: 200),
padding: const EdgeInsetsDirectional.all(10),
child: SelectableText.rich(TextSpan(text: widget.requestRewrites.rules[index].responseBody),
style: const TextStyle(fontSize: 12)),
))
cell(Text(widget.requestRewrites.rules[index].type.name)),
],
selected: selected == index,
onSelectChanged: (value) {
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,10 @@ packages:
dependency: "direct main"
description:
name: logger
sha256: "66cb048220ca51cf9011da69fa581e4ee2bed4be6e82870d9e9baae75739da49"
sha256: ba3bc83117b2b49bdd723c0ea7848e8285a0fbc597ba09203b20d329d020c24a
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.1"
version: "2.0.2"
logging:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: network_proxy
description: network proxy
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 1.0.2+3
version: 1.0.2+5

environment:
sdk: '>=3.0.2 <4.0.0'
Expand Down

0 comments on commit 36e32d2

Please sign in to comment.