package com.example.defenseapplication.utils

import bean.LoginBean
import com.blankj.utilcode.util.SPUtils
import com.google.gson.Gson

//存储登录的token和userid
object UserinfoUtil {

    private const val KEY_USER = "NAME_USER"
    private val gson = Gson()

    fun saveUserInfo(userinfo: LoginBean?) {
        userinfo ?: return
        ApiConfig.updateAuthToken(userinfo.access_token);
        val json = gson.toJson(userinfo)
        SPUtils.getInstance().put(KEY_USER, json)
    }

    fun getUserInfo(): LoginBean? {
        val string = SPUtils.getInstance().getString(KEY_USER)
        if (string.isNullOrEmpty()) {
            return null
        }
        return gson.fromJson(string, LoginBean::class.java)
    }

    fun clearUserInfo() {
        SPUtils.getInstance().remove(KEY_USER)
        ApiConfig.updateAuthToken("")
    }

}