联合支付功能调试

This commit is contained in:
2026-03-23 18:13:55 +08:00
parent bf8444e5d5
commit c4a6101a7b
13 changed files with 350 additions and 143 deletions
@@ -32,10 +32,10 @@ import com.sw.dualscreen.databinding.ActivityMainBinding
import com.sw.dualscreen.dialog.RemindDialog
import com.sw.dualscreen.ext.dp
import com.sw.dualscreen.ext.gone
import com.sw.dualscreen.ext.invisible
import com.sw.dualscreen.ext.load
import com.sw.dualscreen.ext.visible
import com.sw.dualscreen.model.response.ChargeModeEvent
import com.sw.dualscreen.model.response.ClickBackEvent
import com.sw.dualscreen.model.response.FoodInfo
import com.sw.dualscreen.model.response.FoodOrder
import com.sw.dualscreen.model.response.PaySuccessEvent
@@ -79,7 +79,6 @@ import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.Locale
import java.util.concurrent.Executors
import kotlin.math.abs
import kotlin.math.roundToInt
/**
@@ -91,6 +90,9 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
companion object {
private const val TAG = "MainActivity"
const val TIME_OUT = 5 * 60 * 1000L
const val WEIGHT_CHANGE_VALUE = 25
const val WEIGHT_RESET_RECOGNIZE = 10
}
private val viewModel by viewModels<UserViewModel>()
@@ -101,28 +103,36 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
private var presentation: MainScreenPresentation? = null
private val executor = Executors.newSingleThreadExecutor()
private val handler by lazy { Handler(Looper.getMainLooper()) }
private val searchFoodList = mutableListOf<FoodInfo>()
private val adapter by lazy {
MainFoodListAdapter(searchFoodList).apply {
MainFoodListAdapter(mutableListOf()).apply {
setOnItemClickListener { adapter, view, position ->
if (searchFoodList[position].isChecked) {
if (items[position].isChecked) {
return@setOnItemClickListener
}
searchFoodList.forEachIndexed { index, info ->
searchFoodList[index].isChecked = index == position
items.forEachIndexed { index, info ->
items[index].isChecked = index == position
}
val item = searchFoodList[position]
val item = items[position]
log("itemClick ${item.foodName}, position = $position")
item.photoUri = null
checkedItem = item
notifyDataSetChanged()
updateCurrentFood(item)
//0-计费,1-不计费
val mode = SPUtil.getInstance().get(GlobalKey.KEY_CHARGE_MODE, 0)
//不计费模式切换菜品,重新加载数据
if (mode == 1) {
presentation?.isAgainLoadData = false
presentation?.loadUnbilledMode()
if (settlementMode == 0) {
//联合支付,切换菜品重新请求接口
presentation?.let {
it.pauseCamera()
it.step2FaceRecognizing(item, true)
}
// presentation?.step3ShowRecognizeResult(presentation!!.currentUserId?:"")
} else {
//0-计费,1-不计费
val mode = SPUtil.getInstance().get(GlobalKey.KEY_CHARGE_MODE, 0)
//不计费模式切换菜品,重新加载数据
if (mode == 1) {
presentation?.isAgainLoadData = false
presentation?.loadUnbilledMode()
}
}
}
}
@@ -132,7 +142,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
private var cameraProviderFuture: ListenableFuture<ProcessCameraProvider>? = null
private var canIdentify: Boolean = false
private var bottomSheetDialog: CustomBottomSheetDialog? = null
private var lastWeight: Double = 0.0
private var lastWeight: Int = 0
private var lastPhotoUri: Uri? = null // 最后拍照的图片
private var debouncer = Debouncer(5000)
private var isRecognitionFood = true
@@ -200,8 +210,8 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
checkedItem = it
it.photoUri = null
updateCurrentFood(it)
searchFoodList.forEachIndexed { index, info ->
searchFoodList[index].isChecked = false
adapter.items.forEachIndexed { index, info ->
adapter.items[index].isChecked = false
}
adapter.notifyDataSetChanged()
}
@@ -209,7 +219,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
}
binding.recyclerview.let {
it.layoutManager = GridLayoutManager(this, 2)
it.layoutManager = GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false)
it.adapter = adapter
}
binding.tvFoodName.setOnClickListener {
@@ -220,7 +230,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
}
}
private var recognizeWeight = 0.0
private var recognizeWeight = 0
fun updateCurrentFood(foodInfo: FoodInfo?) {
if (foodInfo == null) {
binding.tvFoodName.text = "-"
@@ -245,52 +255,75 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
binding.ivImg.load(imgUrl)
}
override fun registerDataChange() {
super.registerDataChange()
SensorScaleUtils.addWeightListener { weight ->
log("registerDataChange weight = $weight")
//runOnUiThread { binding.tvShowWeight.text="${weight*1000}克" }
if (presentation?.jointPaymentQueryFinish == true) {
//0-即放即取,1-余量计量
val mealPickupMode = SPUtil.getInstance().get(GlobalKey.KEY_PICKUP_MODE, 0) ?: 0
if (mealPickupMode == 0 && weight <= 0.005) {
isAnalyzing = false
presentation?.step1FoodRecognizing()
} else {
return@addWeightListener
}
private fun readWeight(weight: Int) {
log("registerDataChange weight = $weight")
//runOnUiThread { binding.tvShowWeight.text="${weight*1000}克" }
if (settlementMode == 0) {
//联合支付-------------------------------------------
//0-即放即取,1-余量计量
val mealPickupMode = SPUtil.getInstance().get(GlobalKey.KEY_PICKUP_MODE, 0) ?: 0
if (mealPickupMode == 1) {
return
}
//if (presentation?.currentFood == null) return@addWeightListener
if (lastWeight - weight > 0.005) {
//及放及取,从秤上拿走超过5克
if (presentation?.mealPickupMode == 0) {
presentation?.updateWeight(weight)
lastWeight = weight
return@addWeightListener
//即放即取+联合支付人脸查询订单完成
if (presentation!!.jointPaymentQueryFinish) {
if (weight > WEIGHT_RESET_RECOGNIZE) {
//已识别完成人脸并查询到订单数据,增加重量不再重新识别
return
}
//重量小于等于5克时改为识别中页面
isAnalyzing = false
presentation?.step1FoodRecognizing()
return
}
if (isPageVisible.not() || isStartRecognize.not()) {
if (ActivityManager.currentActivity() is MainActivity) {
isPageVisible = true
isStartRecognize = true
}
return@addWeightListener
if (weight <= WEIGHT_RESET_RECOGNIZE) {
//秤上重量过下,显示识别中页面
presentation?.step1FoodRecognizing()
return
}
//0.05
// val isWeightChange = abs(weight - lastWeight) > 0.02
val isWeightChange = weight - lastWeight > 0.02
val isWeightChange = weight - lastWeight > WEIGHT_CHANGE_VALUE
if (weight != lastWeight) {
log("recognizeByWeight---00001,weight=$weight,lastWeight=$lastWeight,isWeightChange=$isWeightChange")
}
recognizeByWeight(weight, isWeightChange)
return
}
//独立支付-------------------------------------------
if (lastWeight - weight > 5) {
//及放及取,从秤上拿走超过5克
if (presentation?.mealPickupMode == 0) {
presentation?.updateWeight(weight)
lastWeight = weight
return
}
}
if (isPageVisible.not() || isStartRecognize.not()) {
if (ActivityManager.currentActivity() is MainActivity) {
isPageVisible = true
isStartRecognize = true
}
return
}
// val isWeightChange = abs(weight - lastWeight) > 0.02
val isWeightChange = weight - lastWeight > WEIGHT_CHANGE_VALUE
if (weight != lastWeight) {
log("recognizeByWeight---00001,weight=$weight,lastWeight=$lastWeight,isWeightChange=$isWeightChange")
}
recognizeByWeight(weight, isWeightChange)
}
override fun registerDataChange() {
super.registerDataChange()
SensorScaleUtils.addWeightListener { value ->
readWeight(value)
}
}
private fun recognizeByWeight(weight: Double, isWeightChange: Boolean, block: () -> Unit = {}) {
private fun recognizeByWeight(weight: Int, isWeightChange: Boolean, block: () -> Unit = {}) {
presentation?.updateWeight(weight)
if (weight <= 0.005) {
if (weight <= WEIGHT_RESET_RECOGNIZE) {
log("recognizeByWeight---00002,秤重未超过5克")
//检测到秤上没有东西,重新启动识别菜品 && presentation?.mealPickupMode == 1
isRecognitionFood = true
@@ -374,9 +407,10 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
// e.printStackTrace()
// list
// }
searchFoodList.clear()
searchFoodList.addAll(list)
adapter.notifyDataSetChanged()
adapter.submitList(list)
binding.recyclerview.visible()
checkedItem = list[0].also {
it.photoUri = lastPhotoUri
@@ -559,10 +593,14 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
}
@SuppressLint("NotifyDataSetChanged")
fun clearFoodList() {
//adapter.updateData(listOf())
searchFoodList.clear()
adapter.notifyDataSetChanged()
fun clearRecognizeFood() {
log("clearRecognizeFood")
binding.recyclerview.invisible()
adapter.submitList(null)
binding.tvToSearch.gone()
binding.previewView.visible()
binding.ivImg.gone()
}
private fun updateDateTime() {
@@ -594,7 +632,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
log("onResume-00001isRefreshPage=$isRefreshPage")
if (presentation == null || !presentation!!.isShowing) {
log("onResume-00002,副屏未显示,加载副屏")
lastWeight = 0.0
lastWeight = 0
setupSecondaryDisplay()
presentation?.step1FoodRecognizing()
} else {
@@ -603,7 +641,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
isRefreshPage = false
//已显示副屏,则进行更新
val weight = lastWeight
lastWeight = 0.0
lastWeight = 0
presentation?.hideRecImage()
presentation?.step1FoodRecognizing()
@@ -662,7 +700,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
eatWeight: Int,
eatNum: Int,
userId: String?,
isMember:Boolean,
isMember: Boolean,
notEnoughOneBlock: () -> Unit = {},
successBlock: () -> Unit
) {
@@ -800,36 +838,36 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
val mealPickupMode = SPUtil.getInstance().get(GlobalKey.KEY_PICKUP_MODE, 0) ?: 0
//0-计费,1-不计费
val chargeMode = SPUtil.getInstance().get(GlobalKey.KEY_CHARGE_MODE, 0) ?: 0
var foodWeight = 0.0
var eatWeight = 0.0
var foodWeight = 0
var eatWeight = 0
if (chargeMode == 0) {
//计费
if (mealPickupMode == 0) {
//即放即取
eatWeight = lastWeight * 1000
foodWeight = lastWeight * 1000
eatWeight = lastWeight
foodWeight = lastWeight
} else {
//余量计量
eatWeight = (recognizeWeight - lastWeight) * 1000
foodWeight = recognizeWeight * 1000
eatWeight = recognizeWeight - lastWeight
foodWeight = recognizeWeight
log("eatWeight=$eatWeight, recognizeWeight=$recognizeWeight, lastWeight=$lastWeight")
}
} else {
//不计费
foodWeight = recognizeWeight * 1000
foodWeight = recognizeWeight
eatWeight = if (mealPickupMode == 0) {
//即放即取
recognizeWeight * 1000
recognizeWeight
} else {
//余量计量
(recognizeWeight - lastWeight) * 1000
recognizeWeight - lastWeight
}
}
val eatNum = getEatNum(eatWeight, checkedItem!!.specWeight)
val eatNum = getEatNum(eatWeight.toDouble(), checkedItem!!.specWeight)
createOrder(
foodInfo = checkedItem!!,
foodWeight = foodWeight.roundToInt(),
eatWeight = eatWeight.roundToInt(),
foodWeight = foodWeight,
eatWeight = eatWeight,
eatNum = eatNum,
userId = null,
isMember = false
@@ -956,7 +994,8 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
// private val dealyMillis = 10 * 60 * 1000L
private val initialDelay = 10 * 1000L
private val dealyMillis = 5 * 1000L
// private var taskPageNo = 1
// private var taskPageNo = 1
fun startFaceTask() {
faceTaskJob =
intervalExecutor.startIntervalTaskWithInitialDelay(initialDelay, dealyMillis) {
@@ -1097,13 +1136,6 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
}
}
/**
* 隐藏搜索按钮
*/
fun hideSearchView() {
binding.tvToSearch.gone()
}
// @Subscribe(threadMode = ThreadMode.MAIN)
// public fun onResetBox(event: ResetBoxEvent) {
// ObjectBox.boxStore.close()