|
@@ -0,0 +1,181 @@
|
|
|
+package com.yyxx.eyuangame.demo;
|
|
|
+
|
|
|
+import android.content.Context;
|
|
|
+import android.content.SharedPreferences;
|
|
|
+import android.text.TextUtils;
|
|
|
+
|
|
|
+import org.json.JSONException;
|
|
|
+import org.json.JSONObject;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author #Suyghur.
|
|
|
+ * Created on 2020/12/21
|
|
|
+ */
|
|
|
+public class CacheInfo {
|
|
|
+
|
|
|
+ public static RoleInfo setDemoRoleInfo(Context context, String userId) {
|
|
|
+ RoleInfo roleInfo = new RoleInfo();
|
|
|
+ long ts = System.currentTimeMillis() / 1000;
|
|
|
+ roleInfo.setRoleId("aaa" + ts);
|
|
|
+ roleInfo.setRoleName("角色名123");
|
|
|
+ roleInfo.setRoleLevel("110");
|
|
|
+ roleInfo.setRoleCTime(ts);
|
|
|
+ roleInfo.setServerId("333");
|
|
|
+ roleInfo.setServerName("服务器名333");
|
|
|
+ roleInfo.setVipLevel("1");
|
|
|
+ roleInfo.setBalance("600");
|
|
|
+ roleInfo.setTotalPurchase(0);
|
|
|
+ String info = roleInfo.toJsonString();
|
|
|
+ SharedPreferences sp = context.getSharedPreferences("app_role_info", Context.MODE_PRIVATE);
|
|
|
+ SharedPreferences.Editor editor = sp.edit();
|
|
|
+ editor.putString(userId, info);
|
|
|
+ editor.apply();
|
|
|
+ return roleInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static RoleInfo getDemoRoleInfo(Context context, String userId) {
|
|
|
+ try {
|
|
|
+ SharedPreferences sp = context.getSharedPreferences("app_role_info", Context.MODE_PRIVATE);
|
|
|
+ String info = sp.getString(userId, "");
|
|
|
+ if (TextUtils.isEmpty(info)) {
|
|
|
+ //不存在则创建
|
|
|
+ return setDemoRoleInfo(context, userId);
|
|
|
+ } else {
|
|
|
+ JSONObject jsonObject = new JSONObject(info);
|
|
|
+ RoleInfo roleInfo = new RoleInfo();
|
|
|
+ roleInfo.setRoleId(jsonObject.getString("role_id"));
|
|
|
+ roleInfo.setRoleName(jsonObject.getString("role_name"));
|
|
|
+ roleInfo.setRoleLevel(jsonObject.getString("role_level"));
|
|
|
+ roleInfo.setRoleCTime(jsonObject.getLong("role_ctime"));
|
|
|
+ roleInfo.setServerId(jsonObject.getString("server_id"));
|
|
|
+ roleInfo.setServerName(jsonObject.getString("server_name"));
|
|
|
+ roleInfo.setVipLevel(jsonObject.getString("vip_level"));
|
|
|
+ roleInfo.setBalance(jsonObject.getString("balance"));
|
|
|
+ roleInfo.setTotalPurchase(jsonObject.getInt("total_purchase"));
|
|
|
+ return roleInfo;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static class RoleInfo {
|
|
|
+
|
|
|
+ private String roleId = "";
|
|
|
+ private String roleName = "";
|
|
|
+ private String roleLevel = "";
|
|
|
+ private long roleCTime = 0L;
|
|
|
+ private String serverId = "";
|
|
|
+ private String serverName = "";
|
|
|
+ private String vipLevel = "";
|
|
|
+ private String balance = "";
|
|
|
+ private int totalPurchase = 0;
|
|
|
+
|
|
|
+ public String getRoleId() {
|
|
|
+ return roleId;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setRoleId(String roleId) {
|
|
|
+ this.roleId = roleId;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getRoleName() {
|
|
|
+ return roleName;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setRoleName(String roleName) {
|
|
|
+ this.roleName = roleName;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getRoleLevel() {
|
|
|
+ return roleLevel;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setRoleLevel(String roleLevel) {
|
|
|
+ this.roleLevel = roleLevel;
|
|
|
+ }
|
|
|
+
|
|
|
+ public long getRoleCTime() {
|
|
|
+ return roleCTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setRoleCTime(long roleCTime) {
|
|
|
+ this.roleCTime = roleCTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getServerId() {
|
|
|
+ return serverId;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setServerId(String serverId) {
|
|
|
+ this.serverId = serverId;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getServerName() {
|
|
|
+ return serverName;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setServerName(String serverName) {
|
|
|
+ this.serverName = serverName;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getVipLevel() {
|
|
|
+ return vipLevel;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setVipLevel(String vipLevel) {
|
|
|
+ this.vipLevel = vipLevel;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getBalance() {
|
|
|
+ return balance;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setBalance(String balance) {
|
|
|
+ this.balance = balance;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getTotalPurchase() {
|
|
|
+ return totalPurchase;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setTotalPurchase(int totalPurchase) {
|
|
|
+ this.totalPurchase = totalPurchase;
|
|
|
+ }
|
|
|
+
|
|
|
+ public final String toJsonString() {
|
|
|
+ try {
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("role_id", this.roleId);
|
|
|
+ jsonObject.put("role_name", this.roleName);
|
|
|
+ jsonObject.put("role_level", this.roleLevel);
|
|
|
+ jsonObject.put("server_id", this.serverId);
|
|
|
+ jsonObject.put("server_name", this.serverName);
|
|
|
+ jsonObject.put("vip_level", this.vipLevel);
|
|
|
+ jsonObject.put("balance", this.balance);
|
|
|
+ jsonObject.put("total_purchase", this.totalPurchase);
|
|
|
+ jsonObject.put("role_ctime", this.roleCTime);
|
|
|
+ return jsonObject.toString();
|
|
|
+ } catch (JSONException var2) {
|
|
|
+ var2.printStackTrace();
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String toString() {
|
|
|
+ return "RoleInfo{" +
|
|
|
+ "roleId='" + roleId + '\'' +
|
|
|
+ ", roleName='" + roleName + '\'' +
|
|
|
+ ", roleLevel='" + roleLevel + '\'' +
|
|
|
+ ", roleCTime=" + roleCTime +
|
|
|
+ ", serverCode='" + serverId + '\'' +
|
|
|
+ ", serverName='" + serverName + '\'' +
|
|
|
+ ", vipLevel='" + vipLevel + '\'' +
|
|
|
+ ", balance='" + balance + '\'' +
|
|
|
+ ", totalPurchase=" + totalPurchase +
|
|
|
+ '}';
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|