Skip to content

Commit

Permalink
prepare for demo
Browse files Browse the repository at this point in the history
  • Loading branch information
iiinotlll committed Dec 24, 2024
1 parent 0cc7d8d commit c42d2ed
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import retrofit2.http.POST
import retrofit2.http.PUT
import retrofit2.http.Query

private const val BASE_URL = "http://10.0.2.2:8088/"
private const val BASE_URL = "https://342e0955.r19.cpolar.top"

// 拦截器,用于在请求头中添加 Authorization 信息
class AuthenticationInterceptor : Interceptor {
Expand Down Expand Up @@ -75,3 +75,9 @@ object CoToLiveApi {
retrofit.create(CoToLiveApiService::class.java)
}
}

data class ErrorResponse(
val status: String,
val message: String
)

Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ fun LogInScreenLayout(modifier: Modifier = Modifier, navController: NavControlle
is LogInUiState.Error -> {
// 后端错误,显示错误信息
Log.e("LogIn", "登录失败")
checkResult = "登陆失败"
checkResult = logInUiState.message
snackbarViewModel.showErrSnackbar(checkResult)
}
is LogInUiState.Success -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import com.example.cotolive.network.ArticleDel
import com.example.cotolive.network.ArticleGet
import com.example.cotolive.network.ArticleSent
import com.example.cotolive.network.CoToLiveApi
import com.example.cotolive.network.ErrorResponse
import com.google.gson.Gson
import kotlinx.coroutines.launch
import retrofit2.HttpException
import java.io.IOException
Expand Down Expand Up @@ -69,7 +71,15 @@ class ArticleManageViewModel : ViewModel() {
} catch (e: HttpException) {
Log.e("ArticleManageViewModel", "HTTP error", e)
// 这里可以获取到 HTTP 错误的详细信息
val errorMessage = e.response()?.errorBody()?.string() ?: "服务器错误,请稍后再试"
val errorMessage = try {
val errorBody = e.response()?.errorBody()?.string()
val gson = Gson()
val errorResponse = gson.fromJson(errorBody, ErrorResponse::class.java)
errorResponse?.message ?: "服务器错误,请稍后再试"
} catch (ex: Exception) {
// 如果解析失败,使用通用错误信息
"解析错误,请稍后再试"
}
articleModifyUiState = ArticleManageUiState.Error(errorMessage)
}
}
Expand All @@ -94,7 +104,15 @@ class ArticleManageViewModel : ViewModel() {
} catch (e: HttpException) {
Log.e("ArticleManageViewModel", "HTTP error", e)
// 这里可以获取到 HTTP 错误的详细信息
val errorMessage = e.response()?.errorBody()?.string() ?: "服务器错误,请稍后再试"
val errorMessage = try {
val errorBody = e.response()?.errorBody()?.string()
val gson = Gson()
val errorResponse = gson.fromJson(errorBody, ErrorResponse::class.java)
errorResponse?.message ?: "服务器错误,请稍后再试"
} catch (ex: Exception) {
// 如果解析失败,使用通用错误信息
"解析错误,请稍后再试"
}
articleNewPostUiState = ArticleManageUiState.Error(errorMessage)
}
}
Expand All @@ -119,7 +137,15 @@ class ArticleManageViewModel : ViewModel() {
} catch (e: HttpException) {
Log.e("ArticleManageViewModel", "HTTP error", e)
// 这里可以获取到 HTTP 错误的详细信息
val errorMessage = e.response()?.errorBody()?.string() ?: "服务器错误,请稍后再试"
val errorMessage = try {
val errorBody = e.response()?.errorBody()?.string()
val gson = Gson()
val errorResponse = gson.fromJson(errorBody, ErrorResponse::class.java)
errorResponse?.message ?: "服务器错误,请稍后再试"
} catch (ex: Exception) {
// 如果解析失败,使用通用错误信息
"解析错误,请稍后再试"
}
articleDelUiState = ArticleManageUiState.Error(errorMessage)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.cotolive.network.ArticleAbstract
import com.example.cotolive.network.CoToLiveApi
import com.example.cotolive.network.ErrorResponse
import com.google.gson.Gson
import kotlinx.coroutines.launch
import retrofit2.HttpException
import java.io.IOException
Expand Down Expand Up @@ -42,7 +44,15 @@ class FetchAbstractsViewModel : ViewModel() {
} catch (e: HttpException) {
Log.e("FetchAbstractViewModel", "HTTP error", e)
// 这里可以获取到 HTTP 错误的详细信息
val errorMessage = e.response()?.errorBody()?.string() ?: "服务器错误,请稍后再试"
val errorMessage = try {
val errorBody = e.response()?.errorBody()?.string()
val gson = Gson()
val errorResponse = gson.fromJson(errorBody, ErrorResponse::class.java)
errorResponse?.message ?: "服务器错误,请稍后再试"
} catch (ex: Exception) {
// 如果解析失败,使用通用错误信息
"解析错误,请稍后再试"
}
fetchAbstractUiState = FetchAbstractUiState.Error(errorMessage)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.cotolive.network.CoToLiveApi
import com.example.cotolive.network.ErrorResponse
import com.example.cotolive.network.LogInPostMessage
import com.example.cotolive.network.TokenManager
import com.google.gson.Gson
import kotlinx.coroutines.launch
import retrofit2.HttpException
import java.io.IOException
Expand Down Expand Up @@ -42,7 +44,16 @@ class LogInViewModel : ViewModel() {
} catch (e: HttpException) {
Log.e("LogInViewModel", "HTTP error", e)
// 这里可以获取到 HTTP 错误的详细信息
val errorMessage = e.response()?.errorBody()?.string() ?: "服务器错误,请稍后再试"
val errorMessage = try {
val errorBody = e.response()?.errorBody()?.string()
val gson = Gson()
val errorResponse = gson.fromJson(errorBody, ErrorResponse::class.java)
errorResponse?.message ?: "服务器错误,请稍后再试"
} catch (ex: Exception) {
// 如果解析失败,使用通用错误信息
"解析错误,请稍后再试"
}

logInUiState = LogInUiState.Error(errorMessage)
}
logInCallCnt ++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.cotolive.network.CoToLiveApi
import com.example.cotolive.network.ErrorResponse
import com.example.cotolive.network.SignUpRequestMessage
import com.google.gson.Gson
import kotlinx.coroutines.launch
import retrofit2.HttpException
import java.io.IOException
Expand Down Expand Up @@ -39,7 +41,15 @@ class SignUpViewModel : ViewModel() {
} catch (e: HttpException) {
Log.e("SignUpViewModel", "HTTP error", e)
// 这里可以获取到 HTTP 错误的详细信息
val errorMessage = e.response()?.errorBody()?.string() ?: "服务器错误,请稍后再试"
val errorMessage = try {
val errorBody = e.response()?.errorBody()?.string()
val gson = Gson()
val errorResponse = gson.fromJson(errorBody, ErrorResponse::class.java)
errorResponse?.message ?: "服务器错误,请稍后再试"
} catch (ex: Exception) {
// 如果解析失败,使用通用错误信息
"解析错误,请稍后再试"
}
signUpUiState = SignUpUiState.Error(errorMessage)
}
signUpCallCnt ++
Expand Down
2 changes: 1 addition & 1 deletion CoToLive/app/src/main/res/xml/network_security_config.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">10.0.2.2</domain>
<domain includeSubdomains="true">https://342e0955.r19.cpolar.top</domain>
</domain-config>
</network-security-config>
24 changes: 12 additions & 12 deletions colv-backend/router/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (dbh *MysqlHandler) HandleUserLogin(c *gin.Context) {

// bind json
if err := c.ShouldBind(&usrLogin); err != nil {
c.JSON(400, gin.H{"status": "error", "message": "invalid request"})
c.JSON(400, gin.H{"status": "error", "message": "无效请求"})
return
}

Expand All @@ -47,7 +47,7 @@ func (dbh *MysqlHandler) HandleUserLogin(c *gin.Context) {
} else {
token, err := GenerateToken(user.UID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "message": "could not generate token"})
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "message": "无法生成 Token"})
return
}
// 返回 Token
Expand All @@ -64,7 +64,7 @@ func (dbh *MysqlHandler) HandleUserSignUp(c *gin.Context) {

// bind json
if err := c.ShouldBind(&usrSignup); err != nil {
c.JSON(400, gin.H{"status": "error", "message": "invalid request"})
c.JSON(400, gin.H{"status": "error", "message": "无效请求"})
return
}

Expand All @@ -84,13 +84,13 @@ func (dbh *MysqlHandler) HandleArticlePost(c *gin.Context) {

userID, exists := c.Get("UID")
if !exists {
c.JSON(400, gin.H{"status": "error", "message": "UID not found"})
c.JSON(400, gin.H{"status": "error", "message": "未找到 UID"})
return
}

// bind json
if err := c.ShouldBind(&articlePost); err != nil {
c.JSON(400, gin.H{"status": "error", "message": "invalid request"})
c.JSON(400, gin.H{"status": "error", "message": "无效请求"})
return
}

Expand All @@ -109,13 +109,13 @@ func (dbh *MysqlHandler) HandleArticleRead(c *gin.Context) {

userID, exists := c.Get("UID")
if !exists {
c.JSON(400, gin.H{"status": "error", "message": "UID not found"})
c.JSON(400, gin.H{"status": "error", "message": "未找到 UID"})
return
}

// bind json
if err := c.ShouldBind(&articleRead); err != nil {
c.JSON(400, gin.H{"status": "error", "message": "invalid request"})
c.JSON(400, gin.H{"status": "error", "message": "无效请求"})
return
}

Expand All @@ -129,7 +129,7 @@ func (dbh *MysqlHandler) HandleArticleRead(c *gin.Context) {
func (dbh *MysqlHandler) HandleArticleAbstractRead(c *gin.Context) {
userID, exists := c.Get("UID")
if !exists {
c.JSON(400, gin.H{"status": "error", "message": "UID not found"})
c.JSON(400, gin.H{"status": "error", "message": "未找到 UID"})
return
}

Expand All @@ -143,7 +143,7 @@ func (dbh *MysqlHandler) HandleArticleAbstractRead(c *gin.Context) {
func (dbh *MysqlHandler) HandleAriticleModify(c *gin.Context) {
userID, exists := c.Get("UID")
if !exists {
c.JSON(400, gin.H{"status": "error", "message": "UID not found"})
c.JSON(400, gin.H{"status": "error", "message": "未找到 UID"})
return
}

Expand All @@ -155,7 +155,7 @@ func (dbh *MysqlHandler) HandleAriticleModify(c *gin.Context) {

// bind json
if err := c.ShouldBind(&articleModify); err != nil {
c.JSON(400, gin.H{"status": "error", "message": "invalid request"})
c.JSON(400, gin.H{"status": "error", "message": "无效请求"})
return
}

Expand All @@ -169,7 +169,7 @@ func (dbh *MysqlHandler) HandleAriticleModify(c *gin.Context) {
func (dbh *MysqlHandler) HandleAriticleDelete(c *gin.Context) {
userID, exists := c.Get("UID")
if !exists {
c.JSON(400, gin.H{"status": "error", "message": "UID not found"})
c.JSON(400, gin.H{"status": "error", "message": "未找到 UID"})
return
}

Expand All @@ -179,7 +179,7 @@ func (dbh *MysqlHandler) HandleAriticleDelete(c *gin.Context) {

// bind json
if err := c.ShouldBind(&articleDelete); err != nil {
c.JSON(400, gin.H{"status": "error", "message": "invalid request"})
c.JSON(400, gin.H{"status": "error", "message": "无效请求"})
return
}

Expand Down
4 changes: 2 additions & 2 deletions colv-backend/sqldb/sql_article.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (db *MysqlDB) PostArticle(uid uint, title, article_data string) error {
var exists bool
result := db.DB.Table(userTableName).Select("1").Where("user_UID = ?", uid).Limit(1).Find(&exists)
if result.RowsAffected == 0 {
return fmt.Errorf("uid not found")
return fmt.Errorf("未找到 UID")
}

// 向 DB 中写入 article
Expand All @@ -40,7 +40,7 @@ func (db *MysqlDB) GetArticle(uid, article_id uint) (*Article, error) {
return nil, result.Error
}
if articleFromDB.BelongsToUID != uid {
return nil, fmt.Errorf("wrong belongs to BelongsToUID = %d", articleFromDB.BelongsToUID)
return nil, fmt.Errorf("用户无权限")
}
return &articleFromDB, nil
}
Expand Down
4 changes: 2 additions & 2 deletions colv-backend/sqldb/sql_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (db *MysqlDB) UserSignUp(inputName string, inputMail string, inputPwd strin
// 查询是否有匹配的记录
result := db.DB.Table(userTableName).Select("1").Where("user_mail = ?", inputMail).Limit(1).Find(&exists)
if result.RowsAffected > 0 {
return fmt.Errorf("existed mail")
return fmt.Errorf("邮箱已存在")
}

// 生成 bcrypt 哈希
Expand Down Expand Up @@ -54,7 +54,7 @@ func (db *MysqlDB) UserLogin(inputMail string, inputPwd string) (*User, error) {

// 校验密码
if !checkPasswordHash(userFromDB.PwdHash, inputPwd) {
return nil, fmt.Errorf("invalid password")
return nil, fmt.Errorf("密码错误")
}

return &userFromDB, nil
Expand Down

0 comments on commit c42d2ed

Please sign in to comment.