vars.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package dartgen
  2. import "text/template"
  3. var funcMap = template.FuncMap{
  4. "tagGet": tagGet,
  5. "isDirectType": isDirectType,
  6. "isClassListType": isClassListType,
  7. "getCoreType": getCoreType,
  8. "pathToFuncName": pathToFuncName,
  9. "lowCamelCase": lowCamelCase,
  10. }
  11. const apiFileContent = `import 'dart:io';
  12. import 'dart:convert';
  13. import '../vars/kv.dart';
  14. import '../vars/vars.dart';
  15. /// 发送POST请求.
  16. ///
  17. /// data:为你要post的结构体,我们会帮你转换成json字符串;
  18. /// ok函数:请求成功的时候调用,fail函数:请求失败的时候会调用,eventually函数:无论成功失败都会调用
  19. Future apiPost(String path, dynamic data,
  20. {Map<String, String> header,
  21. Function(Map<String, dynamic>) ok,
  22. Function(String) fail,
  23. Function eventually}) async {
  24. await _apiRequest('POST', path, data,
  25. header: header, ok: ok, fail: fail, eventually: eventually);
  26. }
  27. /// 发送GET请求.
  28. ///
  29. /// ok函数:请求成功的时候调用,fail函数:请求失败的时候会调用,eventually函数:无论成功失败都会调用
  30. Future apiGet(String path,
  31. {Map<String, String> header,
  32. Function(Map<String, dynamic>) ok,
  33. Function(String) fail,
  34. Function eventually}) async {
  35. await _apiRequest('GET', path, null,
  36. header: header, ok: ok, fail: fail, eventually: eventually);
  37. }
  38. Future _apiRequest(String method, String path, dynamic data,
  39. {Map<String, String> header,
  40. Function(Map<String, dynamic>) ok,
  41. Function(String) fail,
  42. Function eventually}) async {
  43. var tokens = await getTokens();
  44. try {
  45. var client = HttpClient();
  46. HttpClientRequest r;
  47. if (method == 'POST') {
  48. r = await client.postUrl(Uri.parse('https://' + serverHost + path));
  49. } else {
  50. r = await client.getUrl(Uri.parse('https://' + serverHost + path));
  51. }
  52. r.headers.set('Content-Type', 'application/json');
  53. if (tokens != null) {
  54. r.headers.set('Authorization', tokens.accessToken);
  55. }
  56. if (header != null) {
  57. header.forEach((k, v) {
  58. r.headers.set(k, v);
  59. });
  60. }
  61. var strData = '';
  62. if (data != null) {
  63. strData = jsonEncode(data);
  64. }
  65. r.write(strData);
  66. var rp = await r.close();
  67. var body = await rp.transform(utf8.decoder).join();
  68. print('${rp.statusCode} - $path');
  69. print('-- request --');
  70. print(strData);
  71. print('-- response --');
  72. print('$body \n');
  73. if (rp.statusCode == 404) {
  74. if (fail != null) fail('404 not found');
  75. } else {
  76. Map<String, dynamic> base = jsonDecode(body);
  77. if (rp.statusCode == 200) {
  78. if (base['code'] != 0) {
  79. if (fail != null) fail(base['desc']);
  80. } else {
  81. if (ok != null) ok(base['data']);
  82. }
  83. } else if (base['code'] != 0) {
  84. if (fail != null) fail(base['desc']);
  85. }
  86. }
  87. } catch (e) {
  88. if (fail != null) fail(e.toString());
  89. }
  90. if (eventually != null) eventually();
  91. }
  92. `
  93. const tokensFileContent = `class Tokens {
  94. /// 用于访问的token, 每次请求都必须带在Header里面
  95. final String accessToken;
  96. final int accessExpire;
  97. /// 用于刷新token
  98. final String refreshToken;
  99. final int refreshExpire;
  100. final int refreshAfter;
  101. Tokens(
  102. {this.accessToken,
  103. this.accessExpire,
  104. this.refreshToken,
  105. this.refreshExpire,
  106. this.refreshAfter});
  107. factory Tokens.fromJson(Map<String, dynamic> m) {
  108. return Tokens(
  109. accessToken: m['access_token'],
  110. accessExpire: m['access_expire'],
  111. refreshToken: m['refresh_token'],
  112. refreshExpire: m['refresh_expire'],
  113. refreshAfter: m['refresh_after']);
  114. }
  115. Map<String, dynamic> toJson() {
  116. return {
  117. 'access_token': accessToken,
  118. 'access_expire': accessExpire,
  119. 'refresh_token': refreshToken,
  120. 'refresh_expire': refreshExpire,
  121. 'refresh_after': refreshAfter,
  122. };
  123. }
  124. }
  125. `