parent
03198a77bf
commit
9ee9332188
11 changed files with 488 additions and 51 deletions
@ -0,0 +1,61 @@ |
||||
package com.project.survey.logic.viewmodel |
||||
|
||||
import androidx.lifecycle.LiveData |
||||
import androidx.lifecycle.MutableLiveData |
||||
import blankj.utilcode.util.ActivityUtils |
||||
import com.bingce.data.base.user.UserConfig |
||||
import com.bingce.data.base.user.UserConfigConstants |
||||
import com.bingce.data.database.AppDataBase |
||||
import com.google.gson.Gson |
||||
import com.project.survey.constants.SPConstants |
||||
import com.project.survey.model.CoordinateSystemData |
||||
import com.project.survey.model.LoginBean |
||||
import com.project.survey.network.RetrofitClient |
||||
import com.project.survey.ui.login.LoginActivity |
||||
import com.project.survey.ui.project.ProjectListActivity |
||||
import com.project.survey.util.SPUtils |
||||
|
||||
class CoordinateSystemViewModel : BaseViewModel() { |
||||
|
||||
val api = RetrofitClient.createApiService() |
||||
|
||||
val uploadCoordinateSystemResponse: LiveData<String> |
||||
get() = _uploadCoordinateSystemResponse |
||||
private val _uploadCoordinateSystemResponse = MutableLiveData<String>() |
||||
|
||||
fun uploadCoordinateSystem(coordinateParameter: String) { |
||||
launch { |
||||
val res = api.uploadCoordinateSystem(coordinateParameter = coordinateParameter) |
||||
if (res.success) { |
||||
_uploadCoordinateSystemResponse.postValue(res.message) |
||||
} else { |
||||
errorResponse.postValue(res.message) |
||||
} |
||||
} |
||||
} |
||||
|
||||
val downloadCoordinateSystemResponse: LiveData<CoordinateSystemData> |
||||
get() = _downloadCoordinateSystemResponse |
||||
private val _downloadCoordinateSystemResponse = MutableLiveData<CoordinateSystemData>() |
||||
|
||||
fun downloadCoordinateSystem() { |
||||
launch { |
||||
val res = api.downloadCoordinateSystem() |
||||
if (res.success) { |
||||
if (res.data == null || res.data is String) { |
||||
errorResponse.postValue(res.message) |
||||
return@launch |
||||
} |
||||
try { |
||||
val gson = Gson() |
||||
val data = gson.fromJson(res.data.toString(), CoordinateSystemData::class.java) |
||||
_downloadCoordinateSystemResponse.postValue(data) |
||||
} catch (e: Exception) { |
||||
errorResponse.postValue(res.message) |
||||
} |
||||
} else { |
||||
errorResponse.postValue(res.message) |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,8 @@ |
||||
package com.project.survey.model |
||||
|
||||
import androidx.annotation.Keep |
||||
|
||||
@Keep |
||||
data class CoordinateSystemData( |
||||
val ZBZHCS_ZHCS: String |
||||
) |
@ -0,0 +1,192 @@ |
||||
package com.project.survey.util; |
||||
import javax.crypto.Cipher; |
||||
import javax.crypto.SecretKey; |
||||
import javax.crypto.SecretKeyFactory; |
||||
import javax.crypto.spec.DESKeySpec; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.security.SecureRandom; |
||||
import java.util.*; |
||||
|
||||
import blankj.utilcode.util.LogUtils; |
||||
|
||||
public class DESUtil { |
||||
|
||||
// 模拟获取系统配置的方法,可根据实际情况修改
|
||||
private static String getSystemConfig(String key, String defaultValue) { |
||||
// 这里简单返回默认值,实际中可从配置文件等读取
|
||||
return defaultValue; |
||||
} |
||||
|
||||
// 模拟将字符串转换为布尔值的方法
|
||||
private static boolean toBoolean(String value) { |
||||
return Boolean.parseBoolean(value); |
||||
} |
||||
|
||||
// 模拟分割字符串的方法
|
||||
private static String[] split(String str, String delimiter) { |
||||
return str.split(delimiter); |
||||
} |
||||
|
||||
// 模拟判断对象是否为空的方法
|
||||
private static boolean isNotEmpty(Object obj) { |
||||
return obj != null && (!obj.toString().isEmpty()); |
||||
} |
||||
|
||||
// 模拟判断是否为类似 FormData 的对象,这里用 Map 模拟
|
||||
private static boolean isFormData(Object obj) { |
||||
return obj instanceof Map; |
||||
} |
||||
|
||||
// 模拟判断是否为数字的方法
|
||||
private static boolean isNumber(Object obj) { |
||||
return obj instanceof Number; |
||||
} |
||||
|
||||
// 模拟生成随机数的方法
|
||||
private static int random(int bound) { |
||||
Random random = new Random(); |
||||
return random.nextInt(bound); |
||||
} |
||||
|
||||
/** |
||||
* DES 加密方法 |
||||
* @param message 待加密的信息 |
||||
* @param key 加密密钥 |
||||
* @return 加密后的十六进制字符串 |
||||
* @throws Exception 加密过程中可能出现的异常 |
||||
*/ |
||||
public static String encryptByDES(String message, String key) throws Exception { |
||||
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8)); |
||||
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); |
||||
SecretKey secretKey = keyFactory.generateSecret(desKeySpec); |
||||
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); |
||||
cipher.init(Cipher.ENCRYPT_MODE, secretKey, new SecureRandom()); |
||||
byte[] encryptedBytes = cipher.doFinal(message.getBytes(StandardCharsets.UTF_8)); |
||||
StringBuilder hexString = new StringBuilder(); |
||||
for (byte b : encryptedBytes) { |
||||
String hex = Integer.toHexString(0xFF & b); |
||||
if (hex.length() == 1) { |
||||
hexString.append('0'); |
||||
} |
||||
hexString.append(hex); |
||||
} |
||||
return hexString.toString(); |
||||
} |
||||
|
||||
public static String encryptByDES(String message) throws Exception { |
||||
return encryptByDES(message, "bQ85PtpQtEwJ7FkSmrGMFQ9S"); |
||||
} |
||||
|
||||
/** |
||||
* DES 解密方法 |
||||
* @param ciphertext 加密后的十六进制字符串 |
||||
* @param key 解密密钥 |
||||
* @return 解密后的原始字符串 |
||||
* @throws Exception 解密过程中可能出现的异常 |
||||
*/ |
||||
public static String decryptByDES(String ciphertext, String key) throws Exception { |
||||
byte[] encryptedBytes = hexStringToByteArray(ciphertext); |
||||
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8)); |
||||
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); |
||||
SecretKey secretKey = keyFactory.generateSecret(desKeySpec); |
||||
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); |
||||
cipher.init(Cipher.DECRYPT_MODE, secretKey, new SecureRandom()); |
||||
byte[] decryptedBytes = cipher.doFinal(encryptedBytes); |
||||
return new String(decryptedBytes, StandardCharsets.UTF_8); |
||||
} |
||||
|
||||
// 将十六进制字符串转换为字节数组的辅助方法
|
||||
private static byte[] hexStringToByteArray(String s) { |
||||
int len = s.length(); |
||||
byte[] data = new byte[len / 2]; |
||||
for (int i = 0; i < len; i += 2) { |
||||
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) |
||||
+ Character.digit(s.charAt(i + 1), 16)); |
||||
} |
||||
return data; |
||||
} |
||||
|
||||
/** |
||||
* 系统请求参数加密方法 |
||||
* @param params 待加密的参数,用 Map 表示 |
||||
* @return 包含随机参数键的 Map |
||||
*/ |
||||
public static Map<String, Object> toParamsEncrypt(Map<String, Object> params) { |
||||
// 启用请求参数加密
|
||||
boolean encrypt = toBoolean(getSystemConfig("JE_SYS_ENCRYPT", "true")); |
||||
// 加密参数编码
|
||||
String[] fields = split(getSystemConfig("JE_SYS_ENCRYPT_FIELD", "funcCode,tableCode,j_query,password,account,code,device,objName,objCode,typeName,FUNCINFO_FUNCCODE,funcId,tableName,ddListCodes,excludes,strData,type,beanId,metaInfo,JE_WORKFLOW_PROCESSINFO_ID,metaInfoXml,FUNCINFO_WHERESQL,DICTIONARY_DDCODE,ableCode,queryColumns,menuId,SY_PRODUCT_ID,pkValue,JE_CORE_RESOURCETABLE_ID,RESOURCETABLE_TABLECODE,userId,deptId,templateCode,typeCode"), ","); |
||||
// 随机参数编码
|
||||
List<String> fieldKeys = new ArrayList<>(); |
||||
// 加密秘钥
|
||||
String key = getSystemConfig("JE_SYS_ENCRYPT_KEY", "bQ85PtpQtEwJ7FkSmrGMFQ9S"); |
||||
// 启用加密 && 有加密参数
|
||||
if (encrypt && fields.length > 0 && isNotEmpty(params)) { |
||||
String[] reg = "abcdefghigklmnopqrstuvwxyz".split(""); |
||||
Map<String, Object> encryptedParams = new HashMap<>(params); |
||||
for (String field : fields) { |
||||
// 随机参数
|
||||
String fieldKey; |
||||
do { |
||||
fieldKey = reg[random(23)] + random(100); |
||||
} while (fieldKeys.contains(fieldKey)); |
||||
fieldKeys.add(fieldKey); |
||||
// 加密内容
|
||||
Object value = params.get(field); |
||||
if (isNotEmpty(value)) { |
||||
if (isNumber(value)) { |
||||
value = value.toString(); |
||||
} |
||||
try { |
||||
value = encryptByDES(value.toString(), key); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
encryptedParams.remove(field); // 删除原始参数
|
||||
encryptedParams.put(fieldKey, value); // 增加随机参数,加密信息
|
||||
} |
||||
} |
||||
encryptedParams.put("params-keys", String.join(",", fieldKeys)); |
||||
return encryptedParams; |
||||
} |
||||
return params; |
||||
} |
||||
|
||||
|
||||
public static void main(String[] args) { |
||||
try { |
||||
//值加密
|
||||
String message = "zlh3"; |
||||
String message2 = "1qaz2wsx!123"; |
||||
String message3 = "123"; |
||||
String key = "bQ85PtpQtEwJ7FkSmrGMFQ9S"; // DES 密钥长度必须为 8 字节
|
||||
String encrypted = encryptByDES(message, key); |
||||
String encrypted2 = encryptByDES(message2, key); |
||||
String encrypted3 = encryptByDES(message3, key); |
||||
LogUtils.d("加密后的信息: " + encrypted); |
||||
LogUtils.d("加密后的信息2: " + encrypted2); |
||||
LogUtils.d("加密后的信息3: " + encrypted3); |
||||
// String decrypted = decryptByDES(encrypted, key);
|
||||
// System.out.println("解密后的信息: " + decrypted);
|
||||
// String decrypted2 = decryptByDES(encrypted2, key);
|
||||
// System.out.println("解密后的信息2: " + decrypted2);
|
||||
|
||||
//字段加密
|
||||
Map<String, Object> params = new HashMap<>(); |
||||
// Map<String, Object> params2 = new HashMap<>();
|
||||
// Map<String, Object> params3 = new HashMap<>();
|
||||
params.put("account", "zlh3"); |
||||
params.put("password", "1qaz2wsx!123"); |
||||
params.put("code", "123"); |
||||
Map<String, Object> result = toParamsEncrypt(params); |
||||
LogUtils.d("加密后的参数: " + result); |
||||
// Map<String, Object> result2 = toParamsEncrypt(params2);
|
||||
// System.out.println("加密后的参数: " + result2);
|
||||
// Map<String, Object> result3 = toParamsEncrypt(params3);
|
||||
// System.out.println("加密后的参数: " + result3);
|
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
|
||||
} |
||||
} |
||||
} |
@ -0,0 +1,22 @@ |
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto"> |
||||
|
||||
<!-- <item--> |
||||
<!-- android:id="@+id/menu_fragment_coordinate_system_library"--> |
||||
<!-- android:title="@string/pick_from_library"--> |
||||
<!-- app:showAsAction="ifRoom" />--> |
||||
|
||||
<!-- <item--> |
||||
<!-- android:id="@+id/menu_fragment_coordinate_system_save"--> |
||||
<!-- android:title="@string/save_as_template"--> |
||||
<!-- app:showAsAction="ifRoom" />--> |
||||
|
||||
<item |
||||
android:id="@+id/menu_fragment_coordinate_system_share" |
||||
android:title="@string/share_coordinate_system" |
||||
app:showAsAction="never" /> |
||||
<item |
||||
android:id="@+id/menu_fragment_coordinate_system_import" |
||||
android:title="@string/import_coordinate_system" |
||||
app:showAsAction="never" /> |
||||
</menu> |
@ -0,0 +1,70 @@ |
||||
{ |
||||
"formatVersion": 1, |
||||
"database": { |
||||
"version": 1, |
||||
"identityHash": "ee5658c06dcecbae4ac140ea81c3be9d", |
||||
"entities": [ |
||||
{ |
||||
"tableName": "LocalResultExportFormat", |
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, `format_name` TEXT, `format_content` TEXT, `divided_symbols` TEXT, `file_suffix` INTEGER NOT NULL, `file_header` INTEGER NOT NULL, `angle_format` INTEGER NOT NULL, PRIMARY KEY(`id`))", |
||||
"fields": [ |
||||
{ |
||||
"fieldPath": "id", |
||||
"columnName": "id", |
||||
"affinity": "TEXT", |
||||
"notNull": true |
||||
}, |
||||
{ |
||||
"fieldPath": "format_name", |
||||
"columnName": "format_name", |
||||
"affinity": "TEXT", |
||||
"notNull": false |
||||
}, |
||||
{ |
||||
"fieldPath": "format_content", |
||||
"columnName": "format_content", |
||||
"affinity": "TEXT", |
||||
"notNull": false |
||||
}, |
||||
{ |
||||
"fieldPath": "divided_symbols", |
||||
"columnName": "divided_symbols", |
||||
"affinity": "TEXT", |
||||
"notNull": false |
||||
}, |
||||
{ |
||||
"fieldPath": "file_suffix", |
||||
"columnName": "file_suffix", |
||||
"affinity": "INTEGER", |
||||
"notNull": true |
||||
}, |
||||
{ |
||||
"fieldPath": "file_header", |
||||
"columnName": "file_header", |
||||
"affinity": "INTEGER", |
||||
"notNull": true |
||||
}, |
||||
{ |
||||
"fieldPath": "angle_format", |
||||
"columnName": "angle_format", |
||||
"affinity": "INTEGER", |
||||
"notNull": true |
||||
} |
||||
], |
||||
"primaryKey": { |
||||
"autoGenerate": false, |
||||
"columnNames": [ |
||||
"id" |
||||
] |
||||
}, |
||||
"indices": [], |
||||
"foreignKeys": [] |
||||
} |
||||
], |
||||
"views": [], |
||||
"setupQueries": [ |
||||
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", |
||||
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'ee5658c06dcecbae4ac140ea81c3be9d')" |
||||
] |
||||
} |
||||
} |
Loading…
Reference in new issue