package com.doumee.keyCabinet.ui.main; import android.app.Application; import android.text.TextUtils; import androidx.annotation.NonNull; import androidx.databinding.ObservableField; import androidx.hilt.lifecycle.ViewModelInject; import androidx.lifecycle.MutableLiveData; import com.doumee.keyCabinet.MApplication; import com.doumee.keyCabinet.R; import com.doumee.keyCabinet.bean.ConfigBean; import com.doumee.keyCabinet.bean.DevConfigBean; import com.doumee.keyCabinet.bean.FaceUserBean; import com.doumee.keyCabinet.bean.RecyclerBraceletBean; import com.doumee.keyCabinet.event.DevConfigEvent; import com.doumee.keyCabinet.event.ReLoginEvent; import com.doumee.keyCabinet.http.Apis; import com.doumee.keyCabinet.http.param.BaseResponse; import com.doumee.keyCabinet.http.param.DevErrInfoParam; import com.doumee.keyCabinet.http.param.DevLoginParam; import com.doumee.keyCabinet.http.param.FaceUserParam; import com.doumee.keyCabinet.http.param.RecyclerSHParam; import com.doumee.keyCabinet.http.param.RequestBaseObject; import com.doumee.keyCabinet.ui.guide.GuideActivity; import com.doumee.keyCabinet.utils.BraceletLogUtils; import com.doumee.keyCabinet.utils.LMobileInfo; import com.doumee.keyCabinet.utils.LanguageUtil; import com.doumee.keyCabinet.utils.ParamsUtil; import com.doumee.lib_coremodel.base.BaseModel; import com.doumee.lib_coremodel.base.DataViewModel; import com.doumee.lib_coremodel.http.rxJava.SimpleObserver; import com.doumee.lib_coremodel.http.utils.RxUtils; import com.doumee.lib_coremodel.util.SpUtil; import org.greenrobot.eventbus.EventBus; import java.util.List; import okhttp3.Headers; import retrofit2.Response; public class MainVM extends DataViewModel { public ObservableField dayObs = new ObservableField<>(""); public ObservableField timeObs = new ObservableField<>(""); private MutableLiveData> faceLD = new MutableLiveData<>(); @ViewModelInject public MainVM(@NonNull Application application, BaseModel model) { super(application, model); } @Override protected void clickByTypeCommand(int type) { doAction(type); } private boolean isReq = false; public void resetReq(){ isReq = false; } public void devLogin(){ if (true) { return; } //清除登录信息 if(isReq){ return; } isReq = true; MApplication.setCookie(""); MApplication.saveConfigBean(new ConfigBean()); RequestBaseObject request = new RequestBaseObject<>(); DevLoginParam param = new DevLoginParam(); param.setDevCode(LMobileInfo.getDeviceUniqueId()); request.setParam(param); getRetrofitService(Apis.class).devLogin(0, ParamsUtil.encodeRequestBody(request)) .compose(RxUtils.schedulersTransformer()) .subscribe(new SimpleObserver>>(rxJavaGcManager) { @Override public void onNext(@NonNull Response> base) { Headers headers = base.headers();//我们通过Response的headers对象获取到响应头信息。 List values = headers.values("Set-Cookie");//通过“Set-Cookie”字段获取到服务器返回的Cookie信息 if(values!=null&&values.size()>0){ MApplication.setCookie(values.get(0)); } BaseResponse response = base.body(); if(response==null){ toast(MApplication.mContext.getString(R.string.guide_toast_1)); } if("000000".equals(response.getErrorCode())){ if(response.getRecord()!=null){ if("0".equals(response.getRecord().getStatus())){ startActivity(GuideActivity.class); finish(); return; } DevConfigBean devConfigBean = response.getRecord(); ConfigBean configBean = new ConfigBean(); configBean.setShopId(devConfigBean.getShopId()); configBean.setCapacity(devConfigBean.getCapacity()); configBean.setMoney(devConfigBean.getMoney()); configBean.setCategory(devConfigBean.getCategory()); configBean.setCurNum(devConfigBean.getCurNum()); configBean.setName(devConfigBean.getName()); configBean.setGymId(devConfigBean.getVenueId()); configBean.setLanguage(devConfigBean.getLanguage()); String language = devConfigBean.getLanguage(); if(!TextUtils.isEmpty(language)){ //转小写 language = language.toLowerCase(); if("zh".equals(language)){ MApplication.nLan = 0; }else { MApplication.nLan = 1; } } SpUtil.saveString("language",language); LanguageUtil.changeAppLanguage(MApplication.mContext,language); MApplication.saveConfigBean(configBean); }else { toast(MApplication.mContext.getString(R.string.guide_toast_1)); } }else if("33010101".equals(response.getErrorCode())) { toast(MApplication.mContext.getString(R.string.guide_toast_2)+ MApplication.mContext.getString(R.string.symbol_colon)+param.getDevCode()); }else { toast(response.getErrorMsg()); } } @Override public void onError(@NonNull Throwable e) { toast(e.getMessage()); } @Override public void onComplete() { isReq = false; } }); } public void devHeart(){ if (true) { return; } RequestBaseObject request = new RequestBaseObject<>(); DevLoginParam param = new DevLoginParam(); param.setDevCode(LMobileInfo.getDeviceUniqueId()); request.setParam(param); getRetrofitService(Apis.class).heart(MApplication.getConfigBean().getLanguage(),MApplication.getCookie(),MApplication.getConfigBean().getShopId(), MApplication.getConfigBean().getGymId(),ParamsUtil.encodeRequestBody(request)) .compose(RxUtils.schedulersTransformer()) .subscribe(new SimpleObserver>(rxJavaGcManager) { @Override public void onNext(@NonNull BaseResponse response) { if(response!=null&&"000000".equals(response.getErrorCode())){ if(response.getRecord()!=null){ EventBus.getDefault().post(new DevConfigEvent(response.getRecord())); } }else if("00016".equals(response.getErrorCode())||"00068".equals(response.getErrorCode())) { EventBus.getDefault().post(new ReLoginEvent()); }else { } } @Override public void onError(@NonNull Throwable e) { System.out.println(e.getMessage()); } @Override public void onComplete() { } }); } public MutableLiveData> getFaceLD() { return faceLD; } public void getFaceDatas() { if (true) { return; } RequestBaseObject request = new RequestBaseObject<>(); FaceUserParam param = new FaceUserParam(); param.setLastTime(SpUtil.getString("LastTime")); param.setType("0"); request.setParam(param); //showLoading(true); getRetrofitService(Apis.class).allFaceList(MApplication.getConfigBean().getLanguage(),MApplication.getCookie(),MApplication.getConfigBean().getShopId(), MApplication.getConfigBean().getGymId(), ParamsUtil.encodeRequestBody(request)) .compose(RxUtils.schedulersTransformer()) .subscribe(new SimpleObserver>(rxJavaGcManager) { @Override public void onNext(@NonNull BaseResponse response) { if("000000".equals(response.getErrorCode())){ //查询数据 if(response.getRecordList()!=null&&response.getRecordList().size()>0){ faceLD.setValue(response.getRecordList()); } }else if("00016".equals(response.getErrorCode())||"00068".equals(response.getErrorCode())) { EventBus.getDefault().post(new ReLoginEvent()); }else { } } @Override public void onError(@NonNull Throwable e) { //toast(e.getMessage()); } @Override public void onComplete() { //hideLoading(); } }); } public void upErrInfo(SpUtil.SPBean bean) { if (true) { return; } RequestBaseObject request = new RequestBaseObject<>(); DevErrInfoParam param = new DevErrInfoParam(); param.setDevCode(LMobileInfo.getDeviceUniqueId()); param.setErrInfo(bean.getValue()); request.setParam(param); getRetrofitService(Apis.class).upErrInfo(MApplication.getConfigBean().getLanguage(),MApplication.getCookie(),MApplication.getConfigBean().getShopId(), MApplication.getConfigBean().getGymId(), ParamsUtil.encodeRequestBody(request)) .compose(RxUtils.schedulersTransformer()) .subscribe(new SimpleObserver(rxJavaGcManager) { @Override public void onNext(@NonNull BaseResponse response) { if("000000".equals(response.getErrorCode())){ SpUtil.remoreString(bean.getKey()); }else if("00016".equals(response.getErrorCode())||"00068".equals(response.getErrorCode())) { EventBus.getDefault().post(new ReLoginEvent()); }else { } } @Override public void onError(@NonNull Throwable e) { } @Override public void onComplete() { } }); } /** * 上传手环日志 */ public void upBraceletLog() { if (true) { return; } if(BraceletLogUtils.COUNT>100||(System.currentTimeMillis() - BraceletLogUtils.lastTime > 600000)){ BraceletLogUtils.lastTime = System.currentTimeMillis(); String text = BraceletLogUtils.getAllLog(); if(TextUtils.isEmpty(text)){ return; } RequestBaseObject request = new RequestBaseObject<>(); DevErrInfoParam param = new DevErrInfoParam(); param.setDevCode(LMobileInfo.getDeviceUniqueId()); param.setErrInfo(text); request.setParam(param); getRetrofitService(Apis.class).upErrInfo(MApplication.getConfigBean().getLanguage(),MApplication.getCookie(),MApplication.getConfigBean().getShopId(), MApplication.getConfigBean().getGymId(), ParamsUtil.encodeRequestBody(request)) .compose(RxUtils.schedulersTransformer()) .subscribe(new SimpleObserver(rxJavaGcManager) { @Override public void onNext(@NonNull BaseResponse response) { if("000000".equals(response.getErrorCode())){ } } @Override public void onError(@NonNull Throwable e) { } @Override public void onComplete() { } }); } } }