package com.zhaoyang.driver.netUtls
|
|
import android.content.Context
|
import android.os.CountDownTimer
|
import android.os.Handler
|
import android.os.Looper
|
import android.view.LayoutInflater
|
import android.view.View
|
import cn.sinata.xldutils.utils.Base64
|
import com.zhaoyang.driver.base.MyBaseActivity
|
import com.zhaoyang.driver.base.MyBaseFragment
|
import com.zhaoyang.driver.utils.Cache.CacheKey
|
import com.zhaoyang.driver.utils.DES
|
import kotlin.collections.HashMap
|
|
fun delayTo(time: Long, clickAction: () -> Unit) {
|
Handler(Looper.getMainLooper()).postDelayed({
|
clickAction()
|
}, time)
|
}
|
|
fun MyBaseActivity.callNet(
|
api: String,
|
map: HashMap<String?, Any?>,
|
clickAction: (responseString: String?) -> Unit
|
) {
|
callNet(true, api, map, clickAction)
|
}
|
|
fun MyBaseActivity.callNet(
|
showDialog: Boolean,
|
api: String,
|
map: HashMap<String?, Any?>,
|
clickAction: (responseString: String?) -> Unit
|
) {
|
if (showDialog)
|
showDialog()
|
if (api.startsWith("api")) {
|
map["appid"] = getAppId()
|
var sign = DES.getSign(map)
|
map["sign"] = sign
|
}
|
HttpManager.getInstance().post(api, map, object : MyObserver(this) {
|
override fun success(responseString: String?) {
|
if (showDialog)
|
dismissDialog()
|
clickAction(responseString)
|
}
|
|
})
|
}
|
|
fun MyBaseFragment.callNet(
|
api: String,
|
map: HashMap<String?, Any?>,
|
clickAction: (responseString: String?) -> Unit
|
) {
|
this.showDialog()
|
if (api.startsWith("api")) {
|
map["appid"] = getAppId()
|
var sign = DES.getSign(map)
|
map["sign"] = sign
|
}
|
HttpManager.getInstance().post(api, map, object : MyObserver(this) {
|
override fun success(responseString: String?) {
|
dismissDialog()
|
clickAction(responseString)
|
}
|
})
|
}
|
|
fun getAppId(): String {
|
var appid = CacheKey.getKeyStr("appid")
|
if (appid.isEmpty()){
|
return ""
|
} else{
|
return appid
|
}
|
}
|
|
fun getUserId(): Int {
|
var user = CacheKey.getKeyStr("userId")
|
if (user.isNullOrEmpty()) {
|
user = "-1"
|
}
|
return user.toInt()
|
}
|
|
fun getToken(): String {
|
return CacheKey.getKeyStr("token")
|
}
|
|
|
@UseExperimental(ExperimentalStdlibApi::class)
|
fun getBase64Str(s: String): String {
|
return Base64.encode(s.toByteArray(), Base64.DEFAULT)!!.decodeToString().trim()
|
}
|
|
|
@UseExperimental(ExperimentalStdlibApi::class)
|
fun getBase64StrD(s: String): String {
|
return Base64.decode(s.toByteArray(), Base64.DEFAULT)!!.decodeToString().trim()
|
}
|
|
fun MyBaseActivity.callNet(
|
api: String,
|
map: HashMap<String?, Any?>,
|
clickAction: (responseString: String?) -> Unit,
|
clickActionError: (errorString: String?) -> Unit
|
) {
|
this.showDialog()
|
if (api.startsWith("api")) {
|
map["appid"] = getAppId()
|
var sign = DES.getSign(map)
|
map["sign"] = sign
|
}
|
HttpManager.getInstance().post(api, map, object : MyObserver(this) {
|
override fun success(responseString: String?) {
|
dismissDialog()
|
clickAction(responseString)
|
}
|
|
override fun onError(code: Int, msg: String?, resposeString: String?) {
|
super.onError(code, msg, resposeString)
|
clickActionError(msg)
|
}
|
})
|
|
}
|
|
|
fun MyBaseActivity.callNetDriver(
|
api: String,
|
map: HashMap<String?, Any?>,
|
clickAction: (responseString: String?) -> Unit,
|
clickActionError: (errorString: String?) -> Unit
|
) {
|
this.showDialog()
|
if (api.startsWith("api")) {
|
map["appid"] = getAppId()
|
var sign = DES.getSign(map)
|
map["sign"] = sign
|
}
|
HttpManager.getInstance().post(api, map, object : MyObserver(this) {
|
override fun success(responseString: String?) {
|
dismissDialog()
|
clickAction(responseString)
|
}
|
|
override fun onError(code: Int, msg: String?, resposeString: String?) {
|
clickActionError(resposeString)
|
}
|
})
|
|
}
|
|
|
fun MyBaseFragment.callNet(
|
api: String,
|
map: HashMap<String?, Any?>,
|
clickAction: (responseString: String?) -> Unit,
|
clickActionError: (errorString: String?) -> Unit
|
) {
|
this.showDialog()
|
if (api.startsWith("api")) {
|
map["appid"] = getAppId()
|
var sign = DES.getSign(map)
|
map["sign"] = sign
|
}
|
HttpManager.getInstance().post(api, map, object : MyObserver(this) {
|
override fun success(responseString: String?) {
|
dismissDialog()
|
clickAction(responseString)
|
}
|
|
override fun onError(code: Int, msg: String?, resposeString: String?) {
|
super.onError(code, msg, resposeString)
|
|
clickActionError(msg)
|
}
|
})
|
|
}
|
|
fun callNet(
|
context: Context,
|
api: String,
|
map: HashMap<String?, Any?>,
|
clickAction: (responseString: String?) -> Unit
|
) {
|
if (api.startsWith("api")) {
|
map["appid"] = getAppId()
|
var sign = DES.getSign(map)
|
map["sign"] = sign
|
}
|
HttpManager.getInstance().post(api, map, object : MyObserver(context) {
|
override fun success(responseString: String?) {
|
clickAction(responseString)
|
}
|
})
|
}
|
|
fun getMapByAny(): HashMap<String?, Any?> {
|
return HashMap()
|
}
|
|
|
fun createView(layout: Int, context: Context): View {
|
return LayoutInflater.from(context).inflate(layout, null)
|
}
|
|
fun timeOver(click: (mill: Int) -> Unit): CountDownTimer {
|
val timer by lazy {
|
object : CountDownTimer(60000, 1000) {
|
override fun onFinish() {
|
click(0)
|
}
|
|
override fun onTick(millisUntilFinished: Long) {
|
click((millisUntilFinished / 1000).toInt())
|
}
|
}
|
}
|
timer.start()
|
return timer
|
}
|
|
|
fun timeOver(timeDown:Long,click: (mill: Int) -> Unit): CountDownTimer {
|
val timer by lazy {
|
object : CountDownTimer(timeDown, 1000) {
|
override fun onFinish() {
|
click(0)
|
}
|
|
override fun onTick(millisUntilFinished: Long) {
|
click((millisUntilFinished / 1000).toInt())
|
}
|
}
|
}
|
timer.start()
|
return timer
|
}
|
|
|
fun timeStart(click: (mill: Int) -> Unit): CountDownTimer {
|
val timer by lazy {
|
object : CountDownTimer(100000, 1000) {
|
override fun onFinish() {
|
click(0)
|
}
|
|
override fun onTick(millisUntilFinished: Long) {
|
var num = ((100000 - millisUntilFinished.toInt()) / 1000)
|
click(num)
|
}
|
}
|
}
|
timer.start()
|
return timer
|
}
|