admin
2021-05-28 5965c01b38a2e83cecd7616daa11185fc2499303
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package com.tejia.lijin.app.util.user;
 
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.tejia.lijin.app.ui.mine.LoginSelectActivity;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.tejia.lijin.app.R;
import com.tejia.lijin.app.entity.UserInfo;
import com.tejia.lijin.app.entity.user.UserLoginStatus;
import com.tejia.lijin.app.ui.mine.LoginActivity;
import com.tejia.lijin.app.util.Constant;
import com.tejia.lijin.app.util.downutil.StringUtils;
 
import org.json.JSONException;
import org.json.JSONObject;
 
import static android.content.Context.MODE_PRIVATE;
 
public class UserUtil {
 
    private static Gson gson = new GsonBuilder().serializeNulls().create();
 
    /**
     * SharedPreferences中获取用户信息
     *
     * @param context
     * @return
     */
    public static UserInfo getUserInfo(Context context) {
        SharedPreferences sp = context.getSharedPreferences("user", MODE_PRIVATE);
        String userInfo = sp.getString("userinfo_new", "");
        if (StringUtils.isNullOrEmpty(userInfo))
            return null;
        UserInfo info = gson.fromJson(userInfo, new TypeToken<UserInfo>() {
        }.getType());
        return info;
    }
 
 
    /**
     * 获取登录的uid
     *
     * @param context
     * @return
     */
    public static Long getUid(Context context) {
        SharedPreferences sp = context.getSharedPreferences("user", MODE_PRIVATE);
        String uid = sp.getString("uid", "");
        if (StringUtils.isNullOrEmpty(uid) || uid.equalsIgnoreCase("0"))
            return null;
        return Long.parseLong(uid);
    }
 
 
    /**
     * 是否同意了用户协议
     *
     * @param context
     * @return
     */
    public static boolean isAgreeUserProtocol(Context context) {
        if (Constant.TEST)
            return true;
 
        SharedPreferences sp = context.getSharedPreferences("user", MODE_PRIVATE);
        String protocol = sp.getString("protocol-agree", "");
        if (StringUtils.isNullOrEmpty(protocol))
            return false;
        else
            return true;
    }
 
 
    /**
     * 是否已经显示了用户引导
     *
     * @param context
     * @return
     */
    public static boolean isShownUserGuide(Context context) {
        return true;
//        SharedPreferences sp = context.getSharedPreferences("user", MODE_PRIVATE);
//        String protocol = sp.getString("shown-user-guide", "");
//        if (StringUtils.isNullOrEmpty(protocol))
//            return false;
//        else
//            return true;
    }
 
 
    /**
     * 同意用户协议
     *
     * @param context
     */
    public static void agreeUserProtocol(Context context) {
        SharedPreferences sp = context.getSharedPreferences("user", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("protocol-agree", "1");
        editor.commit();
    }
 
 
    public static void shownUserGuide(Context context) {
        SharedPreferences sp = context.getSharedPreferences("user", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("shown-user-guide", "1");
        editor.commit();
    }
 
    /**
     * 是否登录
     *
     * @param context
     * @return
     */
    public static boolean isLogin(Context context) {
        SharedPreferences sp = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        return sp.getBoolean("isLogin", false);
    }
 
    public static void setInviteCode(Context context, String inviteCode) {
        SharedPreferences sp = context.getSharedPreferences("user-info", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("inviteCode", inviteCode);
        editor.commit();
    }
 
    public static String getInviteCode(Context context) {
        SharedPreferences sp = context.getSharedPreferences("user-info", Context.MODE_PRIVATE);
        return sp.getString("inviteCode", "");
    }
 
    /**
     * 获取登录状态
     *
     * @param context
     * @return
     */
    public static UserLoginStatus getLoginState(Context context) {
        boolean isLogin = isLogin(context);
        if (isLogin) {
            String inviteCode = getInviteCode(context);
            if (StringUtils.isNullOrEmpty(inviteCode))
                return UserLoginStatus.LOINGED_NO_ACTIVE;
            else
                return UserLoginStatus.LOGINED_ACTIVED;
 
        } else {
            return UserLoginStatus.NOT_LOGIN;
        }
    }
 
    public static void logout(Context context) {
        SharedPreferences sp = context.getSharedPreferences("user", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("isLogin", false);
        editor.putBoolean("isWxBind", false);
        editor.putString("uid", "0");
        editor.remove("userinfo");
        editor.remove("userinfo_new");
        editor.commit();
 
        //清除个人信息缓存
        sp = context.getSharedPreferences("user-info", MODE_PRIVATE);
        editor = sp.edit();
        editor.clear();
        editor.commit();
        MiPushClient.setAlias(context, sp.getString("uid", "0"), null);
    }
 
    public static int getUserLevelIcon(String level) {
        int resourceId = 0;
        switch (level) {
            case "noActive":
                resourceId = R.drawable.noactive;
                break;
            case "daRen":
            case "actived":
                resourceId = R.drawable.actived;
                break;
            case "normalVIP":
            case "vipPre1":
                resourceId = R.drawable.vippre1;
                break;
            case "highVIP":
            case "vipPre2":
                resourceId = R.drawable.vippre2;
                break;
            case "vipApply":
                resourceId = R.drawable.vipapply;
                break;
            case "superVIP":
            case "vip":
                resourceId = R.drawable.vip;
                break;
            case "tearcherApply":
                resourceId = R.drawable.tearcherapply;
                break;
            case "tearcher":
                resourceId = R.drawable.tearcher;
                break;
        }
 
        return resourceId;
    }
 
    /**
     * 登录成功
     *
     * @param context
     * @param info
     */
    public static void loginSuccess(Context context, UserInfo info) {
        SharedPreferences sp = context.getSharedPreferences("user", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("isLogin", true);
        editor.putBoolean("isFirstInput", false);
        editor.putString("uid", info.getId());
        editor.putString("openid", info.getOpenid());
        editor.putString("portrait", info.getPortrait());
        editor.putString("userinfo_new", new Gson().toJson(info));
        editor.commit();
        MiPushClient.setAlias(context, info.getId(), null);
    }
 
 
    public static void jumpLogin(Context context) {
        context.startActivity(new Intent(context, LoginSelectActivity.class));
    }
 
 
}