vars.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package dartgen
  2. import "text/template"
  3. var funcMap = template.FuncMap{
  4. "getBaseName": getBaseName,
  5. "getPropertyFromMember": getPropertyFromMember,
  6. "isDirectType": isDirectType,
  7. "isClassListType": isClassListType,
  8. "getCoreType": getCoreType,
  9. "pathToFuncName": pathToFuncName,
  10. "lowCamelCase": lowCamelCase,
  11. }
  12. const (
  13. apiFileContent = `import 'dart:io';
  14. import 'dart:convert';
  15. import '../vars/kv.dart';
  16. import '../vars/vars.dart';
  17. /// 发送POST请求.
  18. ///
  19. /// data:为你要post的结构体,我们会帮你转换成json字符串;
  20. /// ok函数:请求成功的时候调用,fail函数:请求失败的时候会调用,eventually函数:无论成功失败都会调用
  21. Future apiPost(String path, dynamic data,
  22. {Map<String, String> header,
  23. Function(Map<String, dynamic>) ok,
  24. Function(String) fail,
  25. Function eventually}) async {
  26. await _apiRequest('POST', path, data,
  27. header: header, ok: ok, fail: fail, eventually: eventually);
  28. }
  29. /// 发送GET请求.
  30. ///
  31. /// ok函数:请求成功的时候调用,fail函数:请求失败的时候会调用,eventually函数:无论成功失败都会调用
  32. Future apiGet(String path,
  33. {Map<String, String> header,
  34. Function(Map<String, dynamic>) ok,
  35. Function(String) fail,
  36. Function eventually}) async {
  37. await _apiRequest('GET', path, null,
  38. header: header, ok: ok, fail: fail, eventually: eventually);
  39. }
  40. Future _apiRequest(String method, String path, dynamic data,
  41. {Map<String, String> header,
  42. Function(Map<String, dynamic>) ok,
  43. Function(String) fail,
  44. Function eventually}) async {
  45. var tokens = await getTokens();
  46. try {
  47. var client = HttpClient();
  48. HttpClientRequest r;
  49. if (method == 'POST') {
  50. r = await client.postUrl(Uri.parse('https://' + serverHost + path));
  51. } else {
  52. r = await client.getUrl(Uri.parse('https://' + serverHost + path));
  53. }
  54. r.headers.set('Content-Type', 'application/json');
  55. if (tokens != null) {
  56. r.headers.set('Authorization', tokens.accessToken);
  57. }
  58. if (header != null) {
  59. header.forEach((k, v) {
  60. r.headers.set(k, v);
  61. });
  62. }
  63. var strData = '';
  64. if (data != null) {
  65. strData = jsonEncode(data);
  66. }
  67. r.write(strData);
  68. var rp = await r.close();
  69. var body = await rp.transform(utf8.decoder).join();
  70. print('${rp.statusCode} - $path');
  71. print('-- request --');
  72. print(strData);
  73. print('-- response --');
  74. print('$body \n');
  75. if (rp.statusCode == 404) {
  76. if (fail != null) fail('404 not found');
  77. } else {
  78. Map<String, dynamic> base = jsonDecode(body);
  79. if (rp.statusCode == 200) {
  80. if (base['code'] != 0) {
  81. if (fail != null) fail(base['desc']);
  82. } else {
  83. if (ok != null) ok(base['data']);
  84. }
  85. } else if (base['code'] != 0) {
  86. if (fail != null) fail(base['desc']);
  87. }
  88. }
  89. } catch (e) {
  90. if (fail != null) fail(e.toString());
  91. }
  92. if (eventually != null) eventually();
  93. }
  94. `
  95. apiFileContentV2 = `import 'dart:io';
  96. import 'dart:convert';
  97. import '../vars/kv.dart';
  98. import '../vars/vars.dart';
  99. /// send request with post method
  100. ///
  101. /// data: any request class that will be converted to json automatically
  102. /// ok: is called when request succeeds
  103. /// fail: is called when request fails
  104. /// eventually: is always called until the nearby functions returns
  105. Future apiPost(String path, dynamic data,
  106. {Map<String, String>? header,
  107. Function(Map<String, dynamic>)? ok,
  108. Function(String)? fail,
  109. Function? eventually}) async {
  110. await _apiRequest('POST', path, data,
  111. header: header, ok: ok, fail: fail, eventually: eventually);
  112. }
  113. /// send request with get method
  114. ///
  115. /// ok: is called when request succeeds
  116. /// fail: is called when request fails
  117. /// eventually: is always called until the nearby functions returns
  118. Future apiGet(String path,
  119. {Map<String, String>? header,
  120. Function(Map<String, dynamic>)? ok,
  121. Function(String)? fail,
  122. Function? eventually}) async {
  123. await _apiRequest('GET', path, null,
  124. header: header, ok: ok, fail: fail, eventually: eventually);
  125. }
  126. Future _apiRequest(String method, String path, dynamic data,
  127. {Map<String, String>? header,
  128. Function(Map<String, dynamic>)? ok,
  129. Function(String)? fail,
  130. Function? eventually}) async {
  131. var tokens = await getTokens();
  132. try {
  133. var client = HttpClient();
  134. HttpClientRequest r;
  135. if (method == 'POST') {
  136. r = await client.postUrl(Uri.parse('https://' + serverHost + path));
  137. } else {
  138. r = await client.getUrl(Uri.parse('https://' + serverHost + path));
  139. }
  140. r.headers.set('Content-Type', 'application/json');
  141. if (tokens != null) {
  142. r.headers.set('Authorization', tokens.accessToken);
  143. }
  144. if (header != null) {
  145. header.forEach((k, v) {
  146. r.headers.set(k, v);
  147. });
  148. }
  149. var strData = '';
  150. if (data != null) {
  151. strData = jsonEncode(data);
  152. }
  153. r.write(strData);
  154. var rp = await r.close();
  155. var body = await rp.transform(utf8.decoder).join();
  156. print('${rp.statusCode} - $path');
  157. print('-- request --');
  158. print(strData);
  159. print('-- response --');
  160. print('$body \n');
  161. if (rp.statusCode == 404) {
  162. if (fail != null) fail('404 not found');
  163. } else {
  164. Map<String, dynamic> base = jsonDecode(body);
  165. if (rp.statusCode == 200) {
  166. if (base['code'] != 0) {
  167. if (fail != null) fail(base['desc']);
  168. } else {
  169. if (ok != null) ok(base['data']);
  170. }
  171. } else if (base['code'] != 0) {
  172. if (fail != null) fail(base['desc']);
  173. }
  174. }
  175. } catch (e) {
  176. if (fail != null) fail(e.toString());
  177. }
  178. if (eventually != null) eventually();
  179. }`
  180. tokensFileContent = `class Tokens {
  181. /// 用于访问的token, 每次请求都必须带在Header里面
  182. final String accessToken;
  183. final int accessExpire;
  184. /// 用于刷新token
  185. final String refreshToken;
  186. final int refreshExpire;
  187. final int refreshAfter;
  188. Tokens(
  189. {this.accessToken,
  190. this.accessExpire,
  191. this.refreshToken,
  192. this.refreshExpire,
  193. this.refreshAfter});
  194. factory Tokens.fromJson(Map<String, dynamic> m) {
  195. return Tokens(
  196. accessToken: m['access_token'],
  197. accessExpire: m['access_expire'],
  198. refreshToken: m['refresh_token'],
  199. refreshExpire: m['refresh_expire'],
  200. refreshAfter: m['refresh_after']);
  201. }
  202. Map<String, dynamic> toJson() {
  203. return {
  204. 'access_token': accessToken,
  205. 'access_expire': accessExpire,
  206. 'refresh_token': refreshToken,
  207. 'refresh_expire': refreshExpire,
  208. 'refresh_after': refreshAfter,
  209. };
  210. }
  211. }
  212. `
  213. tokensFileContentV2 = `class Tokens {
  214. /// 用于访问的token, 每次请求都必须带在Header里面
  215. final String accessToken;
  216. final int accessExpire;
  217. /// 用于刷新token
  218. final String refreshToken;
  219. final int refreshExpire;
  220. final int refreshAfter;
  221. Tokens({
  222. required this.accessToken,
  223. required this.accessExpire,
  224. required this.refreshToken,
  225. required this.refreshExpire,
  226. required this.refreshAfter
  227. });
  228. factory Tokens.fromJson(Map<String, dynamic> m) {
  229. return Tokens(
  230. accessToken: m['access_token'],
  231. accessExpire: m['access_expire'],
  232. refreshToken: m['refresh_token'],
  233. refreshExpire: m['refresh_expire'],
  234. refreshAfter: m['refresh_after']);
  235. }
  236. Map<String, dynamic> toJson() {
  237. return {
  238. 'access_token': accessToken,
  239. 'access_expire': accessExpire,
  240. 'refresh_token': refreshToken,
  241. 'refresh_expire': refreshExpire,
  242. 'refresh_after': refreshAfter,
  243. };
  244. }
  245. }
  246. `
  247. )