Skip to content

Commit

Permalink
Merge pull request #66 from CofbroTeam/feature/signPhoto_xxxd86
Browse files Browse the repository at this point in the history
修复图片签到无法成功的问题
  • Loading branch information
cofbro committed Nov 27, 2023
2 parents caada4b + 96fc2bf commit f98b57e
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 74 deletions.
133 changes: 63 additions & 70 deletions app/src/main/java/com/cofbro/qian/mapsetting/MapActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ class MapActivity : BaseActivity<MapViewModel, ActivityMapBinding>(), AMap.OnMar
private var alreadySign = false
private var cookies = ""
private var remark = ""
private var preSignUrl = ""
private var mStatus = true
private var otherSignUsers: JSONArray? = null
private var alreadySignCount = 0
private var loadingDialog: Dialog? = null
private var preSignOther = false
override fun onActivityCreated(savedInstanceState: Bundle?) {

getAvtarImage()
Expand Down Expand Up @@ -472,9 +472,7 @@ class MapActivity : BaseActivity<MapViewModel, ActivityMapBinding>(), AMap.OnMar


}

private fun signRecord(body: String = "", cookies: String = "") {

if (alreadySign) return
if(body.isNotEmpty()){
val status = body.contains("成功") || body.contains("success")
Expand All @@ -486,24 +484,12 @@ class MapActivity : BaseActivity<MapViewModel, ActivityMapBinding>(), AMap.OnMar
}

}

private fun record(uid: String, status: Boolean) {
val courseName = viewModel.courseName
val statusName = if (status) "成功" else "失败"
val username = if (remark.isNotEmpty()) "$uid - ($remark)" else uid
SignRecorder.record(applicationContext, username, courseName!!, statusName)
}

// private suspend fun signWith(id: String, code: String = "", cookies: String) {
// val uid = findUID(cookies)
// val signWithPreSign = preSignUrl.substringBefore("uid=") + "uid=$uid"
// viewModel.preSign(signWithPreSign, cookies)
// if (qrCodeId.isEmpty()) {
// signTogether(id, code, cookies)
// } else {
// signTogether(qrCodeId, cookies)
// }
// }
private suspend fun analysisAndStartSign(aid: String) {
viewModel.analysis(URL.getAnalysisPath(aid))
}
Expand Down Expand Up @@ -557,71 +543,76 @@ class MapActivity : BaseActivity<MapViewModel, ActivityMapBinding>(), AMap.OnMar
val data = it.data?.body?.string()
val analysis2Code = data?.substringAfter("code='+'")?.substringBefore("'") ?: ""
viewModel.analysis2(URL.getAnalysis2Path(analysis2Code))
// lifecycleScope.launch(Dispatchers.IO) {
// viewModel.sign(viewModel.signUrl)
// }
}

}
viewModel.preSignLiveData.observe(this) {
lifecycleScope.launch(Dispatchers.IO) {
it.data?.body?.string()?.let {
val html = Jsoup.parse(it)
val locationText = html.getElementById("locationText")?.`val`()

val latitude = html.getElementById("locationLatitude")?.`val`()
val longitude = html.getElementById("locationLongitude")?.`val`()
val statusContent =
html.getElementsByClass("zsign_success zsign_hook").select(">h1").text()
binding?.mainKeywords?.apply {
hint = if (locationText?.isNotEmpty() == true) {
locationText
} else {
"老师未设置位置,请点击搜索"
}
}

if (!latitude.isNullOrEmpty() && !longitude.isNullOrEmpty()) {
viewModel.currentTipPoint =
LatLng(latitude.toDouble(), longitude.toDouble())
addLatLngMarker(
LatLng(
viewModel.currentTipPoint.latitude,
viewModel.currentTipPoint.longitude
), default = true
)
viewModel.default_Sign_Location = locationText
viewModel.default_Sign_Location = locationText
viewModel.statuscontent = statusContent
viewModel.default_Sign_Lating =
LatLng(latitude.toDouble(), longitude.toDouble())
if(preSignOther){
/**
* 老师未设置位置 设置提醒
* 代签无需进行操作
*/
if (locationText?.isEmpty() == true && statusContent != "签到成功") {
ToastUtil.show(applicationContext, "老师未设置位置,默认位置为自己位置")
viewModel.default_Sign_Lating = viewModel.default_My_Lating

} else if (statusContent == "签到成功") {
alreadySign = true
}

CacheUtils.cache["default_Sign_latitude"] = latitude
CacheUtils.cache["default_Sign_longitude"] = longitude
} else {
val lat = html.getElementById("latitude")?.`val`() ?: ""
val long = html.getElementById("longitude")?.`val`() ?: ""
if (lat.isNotEmpty() && long.isNotEmpty()) {
viewModel.currentTipPoint = LatLng(lat.toDouble(), lat.toDouble())
addLatLngMarker(LatLng(lat.toDouble(), long.toDouble()), default = true)
viewModel.default_Sign_Lating = LatLng(lat.toDouble(), lat.toDouble())
viewModel.default_Sign_Location = locationText
viewModel.statuscontent = statusContent
CacheUtils.cache["default_Sign_latitude"] = lat
CacheUtils.cache["default_Sign_longitude"] = long
}
}else{
viewModel.preSignWebGet(it,
onSuccess = { preWeb->
binding?.mainKeywords?.apply {
hint = if (preWeb.locationText?.isNotEmpty() == true) {
preWeb.locationText
} else {
"老师未设置位置,请点击搜索"
}
}
if (!preWeb.latitude.isNullOrEmpty() && !preWeb.longitude.isNullOrEmpty()) {
viewModel.currentTipPoint =
LatLng(preWeb.latitude.toDouble(), preWeb.longitude.toDouble())
addLatLngMarker(
LatLng(
viewModel.currentTipPoint.latitude,
viewModel.currentTipPoint.longitude
), default = true
)
viewModel.default_Sign_Location = preWeb.locationText
viewModel.default_Sign_Location = preWeb.locationText
viewModel.statuscontent = preWeb.statusContent
viewModel.default_Sign_Lating =
LatLng(preWeb.latitude.toDouble(), preWeb.longitude.toDouble())
/**
* 老师未设置位置 设置提醒
*/
if (preWeb.locationText?.isEmpty() == true && preWeb.statusContent != "签到成功") {
ToastUtil.show(applicationContext, "老师未设置位置,默认位置为自己位置")
viewModel.default_Sign_Lating = viewModel.default_My_Lating
} else if (preWeb.statusContent == "签到成功") {
alreadySign = true
}
CacheUtils.cache["default_Sign_latitude"] = preWeb.latitude
CacheUtils.cache["default_Sign_longitude"] = preWeb.longitude
} else {
val lat = preWeb.html.getElementById("latitude")?.`val`() ?: ""
val long = preWeb.html.getElementById("longitude")?.`val`() ?: ""
if (lat.isNotEmpty() && long.isNotEmpty()) {
viewModel.currentTipPoint = LatLng(lat.toDouble(), lat.toDouble())
addLatLngMarker(LatLng(lat.toDouble(), long.toDouble()), default = true)
viewModel.default_Sign_Lating = LatLng(lat.toDouble(), lat.toDouble())
viewModel.default_Sign_Location = preWeb.locationText
viewModel.statuscontent = preWeb.statusContent
if (preWeb.locationText?.isEmpty() == true && preWeb.statusContent != "签到成功") {
ToastUtil.show(applicationContext, "老师未设置位置,默认位置为自己位置")
viewModel.default_Sign_Lating = viewModel.default_My_Lating
} else if (preWeb.statusContent == "签到成功") {
alreadySign = true
}
CacheUtils.cache["default_Sign_latitude"] = lat
CacheUtils.cache["default_Sign_longitude"] = long
}
}

})
preSignOther = false
}


}


Expand Down Expand Up @@ -723,8 +714,10 @@ class MapActivity : BaseActivity<MapViewModel, ActivityMapBinding>(), AMap.OnMar
if (signWith && (data.contains("success") || data.contains("签到成功"))) {
// 如果本账号签到成功,则开始自动签到其他绑定账号
signWithAccounts()
preSignOther=true
}else{
val intent = Intent(applicationContext, MainActivity::class.java)
preSignOther=false
startActivity(intent)
}
}
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/com/cofbro/qian/mapsetting/util/PreWeb.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.cofbro.qian.mapsetting.util

import org.jsoup.nodes.Document

class PreWeb(
val html: Document,
val locationText:String?,
val latitude:String?,
val longitude:String?,
val statusContent:String)
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ import com.amap.api.services.poisearch.PoiSearchV2
import com.cofbro.hymvvmutils.base.BaseViewModel
import com.cofbro.hymvvmutils.base.ResponseMutableLiveData
import com.cofbro.qian.mapsetting.repository.MapRepository
import com.cofbro.qian.mapsetting.util.PreWeb
import com.cofbro.qian.utils.HtmlParser
import com.cofbro.qian.utils.NetworkUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.Response
import org.jsoup.Jsoup
import org.jsoup.nodes.Document

class MapViewModel : BaseViewModel<MapRepository>() {
val preSignLiveData = ResponseMutableLiveData<Response>()
Expand Down Expand Up @@ -103,4 +107,8 @@ class MapViewModel : BaseViewModel<MapRepository>() {
}
}
}
fun preSignWebGet(it:String,onSuccess: (PreWeb) -> Unit = {}){
onSuccess(HtmlParser.parsepreSignWebGet(it))
}

}
20 changes: 17 additions & 3 deletions app/src/main/java/com/cofbro/qian/photo/PhotoSignActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class PhotoSignActivity : BaseActivity<PhotoSignViewModel, ActivityPhotoSignBind
private fun doNetwork() {
lifecycleScope.launch(Dispatchers.IO) {
viewModel.requestToken(URL.getUploadToken())
analysisAndStartSign(aid)
}
}

Expand Down Expand Up @@ -77,6 +78,14 @@ class PhotoSignActivity : BaseActivity<PhotoSignViewModel, ActivityPhotoSignBind
}
}
}
viewModel.analysisLiveData.observe(this) {
lifecycleScope.launch(Dispatchers.IO) {
val data = it.data?.body?.string()
val analysis2Code = data?.substringAfter("code='+'")?.substringBefore("'") ?: ""
viewModel.analysis2(URL.getAnalysis2Path(analysis2Code))
}

}
}

private fun initView() {
Expand Down Expand Up @@ -118,10 +127,15 @@ class PhotoSignActivity : BaseActivity<PhotoSignViewModel, ActivityPhotoSignBind
})
}
}

private suspend fun analysisAndStartSign(aid: String) {
viewModel.analysis(URL.getAnalysisPath(aid))
}
private fun sign() {
val uid = CacheUtils.cache["uid"] ?: ""
viewModel.sign(URL.getSignWithPhoto(aid, uid, objectId))
lifecycleScope.launch(Dispatchers.IO) {
val uid = CacheUtils.cache["uid"] ?: ""
viewModel.sign(URL.getSignWithPhoto(aid, uid, objectId))
}

}

private fun showLoadingView() {
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/com/cofbro/qian/photo/PhotoSignViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ class PhotoSignViewModel : BaseViewModel<PhotoSignRepository>() {
val tokenLiveData = ResponseMutableLiveData<Response>()
val uploadImageLiveData = ResponseMutableLiveData<Response>()
val signLiveData = ResponseMutableLiveData<Response>()
val analysisLiveData = ResponseMutableLiveData<Response>()
suspend fun analysis(url: String) {
repository.request(analysisLiveData, false) {
val request = NetworkUtils.buildClientRequest(url)
NetworkUtils.request(request)
}
}
suspend fun analysis2(url: String, cookies: String = "") {
repository.request(ResponseMutableLiveData(), false) {
val request = if (cookies.isEmpty()) {
NetworkUtils.buildClientRequest(url)
} else NetworkUtils.buildClientRequest(url, cookies)
NetworkUtils.request(request)
}
}

suspend fun requestToken(url: String) {
val request = NetworkUtils.buildClientRequest(url)
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/cofbro/qian/profile/ProfileViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.cofbro.qian.profile

import com.cofbro.hymvvmutils.base.BaseViewModel
import com.cofbro.hymvvmutils.base.ResponseMutableLiveData
import com.cofbro.qian.utils.CacheUtils
import com.cofbro.qian.utils.DefaultRepository
import com.cofbro.qian.utils.NetworkUtils
import okhttp3.Response

class ProfileViewModel : BaseViewModel<DefaultRepository>() {
val uid = CacheUtils.cache["uid"] ?: ""
Expand Down
15 changes: 14 additions & 1 deletion app/src/main/java/com/cofbro/qian/utils/HtmlParser.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.cofbro.qian.utils

import com.alibaba.fastjson.JSONObject
import com.cofbro.qian.mapsetting.util.PreWeb
import com.cofbro.qian.mapsetting.viewmodel.MapViewModel
import org.jsoup.Jsoup
import org.jsoup.nodes.Document

object HtmlParser {
fun parseHomeworkHTML(html: String): List<JSONObject> {
Expand Down Expand Up @@ -64,4 +67,14 @@ object HtmlParser {
val doc = Jsoup.parse(html)
return doc.getElementById("signCode")?.`val`() ?: ""
}
}
fun parsepreSignWebGet(it:String): PreWeb {
val html = Jsoup.parse(it)
val locationText = html.getElementById("locationText")?.`val`()
val latitude = html.getElementById("locationLatitude")?.`val`()
val longitude = html.getElementById("locationLongitude")?.`val`()
val statusContent =
html.getElementsByClass("zsign_success zsign_hook").select(">h1").text()
return PreWeb(html, locationText, latitude, longitude, statusContent)
}

}
2 changes: 2 additions & 0 deletions app/src/main/java/com/cofbro/qian/utils/NetworkUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.cofbro.qian.utils

import com.cofbro.hymvvmutils.base.BaseResponse
import com.cofbro.hymvvmutils.base.DataState
import com.cofbro.qian.mapsetting.viewmodel.MapViewModel
import okhttp3.Call
import okhttp3.Callback
import okhttp3.MediaType.Companion.toMediaTypeOrNull
Expand Down Expand Up @@ -197,4 +198,5 @@ object NetworkUtils {
})
}


}

0 comments on commit f98b57e

Please sign in to comment.