TakeCodeFragment.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package com.emato.ich.fragment;
  2. import android.graphics.drawable.Drawable;
  3. import android.os.Bundle;
  4. import android.os.CountDownTimer;
  5. import android.text.Editable;
  6. import android.util.Log;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.TextView;
  11. import androidx.annotation.NonNull;
  12. import androidx.annotation.Nullable;
  13. import androidx.fragment.app.Fragment;
  14. import androidx.navigation.fragment.NavHostFragment;
  15. import com.emato.ich.MainActivity;
  16. import com.emato.ich.R;
  17. import com.emato.ich.api.ICSPClient;
  18. import com.emato.ich.api.ICSPResponseCodeEnum;
  19. import com.emato.ich.api.SystemConfigConstant;
  20. import com.emato.ich.databinding.FragmentTakeCodeBinding;
  21. import com.emato.ich.entity.vo.PreparedOrderResponseVo;
  22. import com.emato.ich.entity.vo.ResponseData;
  23. import com.emato.ich.entity.vo.TakeParcelVo;
  24. import com.emato.ich.utils.BaseUtils;
  25. import com.emato.ich.utils.StringUtils;
  26. import com.emato.ich.utils.JacksonUtils;
  27. import com.emato.ich.utils.TimeOutUtils;
  28. import com.fasterxml.jackson.core.JsonProcessingException;
  29. import com.fasterxml.jackson.core.type.TypeReference;
  30. import com.fasterxml.jackson.databind.ObjectMapper;
  31. import com.google.android.material.snackbar.Snackbar;
  32. import org.jetbrains.annotations.NotNull;
  33. import java.io.IOException;
  34. import java.io.InputStream;
  35. import java.net.URL;
  36. import okhttp3.Call;
  37. import okhttp3.Callback;
  38. import okhttp3.Response;
  39. /**
  40. * 取件码输入页面
  41. */
  42. public class TakeCodeFragment extends Fragment {
  43. private FragmentTakeCodeBinding binding;
  44. private static final String TAG = TakeCodeFragment.class.getName();
  45. private CountDownTimer timer;
  46. @Nullable
  47. @org.jetbrains.annotations.Nullable
  48. @Override
  49. public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
  50. binding = FragmentTakeCodeBinding.inflate(inflater, container, false);
  51. return binding.getRoot();
  52. }
  53. public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
  54. super.onViewCreated(view, savedInstanceState);
  55. BaseUtils.disableEditText(binding.editTextTextPersonName6);
  56. try {
  57. MainActivity activity = ((MainActivity) getActivity());
  58. timer = TimeOutUtils.timeout(activity, TakeCodeFragment.this, binding.timeout, 60);
  59. timer.start();
  60. } catch (Exception e) {
  61. Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
  62. }
  63. // binding.textView.setBackgroundResource(R.drawable.e_mp_qrcode_8x8_backup);
  64. MainActivity activity = (MainActivity) getActivity();
  65. String url = activity.getConfigMap().get(SystemConfigConstant.cabinet_take_object_qrcode_url);
  66. if (getActivity() != null && activity.getConfigMap().size() > 0 && !StringUtils.isNullOrEmpty(url)) {
  67. ICSPClient.getWeChatMPImage(url, new Callback() {
  68. @Override
  69. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  70. Log.e(TAG, "onFailure: 请求微信公众号图片错误! 网络异常! ", e);
  71. }
  72. @Override
  73. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  74. getActivity().runOnUiThread(() -> {
  75. try {
  76. InputStream is = response.body().byteStream();
  77. final Drawable d = Drawable.createFromStream(is, "src");
  78. binding.textView.setBackground(d);
  79. is.close();
  80. } catch (Exception e) {
  81. Log.e(TAG, "onResponse: 设置图片异常! ", e);
  82. }
  83. });
  84. }
  85. });
  86. }
  87. // 选中输入框
  88. // binding.editTextTextPersonName6.requestFocus();
  89. // 输入逻辑
  90. binding.editTextTextPersonName6.setOnFocusChangeListener((view1, hasFocus) -> {
  91. if (hasFocus) {
  92. binding.takeInputButton0.setOnClickListener(v -> {
  93. int start = binding.editTextTextPersonName6.getSelectionStart();
  94. binding.editTextTextPersonName6.getText().insert(start,"0");
  95. });
  96. binding.takeInputButton1.setOnClickListener(v -> {
  97. int start = binding.editTextTextPersonName6.getSelectionStart();
  98. binding.editTextTextPersonName6.getText().insert(start,"1");
  99. });
  100. binding.takeInputButton2.setOnClickListener(v -> {
  101. int start = binding.editTextTextPersonName6.getSelectionStart();
  102. binding.editTextTextPersonName6.getText().insert(start,"2");
  103. });
  104. binding.takeInputButton3.setOnClickListener(v -> {
  105. int start = binding.editTextTextPersonName6.getSelectionStart();
  106. binding.editTextTextPersonName6.getText().insert(start,"3");
  107. });
  108. binding.takeInputButton4.setOnClickListener(v -> {
  109. int start = binding.editTextTextPersonName6.getSelectionStart();
  110. binding.editTextTextPersonName6.getText().insert(start,"4");
  111. });
  112. binding.takeInputButton5.setOnClickListener(v -> {
  113. int start = binding.editTextTextPersonName6.getSelectionStart();
  114. binding.editTextTextPersonName6.getText().insert(start,"5");
  115. });
  116. binding.takeInputButton6.setOnClickListener(v -> {
  117. int start = binding.editTextTextPersonName6.getSelectionStart();
  118. binding.editTextTextPersonName6.getText().insert(start,"6");
  119. });
  120. binding.takeInputButton7.setOnClickListener(v -> {
  121. int start = binding.editTextTextPersonName6.getSelectionStart();
  122. binding.editTextTextPersonName6.getText().insert(start,"7");
  123. });
  124. binding.takeInputButton8.setOnClickListener(v -> {
  125. int start = binding.editTextTextPersonName6.getSelectionStart();
  126. binding.editTextTextPersonName6.getText().insert(start,"8");
  127. });
  128. binding.takeInputButton9.setOnClickListener(v -> {
  129. int start = binding.editTextTextPersonName6.getSelectionStart();
  130. binding.editTextTextPersonName6.getText().insert(start,"9");
  131. });
  132. binding.takeInputButtonCancel.setOnClickListener(v -> {
  133. Editable text = binding.editTextTextPersonName6.getText();
  134. if (text.length() > 0) {
  135. text.delete(text.length()-1, text.length());
  136. binding.editTextTextPersonName6.setText(text, TextView.BufferType.EDITABLE);
  137. binding.editTextTextPersonName6.setSelection(binding.editTextTextPersonName6.getText().length());
  138. }
  139. });
  140. }
  141. });
  142. binding.takeInputButtonConfirm.setOnClickListener(view1 -> {
  143. // TODO 取件逻辑
  144. String takeParcelCode = binding.editTextTextPersonName6.getText().toString();
  145. if (null != takeParcelCode && !"".equals(takeParcelCode.trim()) && takeParcelCode.length() > 0) {
  146. TakeParcelVo takeParcelVo = new TakeParcelVo();
  147. takeParcelVo.setClientId(BaseUtils.getClientId());
  148. takeParcelVo.setPickUp(takeParcelCode);
  149. takeParcelVo.setReopen("0");
  150. ICSPClient.takeParcel("", takeParcelVo, new Callback() {
  151. @Override
  152. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  153. // TODO 取件接口请求失败逻辑处理
  154. Snackbar.make(view, "取件失败!网络异常!", Snackbar.LENGTH_LONG).show();
  155. Log.e(TAG, "onFailure: 取件请求失败!", e);
  156. }
  157. @Override
  158. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  159. // TODO 返回锁板和锁id, 开柜门
  160. getActivity().runOnUiThread(() -> {
  161. String parseResponse = ICSPClient.isSuccessfulAndParseResponse(response);
  162. try {
  163. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  164. ResponseData<PreparedOrderResponseVo> responseData = null;
  165. try {
  166. responseData = objectMapper.readValue(parseResponse, new TypeReference<ResponseData<PreparedOrderResponseVo>>() {
  167. });
  168. ICSPResponseCodeEnum.responseHint(view1, responseData);
  169. } catch (JsonProcessingException e) {
  170. Log.e(TAG, "onResponse: 解析取件开门响应信息失败! ", e);
  171. } catch (RuntimeException e) {
  172. Log.e(TAG, "onResponse: 未知错误! ", e);
  173. Snackbar.make(view1, "未知错误!", Snackbar.LENGTH_LONG).show();
  174. }
  175. if (null != responseData && responseData.getCode().equals(ICSPResponseCodeEnum.OK.getCode())) {
  176. // 开门
  177. Log.i(TAG, "onResponse: 取件成功! ");
  178. Snackbar.make(view1, "取件成功!", Snackbar.LENGTH_LONG).show();
  179. Bundle bundle = new Bundle();
  180. bundle.putString("takeParcelCode", takeParcelCode);
  181. bundle.putString("takeCabinetNo", responseData.getData().getLockerName());
  182. ((MainActivity) getActivity()).getBundleMap().put(TakeCodeFragment.class.getName(), bundle);
  183. NavHostFragment.findNavController(TakeCodeFragment.this).navigate(R.id.action_takeCodeFragment_to_takeSuccessFragment);
  184. }
  185. } catch (RuntimeException e) {
  186. Log.e(TAG, "onResponse: 取件失败! ", e);
  187. Snackbar.make(view1, "取件失败!", Snackbar.LENGTH_LONG).show();
  188. }
  189. });
  190. }
  191. });
  192. } else {
  193. Snackbar.make(view, "取件失败!取件码不能为空!", Snackbar.LENGTH_LONG).show();
  194. }
  195. });
  196. binding.returnBtn.setOnClickListener(view1 -> NavHostFragment.findNavController(TakeCodeFragment.this)
  197. .navigate(R.id.action_takeCodeFragment_to_takeFragment));
  198. binding.editTextTextPersonName6.requestFocus();
  199. }
  200. @Override
  201. public void onDestroy() {
  202. super.onDestroy();
  203. binding = null;
  204. timer.cancel();
  205. }
  206. }