|
|
@@ -73,7 +73,7 @@ public class AddressController extends MyController {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 新增或修改收货地址
|
|
|
+ * 新增收货地址
|
|
|
*/
|
|
|
@EmptyInterface({"recipient_name", "phone_number", "detailed_address"})
|
|
|
public void save() {
|
|
|
@@ -81,15 +81,8 @@ public class AddressController extends MyController {
|
|
|
JSONObject json = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);
|
|
|
|
|
|
Address address = new Address();
|
|
|
- Integer id = json.getInteger("id");
|
|
|
- if (id != null) {
|
|
|
- address = service.findById(id, user.getId());
|
|
|
- if (address == null) {
|
|
|
- renderJson(MyRet.fail("地址不存在"));
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
+ // 校验收货人姓名
|
|
|
String recipientName = json.getString("recipient_name");
|
|
|
if (StrKit.isBlank(recipientName)) {
|
|
|
renderJson(MyRet.fail("收货人姓名不能为空"));
|
|
|
@@ -97,6 +90,7 @@ public class AddressController extends MyController {
|
|
|
}
|
|
|
address.setRecipientName(recipientName);
|
|
|
|
|
|
+ // 校验手机号码
|
|
|
String phoneNumber = json.getString("phone_number");
|
|
|
if (StrKit.isBlank(phoneNumber)) {
|
|
|
renderJson(MyRet.fail("手机号码不能为空"));
|
|
|
@@ -104,6 +98,7 @@ public class AddressController extends MyController {
|
|
|
}
|
|
|
address.setPhoneNumber(phoneNumber);
|
|
|
|
|
|
+ // 校验详细地址
|
|
|
String detailedAddress = json.getString("detailed_address");
|
|
|
if (StrKit.isBlank(detailedAddress)) {
|
|
|
renderJson(MyRet.fail("详细地址不能为空"));
|
|
|
@@ -111,6 +106,7 @@ public class AddressController extends MyController {
|
|
|
}
|
|
|
address.setDetailedAddress(detailedAddress);
|
|
|
|
|
|
+ // 处理是否默认地址
|
|
|
Object isDefaultObj = json.get("is_default");
|
|
|
if (isDefaultObj != null) {
|
|
|
try {
|
|
|
@@ -129,18 +125,73 @@ public class AddressController extends MyController {
|
|
|
|
|
|
address.setUserId(user.getId().intValue());
|
|
|
|
|
|
+ if (service.save(address)) {
|
|
|
+ renderJson(MyRet.ok("保存成功").setData(address));
|
|
|
+ } else {
|
|
|
+ renderJson(MyRet.fail("保存失败"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改收货地址
|
|
|
+ */
|
|
|
+ public void update() {
|
|
|
+ User user = userService.findUserByMobileNumber(getSessionAttr("mobile_number"));
|
|
|
+ JSONObject json = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);
|
|
|
+
|
|
|
+ // 校验地址ID
|
|
|
+ Integer id = json.getInteger("id");
|
|
|
if (id == null) {
|
|
|
- if (service.save(address)) {
|
|
|
- renderJson(MyRet.ok("保存成功").setData(address));
|
|
|
- } else {
|
|
|
- renderJson(MyRet.fail("保存失败"));
|
|
|
+ renderJson(MyRet.fail("地址ID不能为空"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查找并验证地址是否存在
|
|
|
+ Address address = service.findById(id, user.getId());
|
|
|
+ if (address == null) {
|
|
|
+ renderJson(MyRet.fail("地址不存在"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新收货人姓名(如果提供)
|
|
|
+ String recipientName = json.getString("recipient_name");
|
|
|
+ if (StrKit.notBlank(recipientName)) {
|
|
|
+ address.setRecipientName(recipientName);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新手机号码(如果提供)
|
|
|
+ String phoneNumber = json.getString("phone_number");
|
|
|
+ if (StrKit.notBlank(phoneNumber)) {
|
|
|
+ address.setPhoneNumber(phoneNumber);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新详细地址(如果提供)
|
|
|
+ String detailedAddress = json.getString("detailed_address");
|
|
|
+ if (StrKit.notBlank(detailedAddress)) {
|
|
|
+ address.setDetailedAddress(detailedAddress);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理是否默认地址(如果提供)
|
|
|
+ Object isDefaultObj = json.get("is_default");
|
|
|
+ if (isDefaultObj != null) {
|
|
|
+ try {
|
|
|
+ int isDefaultValue = com.alibaba.fastjson.util.TypeUtils.castToInt(isDefaultObj);
|
|
|
+ if (isDefaultValue == 0 || isDefaultValue == 1) {
|
|
|
+ address.setIsDefault(isDefaultValue);
|
|
|
+ } else {
|
|
|
+ renderJson(MyRet.fail("is_default 字段的值必须是 0 或 1"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ renderJson(MyRet.fail("is_default 字段格式不正确,必须是数字 0 或 1"));
|
|
|
+ return;
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ if (service.update(address)) {
|
|
|
+ renderJson(MyRet.ok("更新成功").setData(address));
|
|
|
} else {
|
|
|
- if (service.update(address)) {
|
|
|
- renderJson(MyRet.ok("更新成功").setData(address));
|
|
|
- } else {
|
|
|
- renderJson(MyRet.fail("更新失败"));
|
|
|
- }
|
|
|
+ renderJson(MyRet.fail("更新失败"));
|
|
|
}
|
|
|
}
|
|
|
|