Hibok
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

256 linhas
9.0 KiB

  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:amap_location/amap_location.dart';
  4. import 'package:chat/data/constants.dart';
  5. import 'package:chat/utils/HttpUtil.dart';
  6. import 'package:crclib/crclib.dart';
  7. import 'package:shared_preferences/shared_preferences.dart';
  8. import 'package:vibration/vibration.dart';
  9. import '../models/UserInfo.dart';
  10. class UserData {
  11. //私有构造函数
  12. UserData._internal();
  13. //保存单例
  14. static UserData _singleton = new UserData._internal();
  15. //工厂构造函数
  16. factory UserData() => _singleton;
  17. int language = 0;
  18. int deviceLanguage = 0;
  19. double latitude = 10.7517368433358;
  20. double longitude = 106.600798525456;
  21. UserInfo basicInfo = new UserInfo();
  22. int totalMoney = 0; //用户总金额
  23. String bindCardId = '';
  24. bool isCanProgram = true; //用户能否发约会节目
  25. int picNum = 0; //用户相册的数量
  26. int incomeMoney = 0; //用户收入金额
  27. int frozenMoney = 0; //用户冻结金额
  28. bool isFirstTip = false; //第一次进来提示完善资料
  29. int mobile = 0; //是否绑定手机号
  30. int freeNum = 0; //免费次数
  31. int usedNum = 0; //使用的次数
  32. bool haveReview = false; //有照片在审核
  33. String memberEndTime = ''; //会员到期时间
  34. bool hasLocationPermission = false; //是否有定位权限
  35. bool isBannerStatus = false; //是否支付状态
  36. int agentId = 0; //代理id
  37. int redPacketSW = 0; //红包开关
  38. int giftSwitch = 0; //打赏礼物开关
  39. int bannerZF = 0; //苹果内购开关
  40. int sign = 0; //签到开关
  41. int groupQRCode = 0; //群二维码开关
  42. int addFdDistanceSwitch = 0; //二维码加好友距离判断开关
  43. int inviteCodePrice = 0; //绑定邀请码奖励
  44. int accountPrice = 0; //解锁账号金额
  45. int redPhotoPrice = 0; //红包照片金额
  46. int addProgramPrice = 120; //发布节目金额
  47. double keyboardHeight = 0; //缓存键盘高度
  48. bool privatyMsgPushSwitch = true; //私人消息推送开关
  49. bool needRequestPushPermission = false; //是否需要推送权限
  50. bool newDateSwitch = false; //有新约会提醒开关
  51. bool applyCheckSwitch = false; //查看资料提醒开关
  52. bool acceptCheckSwitch = false; //通过查看资料提醒开关
  53. bool checkPhotoSwitch = false; //查看红包照片提醒开关
  54. bool codeSucessSwitch = false; //邀请码申请成功提醒开关
  55. bool stationOpenSwitch = true; //开启电台开关
  56. bool myProgramOpenSwitch = true; //我的节目开关
  57. bool nearbyOpenSwitch = true; //附近的人开关
  58. bool isTranslateUser = false;
  59. List contactList = [];
  60. bool isInChina = false;
  61. int homemainIndex = 0;
  62. String currentCity = '';
  63. String deviceLanguageCode;
  64. bool shockNoticSwitch = true; //震动通知
  65. bool isShocking = false; //是否震动中
  66. Map<String, dynamic> toJson() {
  67. Map<String, dynamic> json = new Map<String, dynamic>();
  68. json['language'] = language;
  69. json['deviceLanguage'] = deviceLanguage;
  70. json['latitude'] = latitude;
  71. json['longitude'] = longitude;
  72. json['basicInfo'] = jsonEncode(basicInfo).toString();
  73. json['totalMoney'] = totalMoney;
  74. json['bindCardId'] = bindCardId;
  75. json['isCanProgram'] = isCanProgram;
  76. json['picNum'] = picNum;
  77. json['incomeMoney'] = incomeMoney;
  78. json['frozenMoney'] = frozenMoney;
  79. json['isFirstTip'] = isFirstTip;
  80. json['mobile'] = mobile;
  81. json['freeNum'] = freeNum;
  82. json['usedNum'] = usedNum;
  83. json['haveReview'] = haveReview;
  84. json['memberEndTime'] = memberEndTime;
  85. json['isBannerStatus'] = isBannerStatus;
  86. json['agentId'] = agentId;
  87. json['redPacketSW'] = redPacketSW;
  88. json['giftSwitch'] = giftSwitch;
  89. json['bannerZF'] = bannerZF;
  90. json['sign'] = sign;
  91. json['groupQRCode'] = groupQRCode;
  92. json['addFdDistanceSwitch'] = addFdDistanceSwitch;
  93. json['inviteCodePrice'] = inviteCodePrice;
  94. json['accountPrice'] = accountPrice;
  95. json['redPhotoPrice'] = redPhotoPrice;
  96. json['addProgramPrice'] = addProgramPrice;
  97. json['keyboardHeight'] = keyboardHeight;
  98. json['privatyMsgPushSwitch'] = privatyMsgPushSwitch;
  99. json['needRequestPushPermission'] = needRequestPushPermission;
  100. json['newDateSwitch'] = newDateSwitch;
  101. json['applyCheckSwitch'] = applyCheckSwitch;
  102. json['acceptCheckSwitch'] = acceptCheckSwitch;
  103. json['checkPhotoSwitch'] = checkPhotoSwitch;
  104. json['codeSucessSwitch'] = codeSucessSwitch;
  105. json['stationOpenSwitch'] = stationOpenSwitch;
  106. json['myProgramOpenSwitch'] = myProgramOpenSwitch;
  107. json['nearbyOpenSwitch'] = nearbyOpenSwitch;
  108. json['isInChina'] = isInChina;
  109. return json;
  110. }
  111. fromLocalJson(Map<String, dynamic> data) {
  112. language = data['language'];
  113. deviceLanguage = data['deviceLanguage'];
  114. latitude = data['latitude'];
  115. longitude = data['longitude'];
  116. basicInfo = UserInfo.fromLocalJson(json.decode(data['basicInfo']));
  117. totalMoney = data['totalMoney'];
  118. bindCardId = data['bindCardId'];
  119. isCanProgram = data['isCanProgram'];
  120. picNum = data['picNum'];
  121. incomeMoney = data['incomeMoney'];
  122. frozenMoney = data['frozenMoney'];
  123. isFirstTip = data['isFirstTip'];
  124. mobile = data['mobile'];
  125. freeNum = data['freeNum'];
  126. usedNum = data['usedNum'];
  127. haveReview = data['haveReview'];
  128. memberEndTime = data['memberEndTime'];
  129. isBannerStatus = data['isBannerStatus'];
  130. agentId = data['agentId'];
  131. redPacketSW = data['redPacketSW'];
  132. giftSwitch = data['giftSwitch'];
  133. bannerZF = data['bannerZF'];
  134. sign = data['sign'];
  135. groupQRCode = data['groupQRCode'];
  136. addFdDistanceSwitch = data['addFdDistanceSwitch'];
  137. inviteCodePrice = data['inviteCodePrice'];
  138. accountPrice = data['accountPrice'];
  139. redPhotoPrice = data['redPhotoPrice'];
  140. addProgramPrice = data['addProgramPrice'];
  141. keyboardHeight = data['keyboardHeight'];
  142. privatyMsgPushSwitch = data['privatyMsgPushSwitch'];
  143. needRequestPushPermission = data['needRequestPushPermission'];
  144. newDateSwitch = data['newDateSwitch'];
  145. applyCheckSwitch = data['applyCheckSwitch'];
  146. acceptCheckSwitch = data['acceptCheckSwitch'];
  147. checkPhotoSwitch = data['checkPhotoSwitch'];
  148. codeSucessSwitch = data['codeSucessSwitch'];
  149. stationOpenSwitch = data['stationOpenSwitch'];
  150. myProgramOpenSwitch = data['myProgramOpenSwitch'];
  151. nearbyOpenSwitch = data['nearbyOpenSwitch'];
  152. isInChina = data['isInChina'];
  153. }
  154. bool isMan() {
  155. return UserData().basicInfo.sex == 1;
  156. }
  157. setKeyboardHeight(double newHeight) async {
  158. SharedPreferences prefs = await SharedPreferences.getInstance();
  159. var oldHeight = prefs.getDouble(Constants.KeyboardHeight);
  160. if (oldHeight != newHeight) {
  161. prefs.setDouble(Constants.KeyboardHeight, newHeight);
  162. }
  163. }
  164. getValidHCoin() {
  165. return incomeMoney - frozenMoney;
  166. }
  167. bool get isSuperVip {
  168. return basicInfo.isMember == 2;
  169. }
  170. bool get isVip {
  171. return basicInfo.isMember > 0;
  172. }
  173. int getSessionId(int friendId) {
  174. var myId = basicInfo.userId;
  175. var idStr = myId > friendId
  176. ? friendId.toString() + myId.toString()
  177. : myId.toString() + friendId.toString();
  178. return Crc32Zlib().convert(utf8.encode(idStr));
  179. }
  180. reset() {
  181. basicInfo = new UserInfo();
  182. bindCardId = '';
  183. isCanProgram = true; //用户能否发约会节目
  184. picNum = 0; //用户相册的数量
  185. incomeMoney = 0; //用户收入金额
  186. frozenMoney = 0;
  187. isFirstTip = false; //第一次进来提示完善资料
  188. mobile = 0; //是否绑定手机号
  189. freeNum = 0; //免费次数
  190. usedNum = 0; //使用的次数
  191. haveReview = false; //有照片在审核
  192. memberEndTime = '';
  193. agentId = 0;
  194. isBannerStatus = false;
  195. homemainIndex = 0;
  196. }
  197. getCurrentPosition({callback}) async {
  198. //使用高德定位
  199. var loc = await AMapLocationClient.getLocation(false);
  200. if (loc != null && loc.latitude != null && loc.longitude != null) {
  201. UserData().latitude = loc.latitude;
  202. UserData().longitude = loc.longitude;
  203. SharedPreferences prefs = await SharedPreferences.getInstance();
  204. await prefs.setDouble(Constants.Latitude, UserData().latitude);
  205. await prefs.setDouble(Constants.Longitude, UserData().longitude);
  206. HttpUtil().getAddress(loc.longitude, loc.latitude);
  207. //HttpUtil().getAddress(106.600798525456, 10.7517368433358);
  208. if (callback != null) callback(loc);
  209. }
  210. }
  211. getCurrentCityKey() {
  212. if (UserData().basicInfo.meetPlace == null ||
  213. UserData().basicInfo.meetPlace == "") return 'HoChiMinhCIty';
  214. var list = UserData().basicInfo.meetPlace.split('-');
  215. if (list.length == 2) {
  216. return list[1];
  217. } else {
  218. return 'HoChiMinhCIty';
  219. }
  220. }
  221. shock() {
  222. //有震动通知
  223. if (UserData().shockNoticSwitch && !UserData().isShocking) {
  224. UserData().isShocking = true;
  225. Vibration.vibrate();
  226. Timer.periodic(Duration(seconds: 3), (timer) async {
  227. UserData().isShocking = false;
  228. timer.cancel();
  229. });
  230. }
  231. }
  232. }