wangzhibo
4 天以前 ae6f40460dcd56af6c5f60ba52c883854c3bac55
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
package nodomain.freeyourgadget.gadgetbridge.womp;
 
import android.content.Context;
import android.content.SharedPreferences;
 
import org.json.JSONObject;
 
/**
 * 分拣员本地登录态:保存手机号密码 7 天,不依赖服务端 session。
 */
public final class WompSession {
    private WompSession() {}
 
    public static SharedPreferences prefs(Context context) {
        return context.getSharedPreferences(WompConfig.PREFS, Context.MODE_PRIVATE);
    }
 
    public static boolean isLoggedIn(Context context) {
        SharedPreferences p = prefs(context);
        String sorterId = p.getString("sorterId", null);
        long loginAt = p.getLong("loginAt", 0);
        return sorterId != null && sorterId.length() > 0
                && (System.currentTimeMillis() - loginAt) < WompConfig.LOGIN_KEEP_MS;
    }
 
    public static void saveCredentials(Context context, String phone, String password) {
        prefs(context).edit()
                .putString("phone", phone == null ? "" : phone)
                .putString("password", password == null ? "" : password)
                .apply();
    }
 
    public static void saveLogin(Context context, String phone, String password, JSONObject sorter) {
        prefs(context).edit()
                .putString("phone", phone)
                .putString("password", password)
                .putString("sorterId", sorter.optString("sorterId"))
                .putString("businessName", sorter.optString("businessName"))
                .putString("sorterJson", sorter.toString())
                .putLong("loginAt", System.currentTimeMillis())
                .apply();
    }
 
    public static void clear(Context context) {
        SharedPreferences p = prefs(context);
        String phone = p.getString("phone", "");
        String password = p.getString("password", "");
        boolean checkedIn = p.getBoolean("checkedIn", false);
        String checkInSorterId = p.getString("checkInSorterId", "");
        String checkInWorkDate = p.getString("checkInWorkDate", "");
        p.edit().clear()
                .putString("phone", phone)
                .putString("password", password)
                .putBoolean("checkedIn", checkedIn)
                .putString("checkInSorterId", checkInSorterId)
                .putString("checkInWorkDate", checkInWorkDate)
                .apply();
    }
 
    public static void setCheckedIn(Context context, boolean checkedIn) {
        SharedPreferences.Editor editor = prefs(context).edit()
                .putBoolean("checkedIn", checkedIn);
        if (checkedIn) {
            editor.putString("checkInSorterId", getSorterId(context))
                    .putString("checkInWorkDate", today());
        }
        editor.apply();
        WompKeepAliveService.onAttendanceChanged(context);
    }
 
    public static boolean isCheckedIn(Context context) {
        SharedPreferences p = prefs(context);
        if (!p.getBoolean("checkedIn", false)) {
            return false;
        }
        String sorterId = p.getString("sorterId", "");
        String owner = p.getString("checkInSorterId", "");
        String workDate = p.getString("checkInWorkDate", "");
        return sorterId.length() > 0 && sorterId.equals(owner) && today().equals(workDate);
    }
 
    public static String today() {
        return new java.text.SimpleDateFormat("yyyy-MM-dd", java.util.Locale.CHINA)
                .format(new java.util.Date());
    }
 
    public static String getSorterId(Context context) {
        return prefs(context).getString("sorterId", "");
    }
 
    public static String getBusinessName(Context context) {
        return prefs(context).getString("businessName", "");
    }
 
    public static String getPhone(Context context) {
        return prefs(context).getString("phone", "");
    }
 
    public static String getPassword(Context context) {
        return prefs(context).getString("password", "");
    }
 
    public static String getSorterJson(Context context) {
        String json = prefs(context).getString("sorterJson", "{}");
        return json == null ? "{}" : json;
    }
}