123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- import 'package:flutter/material.dart';
- import 'package:provider/provider.dart';
- import 'package:shared_preferences/shared_preferences.dart';
- import 'package:sport/application.dart';
- import 'package:sport/bean/login.dart';
- import 'package:sport/pages/my/weight_page.dart';
- import 'package:sport/provider/user_model.dart';
- import 'package:sport/router/navigator_util.dart';
- import 'package:sport/services/api/inject_api.dart';
- import 'package:sport/services/api/resp.dart';
- import 'package:sport/utils/toast.dart';
- class LoginInfoModel extends ChangeNotifier with InjectLoginApi {
- bool isNew = false;
- Future<LoginInfo> getlogin(BuildContext context, String phone, {String? pwd, String? captcha}) async {
- return await loginApi.getLoginInfo(phone, password: pwd, captcha: captcha);
- }
- /// 登录
- Future<bool> login(BuildContext context, LoginInfo loginInfo) async {
- if (loginInfo.code == 0) {
- Data? data = loginInfo.data;
- if (data != null && data.user != null) {
- bool isNew = data.isNew ?? false;
- bool isFillWeight = data.user?.fillWeight ?? false;
- String username = data.user?.name ?? "";
- await saveUser(context, data.user);
- await saveUserInfo({}, token: data.token);
- if (isNew == true) {
- this.isNew = true;
- NavigatorUtil.goBasicInfo(context, username: username);
- } else if (isFillWeight != true) {
- NavigatorUtil.goPage(context, (context) => WeightPage());
- } else {
- NavigatorUtil.goHomePage(context);
- }
- return true;
- }
- }
- return false;
- }
- saveUser(BuildContext context, User? user) async {
- Map<String, dynamic> map = {};
- if (user != null) {
- map.addAll(user.toJson());
- if (user.bindInfo != null) {
- map.addAll(user.bindInfo!.toJson());
- }
- Provider.of<UserModel>(context, listen: false).saveUserInfo(user);
- }
- if (map.isNotEmpty) await saveUserInfo(map);
- }
- /// 保存用户信息到 sp 不太熟悉传对象 就这样将就一下
- saveUserInfo(Map<String, dynamic> params, {String? token}) async {
- SharedPreferences prefs = await SharedPreferences.getInstance();
- saveMap("", params, prefs);
- // 这里的因为更新的时候不会返回token 可能会覆盖 所以...
- if (token != null) {
- prefs.setString('token', token);
- }
- prefs.setBool("agreement", true);
- print("token $token");
- }
- saveMap(String root, Map<String, dynamic> params, SharedPreferences prefs) {
- params.forEach((k, value) {
- String key = "$root$k";
- if (value is String) {
- prefs.setString(key, value);
- print("putString $key $value");
- } else if (value is int) {
- prefs.setInt(key, value);
- print("putInt $key $value");
- } else if (value is bool) {
- prefs.setBool(key, value);
- print("putBool $key $value");
- } else if (value is double) {
- prefs.setDouble(key, value);
- print("putDouble $key $value");
- } else if (value is Map<String, dynamic>) {
- saveMap(key, value, prefs);
- }
- });
- }
- Future<bool> getCaptcha(String phone) async {
- var data = await loginApi.getCaptcha(phone);
- if (data['code'] == 0) {
- ToastUtil.show("验证码发送成功");
- // Fluttertoast.showToast(
- // msg: "验证码发送成功",
- // toastLength: Toast.LENGTH_SHORT,
- // gravity: ToastGravity.CENTER,
- // backgroundColor: Colors.white,
- // textColor: Colors.black,
- // fontSize: 13.0);
- return true;
- }
- return false;
- }
- Future<bool> verifyCaptch(String phone, String captcha) async {
- var data = await loginApi.verifyCaptcha(phone, captcha);
- if (data['code'] != 0) {
- // Fluttertoast.showToast(
- // msg: "${data['msg']}",
- // toastLength: Toast.LENGTH_SHORT,
- // gravity: ToastGravity.CENTER,
- // backgroundColor: Colors.white,
- // textColor: Colors.black,
- // fontSize: 13.0);
- return false;
- } else if (!data['data']) {
- ToastUtil.show("验证码输入错误");
- // Fluttertoast.showToast(
- // msg: "验证码输入错误",
- // toastLength: Toast.LENGTH_SHORT,
- // gravity: ToastGravity.CENTER,
- // backgroundColor: Colors.white,
- // textColor: Colors.black,
- // fontSize: 13.0);
- return false;
- }
- return true;
- }
- Future<bool> resetPasswordByCaptcha(String? phone, String? captcha, String? password) async {
- var data = await loginApi.resetPasswordByCaptcha(phone, captcha, password);
- if (data['code'] != 0) {
- // Fluttertoast.showToast(
- // msg: "${data['msg']}",
- // toastLength: Toast.LENGTH_SHORT,
- // gravity: ToastGravity.CENTER,
- // backgroundColor: Colors.white,
- // textColor: Colors.black,
- // fontSize: 13.0);
- return false;
- }
- return true;
- }
- Future<bool> resetPasswordByOld(String? oldPassword, String? newPassword) async {
- var data = await loginApi.resetPasswordByOld(oldPassword, newPassword);
- if (data['code'] != 0) {
- // Fluttertoast.showToast(
- // msg: "${data['msg']}",
- // toastLength: Toast.LENGTH_SHORT,
- // gravity: ToastGravity.CENTER,
- // backgroundColor: Colors.white,
- // textColor: Colors.black,
- // fontSize: 13.0);
- return false;
- }
- return true;
- }
- Future<bool> updateUserInfo(BuildContext context, String name, int gender, {int? age, int? provinceId, int? cityId, int? districtId, double? weight, int? ignoreNameCard}) async {
- 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);
- if (data.code == 0) {
- saveUser(context, data.data);
- return true;
- } else {
- // Fluttertoast.showToast(
- // msg: "${loginInfo['msg']}",
- // toastLength: Toast.LENGTH_SHORT,
- // gravity: ToastGravity.CENTER,
- // backgroundColor: Colors.white,
- // textColor: Colors.black,
- // fontSize: 13.0);
- return false;
- }
- }
- Future<bool> updateUserInfoEx(BuildContext context, {int? age, int? provinceId, int? cityId, int? districtId, double weight = 60.0, int? ignoreNameCard}) async {
- var user = Provider.of<UserModel>(context, listen: false);
- return updateUserInfo(context, user.user.name, user.user.gender!, age: age, provinceId: provinceId, cityId: cityId, districtId: districtId, weight: weight, ignoreNameCard: ignoreNameCard);
- }
- // Future<bool> updateUserInfoMap(Map<String, dynamic> params) async {
- // var loginInfo = await loginApi.updateInfoMap(params);
- //
- // if (loginInfo["code"] == 0) {
- // await _saveUserInfo(loginInfo['data']);
- // return true;
- // } else {
- // Fluttertoast.showToast(
- // msg: "${loginInfo['msg']}",
- // toastLength: Toast.LENGTH_SHORT,
- // gravity: ToastGravity.CENTER,
- // backgroundColor: Colors.white,
- // textColor: Colors.black,
- // fontSize: 13.0);
- // return false;
- // }
- // }
- }
- //const final
|