forked from lxm_tools/flutter-picgo
refactor:重构SM.MS请求
This commit is contained in:
@@ -44,7 +44,7 @@ class GithubApi {
|
|||||||
String configStr = await ImageUploadUtils.getPBConfig(PBTypeKeys.github);
|
String configStr = await ImageUploadUtils.getPBConfig(PBTypeKeys.github);
|
||||||
if (!isBlank(configStr)) {
|
if (!isBlank(configStr)) {
|
||||||
GithubConfig config = GithubConfig.fromJson(json.decode(configStr));
|
GithubConfig config = GithubConfig.fromJson(json.decode(configStr));
|
||||||
if (config != null && config.token != null && config.token != '') {
|
if (config != null && !isBlank(config.token)) {
|
||||||
return Options(headers: {"Authorization": 'Token ${config.token}'});
|
return Options(headers: {"Authorization": 'Token ${config.token}'});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,46 @@
|
|||||||
class SMMSApi {
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:dio/dio.dart';
|
||||||
|
import 'package:flutter_picgo/model/smms_config.dart';
|
||||||
|
import 'package:flutter_picgo/resources/pb_type_keys.dart';
|
||||||
|
import 'package:flutter_picgo/utils/image_upload.dart';
|
||||||
|
import 'package:flutter_picgo/utils/net.dart';
|
||||||
|
import 'package:flutter_picgo/utils/strings.dart';
|
||||||
|
|
||||||
|
class SMMSApi {
|
||||||
static const String BASE_URL = 'https://sm.ms/api/v2/';
|
static const String BASE_URL = 'https://sm.ms/api/v2/';
|
||||||
|
|
||||||
static const String GET_PROFILE = 'profile';
|
static Future getProfile() async {
|
||||||
|
var op = await oAuth();
|
||||||
|
Response res = await NetUtils.getInstance().post(BASE_URL + 'profile', options: op);
|
||||||
|
return res.data;
|
||||||
|
}
|
||||||
|
|
||||||
static const String UPLOAD = 'upload';
|
static Future upload(FormData formData) async {
|
||||||
|
var op = await oAuth();
|
||||||
|
Response res = await NetUtils.getInstance().post(BASE_URL + 'upload', data: formData, options: op);
|
||||||
|
return res.data;
|
||||||
|
}
|
||||||
|
|
||||||
static const String DELETE = 'delete';
|
static Future delete(String hash) async {
|
||||||
|
var op = await oAuth();
|
||||||
|
Response res = await NetUtils.getInstance().post(BASE_URL + 'delete/' + hash ?? '', options: op);
|
||||||
|
return res.data;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
/// 获取配置中的Token
|
||||||
|
static Future<Options> oAuth() async {
|
||||||
|
try {
|
||||||
|
String configStr = await ImageUploadUtils.getPBConfig(PBTypeKeys.smms);
|
||||||
|
if (!isBlank(configStr)) {
|
||||||
|
SMMSConfig config = SMMSConfig.fromJson(json.decode(configStr));
|
||||||
|
if (config != null && !isBlank(config.token)) {
|
||||||
|
return Options(headers: {"Authorization": '${config.token}'});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
} catch (e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,112 +0,0 @@
|
|||||||
import 'dart:async';
|
|
||||||
import 'dart:convert';
|
|
||||||
import 'package:dio/dio.dart';
|
|
||||||
import 'package:flutter_picgo/api/smms_api.dart';
|
|
||||||
import 'package:flutter_picgo/model/smms_config.dart';
|
|
||||||
import 'package:flutter_picgo/resources/pb_type_keys.dart';
|
|
||||||
import 'package:flutter_picgo/resources/table_name_keys.dart';
|
|
||||||
import 'package:flutter_picgo/utils/sql.dart';
|
|
||||||
|
|
||||||
const bool inProduction = const bool.fromEnvironment("dart.vm.product");
|
|
||||||
|
|
||||||
Map<String, dynamic> optHeader = {
|
|
||||||
// 'accept-language': 'zh-cn',
|
|
||||||
// 'content-type': 'application/json'
|
|
||||||
};
|
|
||||||
|
|
||||||
var dio = new Dio(BaseOptions(
|
|
||||||
connectTimeout: 30000,
|
|
||||||
receiveTimeout: 30000,
|
|
||||||
sendTimeout: 30000,
|
|
||||||
headers: optHeader,
|
|
||||||
baseUrl: SMMSApi.BASE_URL));
|
|
||||||
|
|
||||||
class SMMSNetUtils {
|
|
||||||
static Future get(String url, {Map<String, dynamic> params}) async {
|
|
||||||
// 拦截器
|
|
||||||
dio.interceptors.add(InterceptorsWrapper(
|
|
||||||
onRequest: (RequestOptions options) async {
|
|
||||||
var token = await oAuth();
|
|
||||||
options.headers["Authorization"] = '$token';
|
|
||||||
},
|
|
||||||
));
|
|
||||||
Response response;
|
|
||||||
if (params != null) {
|
|
||||||
response = await dio.get(url, queryParameters: params);
|
|
||||||
} else {
|
|
||||||
response = await dio.get(url);
|
|
||||||
}
|
|
||||||
// if (response.statusCode != 200) {
|
|
||||||
// dio.reject(response.data["message"] ?? '未知异常');
|
|
||||||
// }
|
|
||||||
return response.data;
|
|
||||||
}
|
|
||||||
|
|
||||||
static Future post(String url, Map<String, dynamic> data) async {
|
|
||||||
dio.interceptors.add(InterceptorsWrapper(
|
|
||||||
onRequest: (RequestOptions options) async {
|
|
||||||
var token = await oAuth();
|
|
||||||
options.headers["Authorization"] = '$token';
|
|
||||||
},
|
|
||||||
));
|
|
||||||
Response response = await dio.post(url, data: data);
|
|
||||||
return response.data;
|
|
||||||
}
|
|
||||||
|
|
||||||
static Future postForm(String url, FormData data) async {
|
|
||||||
dio.interceptors.add(InterceptorsWrapper(
|
|
||||||
onRequest: (RequestOptions options) async {
|
|
||||||
var token = await oAuth();
|
|
||||||
options.headers["Authorization"] = '$token';
|
|
||||||
},
|
|
||||||
));
|
|
||||||
Response response = await dio.post(url, data: data);
|
|
||||||
return response.data;
|
|
||||||
}
|
|
||||||
|
|
||||||
static Future put(String url, Map<String, dynamic> data) async {
|
|
||||||
dio.interceptors.add(InterceptorsWrapper(
|
|
||||||
onRequest: (RequestOptions options) async {
|
|
||||||
var token = await oAuth();
|
|
||||||
options.headers["Authorization"] = '$token';
|
|
||||||
},
|
|
||||||
));
|
|
||||||
if (!inProduction) {
|
|
||||||
dio.interceptors.add(LogInterceptor());
|
|
||||||
}
|
|
||||||
Response response = await dio.put(url, data: data);
|
|
||||||
return response.data;
|
|
||||||
}
|
|
||||||
|
|
||||||
static Future delete(String url, Map<String, dynamic> data) async {
|
|
||||||
dio.interceptors.add(InterceptorsWrapper(
|
|
||||||
onRequest: (RequestOptions options) async {
|
|
||||||
var token = await oAuth();
|
|
||||||
options.headers["Authorization"] = '$token';
|
|
||||||
},
|
|
||||||
));
|
|
||||||
if (!inProduction) {
|
|
||||||
dio.interceptors.add(LogInterceptor());
|
|
||||||
}
|
|
||||||
Response response = await dio.delete(url, data: data);
|
|
||||||
return response.data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 获取配置中的Token
|
|
||||||
static Future oAuth() async {
|
|
||||||
try {
|
|
||||||
var sql = Sql.setTable(TABLE_NAME_PBSETTING);
|
|
||||||
var pbsettingRow =
|
|
||||||
(await sql.getBySql('type = ?', [PBTypeKeys.smms]))?.first;
|
|
||||||
if (pbsettingRow != null &&
|
|
||||||
pbsettingRow["config"] != null &&
|
|
||||||
pbsettingRow["config"] != '') {
|
|
||||||
SMMSConfig config =
|
|
||||||
SMMSConfig.fromJson(json.decode(pbsettingRow["config"]));
|
|
||||||
if (config != null && config.token != null && config.token != '') {
|
|
||||||
return config.token;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e) {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -4,7 +4,6 @@ import 'package:flutter_picgo/api/smms_api.dart';
|
|||||||
import 'package:flutter_picgo/model/uploaded.dart';
|
import 'package:flutter_picgo/model/uploaded.dart';
|
||||||
import 'package:flutter_picgo/resources/pb_type_keys.dart';
|
import 'package:flutter_picgo/resources/pb_type_keys.dart';
|
||||||
import 'package:flutter_picgo/utils/image_upload.dart';
|
import 'package:flutter_picgo/utils/image_upload.dart';
|
||||||
import 'package:flutter_picgo/utils/smms_net.dart';
|
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'package:path/path.dart' as path;
|
import 'package:path/path.dart' as path;
|
||||||
import 'package:flutter_picgo/utils/strategy/image_upload_strategy.dart';
|
import 'package:flutter_picgo/utils/strategy/image_upload_strategy.dart';
|
||||||
@@ -16,7 +15,7 @@ class SMMSImageUpload implements ImageUploadStrategy {
|
|||||||
FormData formData = FormData.fromMap({
|
FormData formData = FormData.fromMap({
|
||||||
"smfile": await MultipartFile.fromFile(file.path, filename: renameImage),
|
"smfile": await MultipartFile.fromFile(file.path, filename: renameImage),
|
||||||
});
|
});
|
||||||
var result = await SMMSNetUtils.postForm(SMMSApi.UPLOAD, formData);
|
var result = await SMMSApi.upload(formData);
|
||||||
var resultmap = json.decode(result);
|
var resultmap = json.decode(result);
|
||||||
if (resultmap["success"]) {
|
if (resultmap["success"]) {
|
||||||
var uploaded = Uploaded(-1, resultmap["data"]["url"], PBTypeKeys.smms,
|
var uploaded = Uploaded(-1, resultmap["data"]["url"], PBTypeKeys.smms,
|
||||||
@@ -32,8 +31,7 @@ class SMMSImageUpload implements ImageUploadStrategy {
|
|||||||
Future<Uploaded> delete(Uploaded uploaded) async {
|
Future<Uploaded> delete(Uploaded uploaded) async {
|
||||||
String hash = await ImageUploadUtils.getUploadedItemInfo(uploaded.id);
|
String hash = await ImageUploadUtils.getUploadedItemInfo(uploaded.id);
|
||||||
if (!isBlank(hash)) {
|
if (!isBlank(hash)) {
|
||||||
String realUrl = path.joinAll([SMMSApi.DELETE, hash]);
|
var result = await SMMSApi.delete(hash);
|
||||||
var result = await SMMSNetUtils.get(realUrl);
|
|
||||||
var resultmap = json.decode(result);
|
var resultmap = json.decode(result);
|
||||||
if (resultmap["success"]) {
|
if (resultmap["success"]) {
|
||||||
return uploaded;
|
return uploaded;
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import 'package:flutter_picgo/api/smms_api.dart';
|
|||||||
import 'package:flutter_picgo/model/smms_config.dart';
|
import 'package:flutter_picgo/model/smms_config.dart';
|
||||||
import 'package:flutter_picgo/resources/pb_type_keys.dart';
|
import 'package:flutter_picgo/resources/pb_type_keys.dart';
|
||||||
import 'package:flutter_picgo/utils/image_upload.dart';
|
import 'package:flutter_picgo/utils/image_upload.dart';
|
||||||
import 'package:flutter_picgo/utils/smms_net.dart';
|
|
||||||
|
|
||||||
abstract class SMMSPageContract {
|
abstract class SMMSPageContract {
|
||||||
loadConfigSuccess(SMMSConfig config);
|
loadConfigSuccess(SMMSConfig config);
|
||||||
@@ -53,7 +52,7 @@ class SMMSPagePresenter {
|
|||||||
|
|
||||||
doTestConfig() async {
|
doTestConfig() async {
|
||||||
try {
|
try {
|
||||||
var result = json.decode((await SMMSNetUtils.post(SMMSApi.GET_PROFILE, null)));
|
var result = json.decode((await SMMSApi.getProfile()));
|
||||||
if (result["success"]) {
|
if (result["success"]) {
|
||||||
_view.testConfigSuccess(result["data"]["username"]);
|
_view.testConfigSuccess(result["data"]["username"]);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user