Skip to content

Commit

Permalink
接口加密
Browse files Browse the repository at this point in the history
  • Loading branch information
haife committed Jul 17, 2020
1 parent 78a713e commit 59d9fd4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
4 changes: 1 addition & 3 deletions mcas/src/main/java/com/haife/mcas/di/module/AppModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,10 @@ static AppManager provideAppManager(Application application) {
@Singleton
@Provides
static QMUITipDialog provideQmuiLoadingDialog(Application application) {
QMUITipDialog mTipDialog = new QMUITipDialog.Builder(application)
return new QMUITipDialog.Builder(application)
.setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
.setTipWord("加载中")
.create();
mTipDialog.show();
return mTipDialog;
}

@Singleton
Expand Down
33 changes: 13 additions & 20 deletions mcas/src/main/java/com/haife/mcas/mvp/BaseModel.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.haife.mcas.mvp;


import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
Expand All @@ -16,7 +12,7 @@

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

Expand All @@ -39,21 +35,22 @@ public BaseModel(IRepositoryManager repositoryManager) {

/**
* 将Object对象里面的属性和值转化成Map对象
*
* @param obj
* @param key
* @return
*/
public static Object encryptObjectToMap(@Nullable Object obj, String key) {
public static HashMap<String,Object> encryptObjectToMap(@Nullable Object obj, String key) {
if (obj != null) {
try {
TreeMap<String, Object> map = new TreeMap<>();
HashMap<String, Object> map = new HashMap<>();
Field[] declaredFields = obj.getClass().getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
map.put(field.getName(), field.get(obj));
}
map.put("sign", getMacData(map, key));
return mapToObject(map, (Class<?>) obj);
map.put("64", getMacData(map, key));
return map;
} catch (Exception ignored) {

}
Expand Down Expand Up @@ -92,7 +89,7 @@ public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) th
* @param map 请求的map
* @return json 字符串
*/
private static String getMacData(TreeMap<String, Object> map, String key) throws Exception {
public static String getMacData(HashMap<String, Object> map, String key) throws Exception {
Map<String, Object> resultMap = sortMapByKey(map);
String sign = "";
for (Map.Entry<String, Object> entry : resultMap.entrySet()) {
Expand All @@ -114,26 +111,22 @@ private static String getMacData(TreeMap<String, Object> map, String key) throws
* @param map
* @return
*/
private static Map<String, Object> sortMapByKey(Map<String, Object> map) {
public static Map<String, Object> sortMapByKey(Map<String, Object> map) {
if (map == null || map.isEmpty()) {
return null;
}

Map<String, Object> sortMap = new TreeMap<String, Object>(new Comparator<String>() {
@Override
public int compare(String lhs, String rhs) {
// 这里改为小写进行比较
Integer l = Integer.parseInt(lhs);
Integer r = Integer.parseInt(rhs);
return l.compareTo(r);
}
Map<String, Object> sortMap = new TreeMap<String, Object>((lhs, rhs) -> {
// 这里改为小写进行比较
Integer l = Integer.parseInt(lhs);
Integer r = Integer.parseInt(rhs);
return l.compareTo(r);
});
sortMap.putAll(map);
return sortMap;
}



/**
* 在框架中 {@link BasePresenter#onDestroy()} 时会默认调用 {@link IModel#onDestroy()}
*/
Expand Down

0 comments on commit 59d9fd4

Please sign in to comment.