充电桩app代码

ConfirmOrderActivity.java 19KB

    package com.electric.chargingpile.activity; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.os.Handler; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import com.blankj.utilcode.util.LogUtils; import com.electric.chargingpile.R; import com.electric.chargingpile.application.MainApplication; import com.electric.chargingpile.constant.BaseConstants; import com.electric.chargingpile.constant.UrlConstants; import com.electric.chargingpile.data.ChargingBean; import com.electric.chargingpile.data.RedEnvelopeBean; import com.electric.chargingpile.manager.ProfileManager; import com.electric.chargingpile.util.JsonUtils; import com.electric.chargingpile.util.LoadingDialog; import com.electric.chargingpile.util.ToastUtil; import com.electric.chargingpile.util.Util; import com.electric.chargingpile.view.PreferentialDialog; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.google.zxing.client.android.constant.ChargingConstants; import com.tencent.bugly.crashreport.CrashReport; import com.zhy.http.okhttp.OkHttpUtils; import com.zhy.http.okhttp.callback.StringCallback; import java.util.List; import okhttp3.Call; /** * 确认订单 * */ public class ConfirmOrderActivity extends AppCompatActivity implements View.OnClickListener { private RelativeLayout layoutRed,rl_end_info; private TextView tv_name,tv_charging_shifu,tv_charging_cost,tv_service_cost,tv_charging_hongbao, tv_zhuang_no,tv_charging_time,tv_charging_liang,tv_tishi,tv_gocomment; private ImageView default_hb,iv_back; private String mRedEnvelopeId=null; private String activeUrl; private ChargingBean mChargingBean; private LoadingDialog mDialog; private int try_count=0; private int delayedSettlementCount=0; @Override protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_submit_orders); mDialog = new LoadingDialog(this); mDialog.setCanceledOnTouchOutside(false); initIntent(); initView(); initData(); } private void initIntent() { mChargingBean = (ChargingBean) getIntent().getSerializableExtra(BaseConstants.CHARGING_DATA); activeUrl = getIntent().getStringExtra(BaseConstants.ACTIVITY_URL); String orderId = getIntent().getStringExtra(ChargingConstants.ORDERID); if (!TextUtils.isEmpty(orderId)){ requestDelayedSettlement(orderId); } } private void requestDelayedSettlement(String orderId) { delayedSettlementCount++; if (this != null && !this.isFinishing()) { mDialog.show(); } // Log.e("TAG", "requestDelayedSettlement: userid: "+MainApplication.userId ); // Log.e("TAG", "requestDelayedSettlement: pwd: "+ MainApplication.userPassword); // Log.e("TAG", "requestDelayedSettlement: orderid: "+ orderId); OkHttpUtils.get() .url(UrlConstants.QUERY_NOT_CLOSE) .addParams(com.electric.chargingpile.constant.ChargingConstants.USERID, MainApplication.userId) .addParams(com.electric.chargingpile.constant.ChargingConstants.PWD, MainApplication.userPassword) .addParams(com.electric.chargingpile.constant.ChargingConstants.ORDERID, orderId) .build() .connTimeOut(15000) .readTimeOut(15000) .execute(new StringCallback() { @Override public void onError(Call call, Exception e) { LogUtils.e(); ToastUtil.showToast(getApplicationContext(), "网络异常,请检查您的网络连接", Toast.LENGTH_SHORT); CrashReport.postCatchedException(e); Log.e(this.getClass().getName(),"requestDelayedSettlement--onError--"+e.getMessage()); } @Override public void onResponse(String response) { // LogUtils.e("hyc requestDelayedSettlement--" + response); String rtnCode = JsonUtils.getKeyResult(response, "rtnCode"); String rtnMsg = JsonUtils.getKeyResult(response, "rtnMsg"); if (null != rtnCode) { if ("01".equals(rtnCode)) { String data = JsonUtils.getKeyResult(response, "data"); ChargingBean chargingBean = JsonUtils.parseToObjectBean(data, ChargingBean.class); int status = chargingBean.getStatus(); if (status >= 2) { mChargingBean=chargingBean; dismissDialog(); upDateView(); } else { if (delayedSettlementCount < 6) { new Handler().postDelayed(new Runnable() { @Override public void run() { requestDelayedSettlement(orderId); } }, 2000); } else { dismissDialog(); startActivity(new Intent(getApplicationContext(), DelayInfoActivity.class)); finish(); } } } if ("02".equals(rtnCode)) { dismissDialog(); startActivity(new Intent(getApplicationContext(), DelayInfoActivity.class)); finish(); } else { dismissDialog(); ToastUtil.showToast(getApplicationContext(),rtnMsg,Toast.LENGTH_SHORT); } } } }); } private void initView() { iv_back = findViewById(R.id.iv_back); tv_name=findViewById(R.id.tv_name); tv_charging_shifu=findViewById(R.id.tv_charging_shifu); tv_charging_cost=findViewById(R.id.tv_charging_cost); tv_service_cost=findViewById(R.id.tv_service_cost); layoutRed = findViewById(R.id.layoutRed); // default_hb = findViewById(R.id.default_hb); tv_charging_hongbao = findViewById(R.id.tv_charging_hongbao); tv_zhuang_no=findViewById(R.id.tv_zhuang_no); tv_charging_time=findViewById(R.id.tv_charging_time); tv_charging_liang=findViewById(R.id.tv_charging_liang); tv_tishi=findViewById(R.id.tv_tishi); rl_end_info=findViewById(R.id.rl_end_info); tv_gocomment = findViewById(R.id.tv_gocomment); iv_back.setOnClickListener(this); layoutRed.setOnClickListener(this); tv_gocomment.setOnClickListener(this); upDateView(); } private void upDateView() { if (mChargingBean!=null){ tv_name.setText(mChargingBean.getZhanname()); tv_charging_shifu.setText(mChargingBean.getUserMoney()); tv_charging_cost.setText(mChargingBean.getElecMoney()+"元"); tv_service_cost.setText(mChargingBean.getServiceMoney()+"元"); tv_charging_hongbao.setText("默认已选择最大优惠 >"); tv_zhuang_no.setText(mChargingBean.getStubId()); tv_charging_time.setText(mChargingBean.getTimeCharge()); tv_charging_liang.setText(mChargingBean.getPower()+"度"); /* if (TextUtils.isEmpty(mChargingBean.getEndInfo())) { rl_end_info.setVisibility(View.GONE); }else{ tv_tishi.setText(mChargingBean.getEndInfo()); rl_end_info.setVisibility(View.VISIBLE); }*/ } } @Override public void onClick(View v) { switch (v.getId()){ case R.id.layoutRed: //优惠券选择 // default_hb.setVisibility(View.GONE); showPreferentialDialog(); break; case R.id.tv_gocomment: if (TextUtils.isEmpty(mRedEnvelopeId)){ pollingChargingStats(); }else{ requestComment(); } break; case R.id.iv_back: finish(); break; } } private void requestComment() { if (this != null && !this.isFinishing()) { mDialog.show(); } // Log.e("TAG_hcy", "requestComment: "+mRedEnvelopeId); OkHttpUtils.get() .url(UrlConstants.SETTLEMENT) .addParams(ChargingConstants.USERID, MainApplication.userId) .addParams(ChargingConstants.PWD, MainApplication.userPassword) .addParams(ChargingConstants.ORDERID, mChargingBean.getOrderId()) .addParams(ChargingConstants.HBID, mRedEnvelopeId) .build() .execute(new StringCallback() { @Override public void onError(Call call, Exception e) { dismissDialog(); LogUtils.e(); ToastUtil.showToast(getApplicationContext(), "网络异常,请检查您的网络连接", Toast.LENGTH_SHORT); CrashReport.postCatchedException(e); Log.e(this.getClass().getName(),"requestComment--onError--"+e.getMessage()); } @Override public void onResponse(String response) { // LogUtils.e("SETTLEMENT=" + response); String rtnCode = JsonUtils.getKeyResult(response, "rtnCode"); String rtnMsg = JsonUtils.getKeyResult(response, "rtnMsg"); dismissDialog(); if (null != rtnCode) { if ("01".equals(rtnCode)) { pollingChargingStats(); } } } }); } private void pollingChargingStats() { try_count++; OkHttpUtils.get().url(UrlConstants.CHARGING_STATUS_URL) .addParams(com.electric.chargingpile.constant.ChargingConstants.USERID, MainApplication.userId) .addParams(com.electric.chargingpile.constant.ChargingConstants.PWD, MainApplication.userPassword) .addParams(com.electric.chargingpile.constant.ChargingConstants.ORDERID, mChargingBean.getOrderId()) .tag(this) .build() .connTimeOut(15000) .readTimeOut(15000) .execute(new StringCallback() { @Override public void onError(Call call, Exception e) { // dismissDialog(); // ToastUtil.showToast(getApplicationContext(),"网络异常,请检查您的网络连接",Toast.LENGTH_SHORT); CrashReport.postCatchedException(e); } @Override public void onResponse(String response) { // LogUtils.e("polling=" + response); String rtnCode = JsonUtils.getKeyResult(response, "rtnCode"); String rtnMsg = JsonUtils.getKeyResult(response, "rtnMsg"); if (null != rtnCode) { if ("01".equals(rtnCode)) { String data = JsonUtils.getKeyResult(response, "data"); ChargingBean chargingBean = JsonUtils.parseToObjectBean(data, ChargingBean.class); int status = chargingBean.getStatus(); if (status >= 4) { // if (mTimer != null) { // mTimer.cancel(); // mTimer = null; // } // if (timer != null) { // timer.cancel(); // timer = null; // } dismissDialog(); ToastUtil.showToast(getApplicationContext(), "本次充电已结束", Toast.LENGTH_SHORT); MainApplication.isAppStart = false; ProfileManager.getInstance().setAppStart(getApplicationContext(), MainApplication.isAppStart); Intent intent = new Intent(getApplicationContext(), OderDetailsActivity.class); intent.putExtra(BaseConstants.CHARGING_DATA, chargingBean); intent.putExtra(BaseConstants.ACTIVITY_URL, activeUrl); startActivity(intent); finish(); } else { if (try_count < 6) { new Handler().postDelayed(new Runnable() { @Override public void run() { pollingChargingStats(); } }, 2000); } else { dismissDialog(); startActivity(new Intent(getApplicationContext(), DelayInfoActivity.class)); finish(); } } } if ("02".equals(rtnCode)) { dismissDialog(); startActivity(new Intent(getApplicationContext(), DelayInfoActivity.class)); finish(); } else { // dismissDialog(); // ToastUtil.showToast(getApplicationContext(),rtnMsg,Toast.LENGTH_SHORT); } } } }); } private void showPreferentialDialog() { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction ft = fragmentManager.beginTransaction(); Fragment prev = fragmentManager.findFragmentByTag(PreferentialDialog.class.getName()); if (prev != null) { ft.remove(prev); } if (TextUtils.isEmpty(mChargingBean.getUserMoney())){ Toast.makeText(this, "结算金额出现问题", Toast.LENGTH_SHORT).show(); return ; } PreferentialDialog dialog= PreferentialDialog.newInstance(mChargingBean.getUserMoney()); dialog.setListener(bean -> { if (bean!=null){ String hongbao = Util.getRedEnvelopeName(bean); mRedEnvelopeId=bean.getId(); tv_charging_hongbao.setText(hongbao+" >"); } }); dialog.show(fragmentManager,PreferentialDialog.class.getName()); fragmentManager.executePendingTransactions(); } private void dismissDialog() { if (this != null && !this.isFinishing()) { if (mDialog.isShowing()) { mDialog.dismiss(); } } } private void initData() { OkHttpUtils.get() .url(com.google.zxing.client.android.constant.UrlConstants.RED_ENVELOPE_LIST) .addParams(ChargingConstants.USERID, MainApplication.userId) .addParams(ChargingConstants.PWD, MainApplication.userPassword) .build() .execute(new StringCallback() { @Override public void onError(Call call, Exception e) { } @Override public void onResponse(String response) { //Log.d("HYC", "onResponse: " + response); Gson gson = new Gson(); List<RedEnvelopeBean> datas = gson.fromJson(response, new TypeToken<List<RedEnvelopeBean>>() { }.getType()); if (datas != null) { removeData(datas); if (datas.size()>0){ layoutRed.setEnabled(true); }else{ layoutRed.setEnabled(false); tv_charging_hongbao.setText("暂无红包/优惠券"); tv_charging_hongbao.setTextColor(Color.parseColor("#DDDDDD")); } }else{ layoutRed.setEnabled(false); tv_charging_hongbao.setText("暂无红包/优惠券"); tv_charging_hongbao.setTextColor(Color.parseColor("#DDDDDD")); } } }); } private void removeData(List<RedEnvelopeBean> datas) { if (mChargingBean!=null){ String userMoney = mChargingBean.getUserMoney(); if (TextUtils.isEmpty(userMoney)){ return; } try { for (int i = datas.size() - 1; i >= 0; i--) { RedEnvelopeBean redEnvelopeBean = datas.get(i); double max_money = Double.parseDouble(redEnvelopeBean.getMax_money()); double money = Double.parseDouble(userMoney); if (money < max_money) { datas.remove(i); } } } catch (NumberFormatException e) { LogUtils.e("polling=" + e.getMessage()); } } } @Override protected void onDestroy() { super.onDestroy(); try_count = 0; delayedSettlementCount = 0; } public static void actionStart(Context context, ChargingBean bean, String activeUrl){ Intent intent = new Intent(context, ConfirmOrderActivity.class); intent.putExtra(BaseConstants.CHARGING_DATA,bean); intent.putExtra(BaseConstants.ACTIVITY_URL,activeUrl); context.startActivity(intent); } }