Created
May 31, 2021 12:48
-
-
Save iota9star/3a970a4084e5e582e392f0f72cefe8b9 to your computer and use it in GitHub Desktop.
简繁体转换
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'dart:io'; | |
void main(List<String> arguments) { | |
get('卡号登陆舰安理会的', func: TransType.ZH_HANT) | |
.then((value) => print(value)) | |
.catchError((e) => print(e)); | |
} | |
Future<R> get( | |
final String value, { | |
final TransType func = TransType.ZH_HANT, | |
}) async { | |
final url = 'https://tool.lu/zhconvert/ajax.html'; | |
final httpClient = HttpClient(); | |
final request = await httpClient.postUrl(Uri.parse(url)); | |
request.headers | |
.set(HttpHeaders.contentTypeHeader, 'application/x-www-form-urlencoded'); | |
request.headers.set(HttpHeaders.refererHeader, 'https://tool.lu/zhconvert/'); | |
request.headers.set(HttpHeaders.userAgentHeader, | |
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36'); | |
final encVal = Uri.encodeComponent(value); | |
request.add(utf8.encode('code=$encVal&operate=${func.fun}')); | |
final response = await request.close(); | |
if (response.statusCode == 200) { | |
final result = await response.transform(utf8.decoder).join(); | |
final retMap = jsonDecode(result); | |
if (retMap['status'] == true) { | |
return R.s(retMap['text']); | |
} else { | |
return R.e(retMap['message']); | |
} | |
} else { | |
return R.e('请求失败'); | |
} | |
} | |
class TransType { | |
final String fun; | |
final String desc; | |
const TransType._(this.fun, this.desc); | |
static const ZH_HANS = TransType._('zh-hans', '简体中文'); | |
static const ZH_HANT = TransType._('zh-hant', '繁體中文'); | |
static const ZH_CN = TransType._('zh-cn', '大陆简体'); | |
static const ZH_TW = TransType._('zh-tw', '台灣正體'); | |
static const ZH_HK = TransType._('zh-hk', '港澳繁體'); | |
} | |
class R { | |
final bool success; | |
final String msg; | |
final String data; | |
@override | |
String toString() { | |
return 'R{success: $success, msg: $msg, data: $data}'; | |
} | |
const R._(this.success, this.msg, this.data); | |
static R s(final String data) { | |
return R._(true, 'success', data); | |
} | |
static R e(final String msg) { | |
return R._(true, msg, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment