UserHelper.kt 3.9 KB
Newer Older
1
package com.yidianling.user
konghaorui committed
2 3 4

import android.text.TextUtils
import com.google.gson.Gson
5 6
import com.ydl.ydlcommon.utils.SharedPreferencesEditor
import com.ydl.ydlcommon.utils.UserInfoCache
7 8
import com.yidianling.user.api.bean.UserResponseBean
import com.yidianling.user.api.bean.UserSettingBean
konghaorui committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

/**
 * 用户信息辅助类
 */
object UserHelper {

    val gson: Gson = Gson()

    //用户信息存储
    private val user_info_name_sp = "ydl_user_info"
    private val user_info_key_sp = "ydl_user_info_key"

    //用户设置存储
    private val user_setting_name_sp = "ydl_user_setting"
    private val user_setting_key_sp = "ydl_user_setting_key"

    //用户信息//缓存
26
    private var userTemp: UserResponseBean? = null
konghaorui committed
27 28

    //用户设置信息缓存
29
    private var userSetting: UserSettingBean? = null
konghaorui committed
30 31

    //存储用户信息
32
    fun setUserinfo(userInfo: UserResponseBean?) {
konghaorui committed
33 34 35 36 37 38 39
        try {
            userTemp = userInfo
            var str = gson.toJson(userTemp)
            SharedPreferencesEditor.putFileString(user_info_name_sp, user_info_key_sp, str)

            UserInfoCache.getInstance().saveYDLUser("\"" + userInfo?.uid + "\"",
                    userInfo?.userInfo?.nick_name, userInfo?.userInfo?.head)
40

konghaorui committed
41 42 43 44 45 46
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    //获取用户设置信息
47
    fun getUserInfo(): UserResponseBean? {
konghaorui committed
48 49 50
        if (userTemp != null) return userTemp!!
        try {
            var obj = SharedPreferencesEditor.getFileString(user_info_name_sp, user_info_key_sp)
51
            var app: UserResponseBean = gson.fromJson(obj, UserResponseBean::class.java)
konghaorui committed
52 53 54
            userTemp = app
            return userTemp!!
        } catch (e: Exception) {
55
            userTemp = UserResponseBean()
konghaorui committed
56 57 58 59
        }
        return userTemp
    }

60 61
    fun getUserInfoStr(): String? {
        if (userTemp != null) return gson.toJson(userTemp)
62
        return SharedPreferencesEditor.getFileString(user_info_name_sp, user_info_key_sp)
63 64 65
    }

    fun updateUserinfo(userInfo: UserResponseBean.UserInfo?) {
konghaorui committed
66 67 68
        getUserInfo()?.userInfo = userInfo
    }

69
    fun updateUserSetting(userseting: UserSettingBean?) {
konghaorui committed
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
        setUserSetting(userseting)
    }

    /**
     * 是否登录
     */
    fun isLogin(): Boolean {
        try {
            var user = getUserInfo()?.userInfo
            if (TextUtils.isEmpty(user?.uid)) {
                return false
            }
            var uid: Int = user?.uid?.toInt() ?: 0
            if (uid > 0) {
                return true
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return false
    }

    /**
     * 是否绑定手机
     */
    fun isBindPhone(): Boolean {
        return !TextUtils.isEmpty(getUserInfo()?.userInfo?.phone)
    }

    /**
     * 存储用户设置信息
     */
102
    fun setUserSetting(userseting: UserSettingBean?) {
konghaorui committed
103 104 105 106 107 108 109 110 111 112 113 114
        try {
            userSetting = userseting
            var str = gson.toJson(userSetting)
            SharedPreferencesEditor.putFileString(user_setting_name_sp, user_setting_key_sp, str)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 获取用户设置信息
     */
115
    fun getUsetSetting(): UserSettingBean? {
konghaorui committed
116 117 118 119
        if (userSetting != null) return userSetting!!
        try {
            var obj = SharedPreferencesEditor.getFileString(user_setting_name_sp, user_setting_key_sp)
            if (TextUtils.isEmpty(obj)) {
120
                setUserSetting(UserSettingBean())
konghaorui committed
121
            }
122
            var app: UserSettingBean = gson.fromJson(obj, UserSettingBean::class.java)
konghaorui committed
123 124 125 126 127 128
            userSetting = app
            return userSetting
        } catch (e: Exception) {
        }
        return userSetting
    }
129 130 131

    fun getUserSettingStr(): String? {
        if (userSetting != null) return gson.toJson(userSetting)
132
        return SharedPreferencesEditor.getFileString(user_setting_name_sp, user_setting_key_sp)
133
    }
konghaorui committed
134
}