/* * ==================================================== * package : * author : Created by nansi. * time : 2019/6/27 3:04 PM * remark : * ==================================================== */ import 'dart:convert'; import 'dart:io'; import 'package:flutter/material.dart'; import 'package:recook/constants/api_v2.dart'; import 'package:recook/manager/http_manager.dart'; import 'package:recook/pages/live/models/king_coin_list_model.dart'; import 'package:recook/utils/storage/hive_store.dart'; import 'package:redux/redux.dart'; import 'package:recook/base/http_result_model.dart'; import 'package:recook/models/home_weather_model.dart'; import 'package:recook/models/user_brief_info_model.dart'; import 'package:recook/models/user_model.dart'; import 'package:recook/pages/live/tencent_im/tencent_im_tool.dart'; import 'package:recook/pages/user/mvp/user_presenter_impl.dart'; import 'package:recook/redux/recook_state.dart'; import 'package:recook/redux/user_brief_redux.dart'; import 'package:recook/utils/print_util.dart'; import 'package:recook/widgets/toast.dart'; class UserManager { static bool shouldRefresh = false; User user; UserBrief userBrief; // 天气数据 HomeWeatherModel homeWeatherModel; ValueNotifier login = ValueNotifier(false); ValueNotifier refreshShoppingCart = ValueNotifier(false); ValueNotifier refreshShoppingCartNumber = ValueNotifier(false); ValueNotifier refreshShoppingCartNumberWithPage = ValueNotifier(false); ValueNotifier refreshGoodsDetailPromotionState = ValueNotifier(false); ValueNotifier refreshUserRole = ValueNotifier(false); // 刷新店铺界面 ValueNotifier refreshShopPage = ValueNotifier(false); // 刷新我的界面 ValueNotifier refreshUserPage = ValueNotifier(false); //支付密码状态改变 ValueNotifier setPassword = ValueNotifier(false); //tabbar 点击状态更改 selectTabbarIndex点击的位置 ValueNotifier selectTabbar = ValueNotifier(false); //从webview扫描返回通知 ValueNotifier openInstallGoodsId = ValueNotifier(false); ValueNotifier openInstallLive = ValueNotifier(false); int selectTabbarIndex; List kingCoinListModelList; bool get haveLogin => login.value; String _identifier = ""; String get indentifier => _identifier; String jpushRid = ''; factory UserManager() => _getInstance(); static UserManager get instance => _getInstance(); static UserManager _instance; UserManager._internal() { // 初始化 } static UserManager _getInstance() { if (_instance == null) { _instance = new UserManager._internal(); _instance.user = User.empty(); _instance.selectTabbarIndex = 0; _instance.userBrief = UserBrief.empty(); } return _instance; } static Future updateUser(User user, Store store) async { instance.user = user; instance.login.value = true; UserManager.instance.activePeople(); String jsonStr = json.encode(user.toJson()); // await SharePreferenceUtils.setString(AppStrings.key_user, jsonStr); // store.dispatch(UpdateUserAction(user)); HiveStore.appBox.put('key_user', jsonStr); UserManager.instance.updateUserBriefInfo(store); int type; if (Platform.isIOS) { type = 2; } else if (Platform.isAndroid) { type = 1; } String code = await UserManager.updateJId(UserManager.instance.jpushRid, type); print(code); return true; } static updateUserInfo(Store store) async { String jsonStr = json.encode(instance.user.toJson()); // await SharePreferenceUtils.setString(AppStrings.key_user, jsonStr); // store.dispatch(UpdateUserAction(instance.user)); HiveStore.appBox.put('key_user', jsonStr); } static logout() async { DPrint.printf("退出登录了 -- ${instance.login.value}"); TencentIMTool.model = null; instance.user = User.empty(); instance.login.value = false; HiveStore.appBox.delete('key_user'); // await SharePreferenceUtils.remove(AppStrings.key_user); // store.dispatch(UpdateUserAction(User.empty())); } Future updateUserBriefInfo(Store store) async { UserPresenterImpl presenterImpl = UserPresenterImpl(); HttpResultModel model = await presenterImpl.getUserBriefInfo(instance.user.info.id); if (!model.result) { Toast.showInfo(model.msg); return false; } _identifier = model.data == null ? '' : model.data.identifier; _instance.userBrief = model.data; store.dispatch(UpdateUserBriefAction(model.data)); return true; } Future activePeople() async { ResultData result = await HttpManager.post(APIV2.userAPI.activePeople, { 'id': UserManager.instance.user.info.id, }); } //上传极光id static Future updateJId(String registrationId, int type) async { ResultData result = await HttpManager.post(APIV2.userAPI.updateJId, { "user_id": UserManager.instance.user.info.id, "registration_id": registrationId, "type": type //1:android 2:ios }); if (result.data != null) { return result.data['code']; } } }