forked from lxm_tools/flutter-picgo
feature:完成SM.MS图床配置页面
This commit is contained in:
7
lib/api/smms_api.dart
Normal file
7
lib/api/smms_api.dart
Normal file
@@ -0,0 +1,7 @@
|
||||
class SMMSApi {
|
||||
|
||||
static const String BASE_URL = 'https://sm.ms/api/v2/';
|
||||
|
||||
static const String GET_PROFILE = 'profile';
|
||||
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter_picgo/model/uploaded.dart';
|
||||
import 'package:flutter_picgo/resources/table_name_keys.dart';
|
||||
import 'package:flutter_picgo/utils/shared_preferences.dart';
|
||||
|
||||
101
lib/utils/smms_net.dart
Normal file
101
lib/utils/smms_net.dart
Normal file
@@ -0,0 +1,101 @@
|
||||
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 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) {}
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,6 @@ class GithubPagePresenter {
|
||||
doLoadConfig() async {
|
||||
try {
|
||||
var configStr = await ImageUpload.getPBConfig(PBTypeKeys.github);
|
||||
debugPrint(configStr);
|
||||
if (!isBlank(configStr)) {
|
||||
GithubConfig config = GithubConfig.fromJson(json.decode(configStr));
|
||||
_view.loadConfig(config);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_picgo/components/loading.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';
|
||||
@@ -11,7 +12,6 @@ class SMMSPage extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _SMMSPageState extends State<SMMSPage> implements SMMSPageContract {
|
||||
|
||||
TextEditingController _tokenController;
|
||||
|
||||
SMMSConfig _config;
|
||||
@@ -36,6 +36,15 @@ class _SMMSPageState extends State<SMMSPage> implements SMMSPageContract {
|
||||
appBar: AppBar(
|
||||
title: Text('SM.MS图床'),
|
||||
centerTitle: true,
|
||||
actions: <Widget>[
|
||||
IconButton(
|
||||
//连接测试
|
||||
icon: Icon(IconData(0xe62a, fontFamily: 'iconfont')),
|
||||
onPressed: () {
|
||||
_testConfig();
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Padding(
|
||||
padding: EdgeInsets.all(10.0),
|
||||
@@ -118,6 +127,18 @@ class _SMMSPageState extends State<SMMSPage> implements SMMSPageContract {
|
||||
}
|
||||
}
|
||||
|
||||
_testConfig() {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
return NetLoadingDialog(
|
||||
loading: true,
|
||||
requestCallBack: _presenter.doTestConfig(),
|
||||
loadingText: "测试连接中...",
|
||||
outsideDismiss: false);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
loadConfigFail(String msg) {
|
||||
Toast.show(msg, context);
|
||||
@@ -139,4 +160,14 @@ class _SMMSPageState extends State<SMMSPage> implements SMMSPageContract {
|
||||
saveConfigSuccess() {
|
||||
Toast.show('保存成功', context);
|
||||
}
|
||||
|
||||
@override
|
||||
testConfigFail(String msg) {
|
||||
Toast.show(msg, context);
|
||||
}
|
||||
|
||||
@override
|
||||
testConfigSuccess(String username) {
|
||||
Toast.show('测试连接成功,您的SM.MS用户名为$username', context);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/cupertino.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/utils/image_upload.dart';
|
||||
import 'package:flutter_picgo/utils/smms_net.dart';
|
||||
|
||||
abstract class SMMSPageContract {
|
||||
|
||||
loadConfigSuccess(SMMSConfig config);
|
||||
|
||||
loadConfigFail(String msg);
|
||||
@@ -14,10 +15,12 @@ abstract class SMMSPageContract {
|
||||
|
||||
saveConfigFail(String msg);
|
||||
|
||||
testConfigSuccess(String username);
|
||||
|
||||
testConfigFail(String msg);
|
||||
}
|
||||
|
||||
class SMMSPagePresenter {
|
||||
|
||||
SMMSPageContract _view;
|
||||
|
||||
SMMSPagePresenter(this._view);
|
||||
@@ -35,7 +38,8 @@ class SMMSPagePresenter {
|
||||
|
||||
doSaveConfig(SMMSConfig config) async {
|
||||
try {
|
||||
var raw = await ImageUpload.savePBConfig(PBTypeKeys.smms, json.encode(config));
|
||||
var raw =
|
||||
await ImageUpload.savePBConfig(PBTypeKeys.smms, json.encode(config));
|
||||
if (raw > 0) {
|
||||
_view.saveConfigSuccess();
|
||||
} else {
|
||||
@@ -47,4 +51,17 @@ class SMMSPagePresenter {
|
||||
}
|
||||
}
|
||||
|
||||
doTestConfig() async {
|
||||
try {
|
||||
var result = json.decode((await SMMSNetUtils.post(SMMSApi.GET_PROFILE, null)));
|
||||
if (result["success"]) {
|
||||
_view.testConfigSuccess(result["data"]["username"]);
|
||||
} else {
|
||||
_view.testConfigFail(result["message"] ?? '未知异常');
|
||||
}
|
||||
} on Error catch (e) {
|
||||
print(e.stackTrace);
|
||||
_view.testConfigFail('$e');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user