123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633 |
- import 'package:dio/dio.dart';
- import 'package:flutter/cupertino.dart';
- import 'package:flutter_habit/common/I18N.dart';
- import 'package:flutter_habit/common/components/PopMenus.dart';
- import 'package:flutter_habit/provider/UserProvider.dart';
- import 'package:flutter_habit/common/utils/ConvertUtils.dart';
- import 'package:provider/provider.dart';
- import 'api.dart';
- import 'Status.dart';
- class Repository {
- Dio? _dio;
- Repository._() {
- if (_dio == null) {
- _dio = Dio(
- BaseOptions(
- connectTimeout: Duration(milliseconds: 8000),
- receiveTimeout: Duration(milliseconds: 8000),
- ),
- );
- }
- }
- static Repository? _repository;
- static Repository? getInstance() {
- if (_repository == null) {
- _repository = Repository._();
- }
- return _repository;
- }
- Future<bool> sendAuthCode(
- BuildContext context,
- String email,
- String purpose,
- ) async {
- Response? response;
- try {
- response = await _dio!.post(
- Api.authCode,
- data: {
- "email": email,
- "purpose": purpose,
- },
- );
- } catch (e) {
- debugPrint(e.toString());
- }
- switch (Status.of(response)) {
- case Status.OK:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("验证码发送成功,5分钟内有效")));
- return true;
- case Status.RES_REPEATED:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("该邮箱已存在")));
- break;
- case Status.RES_NOT_FOUND:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("该邮箱未注册")));
- break;
- case Status.CREATE_FAIL:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("邮件发送失败")));
- break;
- }
- return false;
- }
- Future<bool> signUp(
- BuildContext context,
- String authCode,
- String email,
- String pwd,
- ) async {
- Response? response;
- try {
- response = await _dio!.post(
- "${Api.user}/",
- options: Options(
- headers: {
- "authCode": authCode,
- },
- ),
- data: {
- "email": email,
- "pwd": ConvertUtils.md5Encode(pwd),
- },
- );
- } catch (e) {
- debugPrint(e.toString());
- }
- switch (Status.of(response)) {
- case Status.OK:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("注册成功")));
- return true;
- case Status.RES_NOT_MATCH:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("验证码错误或过期")));
- break;
- case Status.CONNECT_FAIL:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("连接失败")));
- break;
- }
- return false;
- }
- Future<bool> resetPwd(
- BuildContext context,
- String authCode,
- String email,
- String pwd,
- ) async {
- Response? response;
- try {
- response = await _dio!.put(
- "${Api.user}/",
- options: Options(
- headers: {
- "authCode": authCode,
- },
- ),
- data: {
- "email": email,
- "pwd": ConvertUtils.md5Encode(pwd),
- },
- );
- } catch (e) {
- debugPrint(e.toString());
- }
- switch (Status.of(response)) {
- case Status.OK:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("修改密码成功")));
- return true;
- case Status.RES_NOT_MATCH:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("验证码错误或过期")));
- break;
- case Status.CONNECT_FAIL:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("连接失败")));
- break;
- }
- return false;
- }
- Future<Map?> signIn(
- BuildContext context,
- String email,
- String pwd,
- ) async {
- Response? response;
- try {
- response = await _dio!.post(
- Api.token,
- data: {
- "email": email,
- "pwd": ConvertUtils.md5Encode(pwd),
- },
- );
- } catch (e) {
- debugPrint(e.toString());
- }
- switch (Status.of(response)) {
- case Status.OK:
- return response!.data;
- case Status.RES_NOT_MATCH:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("邮箱或密码错误")));
- break;
- case Status.CONNECT_FAIL:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("连接失败")));
- break;
- }
- return {};
- }
- Future<Map?> getUserInfo(int? uid) async {
- Response? response;
- try {
- response = await _dio!.get("${Api.user}/$uid/userInfo");
- } catch (e) {
- debugPrint(e.toString());
- }
- if (Status.of(response) == Status.OK) {
- return response!.data;
- }
- return {};
- }
- Future<int?> getCoin(int? uid) async {
- Response? response;
- try {
- response = await _dio!.get("${Api.user}/$uid/coin");
- } catch (e) {
- debugPrint(e.toString());
- }
- if (Status.of(response) == Status.OK) {
- return response!.data;
- }
- return null;
- }
- Future<List<int>?> getPhoto(int? uid) async {
- Response? response;
- try {
- response = await _dio!.get(
- "${Api.user}/$uid/userPhoto",
- options: Options(
- responseType: ResponseType.bytes,
- ),
- );
- } catch (e) {
- debugPrint(e.toString());
- }
- if (Status.of(response) == Status.OK) {
- return response!.data;
- }
- return null;
- }
- Future<bool> uploadPhoto(
- BuildContext context, String? token, int? uid, List<int> photo) async {
- Response? response;
- try {
- response = await _dio!.put("${Api.user}/$uid/userPhoto",
- options: Options(
- headers: {
- "token": token,
- },
- ),
- data: {
- "photo": photo,
- });
- } catch (e) {
- debugPrint(e.toString());
- }
- switch (Status.of(response)) {
- case Status.OK:
- return true;
- case Status.INVALID_AUTHORIZE:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("登录信息过期")));
- await Provider.of<UserProvider>(context, listen: false)
- .cleanDataAndBackToHome(context);
- break;
- case Status.CONNECT_FAIL:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("连接失败")));
- break;
- }
- return false;
- }
- Future<bool> modifyUserInfo(
- BuildContext context,
- String? token,
- int? uid,
- String? userName,
- String? gender,
- String? birthday,
- ) async {
- Response? response;
- try {
- response = await _dio!.put(
- "${Api.user}/$uid/userInfo",
- options: Options(
- headers: {
- "token": token,
- },
- ),
- data: {
- "userName": userName,
- "gender": gender,
- "birthday": birthday,
- },
- );
- } catch (e) {
- debugPrint(e.toString());
- }
- switch (Status.of(response)) {
- case Status.OK:
- return true;
- case Status.INVALID_AUTHORIZE:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("登录信息过期")));
- await Provider.of<UserProvider>(context, listen: false)
- .cleanDataAndBackToHome(context);
- break;
- case Status.CONNECT_FAIL:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("连接失败")));
- break;
- }
- return false;
- }
- Future<bool?> getFollowState(
- BuildContext context, String? token, int? uid, int? followUid) async {
- Response? response;
- try {
- response = await _dio!.get("${Api.community}/$uid/follow/$followUid",
- options: Options(
- headers: {
- "token": token,
- },
- ));
- } catch (e) {
- debugPrint(e.toString());
- }
- switch (Status.of(response)) {
- case Status.OK:
- return response!.data;
- case Status.INVALID_AUTHORIZE:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("登录信息过期")));
- await Provider.of<UserProvider>(context, listen: false)
- .cleanDataAndBackToHome(context);
- break;
- case Status.CONNECT_FAIL:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("连接失败")));
- break;
- }
- return false;
- }
- Future<String?> follow(
- BuildContext context, String? token, int? uid, int? followUid) async {
- Response? response;
- try {
- response = await _dio!.post(
- Api.follow,
- options: Options(
- headers: {
- "token": token,
- },
- ),
- data: {
- "uid": uid,
- "followUid": followUid,
- },
- );
- } catch (e) {
- debugPrint(e.toString());
- }
- switch (Status.of(response)) {
- case Status.OK:
- return response!.data;
- case Status.RES_NOT_ALLOW:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("不能关注自己")));
- break;
- case Status.INVALID_AUTHORIZE:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("登录信息过期")));
- await Provider.of<UserProvider>(context, listen: false)
- .cleanDataAndBackToHome(context);
- break;
- case Status.CONNECT_FAIL:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("连接失败")));
- break;
- }
- return null;
- }
- Future<List?> getFollowList(
- BuildContext context, String? token, int? uid) async {
- Response? response;
- try {
- response = await _dio!.get(
- Api.follow,
- options: Options(
- headers: {
- "token": token,
- },
- ),
- queryParameters: {
- "uid": uid,
- },
- );
- } catch (e) {
- debugPrint(e.toString());
- }
- switch (Status.of(response)) {
- case Status.OK:
- return response!.data;
- case Status.INVALID_AUTHORIZE:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("登录信息过期")));
- await Provider.of<UserProvider>(context, listen: false)
- .cleanDataAndBackToHome(context);
- break;
- case Status.CONNECT_FAIL:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("连接失败")));
- break;
- }
- return [];
- }
- Future<List?> getUserInfoLikeUserName(
- BuildContext context, String name) async {
- Response? response;
- try {
- response = await _dio!.get("${Api.user}/", queryParameters: {
- "name": name,
- });
- } catch (e) {
- debugPrint(e.toString());
- }
- switch (Status.of(response)) {
- case Status.OK:
- return response!.data;
- case Status.CONNECT_FAIL:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("连接失败")));
- break;
- }
- return [];
- }
- Future<List?> getCoinTop(BuildContext context, int topCount) async {
- Response? response;
- try {
- response = await _dio!.get("${Api.community}/coinTop", queryParameters: {
- "topCount": topCount,
- });
- } catch (e) {
- debugPrint(e.toString());
- }
- switch (Status.of(response)) {
- case Status.OK:
- return response!.data;
- case Status.CONNECT_FAIL:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("连接失败")));
- break;
- }
- return [];
- }
- Future<List?> getGoodsList(BuildContext context) async {
- Response? response;
- try {
- response = await _dio!.get(
- "${Api.shopping}/",
- );
- } catch (e) {
- debugPrint(e.toString());
- }
- switch (Status.of(response)) {
- case Status.OK:
- return response!.data;
- case Status.CONNECT_FAIL:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("连接失败")));
- break;
- }
- return [];
- }
- Future<int?> increaseCoin(BuildContext context, int? uid, String? token) async {
- Response? response;
- try {
- response = await _dio!.put(
- "${Api.user}/$uid/coin",
- options: Options(headers: {
- "token": token,
- }),
- );
- } catch (e) {
- debugPrint(e.toString());
- }
- switch (Status.of(response)) {
- case Status.OK:
- return response!.data;
- case Status.INVALID_AUTHORIZE:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("登录信息过期")));
- await Provider.of<UserProvider>(context, listen: false)
- .cleanDataAndBackToHome(context);
- break;
- case Status.RES_NOT_ALLOW:
- await PopMenus.ban(context);
- Provider.of<UserProvider>(context, listen: false).coins = -666;
- Provider.of<UserProvider>(context, listen: false).refresh();
- break;
- case Status.CONNECT_FAIL:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("连接失败")));
- break;
- }
- return null;
- }
- Future<bool> buyGoods(
- BuildContext context, String? token, int? uid, int? goodsId) async {
- Response? response;
- try {
- response = await _dio!.post("${Api.shopping}/",
- options: Options(headers: {
- "token": token,
- }),
- data: {
- "uid": uid,
- "goodsId": goodsId,
- });
- } catch (e) {
- debugPrint(e.toString());
- }
- switch (Status.of(response)) {
- case Status.OK:
- return true;
- case Status.INVALID_AUTHORIZE:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("登录信息过期")));
- await Provider.of<UserProvider>(context, listen: false)
- .cleanDataAndBackToHome(context);
- break;
- case Status.RES_NOT_ALLOW:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("金币不足")));
- break;
- case Status.RES_NOT_MATCH:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("库存不足")));
- break;
- case Status.CREATE_FAIL:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("邮件发送失败")));
- break;
- case Status.CONNECT_FAIL:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("连接失败")));
- break;
- }
- return false;
- }
- Future<bool> uploadDB(
- BuildContext context, int? uid, String? token, List<int> data) async {
- Response? response;
- try {
- response = await _dio!.put(
- "${Api.user}/$uid/data",
- options: Options(headers: {
- "token": token,
- }),
- data: {"data": data},
- );
- } catch (e) {
- debugPrint(e.toString());
- }
- switch (Status.of(response)) {
- case Status.OK:
- return true;
- case Status.INVALID_AUTHORIZE:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("登录信息过期")));
- await Provider.of<UserProvider>(context, listen: false)
- .cleanDataAndBackToHome(context);
- break;
- case Status.CONNECT_FAIL:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("连接失败")));
- break;
- }
- return false;
- }
- Future<List<int>?> downloadDB(
- BuildContext context, int? uid, String? token) async {
- Response? response;
- try {
- response = await _dio!.get(
- "${Api.user}/$uid/data",
- options: Options(
- responseType: ResponseType.bytes,
- headers: {
- "token": token,
- },
- ),
- );
- } catch (e) {
- debugPrint(e.toString());
- }
- switch (Status.of(response)) {
- case Status.OK:
- return response!.data;
- case Status.RES_NOT_FOUND:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("云端无数据")));
- await Provider.of<UserProvider>(context, listen: false)
- .cleanDataAndBackToHome(context);
- break;
- case Status.INVALID_AUTHORIZE:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("登录信息过期")));
- await Provider.of<UserProvider>(context, listen: false)
- .cleanDataAndBackToHome(context);
- break;
- case Status.CONNECT_FAIL:
- await PopMenus.attention(
- context: context, content: Text(I18N.of("连接失败")));
- break;
- }
- return null;
- }
- }
|