| | |
| | | package com.hdl.photovoltaic.other; |
| | | |
| | | |
| | | import com.google.gson.Gson; |
| | | import com.hdl.photovoltaic.config.UserConfigManage; |
| | | import com.hdl.photovoltaic.uni.HDLUniMP; |
| | | import com.hdl.photovoltaic.uni.HDLUniMPSDKManager; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 原生接收uni发来的数据 |
| | | * 原生接收到uni发来的数据 |
| | | * |
| | | * @param appId -uni小程序指定的appId |
| | | * @param event -uni小程序发来数据 |
| | |
| | | jsonObject = new org.json.JSONObject(data.toString()); |
| | | } |
| | | //住宅模块 |
| | | if (HDLUniMP.UNI_EVENT_HOME_CREATION.equals(event)) { |
| | | if (HDLUniMP.UNI_EVENT_REPLY_HOME_LIST.equals(event)) { |
| | | if (callback != null) { |
| | | callback.invoke(null); |
| | | uniCallbackData(null, "0", "", callback); |
| | | } |
| | | } |
| | | HdlLogLogic.print("uni===原生接收uni发来的数据", event + "\r\n" + data); |
| | |
| | | * @param jsonObject 附件数据(没有数据填null) |
| | | */ |
| | | public void openUniMP(String path, JSONObject jsonObject) { |
| | | JSONObject json = this.createdJsonDate(jsonObject); |
| | | |
| | | JSONObject json = this.createdJsonDate(jsonObject, true); |
| | | HdlLogLogic.print("uni===组装uni发送数据格式", json.toString()); |
| | | HDLUniMPSDKManager.getInstance().openUniMP(HDLUniMP.UNI_APP_ID, path, json, HdlUniLogic.this); |
| | | } |
| | | |
| | |
| | | HDLUniMP.UniCallBackBaseBean callBackBaseBean = new HDLUniMP.UniCallBackBaseBean(); |
| | | callBackBaseBean.setTopic(topic); |
| | | callBackBaseBean.setData(body); |
| | | HDLUniMPSDKManager.getInstance().sendUniMPEvent(HDLUniMP.UNI_APP_ID, HDLUniMP.UNI_APP_ID, callBackBaseBean.getJSONObject()); |
| | | HDLUniMPSDKManager.getInstance().sendUniMPEvent(HDLUniMP.UNI_APP_ID, HDLUniMP.UNI_APP_ID, getJSONObject(callBackBaseBean)); |
| | | } catch (Exception e) { |
| | | HdlLogLogic.print("uni===原生主动向小程序发送通知事件", e.getMessage()); |
| | | } |
| | |
| | | /** |
| | | * 组装uni发送数据格式 |
| | | * |
| | | * @param data -附件数据(没有数据填null) |
| | | * @param data -附件数据(没有数据填null) |
| | | * @param isTokenAndRefreshToken (true=底层默认添加token和refreshToken;false=不加) |
| | | * @return JSONObject |
| | | */ |
| | | public JSONObject createdJsonDate(JSONObject data) { |
| | | private JSONObject createdJsonDate(JSONObject data, boolean isTokenAndRefreshToken) { |
| | | HDLUniMP.UniCallBackBaseBean uniCallBackBaseBean = new HDLUniMP.UniCallBackBaseBean(); |
| | | try { |
| | | if (data == null) { |
| | | data = new JSONObject(); |
| | | } |
| | | data.put("token", UserConfigManage.getInstance().getToken()); |
| | | data.put("refreshToken", UserConfigManage.getInstance().getRefreshToken()); |
| | | if (isTokenAndRefreshToken) { |
| | | data.put("token", UserConfigManage.getInstance().getToken()); |
| | | data.put("refreshToken", UserConfigManage.getInstance().getRefreshToken()); |
| | | } |
| | | uniCallBackBaseBean.setData(data); |
| | | return uniCallBackBaseBean.getJSONObject(); |
| | | 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(uniCallBackBaseBean); |
| | | } |
| | | } catch (Exception e) { |
| | | HdlLogLogic.print("uni===组装uni发送数据格式", e.getMessage()); |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取uni发送对象 |
| | | * |
| | | * @param obj 附加数据 |
| | | * @param code 状态码 |
| | | * @param msg 结果描述的信息 |
| | | */ |
| | | private HDLUniMP.UniCallBackBaseBean getUniCallBackBaseBean(Object obj, String code, String msg) { |
| | | HDLUniMP.UniCallBackBaseBean uniCallBackBaseBean = new HDLUniMP.UniCallBackBaseBean(); |
| | | uniCallBackBaseBean.setCode(code); |
| | | uniCallBackBaseBean.setMes(msg); |
| | | uniCallBackBaseBean.setData(obj); |
| | | return uniCallBackBaseBean; |
| | | } |
| | | |
| | | /** |
| | | * 当前对象转 JSONObject |
| | | * |
| | | * @return JSONObject |
| | | */ |
| | | private JSONObject getJSONObject(Object obj) { |
| | | try { |
| | | String json = new Gson().toJson(obj); |
| | | return new JSONObject(json); |
| | | } catch (Exception e) { |
| | | return new JSONObject(); |
| | | } |
| | | } |
| | | } |
| | | |