Repository.dart 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. import 'package:dio/dio.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter_habit/common/I18N.dart';
  4. import 'package:flutter_habit/common/components/PopMenus.dart';
  5. import 'package:flutter_habit/provider/UserProvider.dart';
  6. import 'package:flutter_habit/common/utils/ConvertUtils.dart';
  7. import 'package:provider/provider.dart';
  8. import 'api.dart';
  9. import 'Status.dart';
  10. class Repository {
  11. Dio? _dio;
  12. Repository._() {
  13. if (_dio == null) {
  14. _dio = Dio(
  15. BaseOptions(
  16. connectTimeout: Duration(milliseconds: 8000),
  17. receiveTimeout: Duration(milliseconds: 8000),
  18. ),
  19. );
  20. }
  21. }
  22. static Repository? _repository;
  23. static Repository? getInstance() {
  24. if (_repository == null) {
  25. _repository = Repository._();
  26. }
  27. return _repository;
  28. }
  29. Future<bool> sendAuthCode(
  30. BuildContext context,
  31. String email,
  32. String purpose,
  33. ) async {
  34. Response? response;
  35. try {
  36. response = await _dio!.post(
  37. Api.authCode,
  38. data: {
  39. "email": email,
  40. "purpose": purpose,
  41. },
  42. );
  43. } catch (e) {
  44. debugPrint(e.toString());
  45. }
  46. switch (Status.of(response)) {
  47. case Status.OK:
  48. await PopMenus.attention(
  49. context: context, content: Text(I18N.of("验证码发送成功,5分钟内有效")));
  50. return true;
  51. case Status.RES_REPEATED:
  52. await PopMenus.attention(
  53. context: context, content: Text(I18N.of("该邮箱已存在")));
  54. break;
  55. case Status.RES_NOT_FOUND:
  56. await PopMenus.attention(
  57. context: context, content: Text(I18N.of("该邮箱未注册")));
  58. break;
  59. case Status.CREATE_FAIL:
  60. await PopMenus.attention(
  61. context: context, content: Text(I18N.of("邮件发送失败")));
  62. break;
  63. }
  64. return false;
  65. }
  66. Future<bool> signUp(
  67. BuildContext context,
  68. String authCode,
  69. String email,
  70. String pwd,
  71. ) async {
  72. Response? response;
  73. try {
  74. response = await _dio!.post(
  75. "${Api.user}/",
  76. options: Options(
  77. headers: {
  78. "authCode": authCode,
  79. },
  80. ),
  81. data: {
  82. "email": email,
  83. "pwd": ConvertUtils.md5Encode(pwd),
  84. },
  85. );
  86. } catch (e) {
  87. debugPrint(e.toString());
  88. }
  89. switch (Status.of(response)) {
  90. case Status.OK:
  91. await PopMenus.attention(
  92. context: context, content: Text(I18N.of("注册成功")));
  93. return true;
  94. case Status.RES_NOT_MATCH:
  95. await PopMenus.attention(
  96. context: context, content: Text(I18N.of("验证码错误或过期")));
  97. break;
  98. case Status.CONNECT_FAIL:
  99. await PopMenus.attention(
  100. context: context, content: Text(I18N.of("连接失败")));
  101. break;
  102. }
  103. return false;
  104. }
  105. Future<bool> resetPwd(
  106. BuildContext context,
  107. String authCode,
  108. String email,
  109. String pwd,
  110. ) async {
  111. Response? response;
  112. try {
  113. response = await _dio!.put(
  114. "${Api.user}/",
  115. options: Options(
  116. headers: {
  117. "authCode": authCode,
  118. },
  119. ),
  120. data: {
  121. "email": email,
  122. "pwd": ConvertUtils.md5Encode(pwd),
  123. },
  124. );
  125. } catch (e) {
  126. debugPrint(e.toString());
  127. }
  128. switch (Status.of(response)) {
  129. case Status.OK:
  130. await PopMenus.attention(
  131. context: context, content: Text(I18N.of("修改密码成功")));
  132. return true;
  133. case Status.RES_NOT_MATCH:
  134. await PopMenus.attention(
  135. context: context, content: Text(I18N.of("验证码错误或过期")));
  136. break;
  137. case Status.CONNECT_FAIL:
  138. await PopMenus.attention(
  139. context: context, content: Text(I18N.of("连接失败")));
  140. break;
  141. }
  142. return false;
  143. }
  144. Future<Map?> signIn(
  145. BuildContext context,
  146. String email,
  147. String pwd,
  148. ) async {
  149. Response? response;
  150. try {
  151. response = await _dio!.post(
  152. Api.token,
  153. data: {
  154. "email": email,
  155. "pwd": ConvertUtils.md5Encode(pwd),
  156. },
  157. );
  158. } catch (e) {
  159. debugPrint(e.toString());
  160. }
  161. switch (Status.of(response)) {
  162. case Status.OK:
  163. return response!.data;
  164. case Status.RES_NOT_MATCH:
  165. await PopMenus.attention(
  166. context: context, content: Text(I18N.of("邮箱或密码错误")));
  167. break;
  168. case Status.CONNECT_FAIL:
  169. await PopMenus.attention(
  170. context: context, content: Text(I18N.of("连接失败")));
  171. break;
  172. }
  173. return {};
  174. }
  175. Future<Map?> getUserInfo(int? uid) async {
  176. Response? response;
  177. try {
  178. response = await _dio!.get("${Api.user}/$uid/userInfo");
  179. } catch (e) {
  180. debugPrint(e.toString());
  181. }
  182. if (Status.of(response) == Status.OK) {
  183. return response!.data;
  184. }
  185. return {};
  186. }
  187. Future<int?> getCoin(int? uid) async {
  188. Response? response;
  189. try {
  190. response = await _dio!.get("${Api.user}/$uid/coin");
  191. } catch (e) {
  192. debugPrint(e.toString());
  193. }
  194. if (Status.of(response) == Status.OK) {
  195. return response!.data;
  196. }
  197. return null;
  198. }
  199. Future<List<int>?> getPhoto(int? uid) async {
  200. Response? response;
  201. try {
  202. response = await _dio!.get(
  203. "${Api.user}/$uid/userPhoto",
  204. options: Options(
  205. responseType: ResponseType.bytes,
  206. ),
  207. );
  208. } catch (e) {
  209. debugPrint(e.toString());
  210. }
  211. if (Status.of(response) == Status.OK) {
  212. return response!.data;
  213. }
  214. return null;
  215. }
  216. Future<bool> uploadPhoto(
  217. BuildContext context, String? token, int? uid, List<int> photo) async {
  218. Response? response;
  219. try {
  220. response = await _dio!.put("${Api.user}/$uid/userPhoto",
  221. options: Options(
  222. headers: {
  223. "token": token,
  224. },
  225. ),
  226. data: {
  227. "photo": photo,
  228. });
  229. } catch (e) {
  230. debugPrint(e.toString());
  231. }
  232. switch (Status.of(response)) {
  233. case Status.OK:
  234. return true;
  235. case Status.INVALID_AUTHORIZE:
  236. await PopMenus.attention(
  237. context: context, content: Text(I18N.of("登录信息过期")));
  238. await Provider.of<UserProvider>(context, listen: false)
  239. .cleanDataAndBackToHome(context);
  240. break;
  241. case Status.CONNECT_FAIL:
  242. await PopMenus.attention(
  243. context: context, content: Text(I18N.of("连接失败")));
  244. break;
  245. }
  246. return false;
  247. }
  248. Future<bool> modifyUserInfo(
  249. BuildContext context,
  250. String? token,
  251. int? uid,
  252. String? userName,
  253. String? gender,
  254. String? birthday,
  255. ) async {
  256. Response? response;
  257. try {
  258. response = await _dio!.put(
  259. "${Api.user}/$uid/userInfo",
  260. options: Options(
  261. headers: {
  262. "token": token,
  263. },
  264. ),
  265. data: {
  266. "userName": userName,
  267. "gender": gender,
  268. "birthday": birthday,
  269. },
  270. );
  271. } catch (e) {
  272. debugPrint(e.toString());
  273. }
  274. switch (Status.of(response)) {
  275. case Status.OK:
  276. return true;
  277. case Status.INVALID_AUTHORIZE:
  278. await PopMenus.attention(
  279. context: context, content: Text(I18N.of("登录信息过期")));
  280. await Provider.of<UserProvider>(context, listen: false)
  281. .cleanDataAndBackToHome(context);
  282. break;
  283. case Status.CONNECT_FAIL:
  284. await PopMenus.attention(
  285. context: context, content: Text(I18N.of("连接失败")));
  286. break;
  287. }
  288. return false;
  289. }
  290. Future<bool?> getFollowState(
  291. BuildContext context, String? token, int? uid, int? followUid) async {
  292. Response? response;
  293. try {
  294. response = await _dio!.get("${Api.community}/$uid/follow/$followUid",
  295. options: Options(
  296. headers: {
  297. "token": token,
  298. },
  299. ));
  300. } catch (e) {
  301. debugPrint(e.toString());
  302. }
  303. switch (Status.of(response)) {
  304. case Status.OK:
  305. return response!.data;
  306. case Status.INVALID_AUTHORIZE:
  307. await PopMenus.attention(
  308. context: context, content: Text(I18N.of("登录信息过期")));
  309. await Provider.of<UserProvider>(context, listen: false)
  310. .cleanDataAndBackToHome(context);
  311. break;
  312. case Status.CONNECT_FAIL:
  313. await PopMenus.attention(
  314. context: context, content: Text(I18N.of("连接失败")));
  315. break;
  316. }
  317. return false;
  318. }
  319. Future<String?> follow(
  320. BuildContext context, String? token, int? uid, int? followUid) async {
  321. Response? response;
  322. try {
  323. response = await _dio!.post(
  324. Api.follow,
  325. options: Options(
  326. headers: {
  327. "token": token,
  328. },
  329. ),
  330. data: {
  331. "uid": uid,
  332. "followUid": followUid,
  333. },
  334. );
  335. } catch (e) {
  336. debugPrint(e.toString());
  337. }
  338. switch (Status.of(response)) {
  339. case Status.OK:
  340. return response!.data;
  341. case Status.RES_NOT_ALLOW:
  342. await PopMenus.attention(
  343. context: context, content: Text(I18N.of("不能关注自己")));
  344. break;
  345. case Status.INVALID_AUTHORIZE:
  346. await PopMenus.attention(
  347. context: context, content: Text(I18N.of("登录信息过期")));
  348. await Provider.of<UserProvider>(context, listen: false)
  349. .cleanDataAndBackToHome(context);
  350. break;
  351. case Status.CONNECT_FAIL:
  352. await PopMenus.attention(
  353. context: context, content: Text(I18N.of("连接失败")));
  354. break;
  355. }
  356. return null;
  357. }
  358. Future<List?> getFollowList(
  359. BuildContext context, String? token, int? uid) async {
  360. Response? response;
  361. try {
  362. response = await _dio!.get(
  363. Api.follow,
  364. options: Options(
  365. headers: {
  366. "token": token,
  367. },
  368. ),
  369. queryParameters: {
  370. "uid": uid,
  371. },
  372. );
  373. } catch (e) {
  374. debugPrint(e.toString());
  375. }
  376. switch (Status.of(response)) {
  377. case Status.OK:
  378. return response!.data;
  379. case Status.INVALID_AUTHORIZE:
  380. await PopMenus.attention(
  381. context: context, content: Text(I18N.of("登录信息过期")));
  382. await Provider.of<UserProvider>(context, listen: false)
  383. .cleanDataAndBackToHome(context);
  384. break;
  385. case Status.CONNECT_FAIL:
  386. await PopMenus.attention(
  387. context: context, content: Text(I18N.of("连接失败")));
  388. break;
  389. }
  390. return [];
  391. }
  392. Future<List?> getUserInfoLikeUserName(
  393. BuildContext context, String name) async {
  394. Response? response;
  395. try {
  396. response = await _dio!.get("${Api.user}/", queryParameters: {
  397. "name": name,
  398. });
  399. } catch (e) {
  400. debugPrint(e.toString());
  401. }
  402. switch (Status.of(response)) {
  403. case Status.OK:
  404. return response!.data;
  405. case Status.CONNECT_FAIL:
  406. await PopMenus.attention(
  407. context: context, content: Text(I18N.of("连接失败")));
  408. break;
  409. }
  410. return [];
  411. }
  412. Future<List?> getCoinTop(BuildContext context, int topCount) async {
  413. Response? response;
  414. try {
  415. response = await _dio!.get("${Api.community}/coinTop", queryParameters: {
  416. "topCount": topCount,
  417. });
  418. } catch (e) {
  419. debugPrint(e.toString());
  420. }
  421. switch (Status.of(response)) {
  422. case Status.OK:
  423. return response!.data;
  424. case Status.CONNECT_FAIL:
  425. await PopMenus.attention(
  426. context: context, content: Text(I18N.of("连接失败")));
  427. break;
  428. }
  429. return [];
  430. }
  431. Future<List?> getGoodsList(BuildContext context) async {
  432. Response? response;
  433. try {
  434. response = await _dio!.get(
  435. "${Api.shopping}/",
  436. );
  437. } catch (e) {
  438. debugPrint(e.toString());
  439. }
  440. switch (Status.of(response)) {
  441. case Status.OK:
  442. return response!.data;
  443. case Status.CONNECT_FAIL:
  444. await PopMenus.attention(
  445. context: context, content: Text(I18N.of("连接失败")));
  446. break;
  447. }
  448. return [];
  449. }
  450. Future<int?> increaseCoin(BuildContext context, int? uid, String? token) async {
  451. Response? response;
  452. try {
  453. response = await _dio!.put(
  454. "${Api.user}/$uid/coin",
  455. options: Options(headers: {
  456. "token": token,
  457. }),
  458. );
  459. } catch (e) {
  460. debugPrint(e.toString());
  461. }
  462. switch (Status.of(response)) {
  463. case Status.OK:
  464. return response!.data;
  465. case Status.INVALID_AUTHORIZE:
  466. await PopMenus.attention(
  467. context: context, content: Text(I18N.of("登录信息过期")));
  468. await Provider.of<UserProvider>(context, listen: false)
  469. .cleanDataAndBackToHome(context);
  470. break;
  471. case Status.RES_NOT_ALLOW:
  472. await PopMenus.ban(context);
  473. Provider.of<UserProvider>(context, listen: false).coins = -666;
  474. Provider.of<UserProvider>(context, listen: false).refresh();
  475. break;
  476. case Status.CONNECT_FAIL:
  477. await PopMenus.attention(
  478. context: context, content: Text(I18N.of("连接失败")));
  479. break;
  480. }
  481. return null;
  482. }
  483. Future<bool> buyGoods(
  484. BuildContext context, String? token, int? uid, int? goodsId) async {
  485. Response? response;
  486. try {
  487. response = await _dio!.post("${Api.shopping}/",
  488. options: Options(headers: {
  489. "token": token,
  490. }),
  491. data: {
  492. "uid": uid,
  493. "goodsId": goodsId,
  494. });
  495. } catch (e) {
  496. debugPrint(e.toString());
  497. }
  498. switch (Status.of(response)) {
  499. case Status.OK:
  500. return true;
  501. case Status.INVALID_AUTHORIZE:
  502. await PopMenus.attention(
  503. context: context, content: Text(I18N.of("登录信息过期")));
  504. await Provider.of<UserProvider>(context, listen: false)
  505. .cleanDataAndBackToHome(context);
  506. break;
  507. case Status.RES_NOT_ALLOW:
  508. await PopMenus.attention(
  509. context: context, content: Text(I18N.of("金币不足")));
  510. break;
  511. case Status.RES_NOT_MATCH:
  512. await PopMenus.attention(
  513. context: context, content: Text(I18N.of("库存不足")));
  514. break;
  515. case Status.CREATE_FAIL:
  516. await PopMenus.attention(
  517. context: context, content: Text(I18N.of("邮件发送失败")));
  518. break;
  519. case Status.CONNECT_FAIL:
  520. await PopMenus.attention(
  521. context: context, content: Text(I18N.of("连接失败")));
  522. break;
  523. }
  524. return false;
  525. }
  526. Future<bool> uploadDB(
  527. BuildContext context, int? uid, String? token, List<int> data) async {
  528. Response? response;
  529. try {
  530. response = await _dio!.put(
  531. "${Api.user}/$uid/data",
  532. options: Options(headers: {
  533. "token": token,
  534. }),
  535. data: {"data": data},
  536. );
  537. } catch (e) {
  538. debugPrint(e.toString());
  539. }
  540. switch (Status.of(response)) {
  541. case Status.OK:
  542. return true;
  543. case Status.INVALID_AUTHORIZE:
  544. await PopMenus.attention(
  545. context: context, content: Text(I18N.of("登录信息过期")));
  546. await Provider.of<UserProvider>(context, listen: false)
  547. .cleanDataAndBackToHome(context);
  548. break;
  549. case Status.CONNECT_FAIL:
  550. await PopMenus.attention(
  551. context: context, content: Text(I18N.of("连接失败")));
  552. break;
  553. }
  554. return false;
  555. }
  556. Future<List<int>?> downloadDB(
  557. BuildContext context, int? uid, String? token) async {
  558. Response? response;
  559. try {
  560. response = await _dio!.get(
  561. "${Api.user}/$uid/data",
  562. options: Options(
  563. responseType: ResponseType.bytes,
  564. headers: {
  565. "token": token,
  566. },
  567. ),
  568. );
  569. } catch (e) {
  570. debugPrint(e.toString());
  571. }
  572. switch (Status.of(response)) {
  573. case Status.OK:
  574. return response!.data;
  575. case Status.RES_NOT_FOUND:
  576. await PopMenus.attention(
  577. context: context, content: Text(I18N.of("云端无数据")));
  578. await Provider.of<UserProvider>(context, listen: false)
  579. .cleanDataAndBackToHome(context);
  580. break;
  581. case Status.INVALID_AUTHORIZE:
  582. await PopMenus.attention(
  583. context: context, content: Text(I18N.of("登录信息过期")));
  584. await Provider.of<UserProvider>(context, listen: false)
  585. .cleanDataAndBackToHome(context);
  586. break;
  587. case Status.CONNECT_FAIL:
  588. await PopMenus.attention(
  589. context: context, content: Text(I18N.of("连接失败")));
  590. break;
  591. }
  592. return null;
  593. }
  594. }