添加上传请求工具

对接上传头像接口
hmxc
小赖 4 years ago
parent 4ee7bc51cd
commit b4dbc40ab8

@ -5,6 +5,7 @@ class API {
static _Login login = _Login();
static _User user = _User();
static _Manager manager = _Manager();
static _Upload upload = _Upload();
}
class _Login {
@ -48,6 +49,9 @@ class _User {
///
String get updateTel => '/user/personalData/updateTel';
///
String get udpdateAvatar => '/user/personalData/updateHeadPortrait';
}
class _Manager {
@ -69,3 +73,11 @@ class _Manager {
///app
String get reportRepairInsert => '/user/reportRepair/insert';
}
class _Upload {
///
String get uploadArticle => '/user/upload/uploadArticle';
///
String get uploadAvatar => '/user/upload/appHeadSculpture';
}

@ -1,11 +1,15 @@
import 'dart:io';
import 'package:akuCommunity/base/base_style.dart';
import 'package:akuCommunity/constants/api.dart';
import 'package:akuCommunity/pages/personal/change_nick_name_page.dart';
import 'package:akuCommunity/pages/personal/update_tel_page.dart';
import 'package:akuCommunity/provider/user_provider.dart';
import 'package:akuCommunity/utils/network/base_file_model.dart';
import 'package:akuCommunity/utils/network/net_util.dart';
import 'package:akuCommunity/widget/bee_scaffold.dart';
import 'package:akuCommunity/utils/headers.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:common_utils/common_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
@ -56,6 +60,7 @@ class _UserProfilePageState extends State<UserProfilePage> {
}
_pickAvatar() async {
final userProvider = Provider.of<UserProvider>(context, listen: false);
PickedFile file = await Get.bottomSheet(CupertinoActionSheet(
title: '选择头像'.text.isIntrinsic.make(),
actions: [
@ -77,8 +82,20 @@ class _UserProfilePageState extends State<UserProfilePage> {
child: '取消'.text.isIntrinsic.make(),
),
));
if (file == null) return;
//TODO upload avatar.
if (file == null)
return;
else {
//Upload Avatar
Function cancel = BotToast.showLoading();
File rawFile = File(file.path);
BaseFileModel model =
await NetUtil().upload(API.upload.uploadAvatar, rawFile);
if (model.status)
userProvider.updateAvatar(model.url);
else
BotToast.showText(text: model.message);
cancel();
}
}
@override

@ -77,11 +77,14 @@ class UserProvider extends ChangeNotifier {
Future setBirthday(DateTime date) async {
BaseModel baseModel = await NetUtil().post(
API.user.setBirthday,
params: {'birthday': DateUtil.formatDate(date,format: "yyyy-MM-dd HH:mm:ss")},
params: {
'birthday': DateUtil.formatDate(date, format: "yyyy-MM-dd HH:mm:ss")
},
showMessage: true,
);
if (baseModel.status) {
_userInfoModel.birthday = DateUtil.formatDate(date,format: "yyyy-MM-dd HH:mm:ss");
_userInfoModel.birthday =
DateUtil.formatDate(date, format: "yyyy-MM-dd HH:mm:ss");
notifyListeners();
}
}
@ -111,4 +114,19 @@ class UserProvider extends ChangeNotifier {
notifyListeners();
}
}
///
Future updateAvatar(String path) async {
BaseModel model = await NetUtil().post(
API.user.udpdateAvatar,
params: {
'fileUrls': [path]
},
showMessage: true,
);
if (model.status) {
_userInfoModel.imgUrls = [path];
notifyListeners();
}
}
}

@ -0,0 +1,19 @@
class BaseFileModel {
String message;
String url;
bool status;
BaseFileModel({
this.message,
this.url,
this.status,
});
BaseFileModel.err(
{this.message = '未知错误', this.url = '', this.status = false});
BaseFileModel.fromJson(Map<String, dynamic> json) {
message = json['message'] ?? '';
url = json['url'] ?? null;
status = json['status'] ?? false;
}
}

@ -1,12 +1,15 @@
import 'dart:io';
import 'package:akuCommunity/pages/sign/sign_in_page.dart';
import 'package:akuCommunity/provider/user_provider.dart';
import 'package:akuCommunity/utils/network/base_file_model.dart';
import 'package:akuCommunity/utils/network/base_list_model.dart';
import 'package:akuCommunity/utils/network/base_model.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:dio/dio.dart';
import 'package:akuCommunity/constants/api.dart';
import 'package:get/get.dart' hide Response;
import 'package:get/get.dart' hide Response, FormData, MultipartFile;
import 'package:logger/logger.dart';
import 'package:power_logger/power_logger.dart';
import 'package:provider/provider.dart';
@ -118,6 +121,27 @@ class NetUtil {
return BaseListModel.err();
}
Future<BaseFileModel> upload(String path, File file) async {
try {
Response res = await _dio.post(path,
data: FormData.fromMap({
'file': await MultipartFile.fromFile(file.path),
}));
_logger.v({
'path': res.request.path,
'header': res.request.headers,
'params': res.request.queryParameters,
'data': res.data,
});
LoggerData.addData(res);
BaseFileModel baseListModel = BaseFileModel.fromJson(res.data);
return baseListModel;
} on DioError catch (e) {
_parseErr(e);
}
return BaseFileModel.err();
}
_parseErr(DioError err) {
_logger.v({
'type': err.type.toString(),

Loading…
Cancel
Save