login_info_model.dart 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import 'package:flutter/material.dart';
  2. import 'package:provider/provider.dart';
  3. import 'package:shared_preferences/shared_preferences.dart';
  4. import 'package:sport/application.dart';
  5. import 'package:sport/bean/login.dart';
  6. import 'package:sport/pages/my/weight_page.dart';
  7. import 'package:sport/provider/user_model.dart';
  8. import 'package:sport/router/navigator_util.dart';
  9. import 'package:sport/services/api/inject_api.dart';
  10. import 'package:sport/services/api/resp.dart';
  11. import 'package:sport/utils/toast.dart';
  12. class LoginInfoModel extends ChangeNotifier with InjectLoginApi {
  13. bool isNew = false;
  14. Future<LoginInfo> getlogin(BuildContext context, String phone, {String? pwd, String? captcha}) async {
  15. return await loginApi.getLoginInfo(phone, password: pwd, captcha: captcha);
  16. }
  17. /// 登录
  18. Future<bool> login(BuildContext context, LoginInfo loginInfo) async {
  19. if (loginInfo.code == 0) {
  20. Data? data = loginInfo.data;
  21. if (data != null && data.user != null) {
  22. bool isNew = data.isNew ?? false;
  23. bool isFillWeight = data.user?.fillWeight ?? false;
  24. String username = data.user?.name ?? "";
  25. await saveUser(context, data.user);
  26. await saveUserInfo({}, token: data.token);
  27. if (isNew == true) {
  28. this.isNew = true;
  29. NavigatorUtil.goBasicInfo(context, username: username);
  30. } else if (isFillWeight != true) {
  31. NavigatorUtil.goPage(context, (context) => WeightPage());
  32. } else {
  33. NavigatorUtil.goHomePage(context);
  34. }
  35. return true;
  36. }
  37. }
  38. return false;
  39. }
  40. saveUser(BuildContext context, User? user) async {
  41. Map<String, dynamic> map = {};
  42. if (user != null) {
  43. map.addAll(user.toJson());
  44. if (user.bindInfo != null) {
  45. map.addAll(user.bindInfo!.toJson());
  46. }
  47. Provider.of<UserModel>(context, listen: false).saveUserInfo(user);
  48. }
  49. if (map.isNotEmpty) await saveUserInfo(map);
  50. }
  51. /// 保存用户信息到 sp 不太熟悉传对象 就这样将就一下
  52. saveUserInfo(Map<String, dynamic> params, {String? token}) async {
  53. SharedPreferences prefs = await SharedPreferences.getInstance();
  54. saveMap("", params, prefs);
  55. // 这里的因为更新的时候不会返回token 可能会覆盖 所以...
  56. if (token != null) {
  57. prefs.setString('token', token);
  58. }
  59. prefs.setBool("agreement", true);
  60. print("token $token");
  61. }
  62. saveMap(String root, Map<String, dynamic> params, SharedPreferences prefs) {
  63. params.forEach((k, value) {
  64. String key = "$root$k";
  65. if (value is String) {
  66. prefs.setString(key, value);
  67. print("putString $key $value");
  68. } else if (value is int) {
  69. prefs.setInt(key, value);
  70. print("putInt $key $value");
  71. } else if (value is bool) {
  72. prefs.setBool(key, value);
  73. print("putBool $key $value");
  74. } else if (value is double) {
  75. prefs.setDouble(key, value);
  76. print("putDouble $key $value");
  77. } else if (value is Map<String, dynamic>) {
  78. saveMap(key, value, prefs);
  79. }
  80. });
  81. }
  82. Future<bool> getCaptcha(String phone) async {
  83. var data = await loginApi.getCaptcha(phone);
  84. if (data['code'] == 0) {
  85. ToastUtil.show("验证码发送成功");
  86. // Fluttertoast.showToast(
  87. // msg: "验证码发送成功",
  88. // toastLength: Toast.LENGTH_SHORT,
  89. // gravity: ToastGravity.CENTER,
  90. // backgroundColor: Colors.white,
  91. // textColor: Colors.black,
  92. // fontSize: 13.0);
  93. return true;
  94. }
  95. return false;
  96. }
  97. Future<bool> verifyCaptch(String phone, String captcha) async {
  98. var data = await loginApi.verifyCaptcha(phone, captcha);
  99. if (data['code'] != 0) {
  100. // Fluttertoast.showToast(
  101. // msg: "${data['msg']}",
  102. // toastLength: Toast.LENGTH_SHORT,
  103. // gravity: ToastGravity.CENTER,
  104. // backgroundColor: Colors.white,
  105. // textColor: Colors.black,
  106. // fontSize: 13.0);
  107. return false;
  108. } else if (!data['data']) {
  109. ToastUtil.show("验证码输入错误");
  110. // Fluttertoast.showToast(
  111. // msg: "验证码输入错误",
  112. // toastLength: Toast.LENGTH_SHORT,
  113. // gravity: ToastGravity.CENTER,
  114. // backgroundColor: Colors.white,
  115. // textColor: Colors.black,
  116. // fontSize: 13.0);
  117. return false;
  118. }
  119. return true;
  120. }
  121. Future<bool> resetPasswordByCaptcha(String? phone, String? captcha, String? password) async {
  122. var data = await loginApi.resetPasswordByCaptcha(phone, captcha, password);
  123. if (data['code'] != 0) {
  124. // Fluttertoast.showToast(
  125. // msg: "${data['msg']}",
  126. // toastLength: Toast.LENGTH_SHORT,
  127. // gravity: ToastGravity.CENTER,
  128. // backgroundColor: Colors.white,
  129. // textColor: Colors.black,
  130. // fontSize: 13.0);
  131. return false;
  132. }
  133. return true;
  134. }
  135. Future<bool> resetPasswordByOld(String? oldPassword, String? newPassword) async {
  136. var data = await loginApi.resetPasswordByOld(oldPassword, newPassword);
  137. if (data['code'] != 0) {
  138. // Fluttertoast.showToast(
  139. // msg: "${data['msg']}",
  140. // toastLength: Toast.LENGTH_SHORT,
  141. // gravity: ToastGravity.CENTER,
  142. // backgroundColor: Colors.white,
  143. // textColor: Colors.black,
  144. // fontSize: 13.0);
  145. return false;
  146. }
  147. return true;
  148. }
  149. Future<bool> updateUserInfo(BuildContext context, String name, int gender, {int? age, int? provinceId, int? cityId, int? districtId, double? weight, int? ignoreNameCard}) async {
  150. RespData<User> data = await loginApi.updateInfo(name, gender, age: age, province_id: provinceId, district_id: districtId, city_id: cityId, weight: weight, ignore_name_card: ignoreNameCard);
  151. if (data.code == 0) {
  152. saveUser(context, data.data);
  153. return true;
  154. } else {
  155. // Fluttertoast.showToast(
  156. // msg: "${loginInfo['msg']}",
  157. // toastLength: Toast.LENGTH_SHORT,
  158. // gravity: ToastGravity.CENTER,
  159. // backgroundColor: Colors.white,
  160. // textColor: Colors.black,
  161. // fontSize: 13.0);
  162. return false;
  163. }
  164. }
  165. Future<bool> updateUserInfoEx(BuildContext context, {int? age, int? provinceId, int? cityId, int? districtId, double weight = 60.0, int? ignoreNameCard}) async {
  166. var user = Provider.of<UserModel>(context, listen: false);
  167. return updateUserInfo(context, user.user.name, user.user.gender!, age: age, provinceId: provinceId, cityId: cityId, districtId: districtId, weight: weight, ignoreNameCard: ignoreNameCard);
  168. }
  169. // Future<bool> updateUserInfoMap(Map<String, dynamic> params) async {
  170. // var loginInfo = await loginApi.updateInfoMap(params);
  171. //
  172. // if (loginInfo["code"] == 0) {
  173. // await _saveUserInfo(loginInfo['data']);
  174. // return true;
  175. // } else {
  176. // Fluttertoast.showToast(
  177. // msg: "${loginInfo['msg']}",
  178. // toastLength: Toast.LENGTH_SHORT,
  179. // gravity: ToastGravity.CENTER,
  180. // backgroundColor: Colors.white,
  181. // textColor: Colors.black,
  182. // fontSize: 13.0);
  183. // return false;
  184. // }
  185. // }
  186. }
  187. //const final