AddressController.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package modules.address;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.jfinal.aop.Before;
  4. import com.jfinal.aop.Inject;
  5. import com.jfinal.kit.StrKit;
  6. import common.interceptor.LoginInterceptor;
  7. import common.interceptor.empty.EmptyInterface;
  8. import common.model.Address;
  9. import common.model.User;
  10. import common.utils.http.MyController;
  11. import common.utils.http.MyRet;
  12. import modules.user.UserService;
  13. import java.util.List;
  14. @Before(LoginInterceptor.class)
  15. public class AddressController extends MyController {
  16. @Inject
  17. private AddressService service;
  18. @Inject
  19. private UserService userService;
  20. public void hello() {
  21. renderText("Hello, Address!");
  22. }
  23. /**
  24. * 获取用户的收货地址列表
  25. */
  26. public void list() {
  27. User user = userService.findUserByMobileNumber(getSessionAttr("mobile_number"));
  28. List<Address> addresses = service.findByUserId(user.getId());
  29. renderJson(MyRet.ok("获取成功").setData(addresses));
  30. }
  31. /**
  32. * 获取单个收货地址详情
  33. */
  34. public void get() {
  35. User user = userService.findUserByMobileNumber(getSessionAttr("mobile_number"));
  36. JSONObject requestBodyJson = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);
  37. Integer addressId;
  38. try {
  39. addressId = requestBodyJson.getInteger("id");
  40. } catch (Exception e) {
  41. renderJson(MyRet.fail("你传入的 id 有些问题:" + e.getMessage()).setData(requestBodyJson));
  42. return;
  43. }
  44. if (addressId == null) {
  45. renderJson(MyRet.fail("id 不能为空"));
  46. return;
  47. }
  48. Address address = service.findById(addressId, user.getId());
  49. if (address == null) {
  50. renderJson(MyRet.fail("地址不存在"));
  51. return;
  52. }
  53. renderJson(MyRet.ok("获取成功").setData(address));
  54. }
  55. /**
  56. * 新增或修改收货地址
  57. */
  58. @EmptyInterface({"recipient_name", "phone_number", "detailed_address"})
  59. public void save() {
  60. User user = userService.findUserByMobileNumber(getSessionAttr("mobile_number"));
  61. JSONObject json = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);
  62. Address address = new Address();
  63. Integer id = json.getInteger("id");
  64. if (id != null) {
  65. address = service.findById(id, user.getId());
  66. if (address == null) {
  67. renderJson(MyRet.fail("地址不存在"));
  68. return;
  69. }
  70. }
  71. String recipientName = json.getString("recipient_name");
  72. if (StrKit.isBlank(recipientName)) {
  73. renderJson(MyRet.fail("收货人姓名不能为空"));
  74. return;
  75. }
  76. address.setRecipientName(recipientName);
  77. String phoneNumber = json.getString("phone_number");
  78. if (StrKit.isBlank(phoneNumber)) {
  79. renderJson(MyRet.fail("手机号码不能为空"));
  80. return;
  81. }
  82. address.setPhoneNumber(phoneNumber);
  83. String detailedAddress = json.getString("detailed_address");
  84. if (StrKit.isBlank(detailedAddress)) {
  85. renderJson(MyRet.fail("详细地址不能为空"));
  86. return;
  87. }
  88. address.setDetailedAddress(detailedAddress);
  89. Object isDefaultObj = json.get("is_default");
  90. if (isDefaultObj != null) {
  91. try {
  92. int isDefaultValue = com.alibaba.fastjson.util.TypeUtils.castToInt(isDefaultObj);
  93. if (isDefaultValue == 0 || isDefaultValue == 1) {
  94. address.setIsDefault(isDefaultValue);
  95. } else {
  96. renderJson(MyRet.fail("is_default 字段的值必须是 0 或 1"));
  97. return;
  98. }
  99. } catch (Exception e) {
  100. renderJson(MyRet.fail("is_default 字段格式不正确,必须是数字 0 或 1"));
  101. return;
  102. }
  103. }
  104. address.setUserId(user.getId().intValue());
  105. if (id == null) {
  106. if (service.save(address)) {
  107. renderJson(MyRet.ok("保存成功").setData(address));
  108. } else {
  109. renderJson(MyRet.fail("保存失败"));
  110. }
  111. } else {
  112. if (service.update(address)) {
  113. renderJson(MyRet.ok("更新成功").setData(address));
  114. } else {
  115. renderJson(MyRet.fail("更新失败"));
  116. }
  117. }
  118. }
  119. /**
  120. * 删除收货地址
  121. */
  122. public void delete() {
  123. User user = userService.findUserByMobileNumber(getSessionAttr("mobile_number"));
  124. JSONObject requestBodyJson = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);
  125. Integer addressId;
  126. try {
  127. addressId = requestBodyJson.getInteger("id");
  128. } catch (Exception e) {
  129. renderJson(MyRet.fail("你传入的 id 有些问题:" + e.getMessage()).setData(requestBodyJson));
  130. return;
  131. }
  132. if (addressId == null) {
  133. renderJson(MyRet.fail("id 不能为空"));
  134. return;
  135. }
  136. if (service.delete(addressId, user.getId())) {
  137. renderJson(MyRet.ok("删除成功"));
  138. } else {
  139. renderJson(MyRet.fail("删除失败"));
  140. }
  141. }
  142. /**
  143. * 设置默认地址
  144. */
  145. public void setDefault() {
  146. User user = userService.findUserByMobileNumber(getSessionAttr("mobile_number"));
  147. JSONObject requestBodyJson = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);
  148. Integer addressId;
  149. try {
  150. addressId = requestBodyJson.getInteger("id");
  151. } catch (Exception e) {
  152. renderJson(MyRet.fail("你传入的 id 有些问题:" + e.getMessage()).setData(requestBodyJson));
  153. return;
  154. }
  155. if (addressId == null) {
  156. renderJson(MyRet.fail("id 不能为空"));
  157. return;
  158. }
  159. if (service.setDefault(addressId, user.getId())) {
  160. renderJson(MyRet.ok("设置成功"));
  161. } else {
  162. renderJson(MyRet.fail("设置失败"));
  163. }
  164. }
  165. }