123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- package com.example.myapplication.service;
- import android.content.Context;
- import android.content.SharedPreferences;
- import com.example.myapplication.model.entity.UserEntity;
- import com.example.myapplication.model.entity.UserRegisterEntity;
- import com.example.myapplication.service.api.ApiServiceManager;
- import com.example.myapplication.utils.CacheUtils;
- import com.example.myapplication.utils.Constants;
- import com.example.myapplication.utils.SPUtils;
- public class UserService {
- private static UserService mInstance;
- private static Context mContext;
- private static UserEntity userEntity;
- public static String sAccessToken;
- public static UserService getInstance(Context context) {
- if (mInstance == null) {
- mInstance = new UserService();
- mContext = context.getApplicationContext();
- }
- return mInstance;
- }
- public UserEntity getLocalUserInfo() {
- if (userEntity == null) {
- setUpLocalUserInfo();
- }
- return userEntity;
- }
- public boolean isLogin() {
- return getLocalUserInfo() != null;
- }
- public void setUpLocalUserInfo() {
- SharedPreferences sharedPreferences = mContext.getSharedPreferences(Constants.SP_FILE_NAME_LOCAL_USER_ENTITY, Context.MODE_PRIVATE);
- if (!sharedPreferences.contains(Constants.SP_KEY_USER_ACCESS_TOKEN)) {
- return;
- }
- userEntity = new UserEntity();
- Long userId = sharedPreferences.getLong(Constants.SP_KEY_USER_ID, 0l);
- // String username = sharedPreferences.getString(Constants.SP_KEY_USER_NAME, null);
- // String nickname = sharedPreferences.getString(Constants.SP_KEY_USER_NICKNAME, null);
- String accessToken = sharedPreferences.getString(Constants.SP_KEY_USER_ACCESS_TOKEN, "");
- // String avatar = sharedPreferences.getString(Constants.SP_KEY_USER_AVATAR, null);
- String phoneNumber = sharedPreferences.getString(Constants.SP_KEY_PHONE_NUMBER, null);
- String avatar = sharedPreferences.getString(Constants.SP_KEY_USER_PHOTO, "");
- // userEntity.setAvatar(avatar);
- // userEntity.setUserId(userId);
- // userEntity.setCellphone(phoneNumber);
- // userEntity.setPayPwd(sharedPreferences.getInt(Constants.SP_KEY_IS_SET_PAY_PWD, 0));
- // userEntity.setUsername(username);
- // userEntity.setNickname(nickname);
- // userEntity.setToken(accessToken);
- // userEntity.setAvatar(avatar);
- ApiServiceManager.getInstance().setAccessToken(accessToken);
- sAccessToken = accessToken;
- }
- /**
- * 保存用户的基本信息,包括token、id等
- */
- public static void saveLocalUserInfo(final UserEntity userEntity) { //save user info after login.
- UserService.userEntity = userEntity;
- SharedPreferences sharedPreferences = mContext.getSharedPreferences(Constants.SP_FILE_NAME_LOCAL_USER_ENTITY, Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sharedPreferences.edit();
- // editor.putLong(Constants.SP_KEY_USER_ID, userEntity.getUserId());
- // if (!TextUtils.isEmpty(userEntity.getAvatar())) {
- // editor.putString(Constants.SP_KEY_USER_PHOTO, userEntity.getAvatar());
- // }
- // if (null != userEntity.getCellphone()) {
- // editor.putString(Constants.SP_KEY_PHONE_NUMBER, userEntity.getCellphone());
- // }
- //
- // if (-1 != userEntity.getPayPwd()) {
- // editor.putInt(Constants.SP_KEY_IS_SET_PAY_PWD, userEntity.getPayPwd());
- // }
- //
- //
- // if (!TextUtils.isEmpty(userEntity.getCorpName())) {
- // editor.putString(Constants.SP_KEY_CORP_NAME, userEntity.getCorpName());
- //
- // }
- //可以为null “”
- // String mobilephone = userEntity.getMobilephone() == null ? "" : userEntity.getMobilephone();
- // editor.putString(Constants.SP_KEY_MOBILE_PHONE, mobilephone);
- //
- // if (null != userEntity.getToken()) {
- // editor.putString(Constants.SP_KEY_USER_ACCESS_TOKEN, userEntity.getToken());
- // }
- ////
- // ApiServiceManager.getInstance().setAccessToken(userEntity.getToken());
- editor.commit();
- // }
- }
- public void saveUserAndNotify(UserEntity userEntity) {
- saveLocalUserInfo(userEntity);
- sendUserEntitySyncEvent();
- }
- public void saveUserAndNotify(UserRegisterEntity userRegisterEntity) {
- // saveLocalUserInfo(userRegisterEntity);
- // sendUserEntitySyncEvent();
- }
- public void sendUserEntitySyncEvent() {
- }
- // public void onUserLogin(UserEntity userEntity) {
- // saveLocalUserInfo(userEntity);
- // sendUserEntitySyncEvent();
- // }
- public void logout() {
- String phoneNumber = getUserAccount();
- SharedPreferences sp = mContext.getSharedPreferences(Constants.SP_FILE_NAME_LOCAL_USER_ENTITY, Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sp.edit();
- editor.clear();
- editor.commit();
- userEntity = null;
- ApiServiceManager.getInstance().setAccessToken(null);
- saveUserAccount(phoneNumber);
- }
- public String getUserAccount() {
- SharedPreferences sharedPreferences = mContext
- .getSharedPreferences(Constants.SP_FILE_NAME_LOCAL_USER_ENTITY, Context.MODE_PRIVATE);
- return sharedPreferences.getString(Constants.SP_KEY_PHONE_NUMBER, "");
- }
- public void saveUserAccount(String phoneNumber) {
- SharedPreferences sharedPreferences = mContext
- .getSharedPreferences(Constants.SP_FILE_NAME_LOCAL_USER_ENTITY, Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sharedPreferences.edit();
- editor.putString(Constants.SP_KEY_PHONE_NUMBER, phoneNumber);
- editor.commit();
- }
- public String getToken() {
- SharedPreferences sharedPreferences = mContext
- .getSharedPreferences(Constants.SP_FILE_NAME_LOCAL_USER_ENTITY, Context.MODE_PRIVATE);
- return sharedPreferences.getString(Constants.SP_KEY_USER_ACCESS_TOKEN, null);
- }
- public boolean hasSetPayPassword() {
- SharedPreferences sharedPreferences = mContext
- .getSharedPreferences(Constants.SP_FILE_NAME_LOCAL_USER_ENTITY, Context.MODE_PRIVATE);
- return 1 == sharedPreferences.getInt(Constants.SP_KEY_IS_SET_PAY_PWD, 0);
- }
- public void setHasSetPayPassword(int hasSetPayPassword) {
- SharedPreferences sharedPreferences = mContext
- .getSharedPreferences(Constants.SP_FILE_NAME_LOCAL_USER_ENTITY, Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sharedPreferences.edit();
- editor.putInt(Constants.SP_KEY_IS_SET_PAY_PWD, hasSetPayPassword);
- editor.commit();
- }
- /**
- * 接收通知的常用手机号 有cellphone 不同
- */
- public String getMobilePhone() {
- SharedPreferences sharedPreferences = mContext
- .getSharedPreferences(Constants.SP_FILE_NAME_LOCAL_USER_ENTITY, Context.MODE_PRIVATE);
- return sharedPreferences.getString(Constants.SP_KEY_MOBILE_PHONE, "");
- }
- public void saveClientId(String clientId) {
- SharedPreferences sharedPreferences = mContext
- .getSharedPreferences(Constants.SP_FILE_NAME_CLIENT_ID, Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sharedPreferences.edit();
- editor.putString(Constants.SP_KEY_CLIENT_ID, clientId);
- editor.apply();
- }
- public String getClientId() {
- SharedPreferences sharedPreferences = mContext
- .getSharedPreferences(Constants.SP_FILE_NAME_CLIENT_ID, Context.MODE_PRIVATE);
- return sharedPreferences.getString(Constants.SP_KEY_CLIENT_ID, "");
- }
- }
|