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, ""); } }