Newer
Older
CasicSmartTube / app / src / main / java / com / casic / smarttube / extensions / String.kt
package com.casic.smarttube.extensions

import com.casic.smarttube.model.ErrorMessageModel
import com.casic.smarttube.utils.LocalConstant
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.pengxh.kt.lite.utils.SaveKeyValues
import org.json.JSONObject
import java.util.*

/**
 * String扩展方法
 */
fun String.separateResponseCode(): Int {
    if (this.isBlank()) {
        return 404
    }
    return JSONObject(this).getInt("code")
}

fun String.toErrorMessage(): String {
    val errorModel = Gson().fromJson<ErrorMessageModel>(
        this, object : TypeToken<ErrorMessageModel>() {}.type
    )
    return errorModel.message.toString()
}

/**
 * 下载路径为 http://xx.com/static/ 拼接downloadUrl
 * */
fun String.appendDownloadUrl(): String {
    if (this.isEmpty()) return this
    val defaultValue = SaveKeyValues.getValue(
        LocalConstant.DEFAULT_SERVER_CONFIG, LocalConstant.SERVER_BASE_URL
    ) as String
    return "$defaultValue/static/${this}"
}

//拼接图片地址
fun String.combineImagePath(): String {
    if (this.isEmpty()) return this
    val defaultValue = SaveKeyValues.getValue(
        LocalConstant.DEFAULT_SERVER_CONFIG, LocalConstant.SERVER_BASE_URL
    ) as String
    return "$defaultValue/static/${this.replace("\\", "/")}"
}