root_page.dart 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'package:amap_location/amap_location.dart';
  4. import 'package:amap_location/amap_location_option.dart';
  5. import 'package:bbyyy/beans/coupon_bean_entity.dart';
  6. import 'package:bbyyy/beans/user_bean_entity.dart';
  7. import 'package:bbyyy/https/MyDio.dart';
  8. import 'package:bbyyy/https/my_request.dart';
  9. import 'package:bbyyy/my_tools/dims.dart';
  10. import 'package:bbyyy/my_tools/easy_loading/easy_loading.dart';
  11. import 'package:bbyyy/my_tools/event_bus.dart';
  12. import 'package:bbyyy/my_tools/my_cookie.dart';
  13. import 'package:bbyyy/my_tools/my_datas.dart';
  14. import 'package:bbyyy/my_tools/my_tools.dart';
  15. import 'package:bbyyy/my_tools/pop_up_queue.dart';
  16. import 'package:bbyyy/nsq/nsq.dart' as Nsq;
  17. import 'package:bbyyy/paegs/gang_page/gang_page.dart';
  18. import 'package:bbyyy/paegs/home_page/home_page.dart';
  19. import 'package:bbyyy/paegs/mine_page/mine_page.dart';
  20. import 'package:bbyyy/paegs/msg_page/msg_page.dart';
  21. import 'package:bbyyy/paegs/root_page/root_page_view.dart';
  22. import 'package:flutter/material.dart';
  23. import 'package:flutter/services.dart';
  24. import 'package:permission_handler/permission_handler.dart';
  25. class RootPage extends StatefulWidget {
  26. @override
  27. _RootPageState createState() => _RootPageState();
  28. }
  29. class _RootPageState extends State<RootPage> {
  30. PageController pageController = PageController(initialPage: 0);
  31. DateTime lastPopTime;
  32. @override
  33. void initState() {
  34. super.initState();
  35. MyCookie().initP();
  36. getLocation();
  37. Nsq.ConnectServer(
  38. Nsq.handlerMessage, // 接收消息处理逻辑
  39. // serverIp: '172.16.104.14',
  40. serverIp: MyCookie().server,
  41. // 服务端返回
  42. serverPort: 4150,
  43. // 服务端返回
  44. topic: '${MyCookie().getUID()}',
  45. // 主题订阅,服务端返回
  46. channel: '${MyCookie().getUID()}',
  47. // 服务端返回uid
  48. clientId: '客户uid',
  49. // 服务端返回uid
  50. hostname: '主机名', // 服务端返回
  51. );
  52. EventBus().emit('检查粘贴板');
  53. EventBus().on('ChangePage', (arg) {
  54. if (RootPageView().bNIndex == 3) {
  55. queryPersonalInformation();
  56. }
  57. pageController.jumpToPage(RootPageView().bNIndex);
  58. setState(() {});
  59. });
  60. MyData().init(context);
  61. queryPersonalInformation();
  62. checkForUpdates(context);
  63. Future.delayed(Duration(seconds: 1), () {
  64. checkCouponEvent();
  65. });
  66. }
  67. @override
  68. void dispose() {
  69. // TODO: implement dispose
  70. EventBus().off('ChangePage');
  71. AMapLocationClient.stopLocation();
  72. super.dispose();
  73. }
  74. @override
  75. Widget build(BuildContext context) {
  76. return WillPopScope(
  77. onWillPop: () async {
  78. if (lastPopTime == null ||
  79. DateTime.now().difference(lastPopTime) > Duration(seconds: 2)) {
  80. lastPopTime = DateTime.now();
  81. EasyLoading.showToast('再按一次退出');
  82. } else {
  83. lastPopTime = DateTime.now();
  84. await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  85. }
  86. return false;
  87. },
  88. child: Scaffold(
  89. resizeToAvoidBottomInset: false,
  90. backgroundColor: Colors.transparent,
  91. body: Column(
  92. children: [
  93. Expanded(
  94. child: PageView(
  95. controller: pageController,
  96. children: [HomePage(), GangPage(), MsgPage(), MinePage()],
  97. physics: NeverScrollableScrollPhysics(),
  98. ),
  99. ),
  100. RootPageView().bottomNavigationBar(),
  101. ],
  102. ),
  103. ),
  104. );
  105. }
  106. void queryPersonalInformation() {
  107. MyDio().query({
  108. "key": "user",
  109. "filters": {
  110. "conditions": ["uid == ${MyCookie().getUID()}"]
  111. },
  112. "dims": userDims,
  113. "paging": [1, 20]
  114. }, (response, hasError) {
  115. if (!hasError) {
  116. UserBeanEntity entity =
  117. UserBeanEntity().fromJson(json.decode(response.data.toString()));
  118. MyCookie().userBean = entity.data.data[0];
  119. EventBus().emit('userChange');
  120. }
  121. }, (error) {});
  122. }
  123. Future<void> getLocation() async {
  124. var locationStatus = await Permission.location.status;
  125. var cameraStatus = await Permission.camera.status;
  126. print('Permission.location.status--------------$locationStatus');
  127. print('Permission.camera.status--------------$cameraStatus');
  128. if (locationStatus.isDenied) {
  129. await Permission.location.request().then((value) async {
  130. print('Permission.location.request()-----$value');
  131. if (value == PermissionStatus.granted) {
  132. await AMapLocationClient.startup(new AMapLocationOption(
  133. desiredAccuracy:
  134. CLLocationAccuracy.kCLLocationAccuracyHundredMeters));
  135. await AMapLocationClient.getLocation(true).then((value) async {
  136. if (Platform.isAndroid) {
  137. MyCookie().location = MyLocation(
  138. province: value.province,
  139. city: value.city,
  140. formattedAddress: value.formattedAddress,
  141. district: value.district,
  142. latitude: value.latitude,
  143. longitude: value.longitude);
  144. EventBus().emit('getLocation');
  145. } else if (Platform.isIOS) {
  146. getAddressByLatitudeAndLongitude(value.latitude, value.longitude);
  147. }
  148. });
  149. }
  150. });
  151. } else if (locationStatus.isGranted) {
  152. await AMapLocationClient.startup(new AMapLocationOption(
  153. desiredAccuracy:
  154. CLLocationAccuracy.kCLLocationAccuracyHundredMeters));
  155. await AMapLocationClient.getLocation(true).then((value) async {
  156. if (Platform.isAndroid) {
  157. MyCookie().location = MyLocation(
  158. province: value.province,
  159. city: value.city,
  160. formattedAddress: value.formattedAddress,
  161. district: value.district,
  162. latitude: value.latitude,
  163. longitude: value.longitude);
  164. if ( value.province!= '四川省' ||
  165. value.city!= '成都市') {
  166. EventBus().emit('getLocation');
  167. }
  168. } else if (Platform.isIOS) {
  169. getAddressByLatitudeAndLongitude(value.latitude, value.longitude);
  170. }
  171. });
  172. }
  173. if (cameraStatus.isDenied) {
  174. await Permission.camera.request().then((value) {
  175. print('Permission.camera.request()-----$value');
  176. });
  177. }
  178. }
  179. //查询优惠券活动
  180. checkCouponEvent() {
  181. MyDio().query({
  182. "key": "coupon_distribute",
  183. "filters": {
  184. "conditions": ["valid==true"],
  185. },
  186. "dims": couponDistributeDims,
  187. "paging": [1, 100]
  188. }, (response, hasError) {
  189. if (!hasError) {
  190. CouponBeanEntity entity =
  191. CouponBeanEntity().fromJson(json.decode(response.data.toString()));
  192. PopUpQueue().coupon.addAll(entity.data.data);
  193. }
  194. }, (error) {});
  195. }
  196. }