[修复] 0元购接口改成post json传参
This commit is contained in:
parent
580f81a67b
commit
e9b7af5de3
|
|
@ -183,15 +183,19 @@ class AdjustManager private constructor() {
|
||||||
|
|
||||||
|
|
||||||
// 设置当前用户渠道类型
|
// 设置当前用户渠道类型
|
||||||
val userChannelType = if (StatisticLogger.isLogEnabled()) {
|
// val userChannelType = if (StatisticLogger.isLogEnabled()) {
|
||||||
// 内部版本强制设置为买量类型
|
// // 内部版本强制设置为买量类型
|
||||||
StatisticLogger.d("内部版本强制设置为买量类型")
|
// StatisticLogger.d("内部版本强制设置为买量类型")
|
||||||
UserChannelController.UserChannelType.PAID
|
// UserChannelController.UserChannelType.PAID
|
||||||
} else {
|
// } else {
|
||||||
determineUserChannelType(attr)
|
// determineUserChannelType(attr)
|
||||||
}
|
// }
|
||||||
|
val userChannelType = determineUserChannelType(attr)
|
||||||
StatisticLogger.d("根据归因数据判断用户渠道类型: $userChannelType")
|
StatisticLogger.d("根据归因数据判断用户渠道类型: $userChannelType")
|
||||||
|
|
||||||
|
// val userChannelType = UserChannelController.UserChannelType.NATURAL
|
||||||
|
// StatisticLogger.d("根据归因数据判断用户渠道类型: $userChannelType")
|
||||||
|
|
||||||
// 设置用户渠道类型
|
// 设置用户渠道类型
|
||||||
val success = UserChannelController.setChannel(userChannelType)
|
val success = UserChannelController.setChannel(userChannelType)
|
||||||
if (success) {
|
if (success) {
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,8 @@ import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import org.json.JSONArray
|
||||||
|
import org.json.JSONObject
|
||||||
|
|
||||||
|
|
||||||
class ZeroBuyViewModel : ViewModel() {
|
class ZeroBuyViewModel : ViewModel() {
|
||||||
|
|
@ -47,30 +48,38 @@ class ZeroBuyViewModel : ViewModel() {
|
||||||
val joinZeroBuyItemIds = SpUtil.instance().getList<Int>(SpUtil.KEY_ZEROBUY_JOINED_ACTIVITY_IDS)
|
val joinZeroBuyItemIds = SpUtil.instance().getList<Int>(SpUtil.KEY_ZEROBUY_JOINED_ACTIVITY_IDS)
|
||||||
requestParams.put("activity_id", itemId.toString())
|
requestParams.put("activity_id", itemId.toString())
|
||||||
|
|
||||||
|
val jsonObj = JSONObject().apply {
|
||||||
|
put("app_id",AndroidUtil.getPackageId())
|
||||||
|
put("device_id",DeviceUtil.generateDeviceId())
|
||||||
|
put("activity_id", itemId)
|
||||||
|
put("purchase_ids", JSONArray(joinZeroBuyItemIds))
|
||||||
|
if (userId > 0) {
|
||||||
|
put("user_id", userId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val result = NetworkUtil.post("${VidiConst.URL_ZERO_BUY}/any", requestHeaders, requestParams, joinZeroBuyItemIds)
|
|
||||||
|
val result = NetworkUtil.postJsonGeneric<ZeroBuyResp>("${VidiConst.URL_ZERO_BUY}/any", jsonObj.toString(), requestHeaders)
|
||||||
when (result) {
|
when (result) {
|
||||||
is Result.Success -> {
|
is Result.Success -> {
|
||||||
val respObj = AndroidUtil.json2Object<ZeroBuyResp>(result.data.string())
|
val respObj = result.data
|
||||||
|
|
||||||
|
if (respObj.code == CODE_HAS_JOINED_BEFORE || respObj.code == 0) {
|
||||||
|
// save
|
||||||
|
val joinedActivityList = SpUtil.instance().getList<Int>(SpUtil.KEY_ZEROBUY_JOINED_ACTIVITY_IDS).toMutableList()
|
||||||
|
if (!joinedActivityList.contains(itemId)) {
|
||||||
|
joinedActivityList.add(itemId)
|
||||||
|
SpUtil.instance().putList(SpUtil.KEY_ZEROBUY_JOINED_ACTIVITY_IDS, joinedActivityList)
|
||||||
|
}
|
||||||
|
|
||||||
respObj?.let {
|
// flow set
|
||||||
if (it.code == CODE_HAS_JOINED_BEFORE || it.code == 0) {
|
val joinItem = respObj.current_purchases?.firstOrNull()
|
||||||
// save
|
if (joinItem != null) {
|
||||||
val joinedActivityList = if(joinZeroBuyItemIds == null) mutableListOf<Int>() else joinZeroBuyItemIds.toMutableList()
|
_ZeroBuyJoinResult.value = Result.Success(joinItem)
|
||||||
if (!joinedActivityList.contains(itemId)) {
|
|
||||||
joinedActivityList.add(itemId)
|
|
||||||
SpUtil.instance().putList(SpUtil.KEY_ZEROBUY_JOINED_ACTIVITY_IDS, joinedActivityList)
|
|
||||||
}
|
|
||||||
|
|
||||||
// flow set
|
|
||||||
_ZeroBuyJoinResult.value = Result.Success(it.current_purchases[0])
|
|
||||||
|
|
||||||
// return
|
|
||||||
return@launch
|
return@launch
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ZeroBuyJoinResult.value = Result.Error(Throwable("empty response"))
|
_ZeroBuyJoinResult.value = Result.Error(Throwable("empty response or code error: ${respObj.code}"))
|
||||||
}
|
}
|
||||||
is Result.Error -> {
|
is Result.Error -> {
|
||||||
_ZeroBuyJoinResult.value = Result.Error(result.exception, result.message)
|
_ZeroBuyJoinResult.value = Result.Error(result.exception, result.message)
|
||||||
|
|
@ -94,20 +103,29 @@ class ZeroBuyViewModel : ViewModel() {
|
||||||
requestParams.put("user_id", userId.toString())
|
requestParams.put("user_id", userId.toString())
|
||||||
}
|
}
|
||||||
val joinZeroBuyItemIds = SpUtil.instance().getList<Int>(SpUtil.KEY_ZEROBUY_JOINED_ACTIVITY_IDS)
|
val joinZeroBuyItemIds = SpUtil.instance().getList<Int>(SpUtil.KEY_ZEROBUY_JOINED_ACTIVITY_IDS)
|
||||||
val result = NetworkUtil.post("${VidiConst.URL_ZERO_BUY}/any", requestHeaders, requestParams, joinZeroBuyItemIds)
|
|
||||||
|
val jsonObj = JSONObject().apply {
|
||||||
|
put("app_id",AndroidUtil.getPackageId())
|
||||||
|
put("device_id",DeviceUtil.generateDeviceId())
|
||||||
|
put("purchase_ids", JSONArray(joinZeroBuyItemIds))
|
||||||
|
if (userId > 0) {
|
||||||
|
put("user_id", userId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
val result = NetworkUtil.postJsonGeneric<ZeroBuyResp>("${VidiConst.URL_ZERO_BUY}/any", jsonObj.toString(), requestHeaders)
|
||||||
when (result) {
|
when (result) {
|
||||||
is Result.Success -> {
|
is Result.Success -> {
|
||||||
val respObj = AndroidUtil.json2Object<ZeroBuyResp>(result.data.string())
|
val respObj = result.data
|
||||||
|
|
||||||
respObj?.user_id?.let {
|
if (userId <= 0) {
|
||||||
if (userId <= 0) {
|
respObj.user_id.let { id ->
|
||||||
AccountManager.saveUserIdInfo(it)
|
AccountManager.saveUserIdInfo(id)
|
||||||
|
saveHasJoinedZeroIds(id, respObj.current_purchases)
|
||||||
saveHasJoinedZeroIds(it, respObj.current_purchases)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
_ZeroBuyListData.value = Result.Success(respObj)
|
||||||
_ZeroBuyListData.value = Result.Success(respObj!!)
|
|
||||||
}
|
}
|
||||||
is Result.Error -> {
|
is Result.Error -> {
|
||||||
_ZeroBuyListData.value = Result.Error(result.exception, result.message)
|
_ZeroBuyListData.value = Result.Error(result.exception, result.message)
|
||||||
|
|
@ -118,11 +136,11 @@ class ZeroBuyViewModel : ViewModel() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun saveHasJoinedZeroIds(userId: Int, currentPurchases: List<ZeroBuyItem>) {
|
private fun saveHasJoinedZeroIds(userId: Int, currentPurchases: List<ZeroBuyItem>?) {
|
||||||
val joinZeroBuyItemIds = SpUtil.instance().getList<Int>(SpUtil.KEY_ZEROBUY_JOINED_ACTIVITY_IDS).toMutableList()
|
val joinZeroBuyItemIds = SpUtil.instance().getList<Int>(SpUtil.KEY_ZEROBUY_JOINED_ACTIVITY_IDS)?.toMutableList() ?: mutableListOf()
|
||||||
currentPurchases.forEach { zero ->
|
currentPurchases?.forEach { zero ->
|
||||||
zero.current_users?.let {
|
zero.current_users?.let {
|
||||||
if (it.contains(userId) && !joinZeroBuyItemIds.contains(userId)) {
|
if (it.contains(userId) && !joinZeroBuyItemIds.contains(zero.id)) {
|
||||||
joinZeroBuyItemIds.add(zero.id)
|
joinZeroBuyItemIds.add(zero.id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,8 @@ import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.SupervisorJob
|
import kotlinx.coroutines.SupervisorJob
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import org.json.JSONArray
|
||||||
|
import org.json.JSONObject
|
||||||
import java.util.concurrent.locks.ReentrantLock
|
import java.util.concurrent.locks.ReentrantLock
|
||||||
import kotlin.Int
|
import kotlin.Int
|
||||||
|
|
||||||
|
|
@ -173,8 +175,24 @@ class ZeroManager private constructor() {
|
||||||
val signOriginStr = "${zeroWithdrawItem.purchase_id}-${userId}-${bankCPFAccount}-${accountType}-${bankCPFAccount}-${accountType}-${ZEROBUY_SECRET}"
|
val signOriginStr = "${zeroWithdrawItem.purchase_id}-${userId}-${bankCPFAccount}-${accountType}-${bankCPFAccount}-${accountType}-${ZEROBUY_SECRET}"
|
||||||
requestParams.put("sign", MD5Util.md5(signOriginStr)!!)
|
requestParams.put("sign", MD5Util.md5(signOriginStr)!!)
|
||||||
|
|
||||||
|
val jsonObj = JSONObject().apply {
|
||||||
|
put("app_id",AndroidUtil.getPackageId())
|
||||||
|
put("device_id",DeviceUtil.generateDeviceId())
|
||||||
|
put("activity_id", zeroWithdrawItem.purchase_id)
|
||||||
|
put("order_id", zeroWithdrawItem.orderId)
|
||||||
|
if (userId > 0) {
|
||||||
|
put("user_id", userId)
|
||||||
|
}
|
||||||
|
put("account", bankCPFAccount)
|
||||||
|
put("account_type", accountType)
|
||||||
|
put("country", Country.BR.name)
|
||||||
|
put("document_type", accountType)
|
||||||
|
put("document_id", bankCPFAccount)
|
||||||
|
put("bank_code", AccountManager.getAccount().bankInfo?.bankAccount!!)
|
||||||
|
put("sign", MD5Util.md5(signOriginStr)!!)
|
||||||
|
}
|
||||||
|
|
||||||
val result = NetworkUtil.post("${VidiConst.URL_ZERO_BUY}/any", requestHeaders, requestParams, joinZeroBuyItemIds)
|
val result = NetworkUtil.postJson("${VidiConst.URL_ZERO_BUY}/any", jsonObj.toString(),requestHeaders)
|
||||||
when (result) {
|
when (result) {
|
||||||
is Result.Success -> {
|
is Result.Success -> {
|
||||||
val respObj = AndroidUtil.json2Object<ZeroBuyWithdrawResp>(result.data.string())
|
val respObj = AndroidUtil.json2Object<ZeroBuyWithdrawResp>(result.data.string())
|
||||||
|
|
@ -226,8 +244,17 @@ class ZeroManager private constructor() {
|
||||||
// withdraw check result 相关参数:
|
// withdraw check result 相关参数:
|
||||||
requestParams.put("order_id", zeroWithdrawItem.orderId)
|
requestParams.put("order_id", zeroWithdrawItem.orderId)
|
||||||
|
|
||||||
|
val jsonObj = JSONObject().apply {
|
||||||
|
put("app_id",AndroidUtil.getPackageId())
|
||||||
|
put("device_id",DeviceUtil.generateDeviceId())
|
||||||
|
put("activity_id", zeroWithdrawItem.purchase_id)
|
||||||
|
put("order_id", zeroWithdrawItem.orderId)
|
||||||
|
if (userId > 0) {
|
||||||
|
put("user_id", userId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val result = NetworkUtil.post("${VidiConst.URL_ZERO_BUY}/any", requestHeaders, requestParams)
|
val result = NetworkUtil.postJson("${VidiConst.URL_ZERO_BUY}/any", jsonObj.toString(),requestHeaders)
|
||||||
//val result = NetworkUtil.post("http://192.168.110.141:8081/any", requestHeaders, requestParams)
|
//val result = NetworkUtil.post("http://192.168.110.141:8081/any", requestHeaders, requestParams)
|
||||||
|
|
||||||
when (result) {
|
when (result) {
|
||||||
|
|
|
||||||
|
|
@ -32,15 +32,14 @@ interface ApiService {
|
||||||
suspend fun postRequest(
|
suspend fun postRequest(
|
||||||
@Url url: String,
|
@Url url: String,
|
||||||
@HeaderMap headers: Map<String, String> = emptyMap(),
|
@HeaderMap headers: Map<String, String> = emptyMap(),
|
||||||
@FieldMap params: Map<String, String> = emptyMap(),
|
@FieldMap params: Map<String, String> = emptyMap()
|
||||||
@Field("JoinedPurchaseIds") JoinedPurchaseIds: List<Int> = emptyList<Int>(),
|
|
||||||
): ResponseBody
|
): ResponseBody
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
suspend fun postJsonRequest(
|
suspend fun postJsonRequest(
|
||||||
@Url url: String,
|
@Url url: String,
|
||||||
@HeaderMap headers: Map<String, String> = emptyMap(),
|
@HeaderMap headers: Map<String, String> = emptyMap(),
|
||||||
@Body body: Any
|
@Body body: okhttp3.RequestBody
|
||||||
): ResponseBody
|
): ResponseBody
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
package com.gamedog.vididin.netbase
|
package com.gamedog.vididin.netbase
|
||||||
|
|
||||||
|
import com.ama.core.architecture.util.AndroidUtil
|
||||||
|
import okhttp3.MediaType.Companion.toMediaType
|
||||||
|
import okhttp3.RequestBody.Companion.toRequestBody
|
||||||
import okhttp3.ResponseBody
|
import okhttp3.ResponseBody
|
||||||
import retrofit2.HttpException
|
import retrofit2.HttpException
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
|
@ -28,11 +31,10 @@ object NetworkUtil {
|
||||||
suspend fun post(
|
suspend fun post(
|
||||||
url: String,
|
url: String,
|
||||||
headers: Map<String, String> = emptyMap(),
|
headers: Map<String, String> = emptyMap(),
|
||||||
params: Map<String, String> = emptyMap(),
|
params: Map<String, String> = emptyMap()
|
||||||
JoinedPurchaseIds : List<Int> = emptyList<Int>()
|
|
||||||
): Result<ResponseBody> {
|
): Result<ResponseBody> {
|
||||||
return executeRequest {
|
return executeRequest {
|
||||||
apiService.postRequest(url, headers, params, JoinedPurchaseIds)
|
apiService.postRequest(url, headers, params)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -43,7 +45,37 @@ object NetworkUtil {
|
||||||
headers: Map<String, String> = emptyMap()
|
headers: Map<String, String> = emptyMap()
|
||||||
): Result<ResponseBody> {
|
): Result<ResponseBody> {
|
||||||
return executeRequest {
|
return executeRequest {
|
||||||
apiService.postJsonRequest(url, headers, body)
|
val requestBody = when (body) {
|
||||||
|
is okhttp3.RequestBody -> body
|
||||||
|
is String -> body.toRequestBody("application/json; charset=utf-8".toMediaType())
|
||||||
|
else -> AndroidUtil.object2Json(body).toRequestBody("application/json; charset=utf-8".toMediaType())
|
||||||
|
}
|
||||||
|
apiService.postJsonRequest(url, headers, requestBody)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend inline fun <reified T> postJsonGeneric(
|
||||||
|
url: String,
|
||||||
|
body: Any,
|
||||||
|
headers: Map<String, String> = emptyMap()
|
||||||
|
): Result<T> {
|
||||||
|
val result = postJson(url, body, headers)
|
||||||
|
return when (result) {
|
||||||
|
is Result.Success -> {
|
||||||
|
try {
|
||||||
|
val json = result.data.string()
|
||||||
|
val data = AndroidUtil.json2Object<T>(json)
|
||||||
|
if (data != null) {
|
||||||
|
Result.Success(data)
|
||||||
|
} else {
|
||||||
|
Result.Error(Exception("JSON解析失败"), "数据解析失败")
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Result.Error(e, "数据解析异常: ${e.message}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
is Result.Error -> Result.Error(result.exception, result.message)
|
||||||
|
is Result.Loading -> Result.Loading
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue