Commit 3a3a064b authored by steve's avatar steve

修改-部分class命名, 新增-api静态header和动态参数

parent 09f4ee4c
package com.example.simboxapi
object Constants {
val APP_VERSION:String = "GlocalMe Call"
var BASE_URL:String = "https://voip-tsm.ucloudlink.com/"
}
\ No newline at end of file
package com.example.simboxapi.data
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.liveData
import com.example.simboxapi.base.BaseResponse
import com.example.simboxapi.data.api.ApiSystemService
import com.example.simboxapi.data.api.ApiService
import com.example.simboxapi.data.entirys.CountryDataBean
import com.example.simboxapi.data.entirys.OnOffImisDataBean
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.joinAll
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONArray
import org.json.JSONObject
import java.lang.Exception
class ViewModelRepository(private val apiSystemService: ApiSystemService) {
class ViewModelRepository(private val apiService: ApiService) {
companion object {
fun getInstance(apiSystemService: ApiSystemService): ViewModelRepository {
return ViewModelRepository(apiSystemService)
fun getInstance(apiService: ApiService): ViewModelRepository {
return ViewModelRepository(apiService)
}
}
......@@ -34,26 +32,26 @@ class ViewModelRepository(private val apiSystemService: ApiSystemService) {
}
}
fun apiPostQueryCountryInfo(langType: String, loginCustomerId: String, partnerCode: String, streamNo: String): LiveData<ResourceViewModel<BaseResponse<ArrayList<CountryDataBean>>>> {
fun apiPostQueryCountryInfo(langType: String, loginCustomerId: String, partnerCode: String, streamNo: String, cookie: String, voipId: String): LiveData<ResourceViewModel<BaseResponse<ArrayList<CountryDataBean>>>> {
val jsonObject = JSONObject()
jsonObject.put("langType", langType)
jsonObject.put("loginCustomerId", loginCustomerId)
jsonObject.put("partnerCode", partnerCode)
jsonObject.put("streamNo", streamNo)
val body = jsonObject.toString().toRequestBody("application/json".toMediaTypeOrNull())
return execute { apiSystemService.apiQueryCountryInfo(body) }
return execute { apiService.apiQueryCountryInfo(body, cookie, voipId) }
}
fun apiPostOnOffImsi(imei:String,loginCustomerId:String,offImsiList:ArrayList<String>,onImsiList:ArrayList<String>,partnerCode:String,streamNo:String):LiveData<ResourceViewModel<BaseResponse<ArrayList<OnOffImisDataBean>>>>{
fun apiPostOnOffImsi(imei: String, loginCustomerId: String, offImsiList: ArrayList<String>, onImsiList: ArrayList<String>, partnerCode: String, streamNo: String, cookie: String, voipId: String, userLabel: String): LiveData<ResourceViewModel<BaseResponse<ArrayList<OnOffImisDataBean>>>> {
val jsonObject = JSONObject()
jsonObject.put("imei",imei)
jsonObject.put("loginCustomerId",loginCustomerId)
jsonObject.put("offImsiList",JSONArray(offImsiList))
jsonObject.put("onImsiList",JSONArray(onImsiList))
jsonObject.put("partnerCode",partnerCode)
jsonObject.put("streamNo",streamNo)
jsonObject.put("imei", imei)
jsonObject.put("loginCustomerId", loginCustomerId)
jsonObject.put("offImsiList", JSONArray(offImsiList))
jsonObject.put("onImsiList", JSONArray(onImsiList))
jsonObject.put("partnerCode", partnerCode)
jsonObject.put("streamNo", streamNo)
val body = jsonObject.toString().toRequestBody("application/json".toMediaTypeOrNull())
return execute { apiSystemService.apiOnOffImsi(body) }
return execute { apiService.apiOnOffImsi(body,cookie,voipId,userLabel) }
}
}
\ No newline at end of file
......@@ -7,9 +7,9 @@ import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
class ApiSystemClient(val context: Context) {
class ApiClient(val context: Context) {
val apiSystemService: ApiSystemService by lazy { provideApiService() }
val apiService: ApiService by lazy { provideApiService() }
companion object {
......@@ -17,12 +17,12 @@ class ApiSystemClient(val context: Context) {
const val WRITE_TIME_OUT = 20000L
const val READ_TIME_OUT = 20000L
fun getInstance(context: Context): ApiSystemClient = ApiSystemClient(context.applicationContext)
fun getInstance(context: Context): ApiClient = ApiClient(context.applicationContext)
}
private fun provideOkHttpClient(): OkHttpClient {
return OkHttpClient.Builder().run {
addInterceptor(SystemUrlInterceptor.getInstance())
addInterceptor(BaseUrlInterceptor.getInstance())
connectTimeout(CONNECTION_TIME_OUT, TimeUnit.MILLISECONDS)
writeTimeout(WRITE_TIME_OUT, TimeUnit.MILLISECONDS)
readTimeout(READ_TIME_OUT, TimeUnit.MILLISECONDS)
......@@ -39,9 +39,9 @@ class ApiSystemClient(val context: Context) {
}
}
private fun provideApiService(): ApiSystemService {
private fun provideApiService(): ApiService {
val okHttpClient = provideOkHttpClient()
val retrofit = provideRetrofit(okHttpClient)
return retrofit.create(ApiSystemService::class.java)
return retrofit.create(ApiService::class.java)
}
}
\ No newline at end of file
package com.example.simboxapi.data.api
import com.example.simboxapi.base.BaseResponse
import com.example.simboxapi.data.entirys.CountryDataBean
import com.example.simboxapi.data.entirys.OnOffImisDataBean
import okhttp3.RequestBody
import retrofit2.http.Body
import retrofit2.http.Header
import retrofit2.http.Headers
import retrofit2.http.POST
interface ApiService {
@Headers(
"Content-Type:application/json",
"Partner-code:simbox",
"Accept-Language:zh-CN",
"langType:zh-CN",
"App-Version:1.9.03"
)
@POST("bss/app/noauth/QueryCoverCountyInfo")
suspend fun apiQueryCountryInfo(@Body body: RequestBody, @Header("Cookie") cookie: String, @Header("voipId") voipId: String): BaseResponse<ArrayList<CountryDataBean>>
@Headers(
"Content-Type:application/json",
"Partner-code:simbox",
"Accept-Language:zh-CN",
"langType:zh-CN",
"app-Version:1.9.03"
)
@POST("bss/dsds/card/OnOffImsi")
suspend fun apiOnOffImsi(@Body body: RequestBody, @Header("Cookie") cookie: String, @Header("voipId") voipId: String, @Header("userLabel") userLabel: String): BaseResponse<ArrayList<OnOffImisDataBean>>
}
\ No newline at end of file
package com.example.simboxapi.data.api
import com.example.simboxapi.base.BaseResponse
import com.example.simboxapi.data.entirys.CountryDataBean
import com.example.simboxapi.data.entirys.OnOffImisDataBean
import okhttp3.RequestBody
import retrofit2.http.Body
import retrofit2.http.POST
interface ApiSystemService {
// /**
// * 登陆
// */
// @POST("api/appUnicorn/login")
// suspend fun apiPostLogin(
// @Query("account") account: String, @Query("password") password: String, @Query("uuid") uuid: String, @Query("device") device: String,
// @Query("type") loginType: String
// ): BaseResponse<LoginDataBean>
//
// /**
// * 取得驗證碼
// */
// @GET("api/appUnicorn/verificationCode/exec")
// suspend fun <T> apiGetVerificationCode(
// @Query("mobile") mobile: String
// ): BaseResponse<T>
//
// /**
// * 注册
// */
// @POST("api/appUnicorn/appUser/addData")
// suspend fun apiPostRegister(
// @Query("account") account: String, @Query("password") pwd: String, @Query("verification_code") verifyCode: String,
// @Query("invite_code") inviteCode: String, @Query("qna_item") quaItem: String, @Query("qna_ans") qnaAns: String
// ): BaseResponse<RegisterDataBean>
@POST("bss/app/noauth/QueryCoverCountyInfo")
suspend fun apiQueryCountryInfo(@Body body: RequestBody):BaseResponse<ArrayList<CountryDataBean>>
@POST("bss/dsds/card/OnOffImsi")
suspend fun apiOnOffImsi(@Body body: RequestBody):BaseResponse<ArrayList<OnOffImisDataBean>>
}
\ No newline at end of file
......@@ -7,11 +7,11 @@ import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.Interceptor
import okhttp3.Response
class SystemUrlInterceptor : Interceptor {
class BaseUrlInterceptor : Interceptor {
companion object {
private val TAG = SystemUrlInterceptor::class.java.simpleName.toString()
fun getInstance(): SystemUrlInterceptor = SystemUrlInterceptor()
private val TAG = BaseUrlInterceptor::class.java.simpleName.toString()
fun getInstance(): BaseUrlInterceptor = BaseUrlInterceptor()
}
override fun intercept(chain: Interceptor.Chain): Response {
......@@ -19,10 +19,9 @@ class SystemUrlInterceptor : Interceptor {
val request = chain.request() //获取 request
val oldHttpUrl = request.url //从 request 中获取原有的 HttpUrl 实例 oldHttpUrl
val builder = request.newBuilder() //获取 request 的创建 builder
builder.removeHeader("urlname")
//批配获得新的 BaseUrl
Log.d(TAG, "intercept: $oldHttpUrl")
//刪除原本的 url, 獲取新的 BaseUrl
builder.removeHeader("urlname")
val newBaseUrl: HttpUrl? = Constants.BASE_URL.toHttpUrlOrNull()
//重建新的 HttpUrl, 修改需要修改url的部份
......
......@@ -9,10 +9,9 @@ import androidx.lifecycle.ViewModelProvider
import com.example.simboxapi.data.Status
import com.example.simboxapi.data.ViewModelFactory
import com.example.simboxapi.data.ViewModelRepository
import com.example.simboxapi.data.api.ApiSystemClient
import com.example.simboxapi.data.api.ApiClient
import com.example.simboxapi.databinding.ActivityChangeSimBinding
import com.example.simboxapi.utils.DatePattern
import kotlinx.coroutines.flow.combine
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList
......@@ -40,19 +39,25 @@ class ChangeSimActivity : AppCompatActivity(), View.OnClickListener {
when(view){
mVB.btnChangeSimPostApi->{
mVB.tvChangSimContent.text = ""
val imei = "864055040488792"
val loginCustomId = "6209e2ea1b7f0d319febe2b2"
val offImsiList = arrayListOf("460015466225468")
val onImsiList = arrayListOf<String>()
val partnerCode = "GCGROUP"
val streamNo = "SIMBOX${getData()}${((Math.random() * 9.toDouble() + 1.toDouble()) * 100000.toDouble()).toInt()}"
apiPostOnOffImsi(imei,loginCustomId,offImsiList,onImsiList,partnerCode,streamNo)
val cookie = "access_token=TGT-2398391-2usdACdYsxPGbfXXnenVOIYRu6fZKFdZHpEAHczU0Sc4BfUZZR"
val voidId = "80000076419"
val userLabel = "businessUser"
apiPostOnOffImsi(imei,loginCustomId,offImsiList,onImsiList,partnerCode,streamNo,cookie,voidId,userLabel)
}
}
}
private fun setViewModel() {
val viewModelRepository: ViewModelRepository = ViewModelRepository.getInstance(ApiSystemClient.getInstance(this).apiSystemService)
val viewModelRepository: ViewModelRepository = ViewModelRepository.getInstance(ApiClient.getInstance(this).apiService)
mChangeSimViewModel = ViewModelProvider(this, ViewModelFactory.getInstance(viewModelRepository))[ChangeSimViewModel::class.java]
}
......@@ -67,8 +72,8 @@ class ChangeSimActivity : AppCompatActivity(), View.OnClickListener {
return simpleDateFormat.format(instance.time)
}
private fun apiPostOnOffImsi(imei: String, loginCustomId: String, offImsiList: ArrayList<String>, onImsiList: ArrayList<String>, partnerCode: String, streamNo: String) {
mChangeSimViewModel.apiPostOnOffImsi(imei, loginCustomId, offImsiList, onImsiList, partnerCode, streamNo).observe(this) {
private fun apiPostOnOffImsi(imei: String, loginCustomId: String, offImsiList: ArrayList<String>, onImsiList: ArrayList<String>, partnerCode: String, streamNo: String,cookie:String,voipId:String,userLabel:String) {
mChangeSimViewModel.apiPostOnOffImsi(imei, loginCustomId, offImsiList, onImsiList, partnerCode, streamNo,cookie,voipId, userLabel).observe(this) {
when (it.status) {
Status.SUCCESS -> {
mVB.tvChangSimContent.text = it.data.toString()
......
......@@ -9,8 +9,8 @@ import com.example.simboxapi.data.entirys.OnOffImisDataBean
class ChangeSimViewModel(private val viewModelRepository: ViewModelRepository) : ViewModel() {
fun apiPostOnOffImsi(imei: String, loginCustomerId: String, offImisList: ArrayList<String>, onImsiList: ArrayList<String>, partnerCode: String, streamNo: String): LiveData<ResourceViewModel<BaseResponse<ArrayList<OnOffImisDataBean>>>> {
return viewModelRepository.apiPostOnOffImsi(imei, loginCustomerId, offImisList, onImsiList, partnerCode, streamNo)
fun apiPostOnOffImsi(imei: String, loginCustomerId: String, offImisList: ArrayList<String>, onImsiList: ArrayList<String>, partnerCode: String, streamNo: String, cookie: String, voipId: String, userLabel: String): LiveData<ResourceViewModel<BaseResponse<ArrayList<OnOffImisDataBean>>>> {
return viewModelRepository.apiPostOnOffImsi(imei, loginCustomerId, offImisList, onImsiList, partnerCode, streamNo, cookie, voipId, userLabel)
}
}
\ No newline at end of file
......@@ -9,7 +9,7 @@ import com.example.simboxapi.base.BaseActivity
import com.example.simboxapi.data.Status
import com.example.simboxapi.data.ViewModelFactory
import com.example.simboxapi.data.ViewModelRepository
import com.example.simboxapi.data.api.ApiSystemClient
import com.example.simboxapi.data.api.ApiClient
import com.example.simboxapi.databinding.ActivityQueryCountryInfoBinding
import com.example.simboxapi.utils.DatePattern
import java.text.SimpleDateFormat
......@@ -38,17 +38,22 @@ class QueryCountryInfoActivity : BaseActivity(), View.OnClickListener {
when (view) {
mVB.btnQueryCountryPostApi -> {
mVB.tvQueryCountryResponse.text = ""
val langType = "zh-TW"
val langType = "zh-CN"
val loginCustomerId = ""
val partnerCode = "GCGROUP"
val streamNo = "SIMBOX${getData()}${((Math.random() * 9.toDouble() + 1.toDouble()) * 100000.toDouble()).toInt()}"
apiPostQueryCountry(langType,loginCustomerId,partnerCode, streamNo)
val cookie = "access_token="
val voipId = ""
apiPostQueryCountry(langType,loginCustomerId,partnerCode, streamNo,cookie,voipId)
}
}
}
private fun setViewModel() {
val viewModelRepository: ViewModelRepository = ViewModelRepository.getInstance(ApiSystemClient.getInstance(this).apiSystemService)
val viewModelRepository: ViewModelRepository = ViewModelRepository.getInstance(ApiClient.getInstance(this).apiService)
mQueryCountryInfo = ViewModelProvider(this, ViewModelFactory.getInstance(viewModelRepository))[QueryCountryInfoViewModel::class.java]
}
......@@ -63,8 +68,8 @@ class QueryCountryInfoActivity : BaseActivity(), View.OnClickListener {
return simpleDateFormat.format(instance.time)
}
private fun apiPostQueryCountry(langType:String,loginCustomerId:String,partnerCode:String,streamNo:String){
mQueryCountryInfo.apiPostQueryCountryInfo(langType, loginCustomerId, partnerCode, streamNo).observe(this) {
private fun apiPostQueryCountry(langType:String,loginCustomerId:String,partnerCode:String,streamNo:String,cookie:String,voipId:String){
mQueryCountryInfo.apiPostQueryCountryInfo(langType, loginCustomerId, partnerCode, streamNo,cookie,voipId).observe(this) {
when (it.status) {
Status.SUCCESS -> {
mVB.tvQueryCountryResponse.text = it.data.toString()
......
......@@ -9,8 +9,8 @@ import com.example.simboxapi.data.entirys.CountryDataBean
class QueryCountryInfoViewModel(private val viewModelRepository: ViewModelRepository) : ViewModel() {
fun apiPostQueryCountryInfo(langType: String, loginCustomerId: String, partnerCode: String, streamNo: String): LiveData<ResourceViewModel<BaseResponse<ArrayList<CountryDataBean>>>>{
return viewModelRepository.apiPostQueryCountryInfo(langType, loginCustomerId, partnerCode, streamNo)
fun apiPostQueryCountryInfo(langType: String, loginCustomerId: String, partnerCode: String, streamNo: String, cookie: String, voipId: String): LiveData<ResourceViewModel<BaseResponse<ArrayList<CountryDataBean>>>> {
return viewModelRepository.apiPostQueryCountryInfo(langType, loginCustomerId, partnerCode, streamNo, cookie, voipId)
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment