修改头像
This commit is contained in:
@@ -19,10 +19,13 @@ import com.example.defenseapplication.bean.ObtainInviterInformationRequest
|
||||
import com.example.defenseapplication.bean.SwitchFamilyRequest
|
||||
import com.example.defenseapplication.bean.UpdatePassword
|
||||
import com.example.defenseapplication.bean.UpdateUserInfo
|
||||
import com.example.defenseapplication.bean.UploadResponse
|
||||
import com.example.defenseapplication.bean.UserInfoBean
|
||||
import com.example.defenseapplication.bean.VerifyOptionPasswordRequest
|
||||
import okhttp3.MultipartBody
|
||||
import retrofit2.http.Body
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.Multipart
|
||||
import retrofit2.http.POST
|
||||
import retrofit2.http.PUT
|
||||
import retrofit2.http.Query
|
||||
@@ -206,6 +209,12 @@ interface ApiService {
|
||||
@POST("/app/revokeInvitation")
|
||||
suspend fun revokeInvitation(@Body request: VerifyOptionPasswordRequest): GetCodeBean<Any>
|
||||
|
||||
/**
|
||||
* 上传文件接口
|
||||
*/
|
||||
@Multipart
|
||||
@POST("/app/upload")
|
||||
suspend fun uploadFile(@retrofit2.http.Part file: MultipartBody.Part): UploadResponse
|
||||
|
||||
/**
|
||||
* 修改家庭用户状态
|
||||
|
||||
@@ -1,129 +0,0 @@
|
||||
package com.example.defenseapplication.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import com.alibaba.sdk.android.oss.ClientConfiguration
|
||||
import com.alibaba.sdk.android.oss.OSS
|
||||
import com.alibaba.sdk.android.oss.OSSClient
|
||||
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider
|
||||
import com.alibaba.sdk.android.oss.common.auth.OSSPlainTextAKSKCredentialProvider
|
||||
import com.alibaba.sdk.android.oss.model.PutObjectRequest
|
||||
import com.alibaba.sdk.android.oss.model.PutObjectResult
|
||||
import java.io.File
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
|
||||
/**
|
||||
* OSS 工具类
|
||||
* 用于上传文件到阿里云 OSS
|
||||
*/
|
||||
object OssUtil {
|
||||
|
||||
private const val TAG = "OssUtil"
|
||||
|
||||
// OSS 配置信息 - 需要根据实际情况配置
|
||||
private const val ENDPOINT = "your-oss-endpoint" // 例如: oss-cn-hangzhou.aliyuncs.com
|
||||
private const val ACCESS_KEY_ID = "your-access-key-id"
|
||||
private const val ACCESS_KEY_SECRET = "your-access-key-secret"
|
||||
private const val BUCKET_NAME = "your-bucket-name"
|
||||
|
||||
private var ossClient: OSS? = null
|
||||
|
||||
/**
|
||||
* 初始化 OSS 客户端
|
||||
*/
|
||||
private fun initOssClient(context: Context): OSS {
|
||||
if (ossClient == null) {
|
||||
// 创建凭证提供者
|
||||
val credentialProvider: OSSCredentialProvider =
|
||||
OSSPlainTextAKSKCredentialProvider(ACCESS_KEY_ID, ACCESS_KEY_SECRET)
|
||||
|
||||
// 创建配置
|
||||
val conf = ClientConfiguration().apply {
|
||||
connectionTimeout = 15 * 1000 // 连接超时,默认15秒
|
||||
socketTimeout = 15 * 1000 // socket超时,默认15秒
|
||||
maxConcurrentRequest = 5 // 最大并发请求数,默认5
|
||||
maxErrorRetry = 2 // 失败后最大重试次数,默认2
|
||||
}
|
||||
|
||||
// 创建 OSS 客户端
|
||||
ossClient = OSSClient(context.applicationContext, ENDPOINT, credentialProvider, conf)
|
||||
}
|
||||
return ossClient!!
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件到 OSS
|
||||
* @param context 上下文
|
||||
* @param filePath 文件路径
|
||||
* @param folderName 文件夹名称(可选,用于分类存储)
|
||||
* @return OSS 文件 URL
|
||||
*/
|
||||
fun uploadFile(context: Context, filePath: String, folderName: String = "avatar"): String? {
|
||||
return try {
|
||||
val file = File(filePath)
|
||||
if (!file.exists()) {
|
||||
Log.e(TAG, "File not found: $filePath")
|
||||
return null
|
||||
}
|
||||
|
||||
// 生成文件名:时间戳 + 原文件名
|
||||
val timestamp = SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault()).format(Date())
|
||||
val originalFileName = file.name
|
||||
val fileName = "${timestamp}_${originalFileName}"
|
||||
val objectKey = "$folderName/$fileName"
|
||||
|
||||
Log.d(TAG, "Uploading file: $filePath to OSS key: $objectKey")
|
||||
|
||||
// 创建上传请求
|
||||
val putObjectRequest = PutObjectRequest(BUCKET_NAME, objectKey, filePath)
|
||||
|
||||
// 执行上传
|
||||
val oss = initOssClient(context)
|
||||
val result: PutObjectResult = oss.putObject(putObjectRequest)
|
||||
|
||||
Log.d(TAG, "Upload success, ETAG: ${result.eTag}")
|
||||
|
||||
// 返回文件 URL
|
||||
"https://$BUCKET_NAME.$ENDPOINT/$objectKey"
|
||||
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Upload failed: ${e.message}", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步上传文件到 OSS
|
||||
* @param context 上下文
|
||||
* @param filePath 文件路径
|
||||
* @param folderName 文件夹名称
|
||||
* @param callback 回调函数
|
||||
*/
|
||||
fun uploadFileAsync(
|
||||
context: Context,
|
||||
filePath: String,
|
||||
folderName: String = "avatar",
|
||||
callback: (String?) -> Unit
|
||||
) {
|
||||
Thread {
|
||||
val result = uploadFile(context, filePath, folderName)
|
||||
callback(result)
|
||||
}.start()
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置 OSS 参数(动态配置)
|
||||
* @param endpoint OSS 端点
|
||||
* @param accessKeyId Access Key ID
|
||||
* @param accessKeySecret Access Key Secret
|
||||
* @param bucketName Bucket 名称
|
||||
*/
|
||||
fun setOssConfig(endpoint: String, accessKeyId: String, accessKeySecret: String, bucketName: String) {
|
||||
// 由于常量无法修改,这里需要重新创建客户端
|
||||
ossClient = null
|
||||
// 注意:实际项目中应该使用动态配置,这里简化处理
|
||||
Log.d(TAG, "OSS config updated")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user