package com.hdl.photovoltaic.other;
|
|
|
import android.text.TextUtils;
|
|
import com.google.gson.Gson;
|
import com.hdl.photovoltaic.HDLApp;
|
import com.hdl.photovoltaic.bean.BaseEventBus;
|
import com.hdl.photovoltaic.config.ConstantManage;
|
import com.hdl.photovoltaic.config.UserConfigManage;
|
import com.hdl.photovoltaic.uni.HDLUniMP;
|
import com.hdl.photovoltaic.uni.HDLUniMPSDKManager;
|
import com.hdl.photovoltaic.utils.WifiUtils;
|
|
import org.greenrobot.eventbus.EventBus;
|
import org.json.JSONObject;
|
|
|
import io.dcloud.feature.unimp.DCUniMPJSCallback;
|
|
/**
|
* uni小程序的逻辑
|
*/
|
public class HdlUniLogic implements HDLUniMPSDKManager.IOnOtherUniMPEventCallBack {
|
private static volatile HdlUniLogic sHdlUniLogic;
|
|
/**
|
* 获取当前对象
|
*
|
* @return HdlAccountLogic
|
*/
|
public static synchronized HdlUniLogic getInstance() {
|
if (sHdlUniLogic == null) {
|
synchronized (HdlUniLogic.class) {
|
if (sHdlUniLogic == null) {
|
sHdlUniLogic = new HdlUniLogic();
|
}
|
}
|
|
}
|
return sHdlUniLogic;
|
}
|
|
/**
|
* 原生接收到uni发来的数据
|
*
|
* @param appId -uni小程序指定的appId
|
* @param event -uni小程序发来数据
|
* @param data JSONObject com.alibaba.fastjson解析处理
|
* @param callback callback.invoke(JSONObject) 用com.alibaba.fastjson
|
*/
|
@Override
|
public void onOtherUniMPEventReceive(String appId, String event, Object data, DCUniMPJSCallback callback) {
|
try {
|
if (!HDLUniMP.UNI_APP_ID.equals(appId)) {
|
return;
|
}
|
org.json.JSONObject jsonObject = getJSONObject(data);
|
String type_value = "";
|
if (jsonObject.has("type")) {
|
type_value = jsonObject.getString("type");
|
}
|
|
if (HDLUniMP.UNI_EVENT_REPLY_HOME_MODEL.equals(event)) {
|
//住宅模块
|
switch (type_value) {
|
case HDLUniMP.UNI_EVENT_REPLY_HOME_CREATION: {
|
//创建
|
//eventbus通知
|
BaseEventBus baseEventBus = new BaseEventBus();
|
baseEventBus.setType(ConstantManage.EVENTBUS_POST_HOME_CREATED);
|
EventBus.getDefault().post(baseEventBus);
|
|
}
|
break;
|
case HDLUniMP.UNI_EVENT_REPLY_HOME_DETAILS: {
|
//详情
|
}
|
break;
|
|
}
|
if (callback != null) {
|
uniCallbackData(null, "0", "", callback);
|
}
|
} else if (HDLUniMP.UNI_EVENT_REPLY_DEVICE_MODEL.equals(event)) {
|
//设备模块
|
switch (type_value) {
|
case HDLUniMP.UNI_EVENT_REPLY_DEVICE_ADD: {
|
//添加
|
}
|
break;
|
case HDLUniMP.UNI_EVENT_REPLY_DEVICE_LIST: {
|
//获取设备列表
|
}
|
break;
|
|
}
|
|
} else if (HDLUniMP.UNI_EVENT_REPLY_WIFI_MODEL.equals(event)) {
|
WifiUtils wifiUtils = new WifiUtils(HDLApp.getInstance());
|
//wifi模块
|
switch (type_value) {
|
case HDLUniMP.UNI_EVENT_REPLY_WIFI_LIST: {
|
//获取wifi列表
|
if (callback != null) {
|
uniCallbackData(wifiUtils.getScanResult(), "0", "", callback);
|
}
|
}
|
break;
|
case HDLUniMP.UNI_EVENT_REPLY_WIFI_INFO: {
|
//当前wifi详情
|
if (callback != null) {
|
uniCallbackData(wifiUtils.getCurrentConnectWifiInfo(), "0", "", callback);
|
}
|
}
|
break;
|
case HDLUniMP.UNI_EVENT_REPLY_WIFI_CONNECT: {
|
//wifi连接
|
}
|
break;
|
|
}
|
}
|
HdlLogLogic.print("uni===原生接收uni发来的数据", event + "\r\n" + data);
|
} catch (Exception e) {
|
HdlLogLogic.print("uni===原生接收uni发来的数据", e.getMessage());
|
}
|
|
}
|
|
/**
|
* 原生打开uni指定页面
|
*
|
* @param path 打开全路径
|
* @param jsonObject 附件数据(没有数据填null)
|
*/
|
public void openUniMP(String path, JSONObject jsonObject) {
|
|
JSONObject json = this.createdJsonDate(jsonObject, true);
|
HdlLogLogic.print("uni===组装uni发送数据格式", json.toString());
|
HDLUniMPSDKManager.getInstance().openUniMP(HDLUniMP.UNI_APP_ID, path, json, HdlUniLogic.this);
|
}
|
|
/**
|
* 原生【主动】向小程序发送通知事件
|
* 注意:需要提前小程序在运行才可成功
|
*
|
* @param topic 主题大类
|
* @param type 功能类
|
* @param body 附件数据(没有数据填null)
|
*/
|
public void sendUni(String topic, String type, String body) {
|
try {
|
HDLUniMP.UniCallBackBaseBean callBackBaseBean = new HDLUniMP.UniCallBackBaseBean();
|
callBackBaseBean.setType(type);
|
callBackBaseBean.setData(body);
|
HDLUniMPSDKManager.getInstance().sendUniMPEvent(HDLUniMP.UNI_APP_ID, topic, getJSONObject(callBackBaseBean));
|
} catch (Exception e) {
|
HdlLogLogic.print("uni===原生主动向小程序发送通知事件", e.getMessage());
|
}
|
}
|
|
/**
|
* 检测是否当前页面注册的callback,是的话则移除
|
*/
|
public void checkRemoveOtherUniMPEventCallBack() {
|
HDLUniMPSDKManager.getInstance().checkRemoveOtherUniMPEventCallBack(this);
|
}
|
|
/**
|
* 组装uni发送数据格式
|
*
|
* @param data -附件数据(没有数据填null)
|
* @param isTokenAndRefreshToken (true=底层默认添加token和refreshToken;false=不加)
|
* @return JSONObject
|
*/
|
private JSONObject createdJsonDate(JSONObject data, boolean isTokenAndRefreshToken) {
|
HDLUniMP.UniCallBackBaseBean uniCallBackBaseBean = new HDLUniMP.UniCallBackBaseBean();
|
try {
|
if (data == null) {
|
data = new JSONObject();
|
}
|
if (isTokenAndRefreshToken) {
|
data.put("token", UserConfigManage.getInstance().getToken());
|
data.put("refreshToken", UserConfigManage.getInstance().getRefreshToken());
|
}
|
uniCallBackBaseBean.setData(data);
|
return getJSONObject(uniCallBackBaseBean);
|
} catch (Exception e) {
|
HdlLogLogic.print("uni===组装uni发送数据格式", e.getMessage());
|
}
|
return new JSONObject();
|
}
|
|
|
/**
|
* 组装uni发送数据格式
|
* 回调数据给uni小程序,统一在这里处理;
|
*
|
* @param obj 附加数据
|
* @param code 状态码-成功(0)或者失败(-2)
|
* @param msg 结果描述的信息
|
* @param callback 回调
|
*/
|
private void uniCallbackData(Object obj, String code, String msg, DCUniMPJSCallback callback) {
|
HDLUniMP.UniCallBackBaseBean uniCallBackBaseBean = new HDLUniMP.UniCallBackBaseBean();
|
try {
|
uniCallBackBaseBean.setCode(code);
|
uniCallBackBaseBean.setMes(msg);
|
uniCallBackBaseBean.setData(obj);
|
if (callback != null) {
|
callback.invoke(getJSONObject(uniCallBackBaseBean));
|
}
|
} catch (Exception e) {
|
HdlLogLogic.print("uni===组装uni发送数据格式", e.getMessage());
|
}
|
|
|
}
|
|
/**
|
* 当前对象转 JSONObject
|
*
|
* @return JSONObject
|
*/
|
private JSONObject getJSONObject(Object obj) {
|
try {
|
if (obj == null) {
|
return new JSONObject();
|
}
|
if (TextUtils.isEmpty(obj.toString())) {
|
return new JSONObject();
|
}
|
String json = new Gson().toJson(obj);
|
return new JSONObject(json);
|
} catch (Exception e) {
|
return new JSONObject();
|
}
|
}
|
}
|