Skip to content

Commit 228ad52

Browse files
committed
Merge branch 'dev' into release
2 parents ed734de + cbf45a8 commit 228ad52

File tree

10 files changed

+84
-47
lines changed

10 files changed

+84
-47
lines changed

app/src/main/java/com/example/wan/android/compose/AccountComponent.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import com.example.wan.android.utils.toast
3838
fun AccountComponent(viewModel: PersonViewModel) {
3939

4040
// 将 LiveData 的数据状态化
41-
val supperUserInfo by viewModel.superUserInfo.observeAsState()
41+
val superUserInfo by viewModel.superUserInfo.observeAsState()
4242

4343
Column(
4444
modifier = Modifier
@@ -72,27 +72,27 @@ fun AccountComponent(viewModel: PersonViewModel) {
7272
) {
7373
Column(modifier = Modifier.weight(1f)) {
7474
Text(
75-
text = supperUserInfo?.userInfo?.nickname ?: "未登录",
75+
text = superUserInfo?.userInfo?.nickname ?: "未登录",
7676
fontSize = 18.sp,
7777
color = colorResource(id = R.color.primaryText)
7878
)
7979
Spacer(modifier = Modifier.height(15.dp))
8080
Row {
8181
Text(
82-
text = "用户名: ${supperUserInfo?.userInfo?.username ?: "null"}",
82+
text = "用户名: ${superUserInfo?.userInfo?.username ?: "null"}",
8383
fontSize = 14.sp,
8484
color = colorResource(id = R.color.secondaryText)
8585
)
8686
Spacer(modifier = Modifier.width(15.dp))
8787
Text(
88-
text = "id: ${supperUserInfo?.userInfo?.id ?: "-1"}",
88+
text = "id: ${superUserInfo?.userInfo?.id ?: "-1"}",
8989
fontSize = 14.sp,
9090
color = colorResource(id = R.color.secondaryText)
9191
)
9292
}
9393
Spacer(modifier = Modifier.height(15.dp))
9494
Text(
95-
text = "邮箱: ${supperUserInfo?.userInfo?.email ?: "null"}",
95+
text = "邮箱: ${superUserInfo?.userInfo?.email ?: "null"}",
9696
fontSize = 14.sp,
9797
color = colorResource(id = R.color.secondaryText)
9898
)
@@ -125,13 +125,13 @@ fun AccountComponent(viewModel: PersonViewModel) {
125125
)
126126
Spacer(modifier = Modifier.weight(1f))
127127
Text(
128-
text = "积分: ${supperUserInfo?.coinInfo?.coinCount}",
128+
text = "积分: ${superUserInfo?.coinInfo?.coinCount}",
129129
fontSize = 14.sp,
130130
color = colorResource(id = R.color.secondaryText)
131131
)
132132
Spacer(modifier = Modifier.width(10.dp))
133133
Text(
134-
text = "排行: ${supperUserInfo?.coinInfo?.rank}",
134+
text = "排行: ${superUserInfo?.coinInfo?.rank}",
135135
fontSize = 14.sp,
136136
color = colorResource(id = R.color.secondaryText)
137137
)
@@ -163,7 +163,7 @@ fun AccountComponent(viewModel: PersonViewModel) {
163163
)
164164
Spacer(modifier = Modifier.weight(1f))
165165
Text(
166-
text = "收藏量: ${supperUserInfo?.collectArticleInfo?.count}",
166+
text = "收藏量: ${superUserInfo?.collectArticleInfo?.count}",
167167
fontSize = 14.sp,
168168
color = colorResource(id = R.color.secondaryText)
169169
)

app/src/main/java/com/example/wan/android/data/model/SupperUserInfo.kt renamed to app/src/main/java/com/example/wan/android/data/model/SuperUserInfo.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.example.wan.android.data.model
22

3-
data class SupperUserInfo(
3+
data class SuperUserInfo(
44
val coinInfo: CoinInfo?,
55
val collectArticleInfo: CollectArticleInfo?,
66
val userInfo: UserInfo

app/src/main/java/com/example/wan/android/index/login/LoginViewModel.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import androidx.lifecycle.MutableLiveData
55
import com.example.wan.android.base.BaseViewModel
66
import com.example.wan.android.constant.AppConst
77
import com.example.wan.android.data.model.LoginFormState
8-
import com.example.wan.android.data.model.SupperUserInfo
8+
import com.example.wan.android.data.model.SuperUserInfo
99
import com.example.wan.android.data.repository.WanRepository
1010
import com.example.wan.android.utils.UserUtils
1111

@@ -21,8 +21,8 @@ class LoginViewModel : BaseViewModel() {
2121
val result = WanRepository.login(username, password)
2222
// 保存用户信息
2323
// 获取用户信息接口返回的数据包含 UserInfo 和其它信息, 而登录注册只返回 UserInfo
24-
UserUtils.saveSupperUserInfo(
25-
SupperUserInfo(
24+
UserUtils.saveSuperUserInfo(
25+
SuperUserInfo(
2626
userInfo = result,
2727
coinInfo = null,
2828
collectArticleInfo = null,
@@ -37,8 +37,8 @@ class LoginViewModel : BaseViewModel() {
3737
launch {
3838
changeLoadingState(AppConst.loading)
3939
val result = WanRepository.register(username, password)
40-
UserUtils.saveSupperUserInfo(
41-
SupperUserInfo(
40+
UserUtils.saveSuperUserInfo(
41+
SuperUserInfo(
4242
userInfo = result,
4343
coinInfo = null,
4444
collectArticleInfo = null,

app/src/main/java/com/example/wan/android/index/person/PersonFragment.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import by.kirich1409.viewbindingdelegate.viewBinding
1313
import com.example.wan.android.R
1414
import com.example.wan.android.base.fragment.VVMBaseFragment
1515
import com.example.wan.android.constant.EventBus
16-
import com.example.wan.android.data.model.SupperUserInfo
16+
import com.example.wan.android.data.model.SuperUserInfo
1717
import com.example.wan.android.databinding.FragmentPersonBinding
1818
import com.example.wan.android.index.like.ArticleLikeActivity
1919
import com.example.wan.android.index.login.LoginActivity
@@ -60,12 +60,12 @@ class PersonFragment : VVMBaseFragment<PersonViewModel, FragmentPersonBinding>()
6060
}
6161

6262
private fun resetInfo() {
63-
val supperUserInfo = SupperUserInfo()
64-
setInfo(supperUserInfo)
63+
val superUserInfo = SuperUserInfo()
64+
setInfo(superUserInfo)
6565
}
6666

6767
@SuppressLint("SetTextI18n")
68-
private fun setInfo(it: SupperUserInfo) {
68+
private fun setInfo(it: SuperUserInfo) {
6969
binding.run {
7070
it.collectArticleInfo?.let {
7171
tvLikeNum.text = "收藏量: ${it.count}"
@@ -90,9 +90,9 @@ class PersonFragment : VVMBaseFragment<PersonViewModel, FragmentPersonBinding>()
9090

9191
private fun initView() {
9292
resetInfo()
93-
val supperUserInfo = UserUtils.getSupperUserInfo()
94-
if (supperUserInfo != null) {
95-
setInfo(supperUserInfo)
93+
val superUserInfo = UserUtils.getSuperUserInfo()
94+
if (superUserInfo != null) {
95+
setInfo(superUserInfo)
9696
}
9797

9898
binding.refresh.setOnRefreshListener {
@@ -198,7 +198,10 @@ class PersonFragment : VVMBaseFragment<PersonViewModel, FragmentPersonBinding>()
198198
}
199199

200200
override fun onLoginSucceed() {
201-
setInfo(UserUtils.getSupperUserInfo()!!)
201+
val superUserInfo = UserUtils.getSuperUserInfo()
202+
if (superUserInfo != null) {
203+
setInfo(superUserInfo)
204+
}
202205
// 登录注册接口没有返回其它信息 获取完整的信息
203206
initData()
204207
}

app/src/main/java/com/example/wan/android/index/person/PersonViewModel.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ package com.example.wan.android.index.person
22

33
import androidx.lifecycle.MutableLiveData
44
import com.example.wan.android.base.BaseViewModel
5-
import com.example.wan.android.data.model.SupperUserInfo
5+
import com.example.wan.android.data.model.SuperUserInfo
66
import com.example.wan.android.data.repository.WanRepository
77
import com.example.wan.android.utils.UserUtils
88

99
class PersonViewModel : BaseViewModel() {
1010

11-
val superUserInfo = MutableLiveData<SupperUserInfo>()
11+
val superUserInfo = MutableLiveData<SuperUserInfo>()
1212

1313
fun getUserInfo() {
1414
launch {
1515
val result = WanRepository.getUserInfo()
1616
// 保存用户信息
1717
// 获取用户信息接口返回的数据包含 UserInfo 和其它信息, 而登录注册只返回 UserInfo
18-
UserUtils.saveSupperUserInfo(result)
18+
UserUtils.saveSuperUserInfo(result)
1919
superUserInfo.postValue(result)
2020
}
2121
}

app/src/main/java/com/example/wan/android/index/setting/SettingActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class SettingActivity : VVMBaseActivity<SettingViewModel, ActivitySettingBinding
162162
putExtra(
163163
Intent.EXTRA_TEXT, MyAppUtils.getAppInfo(
164164
"null",
165-
UserUtils.getSupperUserInfo()?.userInfo?.username ?: "null"
165+
UserUtils.getSuperUserInfo()?.userInfo?.username ?: "null"
166166
)
167167
)
168168
putExtra(Intent.EXTRA_STREAM, File(zipFilePath).getUri())
@@ -192,7 +192,7 @@ class SettingActivity : VVMBaseActivity<SettingViewModel, ActivitySettingBinding
192192
AppDetailDialog(
193193
context = activity,
194194
env = AppConst.BASE_URL,
195-
uid = "${UserUtils.getSupperUserInfo()?.userInfo?.id ?: "null"}",
195+
uid = "${UserUtils.getSuperUserInfo()?.userInfo?.id ?: "null"}",
196196
).show()
197197
}
198198
}

app/src/main/java/com/example/wan/android/network/api/UserService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.example.wan.android.network.api
22

3-
import com.example.wan.android.data.model.SupperUserInfo
3+
import com.example.wan.android.data.model.SuperUserInfo
44
import com.example.wan.android.data.model.UserInfo
55
import okhttp3.ResponseBody
66
import retrofit2.http.Field
@@ -37,7 +37,7 @@ interface UserService {
3737

3838
// 个人信息接口
3939
@GET("/user/lg/userinfo/json")
40-
suspend fun getUserInfo(): ApiResult<SupperUserInfo>
40+
suspend fun getUserInfo(): ApiResult<SuperUserInfo>
4141

4242
// 未读消息数量
4343
@GET("/message/lg/count_unread/json")
Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
11
package com.example.wan.android.utils
22

3+
import android.util.Base64
4+
import javax.crypto.Cipher
5+
import javax.crypto.SecretKey
6+
import javax.crypto.spec.SecretKeySpec
7+
38
fun String.encrypt(key: String): String {
4-
// todo 假装已经加密
5-
return this
9+
// 1. 生成密钥
10+
val secretKey = generateKey(key)
11+
val cipher = Cipher.getInstance("AES")
12+
cipher.init(Cipher.ENCRYPT_MODE, secretKey)
13+
// 2. 把字符串转为字节数组
14+
val toByteArray = this.toByteArray(Charsets.UTF_8)
15+
// 3. 使用 AES 加密字节数组
16+
val encryptedBytes = cipher.doFinal(toByteArray)
17+
// 4. 使用 Base64 把字节数组编码成字符串,以便存储或传输
18+
return Base64.encodeToString(encryptedBytes, Base64.NO_WRAP)
619
}
720

821
fun String.decrypt(key: String): String {
9-
// todo 假装我在解密
10-
return this
22+
// 1. 生成密钥
23+
val secretKey = generateKey(key)
24+
val cipher = Cipher.getInstance("AES")
25+
cipher.init(Cipher.DECRYPT_MODE, secretKey)
26+
// 2. 使用 Base64 把字符串解码成字节数组
27+
val decodeBase64 = Base64.decode(this, Base64.NO_WRAP)
28+
// 3. 使用 AES 解密字节数组
29+
val decryptedBytes = cipher.doFinal(decodeBase64)
30+
// 4. 把字节数组还原字符串
31+
return String(decryptedBytes, Charsets.UTF_8)
32+
}
33+
34+
// 生成一个 16 字节的密钥
35+
private fun generateKey(key: String): SecretKey {
36+
val keyBytes = key.toByteArray(Charsets.UTF_8).copyOf(16) // AES 要求密钥长度为 16 字节
37+
return SecretKeySpec(keyBytes, "AES")
1138
}

app/src/main/java/com/example/wan/android/utils/SerializableUtils.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package com.example.wan.android.utils
33
import com.blankj.utilcode.util.ConvertUtils
44
import java.io.Serializable
55

6-
fun Serializable.toByteArray(): ByteArray {
6+
fun Serializable.serializable2ByteArray(): ByteArray {
77
return ConvertUtils.serializable2Bytes(this)
88
}
99

10-
fun ByteArray.toObject(): Any {
10+
fun ByteArray.byteArray2Serializable(): Any {
1111
return ConvertUtils.bytes2Object(this)
1212
}

app/src/main/java/com/example/wan/android/utils/UserUtils.kt

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,40 @@ package com.example.wan.android.utils
22

33
import com.blankj.utilcode.util.GsonUtils
44
import com.blankj.utilcode.util.SPUtils
5-
import com.example.wan.android.data.model.SupperUserInfo
5+
import com.example.wan.android.data.model.SuperUserInfo
66

77
object UserUtils {
8-
private val spUtils = SPUtils.getInstance("UserInfo")
8+
private const val SP_NAME = "UserInfo"
9+
private const val SP_KEY = "SuperUserInfo"
910
private const val ENCRYPT_KEY = "UserInfo"
11+
private val spUtils = SPUtils.getInstance(SP_NAME)
1012

11-
fun saveSupperUserInfo(supperUserInfo: SupperUserInfo) {
12-
val json = supperUserInfo.toJson() ?: ""
13-
val encryption = json.encrypt(ENCRYPT_KEY)
14-
spUtils.put("SupperUserInfo", encryption)
13+
fun saveSuperUserInfo(superUserInfo: SuperUserInfo) {
14+
try {
15+
val json = superUserInfo.toJson()
16+
val encryption = json?.encrypt(ENCRYPT_KEY)
17+
spUtils.put(SP_KEY, encryption)
18+
} catch (e: Exception) {
19+
e.printStackTrace()
20+
}
1521
}
1622

17-
fun getSupperUserInfo(): SupperUserInfo? {
18-
val encryption = spUtils.getString("SupperUserInfo")
19-
val json = encryption.decrypt(ENCRYPT_KEY)
23+
fun getSuperUserInfo(): SuperUserInfo? {
2024
return try {
21-
GsonUtils.fromJson(json, SupperUserInfo::class.java)
25+
val encryption = spUtils.getString(SP_KEY, null)
26+
val json = encryption?.decrypt(ENCRYPT_KEY)
27+
GsonUtils.fromJson(json, SuperUserInfo::class.java)
2228
} catch (e: Exception) {
29+
e.printStackTrace()
2330
null
2431
}
2532
}
2633

2734
fun clear() {
2835
spUtils.remove("UserInfo")
29-
spUtils.remove("SupperUserInfo")
36+
spUtils.remove("SuperUserInfo")
3037
}
3138

32-
val isLogin get() = getSupperUserInfo()?.userInfo != null && getSupperUserInfo()?.userInfo?.id != null
39+
val isLogin get() = getSuperUserInfo()?.userInfo != null && getSuperUserInfo()?.userInfo?.id != null
3340

3441
}

0 commit comments

Comments
 (0)