基础库
 
 
 
 
 

233 lines
7.9 KiB

  1. import 'package:dio/dio.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_alibc/flutter_alibc.dart';
  4. import 'package:jdsdk/jdsdk.dart';
  5. import 'package:url_launcher/url_launcher.dart';
  6. import 'package:zhiying_comm/zhiying_comm.dart';
  7. void main() => runApp(MyApp());
  8. class MyApp extends StatefulWidget {
  9. @override
  10. _MyAppState createState() => _MyAppState();
  11. }
  12. class _MyAppState extends State<MyApp> {
  13. @override
  14. void initState() {
  15. Jdsdk.init(appKey: '9fc3dec00b9b40cc950dfba5262cd6d3',
  16. appSecret: 'f785613e5fd54a129d0f0359a4e25c23').then((result) {
  17. Logger.debug('京东初始化:${result.toString()}');
  18. });
  19. FlutterAlibc.initAlibc(version: "", appName: "").then((result) {
  20. Logger.debug('${result.errorCode} ${result.errorMessage}');
  21. });
  22. super.initState();
  23. }
  24. @override
  25. Widget build(BuildContext context) {
  26. return MaterialApp(
  27. home: Scaffold(
  28. appBar: AppBar(
  29. title: const Text('智莺-基础库'),
  30. ),
  31. body: HomePage(),
  32. ),
  33. );
  34. }
  35. }
  36. class HomePage extends StatelessWidget {
  37. netPost() async {
  38. dynamic result =
  39. await NetUtil.post('/api/v1/rec/featured?page=1', params: null);
  40. print("result === ${result?.toString()}");
  41. }
  42. @override
  43. Widget build(BuildContext context) {
  44. return SingleChildScrollView(
  45. child: Center(
  46. child: Wrap(
  47. spacing: 10,
  48. children: <Widget>[
  49. RaisedButton(
  50. onPressed: () {
  51. Navigator.push(context, MaterialPageRoute(builder: (_) {
  52. // return DeviceInfoPage();
  53. }));
  54. },
  55. child: Text('设备信息'),
  56. ),
  57. RaisedButton(
  58. onPressed: () {
  59. Navigator.push(context, MaterialPageRoute(builder: (_) {
  60. // return PackageInfoPage();
  61. }));
  62. },
  63. child: Text('应用信息'),
  64. ),
  65. RaisedButton(
  66. onPressed: () {
  67. NetUtil.post('/siteapi/v1/ucenter/login/', params: {
  68. 'username': 'xiangguohui',
  69. 'password': 'fnuo123com'
  70. });
  71. },
  72. child: Text('登录请求'),
  73. ),
  74. RaisedButton(
  75. onPressed: () {
  76. NetUtil.request('/api/v1/rec/featured?page=1', params: null,
  77. onError: (msg) {
  78. print('onERROR = ${msg?.toString() ?? 'onError'}');
  79. }, onSuccess: (json) {
  80. print('onSuccess = ${json?.toString() ?? 'onSuccess'}');
  81. }, onCache: (json) {
  82. print('onCache = ${json?.toString() ?? 'onCache'}');
  83. });
  84. },
  85. child: Text('网络异步请求(带缓存)'),
  86. ),
  87. RaisedButton(
  88. onPressed: () {
  89. netPost();
  90. },
  91. child: Text('网络同步请求(无缓存)'),
  92. ),
  93. RaisedButton(
  94. onPressed: () {
  95. // LogUtil.test();
  96. },
  97. child: Text('显示日志'),
  98. ),
  99. RaisedButton(
  100. onPressed: () {
  101. Navigator.push(context, MaterialPageRoute(builder: (_) {
  102. return Logger();
  103. }));
  104. },
  105. child: Text('打开日志视图'),
  106. ),
  107. RaisedButton(
  108. onPressed: () {
  109. // NetUtil.request('/api/v1/mod', params: {'ids': [6] } ,method: NetMethod.POST,
  110. // onSuccess: (params){
  111. // Logger.log("onSuccess#$params");
  112. // },
  113. // onCache: (params){
  114. // Logger.log("onCache#$params");
  115. // });
  116. testPost();
  117. },
  118. child: Text('测试接口'),
  119. ),
  120. RaisedButton(
  121. onPressed: () async {
  122. Api api = Api('/api/v1/user/info', method: NetMethod.GET);
  123. // api.onCache().then((cache) {
  124. // print('读取缓存 ${cache.toString()}');
  125. // return api.onRequest();
  126. // }).then((result) {
  127. // print('网络请求 ${result.toString().length}');
  128. // }).catchError((error) {
  129. // print('aaaa');
  130. // print('error: ${error.toString()}');
  131. // });
  132. var cache = await api.onCache();
  133. var result = await api.onRequest();
  134. },
  135. child: Text('新网络请求'),
  136. ),
  137. RaisedButton(
  138. onPressed: () {
  139. Navigator.push(context, MaterialPageRoute(builder: (_) {
  140. return TaobaoImageLoader(
  141. 'https://h5.m.taobao.com/app/detail/desc.html?_isH5Des=true#!id=592787622422&type=1&f=&sellerType=B',
  142. onImagesLoad: (images) {
  143. Logger.debug(images.toString());
  144. },
  145. );
  146. }));
  147. // TaobaoUtil.getImage();
  148. },
  149. child: Text('抓淘宝图片'),
  150. ),
  151. RaisedButton(
  152. onPressed: () {
  153. TaobaoAuth.auth(context);
  154. },
  155. child: Text('淘宝授权'),
  156. ),
  157. RaisedButton(
  158. onPressed: () {
  159. //
  160. Dio dio = Dio();
  161. dio.get(
  162. 'http://www.hairuyi.com/?mod=appapi&act=gotojingdong&gid=60291609161&yhq_url=http%3A%2F%2Fcoupon.m.jd.com%2Fcoupons%2Fshow.action%3Fkey%3Dd97e1472a8a24c39a9463dbe72b3fa32%26roleId%3D38088450%26to%3Ditem.jd.com%2F60291609161.html')
  163. .then((value) {
  164. Logger.debug(value.realUri.toString());
  165. });
  166. },
  167. child: Text('获取重定向地址'),
  168. ),
  169. RaisedButton(
  170. onPressed: () {
  171. Jdsdk.openUrl(
  172. url: 'http://www.hairuyi.com/?mod=appapi&act=gotojingdong&gid=60291609161&yhq_url=http%3A%2F%2Fcoupon.m.jd.com%2Fcoupons%2Fshow.action%3Fkey%3Dd97e1472a8a24c39a9463dbe72b3fa32%26roleId%3D38088450%26to%3Ditem.jd.com%2F60291609161.html');
  173. },
  174. child: Text('打开京东详情'),
  175. ),
  176. RaisedButton(
  177. onPressed: () async {
  178. String detailUrl = 'http://www.hairuyi.com/?mod=appapi&act=gotojingdong&gid=60291609161&yhq_url=http%3A%2F%2Fcoupon.m.jd.com%2Fcoupons%2Fshow.action%3Fkey%3Dd97e1472a8a24c39a9463dbe72b3fa32%26roleId%3D38088450%26to%3Ditem.jd.com%2F60291609161.html';
  179. String baseUrl = detailUrl.getBaseUrl();
  180. if (!baseUrl.contains('jd.com')) {
  181. Dio dio = Dio();
  182. var responds = await dio.get(detailUrl);
  183. detailUrl = responds.realUri.toString();
  184. }
  185. Logger.debug(detailUrl);
  186. Jdsdk.openUrl(
  187. url
  188. :
  189. detailUrl
  190. );
  191. },
  192. child: Text('嗨如意转链打开京东'),
  193. ),
  194. RaisedButton(
  195. onPressed: () async {
  196. const url = 'weixin://';
  197. if (await canLaunch(url)) {
  198. await launch(url);
  199. } else {
  200. throw 'Could not launch $url';
  201. }
  202. },
  203. child: Text('url scheme打开app'),
  204. ),
  205. ],
  206. ),
  207. ),
  208. );
  209. }
  210. void testPost() async {
  211. var cached = await NetUtil.getRequestCachedData('/api/v1/mod', params: {
  212. 'ids': [7]
  213. });
  214. print("cahced ${cached?.toString()}");
  215. var param = await NetUtil.post('/api/v1/mod',
  216. params: {
  217. 'ids': [7]
  218. },
  219. method: NetMethod.POST);
  220. print('apapapsdjfdsjf: ${param?.toString()}');
  221. }
  222. }