664d7fd1b86f13e97d99e66329364ec4b2cc701a.svn-base 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package com.example.myapplication.service;
  2. import android.content.Context;
  3. import android.content.SharedPreferences;
  4. import com.example.myapplication.model.entity.UserEntity;
  5. import com.example.myapplication.model.entity.UserRegisterEntity;
  6. import com.example.myapplication.service.api.ApiServiceManager;
  7. import com.example.myapplication.utils.CacheUtils;
  8. import com.example.myapplication.utils.Constants;
  9. import com.example.myapplication.utils.SPUtils;
  10. public class UserService {
  11. private static UserService mInstance;
  12. private static Context mContext;
  13. private static UserEntity userEntity;
  14. public static String sAccessToken;
  15. public static UserService getInstance(Context context) {
  16. if (mInstance == null) {
  17. mInstance = new UserService();
  18. mContext = context.getApplicationContext();
  19. }
  20. return mInstance;
  21. }
  22. public UserEntity getLocalUserInfo() {
  23. if (userEntity == null) {
  24. setUpLocalUserInfo();
  25. }
  26. return userEntity;
  27. }
  28. public boolean isLogin() {
  29. return getLocalUserInfo() != null;
  30. }
  31. public void setUpLocalUserInfo() {
  32. SharedPreferences sharedPreferences = mContext.getSharedPreferences(Constants.SP_FILE_NAME_LOCAL_USER_ENTITY, Context.MODE_PRIVATE);
  33. if (!sharedPreferences.contains(Constants.SP_KEY_USER_ACCESS_TOKEN)) {
  34. return;
  35. }
  36. userEntity = new UserEntity();
  37. Long userId = sharedPreferences.getLong(Constants.SP_KEY_USER_ID, 0l);
  38. // String username = sharedPreferences.getString(Constants.SP_KEY_USER_NAME, null);
  39. // String nickname = sharedPreferences.getString(Constants.SP_KEY_USER_NICKNAME, null);
  40. String accessToken = sharedPreferences.getString(Constants.SP_KEY_USER_ACCESS_TOKEN, "");
  41. // String avatar = sharedPreferences.getString(Constants.SP_KEY_USER_AVATAR, null);
  42. String phoneNumber = sharedPreferences.getString(Constants.SP_KEY_PHONE_NUMBER, null);
  43. String avatar = sharedPreferences.getString(Constants.SP_KEY_USER_PHOTO, "");
  44. // userEntity.setAvatar(avatar);
  45. // userEntity.setUserId(userId);
  46. // userEntity.setCellphone(phoneNumber);
  47. // userEntity.setPayPwd(sharedPreferences.getInt(Constants.SP_KEY_IS_SET_PAY_PWD, 0));
  48. // userEntity.setUsername(username);
  49. // userEntity.setNickname(nickname);
  50. // userEntity.setToken(accessToken);
  51. // userEntity.setAvatar(avatar);
  52. ApiServiceManager.getInstance().setAccessToken(accessToken);
  53. sAccessToken = accessToken;
  54. }
  55. /**
  56. * 保存用户的基本信息,包括token、id等
  57. */
  58. public static void saveLocalUserInfo(final UserEntity userEntity) { //save user info after login.
  59. UserService.userEntity = userEntity;
  60. SharedPreferences sharedPreferences = mContext.getSharedPreferences(Constants.SP_FILE_NAME_LOCAL_USER_ENTITY, Context.MODE_PRIVATE);
  61. SharedPreferences.Editor editor = sharedPreferences.edit();
  62. // editor.putLong(Constants.SP_KEY_USER_ID, userEntity.getUserId());
  63. // if (!TextUtils.isEmpty(userEntity.getAvatar())) {
  64. // editor.putString(Constants.SP_KEY_USER_PHOTO, userEntity.getAvatar());
  65. // }
  66. // if (null != userEntity.getCellphone()) {
  67. // editor.putString(Constants.SP_KEY_PHONE_NUMBER, userEntity.getCellphone());
  68. // }
  69. //
  70. // if (-1 != userEntity.getPayPwd()) {
  71. // editor.putInt(Constants.SP_KEY_IS_SET_PAY_PWD, userEntity.getPayPwd());
  72. // }
  73. //
  74. //
  75. // if (!TextUtils.isEmpty(userEntity.getCorpName())) {
  76. // editor.putString(Constants.SP_KEY_CORP_NAME, userEntity.getCorpName());
  77. //
  78. // }
  79. //可以为null “”
  80. // String mobilephone = userEntity.getMobilephone() == null ? "" : userEntity.getMobilephone();
  81. // editor.putString(Constants.SP_KEY_MOBILE_PHONE, mobilephone);
  82. //
  83. // if (null != userEntity.getToken()) {
  84. // editor.putString(Constants.SP_KEY_USER_ACCESS_TOKEN, userEntity.getToken());
  85. // }
  86. ////
  87. // ApiServiceManager.getInstance().setAccessToken(userEntity.getToken());
  88. editor.commit();
  89. // }
  90. }
  91. public void saveUserAndNotify(UserEntity userEntity) {
  92. saveLocalUserInfo(userEntity);
  93. sendUserEntitySyncEvent();
  94. }
  95. public void saveUserAndNotify(UserRegisterEntity userRegisterEntity) {
  96. // saveLocalUserInfo(userRegisterEntity);
  97. // sendUserEntitySyncEvent();
  98. }
  99. public void sendUserEntitySyncEvent() {
  100. }
  101. // public void onUserLogin(UserEntity userEntity) {
  102. // saveLocalUserInfo(userEntity);
  103. // sendUserEntitySyncEvent();
  104. // }
  105. public void logout() {
  106. String phoneNumber = getUserAccount();
  107. SharedPreferences sp = mContext.getSharedPreferences(Constants.SP_FILE_NAME_LOCAL_USER_ENTITY, Context.MODE_PRIVATE);
  108. SharedPreferences.Editor editor = sp.edit();
  109. editor.clear();
  110. editor.commit();
  111. userEntity = null;
  112. ApiServiceManager.getInstance().setAccessToken(null);
  113. saveUserAccount(phoneNumber);
  114. }
  115. public String getUserAccount() {
  116. SharedPreferences sharedPreferences = mContext
  117. .getSharedPreferences(Constants.SP_FILE_NAME_LOCAL_USER_ENTITY, Context.MODE_PRIVATE);
  118. return sharedPreferences.getString(Constants.SP_KEY_PHONE_NUMBER, "");
  119. }
  120. public void saveUserAccount(String phoneNumber) {
  121. SharedPreferences sharedPreferences = mContext
  122. .getSharedPreferences(Constants.SP_FILE_NAME_LOCAL_USER_ENTITY, Context.MODE_PRIVATE);
  123. SharedPreferences.Editor editor = sharedPreferences.edit();
  124. editor.putString(Constants.SP_KEY_PHONE_NUMBER, phoneNumber);
  125. editor.commit();
  126. }
  127. public String getToken() {
  128. SharedPreferences sharedPreferences = mContext
  129. .getSharedPreferences(Constants.SP_FILE_NAME_LOCAL_USER_ENTITY, Context.MODE_PRIVATE);
  130. return sharedPreferences.getString(Constants.SP_KEY_USER_ACCESS_TOKEN, null);
  131. }
  132. public boolean hasSetPayPassword() {
  133. SharedPreferences sharedPreferences = mContext
  134. .getSharedPreferences(Constants.SP_FILE_NAME_LOCAL_USER_ENTITY, Context.MODE_PRIVATE);
  135. return 1 == sharedPreferences.getInt(Constants.SP_KEY_IS_SET_PAY_PWD, 0);
  136. }
  137. public void setHasSetPayPassword(int hasSetPayPassword) {
  138. SharedPreferences sharedPreferences = mContext
  139. .getSharedPreferences(Constants.SP_FILE_NAME_LOCAL_USER_ENTITY, Context.MODE_PRIVATE);
  140. SharedPreferences.Editor editor = sharedPreferences.edit();
  141. editor.putInt(Constants.SP_KEY_IS_SET_PAY_PWD, hasSetPayPassword);
  142. editor.commit();
  143. }
  144. /**
  145. * 接收通知的常用手机号 有cellphone 不同
  146. */
  147. public String getMobilePhone() {
  148. SharedPreferences sharedPreferences = mContext
  149. .getSharedPreferences(Constants.SP_FILE_NAME_LOCAL_USER_ENTITY, Context.MODE_PRIVATE);
  150. return sharedPreferences.getString(Constants.SP_KEY_MOBILE_PHONE, "");
  151. }
  152. public void saveClientId(String clientId) {
  153. SharedPreferences sharedPreferences = mContext
  154. .getSharedPreferences(Constants.SP_FILE_NAME_CLIENT_ID, Context.MODE_PRIVATE);
  155. SharedPreferences.Editor editor = sharedPreferences.edit();
  156. editor.putString(Constants.SP_KEY_CLIENT_ID, clientId);
  157. editor.apply();
  158. }
  159. public String getClientId() {
  160. SharedPreferences sharedPreferences = mContext
  161. .getSharedPreferences(Constants.SP_FILE_NAME_CLIENT_ID, Context.MODE_PRIVATE);
  162. return sharedPreferences.getString(Constants.SP_KEY_CLIENT_ID, "");
  163. }
  164. }