|
|
@@ -0,0 +1,136 @@
|
|
|
+package modules.address;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.jfinal.aop.Before;
|
|
|
+import com.jfinal.aop.Inject;
|
|
|
+import common.interceptor.LoginInterceptor;
|
|
|
+import common.model.Address;
|
|
|
+import common.model.User;
|
|
|
+import common.utils.http.MyController;
|
|
|
+import common.utils.http.MyRet;
|
|
|
+import modules.user.UserService;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Before(LoginInterceptor.class)
|
|
|
+public class AddressController extends MyController {
|
|
|
+
|
|
|
+ @Inject
|
|
|
+ private AddressService service;
|
|
|
+ @Inject
|
|
|
+ private UserService userService;
|
|
|
+
|
|
|
+ public void index() {
|
|
|
+ renderText("Hello, Address!");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户的收货地址列表
|
|
|
+ */
|
|
|
+ public void list() {
|
|
|
+ User user = userService.findUserByMobileNumber(getSessionAttr("mobile_number"));
|
|
|
+ List<Address> addresses = service.findByUserId(user.getId());
|
|
|
+ renderJson(MyRet.ok("获取成功").setData(addresses));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取单个收货地址详情
|
|
|
+ */
|
|
|
+ public void get() {
|
|
|
+ User user = userService.findUserByMobileNumber(getSessionAttr("mobile_number"));
|
|
|
+ JSONObject requestBodyJson = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);
|
|
|
+ Integer addressId;
|
|
|
+ try {
|
|
|
+ addressId = requestBodyJson.getInteger("id");
|
|
|
+ } catch (Exception e) {
|
|
|
+ renderJson(MyRet.fail("你传入的 id 有些问题:" + e.getMessage()).setData(requestBodyJson));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (addressId == null) {
|
|
|
+ renderJson(MyRet.fail("id 不能为空"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Address address = service.findById(addressId, user.getId());
|
|
|
+ if (address == null) {
|
|
|
+ renderJson(MyRet.fail("地址不存在"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ renderJson(MyRet.ok("获取成功").setData(address));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增或修改收货地址
|
|
|
+ */
|
|
|
+ public void save() {
|
|
|
+ User user = userService.findUserByMobileNumber(getSessionAttr("mobile_number"));
|
|
|
+ Address address = getModel(Address.class, "");
|
|
|
+ address.setUserId(user.getId().intValue());
|
|
|
+
|
|
|
+ if (address.getId() == null) {
|
|
|
+ // 新增
|
|
|
+ if (service.save(address)) {
|
|
|
+ renderJson(MyRet.ok("添加成功").setData(address));
|
|
|
+ } else {
|
|
|
+ renderJson(MyRet.fail("添加失败"));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 修改
|
|
|
+ if (service.update(address)) {
|
|
|
+ renderJson(MyRet.ok("修改成功").setData(address));
|
|
|
+ } else {
|
|
|
+ renderJson(MyRet.fail("修改失败"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除收货地址
|
|
|
+ */
|
|
|
+ public void delete() {
|
|
|
+ User user = userService.findUserByMobileNumber(getSessionAttr("mobile_number"));
|
|
|
+ JSONObject requestBodyJson = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);
|
|
|
+ Integer addressId;
|
|
|
+ try {
|
|
|
+ addressId = requestBodyJson.getInteger("id");
|
|
|
+ } catch (Exception e) {
|
|
|
+ renderJson(MyRet.fail("你传入的 id 有些问题:" + e.getMessage()).setData(requestBodyJson));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (addressId == null) {
|
|
|
+ renderJson(MyRet.fail("id 不能为空"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (service.delete(addressId, user.getId())) {
|
|
|
+ renderJson(MyRet.ok("删除成功"));
|
|
|
+ } else {
|
|
|
+ renderJson(MyRet.fail("删除失败"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置默认地址
|
|
|
+ */
|
|
|
+ public void setDefault() {
|
|
|
+ User user = userService.findUserByMobileNumber(getSessionAttr("mobile_number"));
|
|
|
+ JSONObject requestBodyJson = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);
|
|
|
+ Integer addressId;
|
|
|
+ try {
|
|
|
+ addressId = requestBodyJson.getInteger("id");
|
|
|
+ } catch (Exception e) {
|
|
|
+ renderJson(MyRet.fail("你传入的 id 有些问题:" + e.getMessage()).setData(requestBodyJson));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (addressId == null) {
|
|
|
+ renderJson(MyRet.fail("id 不能为空"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (service.setDefault(addressId, user.getId())) {
|
|
|
+ renderJson(MyRet.ok("设置成功"));
|
|
|
+ } else {
|
|
|
+ renderJson(MyRet.fail("设置失败"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|