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

import android.text.TextUtils
import com.google.gson.Gson
5 6 7 8 9
import com.ydl.ydlcommon.utils.SharedPreferencesEditor
import com.ydl.ydlcommon.utils.UserInfoCache
import com.ydl.ydlcommon.utils.YdlBuryPointUtil
import com.yidianling.router.user.UserResponse
import com.yidianling.router.user.UserSetting
konghaorui committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

/**
 * 用户信息辅助类
 */
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"

    //用户信息//缓存
    private var userTemp: UserResponse? = null

    //用户设置信息缓存
30
    private var userSetting: UserSetting? = null
konghaorui committed
31 32 33 34 35 36 37 38 39 40

    //存储用户信息
    fun setUserinfo(userInfo: UserResponse?) {
        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)
41 42

            YdlBuryPointUtil.reLogin()
konghaorui committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    //获取用户设置信息
    fun getUserInfo(): UserResponse? {
        if (userTemp != null) return userTemp!!
        try {
            var obj = SharedPreferencesEditor.getFileString(user_info_name_sp, user_info_key_sp)
            var app: UserResponse = gson.fromJson(obj, UserResponse::class.java)
            userTemp = app
            return userTemp!!
        } catch (e: Exception) {
            userTemp = UserResponse()
        }
        return userTemp
    }

    fun updateUserinfo(userInfo: UserResponse.UserInfo?) {
        getUserInfo()?.userInfo = userInfo
    }

66
    fun updateUserSetting(userseting: UserSetting?) {
konghaorui committed
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
        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)
    }

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

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