vars.go 3.8 KB

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