parent
d4190694a9
commit
eb7b98b429
@ -0,0 +1,166 @@ |
||||
package xyz.fycz.myreader.model.third3 |
||||
|
||||
import android.util.Base64 |
||||
import android.util.Log |
||||
import xyz.fycz.myreader.common.APPCONST |
||||
import xyz.fycz.myreader.greendao.service.CacheManager |
||||
import xyz.fycz.myreader.greendao.service.CookieStore |
||||
import xyz.fycz.myreader.model.third3.analyzeRule.JsExtensions |
||||
import xyz.fycz.myreader.util.utils.EncoderUtils |
||||
import xyz.fycz.myreader.util.utils.GSON |
||||
import xyz.fycz.myreader.util.utils.fromJsonObject |
||||
import javax.script.SimpleBindings |
||||
|
||||
/** |
||||
* 可在js里调用,source.xxx() |
||||
*/ |
||||
@Suppress("unused") |
||||
abstract class BaseSource : JsExtensions { |
||||
|
||||
open var concurrentRate: String? = null // 并发率 |
||||
open var loginUrl: String? = null // 登录地址 |
||||
//var loginUi: String? // 登录UI |
||||
open var header: String? = null // 请求头 |
||||
|
||||
open fun getTag(): String = "" |
||||
|
||||
open fun getKey(): String = "" |
||||
|
||||
/*fun loginUi(): List<RowUi>? { |
||||
return GSON.fromJsonArray(loginUi) |
||||
}*/ |
||||
|
||||
fun getLoginJs(): String? { |
||||
val loginJs = loginUrl |
||||
return when { |
||||
loginJs == null -> null |
||||
loginJs.startsWith("@js:") -> loginJs.substring(4) |
||||
loginJs.startsWith("<js>") -> |
||||
loginJs.substring(4, loginJs.lastIndexOf("<")) |
||||
else -> loginJs |
||||
} |
||||
} |
||||
|
||||
fun login() { |
||||
getLoginJs()?.let { |
||||
evalJS(it) |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 解析header规则 |
||||
*/ |
||||
fun getHeaderMap(hasLoginHeader: Boolean = false) = HashMap<String, String>().apply { |
||||
this[APPCONST.UA_NAME] = APPCONST.DEFAULT_USER_AGENT |
||||
header?.let { |
||||
GSON.fromJsonObject<Map<String, String>>( |
||||
when { |
||||
it.startsWith("@js:", true) -> |
||||
evalJS(it.substring(4)).toString() |
||||
it.startsWith("<js>", true) -> |
||||
evalJS(it.substring(4, it.lastIndexOf("<"))).toString() |
||||
else -> it |
||||
} |
||||
)?.let { map -> |
||||
putAll(map) |
||||
} |
||||
} |
||||
if (hasLoginHeader) { |
||||
getLoginHeaderMap()?.let { |
||||
putAll(it) |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 获取用于登录的头部信息 |
||||
*/ |
||||
fun getLoginHeader(): String? { |
||||
return CacheManager.get("loginHeader_${getKey()}") |
||||
} |
||||
|
||||
fun getLoginHeaderMap(): Map<String, String>? { |
||||
val cache = getLoginHeader() ?: return null |
||||
return GSON.fromJsonObject(cache) |
||||
} |
||||
|
||||
/** |
||||
* 保存登录头部信息,map格式,访问时自动添加 |
||||
*/ |
||||
fun putLoginHeader(header: String) { |
||||
CacheManager.put("loginHeader_${getKey()}", header) |
||||
} |
||||
|
||||
fun removeLoginHeader() { |
||||
CacheManager.delete("loginHeader_${getKey()}") |
||||
} |
||||
|
||||
/** |
||||
* 获取用户信息,可以用来登录 |
||||
* 用户信息采用aes加密存储 |
||||
*/ |
||||
fun getLoginInfo(): String? { |
||||
try { |
||||
val key = APPCONST.androidId.encodeToByteArray(0, 8) |
||||
val cache = CacheManager.get("userInfo_${getKey()}") ?: return null |
||||
val encodeBytes = Base64.decode(cache, Base64.DEFAULT) |
||||
val decodeBytes = EncoderUtils.decryptAES(encodeBytes, key) |
||||
?: return null |
||||
return String(decodeBytes) |
||||
} catch (e: Exception) { |
||||
Log.e("BaseSource","" + e.localizedMessage) |
||||
return null |
||||
} |
||||
} |
||||
|
||||
fun getLoginInfoMap(): Map<String, String>? { |
||||
return GSON.fromJsonObject(getLoginInfo()) |
||||
} |
||||
|
||||
/** |
||||
* 保存用户信息,aes加密 |
||||
*/ |
||||
fun putLoginInfo(info: String): Boolean { |
||||
return try { |
||||
val key = (APPCONST.androidId).encodeToByteArray(0, 8) |
||||
val encodeBytes = EncoderUtils.encryptAES(info.toByteArray(), key) |
||||
val encodeStr = Base64.encodeToString(encodeBytes, Base64.DEFAULT) |
||||
CacheManager.put("userInfo_${getKey()}", encodeStr) |
||||
true |
||||
} catch (e: Exception) { |
||||
Log.e("BaseSource","" + e.localizedMessage) |
||||
false |
||||
} |
||||
} |
||||
|
||||
fun removeLoginInfo() { |
||||
CacheManager.delete("userInfo_${getKey()}") |
||||
} |
||||
|
||||
fun setVariable(variable: String?) { |
||||
if (variable != null) { |
||||
CacheManager.put("sourceVariable_${getKey()}", variable) |
||||
} else { |
||||
CacheManager.delete("sourceVariable_${getKey()}") |
||||
} |
||||
} |
||||
|
||||
fun getVariable(): String? { |
||||
return CacheManager.get("sourceVariable_${getKey()}") |
||||
} |
||||
|
||||
/** |
||||
* 执行JS |
||||
*/ |
||||
@Throws(Exception::class) |
||||
fun evalJS(jsStr: String, bindingsConfig: SimpleBindings.() -> Unit = {}): Any? { |
||||
val bindings = SimpleBindings() |
||||
bindings.apply(bindingsConfig) |
||||
bindings["java"] = this |
||||
bindings["source"] = this |
||||
bindings["baseUrl"] = getKey() |
||||
bindings["cookie"] = CookieStore |
||||
bindings["cache"] = CacheManager |
||||
return APPCONST.SCRIPT_ENGINE.eval(jsStr, bindings) |
||||
} |
||||
} |
Loading…
Reference in new issue