cloudroam
9 天以前 e103a70d2ac198ebbd7493eb657e2cd23089d028
app/src/main/java/com/example/firstapp/database/service/ApiService.kt
@@ -1,11 +1,39 @@
package com.example.firstapp.database.service
import TokenResponse
import android.content.Context
import com.example.firstapp.database.entity.ApiResponse
import com.example.firstapp.database.entity.KeywordConfig
import com.example.firstapp.database.entity.KeywordEntity
import com.example.firstapp.database.request.ProductOrdersRequest
import com.example.firstapp.database.request.SmsLoginRequest
import com.example.firstapp.database.request.SmsSendRequest
import com.example.firstapp.database.response.AccountCloseResponse
import com.example.firstapp.database.response.AlipayOrderInfoResponse
import com.example.firstapp.database.response.ContentResponse
import com.example.firstapp.database.response.DictResponse
import com.example.firstapp.database.response.LoginResponse
import com.example.firstapp.database.response.SecurityResponse
import com.example.firstapp.database.response.UserInfo
import com.example.firstapp.entity.InvitationRecord
import com.example.firstapp.entity.InvitationRecords
import com.example.firstapp.model.CategoryConfig
import com.example.firstapp.model.CategoryConfigSync
import com.example.firstapp.network.AuthInterceptor
import com.example.firstapp.network.ResponseInterceptor
import com.example.firstapp.network.TokenExpiredInterceptor
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.RequestBody
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
import retrofit2.http.Multipart
import retrofit2.http.POST
import retrofit2.http.Part
import retrofit2.http.Path
import retrofit2.http.Query
import retrofit2.http.Body
import java.util.concurrent.TimeUnit
/**
 * API调用接口
@@ -14,17 +42,89 @@
    @GET("keywords")
    suspend fun getKeywords():ApiResponse<List<KeywordConfig>>  //异步挂起
    @GET("api/config/content/list/view")
    suspend fun getContentById(@Query("id") id: String): ContentResponse
    @GET("sysDict/getByDictCodeAndItemText")
    suspend fun getDictValue(@Query("dictCode") dictCode: String, @Query("itemText") itemText: String): DictResponse
    // 发送短信验证码
    @POST("api/sms/send/code")
    suspend fun sendVerificationCode(@Body request: SmsSendRequest): LoginResponse
    // 验证短信验证码
    @POST("api/login/customer/phone/v2")
    suspend fun verifyCode(@Body request: SmsLoginRequest): TokenResponse
    @GET("config-security/enable-list-all")
    suspend fun getSecurityList(): SecurityResponse
    @POST("v2/alipay/pay-order-info")
    suspend fun getPayOrderInfo(@Body request: ProductOrdersRequest): AlipayOrderInfoResponse
    // 获取用户信息
    @GET("api/customer/info/{phone}")
    suspend fun getUserInfo(@Path("phone") phone: String): ApiResponse<UserInfo>
    @Multipart
    @POST("api/supplier/operation/update")
    suspend fun updateProfile(
        @Part("nickname") nickname: RequestBody,
        @Part avatar: MultipartBody.Part?
    ): ApiResponse<Unit>
    @POST("api/account/close")
    suspend fun closeAccount(): AccountCloseResponse
    @GET("api/categoryConfig/getByUserId/{userId}")
    suspend fun getUserCategories(@Path("userId") currentUserId: String): List<CategoryConfig>
    @POST("api/categoryConfig/saveOrUpdate/")
    suspend fun saveUserCategories(@Body categoryConfigSync: CategoryConfigSync)
    @GET("api/customer/center/invitereward")
    suspend fun getInvitereward(): ApiResponse<InvitationRecords>
}
// 创建Retrofit实例(单例)
object RetrofitClient{
    private const val BASE_URL ="http://47.96.225.205:9009/cloud/"
    //private const val BASE_URL ="http://192.168.1.127:8080/flower/"
    private const val BASE_URL ="http://14.103.144.28:8080/flower/"
    private lateinit var appContext: Context
    // 初始化方法,需要在Application中调用
    fun init(context: Context) {
        appContext = context.applicationContext
    }
    // 创建OkHttpClient,配置拦截器和超时时间
    private val okHttpClient by lazy {
        OkHttpClient.Builder()
            .addInterceptor(AuthInterceptor())
            .addInterceptor(TokenExpiredInterceptor(appContext))
            .addInterceptor(ResponseInterceptor(appContext))
            .connectTimeout(30, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .writeTimeout(30, TimeUnit.SECONDS)
            .build()
    }
    //添加Gson解析器,用于自动将JSON响应转换为Kotlin/Java对象
    private val retrofit = Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build()
    private val retrofit by lazy {
        Retrofit
            .Builder()
            .client(okHttpClient)
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }
    //通过动态代理技术创建ApiService接口的具体实现类
    val apiService:ApiService = retrofit.create(ApiService::class.java)
    val apiService by lazy { retrofit.create(ApiService::class.java) }
}
}