Files
rider-pro-android-app/app/src/main/java/com/aiosman/riderpro/data/AccountService.kt

410 lines
12 KiB
Kotlin
Raw Normal View History

2024-07-29 00:01:09 +08:00
package com.aiosman.riderpro.data
2024-08-11 17:15:17 +08:00
import com.aiosman.riderpro.data.api.ApiClient
2024-08-13 22:32:27 +08:00
import com.aiosman.riderpro.data.api.ChangePasswordRequestBody
2024-08-23 18:31:14 +08:00
import com.aiosman.riderpro.data.api.GoogleRegisterRequestBody
2024-08-11 17:15:17 +08:00
import com.aiosman.riderpro.data.api.LoginUserRequestBody
2024-09-05 22:04:41 +08:00
import com.aiosman.riderpro.data.api.RegisterMessageChannelRequestBody
2024-08-11 17:15:17 +08:00
import com.aiosman.riderpro.data.api.RegisterRequestBody
2024-08-20 19:48:12 +08:00
import com.aiosman.riderpro.data.api.UpdateNoticeRequestBody
2024-08-24 23:11:20 +08:00
import com.aiosman.riderpro.entity.AccountFavouriteEntity
import com.aiosman.riderpro.entity.AccountLikeEntity
import com.aiosman.riderpro.entity.AccountProfileEntity
import com.aiosman.riderpro.entity.NoticePostEntity
import com.aiosman.riderpro.entity.NoticeUserEntity
2024-08-20 19:48:12 +08:00
import com.google.gson.annotations.SerializedName
2024-08-11 21:17:02 +08:00
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.File
2024-08-24 23:11:20 +08:00
/**
* 用户资料
*/
2024-08-11 21:17:02 +08:00
data class AccountProfile(
2024-08-24 23:11:20 +08:00
// 用户ID
2024-08-11 17:15:17 +08:00
val id: Int,
2024-08-24 23:11:20 +08:00
// 用户名
2024-08-11 17:15:17 +08:00
val username: String,
2024-08-24 23:11:20 +08:00
// 昵称
2024-08-11 17:15:17 +08:00
val nickname: String,
2024-08-24 23:11:20 +08:00
// 头像
2024-08-11 17:15:17 +08:00
val avatar: String,
2024-08-24 23:11:20 +08:00
// 关注数
2024-08-11 17:15:17 +08:00
val followingCount: Int,
2024-08-24 23:11:20 +08:00
// 粉丝数
2024-08-11 17:15:17 +08:00
val followerCount: Int,
2024-08-24 23:11:20 +08:00
// 是否关注
val isFollowing: Boolean,
// 个人简介
val bio: String,
// 主页背景图
val banner: String?,
2024-08-11 17:15:17 +08:00
) {
2024-08-24 23:11:20 +08:00
/**
* 转换为Entity
*/
2024-08-11 17:15:17 +08:00
fun toAccountProfileEntity(): AccountProfileEntity {
return AccountProfileEntity(
id = id,
followerCount = followerCount,
followingCount = followingCount,
nickName = nickname,
2024-08-22 23:43:01 +08:00
avatar = "${ApiClient.BASE_SERVER}$avatar",
bio = bio,
2024-08-11 17:15:17 +08:00
country = "Worldwide",
isFollowing = isFollowing,
banner = banner.let {
if (!it.isNullOrEmpty()) {
return@let "${ApiClient.BASE_SERVER}$it"
}
null
}
2024-08-11 17:15:17 +08:00
)
}
}
2024-08-11 21:17:02 +08:00
2024-08-24 23:11:20 +08:00
/**
* 消息关联资料
*/
2024-08-20 19:48:12 +08:00
data class NoticePost(
2024-08-24 23:11:20 +08:00
// 动态ID
2024-08-20 19:48:12 +08:00
@SerializedName("id")
val id: Int,
2024-08-24 23:11:20 +08:00
// 动态内容
2024-08-20 19:48:12 +08:00
@SerializedName("textContent")
2024-08-24 23:11:20 +08:00
// 动态图片
2024-08-20 19:48:12 +08:00
val textContent: String,
2024-08-24 23:11:20 +08:00
// 动态图片
2024-08-20 19:48:12 +08:00
@SerializedName("images")
val images: List<Image>,
2024-08-24 23:11:20 +08:00
// 动态时间
2024-08-20 19:48:12 +08:00
@SerializedName("time")
val time: String,
) {
2024-08-24 23:11:20 +08:00
/**
* 转换为Entity
*/
2024-08-20 19:48:12 +08:00
fun toNoticePostEntity(): NoticePostEntity {
return NoticePostEntity(
id = id,
textContent = textContent,
images = images.map {
it.copy(
2024-08-22 23:43:01 +08:00
url = "${ApiClient.BASE_SERVER}${it.url}",
thumbnail = "${ApiClient.BASE_SERVER}${it.thumbnail}",
2024-08-20 19:48:12 +08:00
)
},
time = ApiClient.dateFromApiString(time)
)
}
}
2024-08-24 23:11:20 +08:00
/**
* 消息关联用户
*/
2024-08-20 19:48:12 +08:00
data class NoticeUser(
2024-08-24 23:11:20 +08:00
// 用户ID
2024-08-20 19:48:12 +08:00
@SerializedName("id")
val id: Int,
2024-08-24 23:11:20 +08:00
// 昵称
2024-08-20 19:48:12 +08:00
@SerializedName("nickName")
val nickName: String,
2024-08-24 23:11:20 +08:00
// 头像
2024-08-20 19:48:12 +08:00
@SerializedName("avatar")
val avatar: String,
) {
2024-08-24 23:11:20 +08:00
/**
* 转换为Entity
*/
2024-08-20 19:48:12 +08:00
fun toNoticeUserEntity(): NoticeUserEntity {
return NoticeUserEntity(
id = id,
nickName = nickName,
2024-08-22 23:43:01 +08:00
avatar = "${ApiClient.BASE_SERVER}$avatar",
2024-08-20 19:48:12 +08:00
)
}
}
2024-08-24 23:11:20 +08:00
/**
* 点赞消息通知
*/
2024-08-20 19:48:12 +08:00
data class AccountLike(
2024-08-24 23:11:20 +08:00
// 是否未读
2024-08-20 19:48:12 +08:00
@SerializedName("isUnread")
val isUnread: Boolean,
2024-08-24 23:11:20 +08:00
// 动态
2024-08-20 19:48:12 +08:00
@SerializedName("post")
val post: NoticePost,
2024-08-24 23:11:20 +08:00
// 点赞用户
2024-08-20 19:48:12 +08:00
@SerializedName("user")
val user: NoticeUser,
2024-08-24 23:11:20 +08:00
// 点赞时间
2024-08-20 19:48:12 +08:00
@SerializedName("likeTime")
val likeTime: String,
) {
fun toAccountLikeEntity(): AccountLikeEntity {
return AccountLikeEntity(
post = post.toNoticePostEntity(),
user = user.toNoticeUserEntity(),
likeTime = ApiClient.dateFromApiString(likeTime)
)
}
}
data class AccountFavourite(
@SerializedName("isUnread")
val isUnread: Boolean,
@SerializedName("post")
val post: NoticePost,
@SerializedName("user")
val user: NoticeUser,
@SerializedName("favoriteTime")
val favouriteTime: String,
) {
fun toAccountFavouriteEntity(): AccountFavouriteEntity {
return AccountFavouriteEntity(
post = post.toNoticePostEntity(),
user = user.toNoticeUserEntity(),
favoriteTime = ApiClient.dateFromApiString(favouriteTime)
)
}
}
data class AccountFollow(
@SerializedName("id")
val id: Int,
@SerializedName("username")
val username: String,
@SerializedName("nickname")
val nickname: String,
@SerializedName("avatar")
val avatar: String,
@SerializedName("isUnread")
val isUnread: Boolean,
@SerializedName("userId")
val userId: Int,
@SerializedName("isFollowing")
val isFollowing: Boolean,
)
//{
// "likeCount": 0,
// "followCount": 0,
// "favoriteCount": 0
//}
data class AccountNotice(
@SerializedName("likeCount")
val likeCount: Int,
@SerializedName("followCount")
val followCount: Int,
@SerializedName("favoriteCount")
val favoriteCount: Int,
@SerializedName("commentCount")
val commentCount: Int,
)
2024-07-29 00:01:09 +08:00
interface AccountService {
2024-08-24 23:11:20 +08:00
/**
* 获取登录当前用户的资料
*/
2024-08-11 17:15:17 +08:00
suspend fun getMyAccountProfile(): AccountProfileEntity
2024-08-24 23:11:20 +08:00
/**
* 获取登录的用户认证信息
*/
2024-07-31 14:50:55 +08:00
suspend fun getMyAccount(): UserAuth
2024-08-24 23:11:20 +08:00
/**
* 使用用户名密码登录
* @param loginName 用户名
* @param password 密码
*/
2024-07-31 14:50:55 +08:00
suspend fun loginUserWithPassword(loginName: String, password: String): UserAuth
2024-08-24 23:11:20 +08:00
/**
* 使用google登录
* @param googleId googleId
*/
2024-08-23 18:31:14 +08:00
suspend fun loginUserWithGoogle(googleId: String): UserAuth
2024-08-24 23:11:20 +08:00
/**
* 退出登录
*/
2024-07-31 14:50:55 +08:00
suspend fun logout()
2024-08-24 23:11:20 +08:00
/**
* 更新用户资料
* @param avatar 头像
* @param nickName 昵称
* @param bio 简介
* @param banner 主页背景图
2024-08-24 23:11:20 +08:00
*/
suspend fun updateProfile(
avatar: UploadImage?,
banner: UploadImage?,
nickName: String?,
bio: String?
)
2024-08-24 23:11:20 +08:00
/**
* 注册用户
* @param loginName 用户名
* @param password 密码
*/
2024-08-11 17:15:17 +08:00
suspend fun registerUserWithPassword(loginName: String, password: String)
2024-08-24 23:11:20 +08:00
/**
* 使用google账号注册
* @param idToken googleIdToken
*/
2024-08-23 18:31:14 +08:00
suspend fun regiterUserWithGoogleAccount(idToken: String)
2024-08-24 23:11:20 +08:00
/**
* 修改密码
* @param oldPassword 旧密码
* @param newPassword 新密码
*/
2024-08-13 22:32:27 +08:00
suspend fun changeAccountPassword(oldPassword: String, newPassword: String)
2024-08-24 23:11:20 +08:00
/**
* 获取我的点赞通知
* @param page 页码
* @param pageSize 每页数量
*/
2024-08-20 19:48:12 +08:00
suspend fun getMyLikeNotice(page: Int, pageSize: Int): ListContainer<AccountLike>
2024-08-24 23:11:20 +08:00
/**
* 获取我的关注通知
* @param page 页码
* @param pageSize 每页数量
*/
2024-08-20 19:48:12 +08:00
suspend fun getMyFollowNotice(page: Int, pageSize: Int): ListContainer<AccountFollow>
2024-08-24 23:11:20 +08:00
/**
* 获取我的收藏通知
* @param page 页码
* @param pageSize 每页数量
*/
2024-08-20 19:48:12 +08:00
suspend fun getMyFavouriteNotice(page: Int, pageSize: Int): ListContainer<AccountFavourite>
2024-08-24 23:11:20 +08:00
/**
* 获取我的通知信息
*/
2024-08-20 19:48:12 +08:00
suspend fun getMyNoticeInfo(): AccountNotice
2024-08-24 23:11:20 +08:00
/**
* 更新通知信息,更新最后一次查看时间
* @param payload 通知信息
*/
2024-08-20 19:48:12 +08:00
suspend fun updateNotice(payload: UpdateNoticeRequestBody)
2024-09-05 22:04:41 +08:00
suspend fun registerMessageChannel(client:String,identifier:String)
2024-07-29 00:01:09 +08:00
}
2024-08-23 18:31:14 +08:00
class AccountServiceImpl : AccountService {
2024-08-11 17:15:17 +08:00
override suspend fun getMyAccountProfile(): AccountProfileEntity {
val resp = ApiClient.api.getMyAccount()
val body = resp.body() ?: throw ServiceException("Failed to get account")
return body.data.toAccountProfileEntity()
2024-07-29 16:50:07 +08:00
}
2024-07-31 14:50:55 +08:00
override suspend fun getMyAccount(): UserAuth {
2024-08-11 17:15:17 +08:00
val resp = ApiClient.api.checkToken()
val body = resp.body() ?: throw ServiceException("Failed to get account")
return UserAuth(body.id)
2024-07-31 14:50:55 +08:00
}
override suspend fun loginUserWithPassword(loginName: String, password: String): UserAuth {
2024-08-11 17:15:17 +08:00
val resp = ApiClient.api.login(LoginUserRequestBody(loginName, password))
val body = resp.body() ?: throw ServiceException("Failed to login")
return UserAuth(0, body.token)
2024-07-31 14:50:55 +08:00
}
2024-08-23 18:31:14 +08:00
override suspend fun loginUserWithGoogle(googleId: String): UserAuth {
2024-08-24 23:11:20 +08:00
val resp = ApiClient.api.login(LoginUserRequestBody(googleId = googleId))
2024-08-23 18:31:14 +08:00
val body = resp.body() ?: throw ServiceException("Failed to login")
return UserAuth(0, body.token)
}
override suspend fun regiterUserWithGoogleAccount(idToken: String) {
val resp = ApiClient.api.registerWithGoogle(GoogleRegisterRequestBody(idToken))
if (resp.code() != 200) {
throw ServiceException("Failed to register")
}
}
2024-07-31 14:50:55 +08:00
override suspend fun logout() {
// do nothing
}
2024-08-11 17:15:17 +08:00
2024-08-13 22:32:27 +08:00
fun createMultipartBody(file: File, filename: String, name: String): MultipartBody.Part {
2024-08-11 21:17:02 +08:00
val requestFile = file.asRequestBody("image/*".toMediaTypeOrNull())
return MultipartBody.Part.createFormData(name, filename, requestFile)
}
override suspend fun updateProfile(
avatar: UploadImage?,
banner: UploadImage?,
nickName: String?,
bio: String?
) {
2024-08-11 21:17:02 +08:00
val nicknameField: RequestBody? = nickName?.toRequestBody("text/plain".toMediaTypeOrNull())
val bioField: RequestBody? = bio?.toRequestBody("text/plain".toMediaTypeOrNull())
2024-08-11 21:17:02 +08:00
val avatarField: MultipartBody.Part? = avatar?.let {
2024-08-13 22:32:27 +08:00
createMultipartBody(it.file, it.filename, "avatar")
2024-07-31 14:50:55 +08:00
}
val bannerField: MultipartBody.Part? = banner?.let {
createMultipartBody(it.file, it.filename, "banner")
}
ApiClient.api.updateProfile(avatarField, bannerField, nicknameField, bioField)
2024-07-31 14:50:55 +08:00
}
2024-08-11 17:15:17 +08:00
override suspend fun registerUserWithPassword(loginName: String, password: String) {
ApiClient.api.register(RegisterRequestBody(loginName, password))
}
2024-08-13 22:32:27 +08:00
override suspend fun changeAccountPassword(oldPassword: String, newPassword: String) {
ApiClient.api.changePassword(ChangePasswordRequestBody(oldPassword, newPassword))
}
2024-08-20 19:48:12 +08:00
override suspend fun getMyLikeNotice(page: Int, pageSize: Int): ListContainer<AccountLike> {
val resp = ApiClient.api.getMyLikeNotices(page, pageSize)
val body = resp.body() ?: throw ServiceException("Failed to get account")
return body
}
override suspend fun getMyFollowNotice(page: Int, pageSize: Int): ListContainer<AccountFollow> {
val resp = ApiClient.api.getMyFollowNotices(page, pageSize)
val body = resp.body() ?: throw ServiceException("Failed to get account")
return body
}
override suspend fun getMyFavouriteNotice(
page: Int,
pageSize: Int
): ListContainer<AccountFavourite> {
val resp = ApiClient.api.getMyFavouriteNotices(page, pageSize)
val body = resp.body() ?: throw ServiceException("Failed to get account")
return body
}
override suspend fun getMyNoticeInfo(): AccountNotice {
val resp = ApiClient.api.getMyNoticeInfo()
val body = resp.body() ?: throw ServiceException("Failed to get account")
return body.data
}
override suspend fun updateNotice(payload: UpdateNoticeRequestBody) {
ApiClient.api.updateNoticeInfo(payload)
}
2024-09-05 22:04:41 +08:00
override suspend fun registerMessageChannel(client: String, identifier: String) {
ApiClient.api.registerMessageChannel(RegisterMessageChannelRequestBody(client,identifier))
}
2024-07-29 00:01:09 +08:00
}