home_view.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:get/get.dart';
  4. import 'package:naiyouwl/app/component/button_select.dart';
  5. import 'package:naiyouwl/app/component/connection_status.dart';
  6. import 'package:naiyouwl/app/component/connection_widget.dart';
  7. import 'package:naiyouwl/app/component/sys_app_bar.dart';
  8. import 'package:naiyouwl/app/controller/controllers.dart';
  9. import 'package:naiyouwl/app/data/model/UserMode.dart';
  10. import 'package:url_launcher/url_launcher.dart';
  11. import '../../../routes/app_pages.dart';
  12. import '../../../utils/shell.dart';
  13. import '../controllers/home_controller.dart';
  14. import '../../connect/views/connect_view.dart';
  15. class HomeView extends GetView<HomeController> {
  16. const HomeView({Key? key}) : super(key: key);
  17. @override
  18. Widget build(BuildContext context) {
  19. return Container(
  20. decoration: const BoxDecoration(
  21. image: DecorationImage(
  22. image: AssetImage("assets/images/main/main.png"),
  23. fit: BoxFit.fill,
  24. ),
  25. ),
  26. child: Scaffold(
  27. backgroundColor: Colors.transparent,
  28. appBar: SysAppBar(title: const Text(""), actions: [
  29. Row(
  30. children: [
  31. IconButton(
  32. icon: const Icon(Icons.refresh),
  33. tooltip: '刷新节点',
  34. onPressed: () {
  35. controller.networkService.fetchNode();
  36. },
  37. ),
  38. IconButton(
  39. onPressed: () {
  40. controller.networkService.outlogin();
  41. },
  42. tooltip: '切换账号',
  43. icon: const Icon(Icons.exit_to_app)
  44. ),
  45. IconButton(
  46. onPressed: () {
  47. Get.toNamed(Routes.CONNECT);
  48. },
  49. tooltip: '连接详情',
  50. icon: const Icon(Icons.info_outline)
  51. )
  52. ],
  53. ),
  54. ],),
  55. body: Obx(() {
  56. if(controller.errAdminMsg.value){
  57. controller.errAdminMsg.value = false;
  58. Future.delayed(Duration.zero, () async {
  59. bool? result = await controllers.dialog.showNormalDialog(
  60. title: "提示",
  61. content: controller.errAdmin.value,
  62. cancelText: "取消",
  63. enterText: "确定",
  64. );
  65. if (result != null && result) {
  66. if(Platform.isWindows){
  67. await onRunAdmin();
  68. }
  69. }
  70. });
  71. }
  72. if (controllers.global.errorMsg.isNotEmpty) {
  73. String message = controllers.global.errorMsg.value;
  74. controllers.global.errorMsg.value = '';
  75. Future.delayed(Duration.zero, () async {
  76. bool? result = await controllers.dialog.showNormalDialog(
  77. title: "提示",
  78. content: message,
  79. cancelText: "取消",
  80. enterText: "确认",
  81. );
  82. if (result != null && result) {
  83. if(controllers.global.statusCode.value == 445){
  84. await launchUrl(Uri.parse(controller.UsersysConfig.value.userBuy ?? ""));
  85. }
  86. }
  87. });
  88. }
  89. return controller.isLoading.value
  90. ? const Center(child: CircularProgressIndicator())
  91. : SingleChildScrollView(
  92. child: Column(
  93. children: [
  94. UserStatusWidget(
  95. isActive: controller.GetEnable(),
  96. isLoading: controller.isLoading.value,
  97. username: controller.GetUserName(),
  98. expiryDate: controller.GetExpiredAt(),
  99. userTraffic: controller.GetTraffic(),
  100. onRefresh: () async {
  101. if (!controller.isLoading.value) {
  102. var userinfo = await controller.networkService.fetchUserinfo();
  103. if(userinfo != null )
  104. {
  105. controller.userMode.value = userinfo;
  106. }
  107. }
  108. },
  109. ),
  110. Padding(
  111. padding: const EdgeInsets.fromLTRB(20, 20, 0, 0),
  112. child: Row(
  113. children: controller.imageMap.entries.expand((entry) {
  114. return [
  115. GestureDetector(
  116. onTap: () => controller.onImageTap(entry.key),
  117. child: Image(
  118. image: AssetImage(entry.value),
  119. width: 60,
  120. height: 60,
  121. ),
  122. ),
  123. const SizedBox(width: 30),
  124. ];
  125. }).toList()..removeLast(),
  126. )
  127. ),
  128. const SizedBox(height: 35,),
  129. Padding(
  130. padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
  131. child: Row(
  132. children: [
  133. Text(controller.getHttp().toString()),
  134. const Spacer(),
  135. Text(controller.getSocket().toString()),
  136. ],
  137. ),
  138. ),
  139. ConnectionWidget(
  140. status: controller.connectStatus.value,
  141. onTap: () {
  142. controller.handleButtonClick();
  143. // if(controller.isConnect.value){
  144. // controller.connectionService.stopConnection();
  145. // } else {
  146. // controller.networkService.fetchAuthUser();
  147. // }
  148. },
  149. ),
  150. Align(
  151. alignment: Alignment.center,
  152. child: Text(controllers.global.msgStatus.value),
  153. ),
  154. const SizedBox(height: 20,),
  155. Padding(
  156. padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
  157. child: SizedBox(
  158. width: 200,
  159. height: 40,
  160. child: ElevatedButton(
  161. onPressed: () {
  162. controller.RouteNode();
  163. },
  164. style: ElevatedButton.styleFrom(
  165. foregroundColor: Colors.black,
  166. backgroundColor: Colors.white,
  167. shape: RoundedRectangleBorder(
  168. borderRadius: BorderRadius.circular(20),
  169. ),
  170. ),
  171. child: Text(controller.GetNode()),
  172. ),
  173. ),
  174. ),
  175. const SizedBox(height: 20,),
  176. Center(
  177. child: Wrap(
  178. spacing: 10,
  179. runSpacing: 10,
  180. alignment: WrapAlignment.center,
  181. children: [
  182. ButtonSelect(
  183. labels: ['setting_mode_rules'.tr,'setting_mode_global'.tr,],
  184. value: controllers.global.modes.indexOf(controllers.global.modesSelect.value),
  185. onSelect: (idx) => {
  186. controllers.global.updateMode(controllers.global.modes[idx])
  187. },
  188. ),
  189. Obx(() => ButtonSelect(
  190. labels: ['TUN关闭', 'TUN开启'],
  191. value: controllers.global.routeModesSelect.value == 'tun' ? 1 : 0,
  192. onSelect: (idx) async {
  193. final bool tunEnabled = idx == 1;
  194. try {
  195. if (tunEnabled && Platform.isMacOS) {
  196. final bool? result = await controllers.dialog.showNormalDialog(
  197. title: "提示",
  198. content: "开启TUN模式需要管理员权限,是否继续?",
  199. cancelText: "取消",
  200. enterText: "确定",
  201. );
  202. if (result != true) return;
  203. }
  204. await controllers.global.updateTunMode(tunEnabled);
  205. } catch (e) {
  206. // 操作失败时不改变状态
  207. return;
  208. }
  209. },
  210. )),
  211. if (Platform.isMacOS)
  212. SizedBox(
  213. height: 32,
  214. child: ElevatedButton(
  215. onPressed: () async {
  216. final result = await controllers.dialog.showInputDialog(
  217. title: "输入管理员密码",
  218. content: "请输入系统管理员密码,将安全保存用于TUN模式切换",
  219. isPassword: true,
  220. );
  221. if (result != null && result.isNotEmpty) {
  222. try {
  223. await saveKeychainPassword(result);
  224. controllers.dialog.showToast("密码已保存");
  225. } catch (e) {
  226. controllers.dialog.showToast("密码保存失败:$e");
  227. }
  228. }
  229. },
  230. child: const Text("保存管理员密码"),
  231. ),
  232. ),
  233. ],
  234. ),
  235. ),
  236. const SizedBox(height: 20,),
  237. Container(
  238. padding: const EdgeInsets.symmetric(vertical: 10),
  239. child: Column(
  240. children: [
  241. Text("版本号:${controllers.global.sysVersion.value}"),
  242. const SizedBox(height: 2),
  243. if (controllers.global.sysInfo.value.isNotEmpty)
  244. Text("当前:${controllers.global.sysInfo.value}"),
  245. ],
  246. ),
  247. ),
  248. ],
  249. ),
  250. );
  251. })
  252. ),
  253. );
  254. }
  255. }
  256. class UserStatusWidget extends StatelessWidget {
  257. final Future<void> Function() onRefresh;
  258. final bool isActive;
  259. final bool isLoading;
  260. final String username;
  261. final String userTraffic;
  262. final String expiryDate;
  263. const UserStatusWidget({
  264. Key? key,
  265. required this.isActive,
  266. required this.isLoading,
  267. required this.username,
  268. required this.userTraffic,
  269. required this.expiryDate,
  270. required this.onRefresh
  271. }) : super(key: key);
  272. @override
  273. Widget build(BuildContext context) {
  274. return Align(
  275. alignment: Alignment.topCenter,
  276. child: Padding(
  277. padding: const EdgeInsets.fromLTRB(5.0, 0, 5.0, 0.0),
  278. child: Row(
  279. children: [
  280. const SizedBox(width: 80, height: 20),
  281. Column(
  282. crossAxisAlignment: CrossAxisAlignment.start,
  283. children: [
  284. Row(
  285. children: [
  286. Container(
  287. width: 150,
  288. child: Text(
  289. username,
  290. overflow: TextOverflow.ellipsis,
  291. softWrap: false,
  292. maxLines: 1,
  293. ),
  294. ),
  295. const SizedBox(width: 2),
  296. IconButton(
  297. tooltip: '刷新账号状态',
  298. icon: isLoading
  299. ? const CircularProgressIndicator()
  300. : Image(image: AssetImage("assets/images/main/refresh.png"), width: 20, height: 20),
  301. onPressed: () {
  302. if (!isLoading) {
  303. onRefresh();
  304. }
  305. },
  306. )
  307. ],
  308. ),
  309. Text(
  310. expiryDate,
  311. style: const TextStyle(fontSize: 12.0),
  312. ),
  313. Text(
  314. userTraffic,
  315. style: const TextStyle(fontSize: 12.0),
  316. )
  317. ],
  318. ),
  319. ],
  320. ),
  321. ),
  322. );
  323. }
  324. }
  325. /*
  326. const SizedBox(width: 20),
  327. if (Platform.isMacOS)
  328. ElevatedButton(
  329. onPressed: () async {
  330. if (controllers.cc_service.installStatus.value) {
  331. if(!controller.isRunning)
  332. {
  333. await controller.connectionService.UninstallService();
  334. } else {
  335. controllers.global.errorMsg.value = "先停止连接,在尝试";
  336. }
  337. } else {
  338. if(!controller.isRunning)
  339. {
  340. await controller.connectionService.installService();
  341. } else {
  342. controllers.global.errorMsg.value = "先停止连接,在尝试";
  343. }
  344. }
  345. // await controller.connectionService.checkServiceInstallation();
  346. },
  347. child: Text(controllers.cc_service.installStatus.value ? '卸载服务' : '安装服务'),
  348. ),
  349. */