PageRenderTime 62ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/wechat-mp-sdk/src/main/java/org/usc/wechat/mp/sdk/util/JsonRtnUtil.java

https://gitlab.com/hackbuteer59/wechat-mp-sdk
Java | 90 lines | 53 code | 14 blank | 23 comment | 9 complexity | a06260132d8b15b9a8b9a47bcbc3c783 MD5 | raw file
  1. package org.usc.wechat.mp.sdk.util;
  2. import java.util.ResourceBundle;
  3. import org.apache.commons.lang3.StringUtils;
  4. import org.usc.wechat.mp.sdk.vo.JsonRtn;
  5. import com.alibaba.fastjson.JSONObject;
  6. /**
  7. *
  8. * @author Shunli
  9. */
  10. public class JsonRtnUtil {
  11. private static final String WECHAT_GLOBAL_MESSAGE_FILE_NAME = "wechate-global-message";
  12. private static final String WECHAT_JSON_RTN_SUCCESS_CODE = "0";
  13. private static ResourceBundle bundle;
  14. static {
  15. try {
  16. bundle = ResourceBundle.getBundle(WECHAT_GLOBAL_MESSAGE_FILE_NAME);
  17. } catch (Exception e) {
  18. }
  19. }
  20. /**
  21. * parse json text to specified class
  22. *
  23. * @param jsonRtn
  24. * @param jsonRtnClazz
  25. * @return
  26. */
  27. public static <T extends JsonRtn> T parseJsonRtn(String jsonRtn, Class<T> jsonRtnClazz) {
  28. T rtn = JSONObject.parseObject(jsonRtn, jsonRtnClazz);
  29. appendErrorHumanMsg(rtn);
  30. return rtn;
  31. }
  32. /**
  33. * append human message to JsonRtn class
  34. *
  35. * @param jsonRtn
  36. * @return
  37. */
  38. private static JsonRtn appendErrorHumanMsg(JsonRtn jsonRtn) {
  39. if (bundle == null || jsonRtn == null || StringUtils.isEmpty(jsonRtn.getErrCode())) {
  40. return null;
  41. }
  42. try {
  43. jsonRtn.setErrHumanMsg(bundle.getString(jsonRtn.getErrCode()));
  44. return jsonRtn;
  45. } catch (Exception e) {
  46. return null;
  47. }
  48. }
  49. /**
  50. * return request is success by JsonRtn object
  51. *
  52. * @param jsonRtn
  53. * @return
  54. */
  55. public static boolean isSuccess(JsonRtn jsonRtn) {
  56. if (jsonRtn == null) {
  57. return false;
  58. }
  59. String errCode = jsonRtn.getErrCode();
  60. if (StringUtils.isEmpty(errCode) || StringUtils.equals(WECHAT_JSON_RTN_SUCCESS_CODE, errCode)) {
  61. return true;
  62. }
  63. return false;
  64. }
  65. public static <T extends JsonRtn> T buildFailureJsonRtn(Class<T> jsonRtnClazz, String errMsg) {
  66. try {
  67. T jsonRtn = jsonRtnClazz.newInstance();
  68. jsonRtn.setErrCode("-1");
  69. jsonRtn.setErrMsg(errMsg);
  70. appendErrorHumanMsg(jsonRtn);
  71. return jsonRtn;
  72. } catch (Exception e) {
  73. return null;
  74. }
  75. }
  76. }