|
|
@@ -3,7 +3,9 @@ package modules.address;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.jfinal.aop.Before;
|
|
|
import com.jfinal.aop.Inject;
|
|
|
+import com.jfinal.kit.StrKit;
|
|
|
import common.interceptor.LoginInterceptor;
|
|
|
+import common.interceptor.empty.EmptyInterface;
|
|
|
import common.model.Address;
|
|
|
import common.model.User;
|
|
|
import common.utils.http.MyController;
|
|
|
@@ -20,7 +22,7 @@ public class AddressController extends MyController {
|
|
|
@Inject
|
|
|
private UserService userService;
|
|
|
|
|
|
- public void index() {
|
|
|
+ public void hello() {
|
|
|
renderText("Hello, Address!");
|
|
|
}
|
|
|
|
|
|
@@ -62,24 +64,71 @@ public class AddressController extends MyController {
|
|
|
/**
|
|
|
* 新增或修改收货地址
|
|
|
*/
|
|
|
+ @EmptyInterface({"recipient_name", "phone_number", "detailed_address"})
|
|
|
public void save() {
|
|
|
User user = userService.findUserByMobileNumber(getSessionAttr("mobile_number"));
|
|
|
- Address address = getModel(Address.class, "");
|
|
|
+ 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("收货人姓名不能为空"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ address.setRecipientName(recipientName);
|
|
|
+
|
|
|
+ String phoneNumber = json.getString("phone_number");
|
|
|
+ if (StrKit.isBlank(phoneNumber)) {
|
|
|
+ renderJson(MyRet.fail("手机号码不能为空"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ address.setPhoneNumber(phoneNumber);
|
|
|
+
|
|
|
+ String detailedAddress = json.getString("detailed_address");
|
|
|
+ if (StrKit.isBlank(detailedAddress)) {
|
|
|
+ renderJson(MyRet.fail("详细地址不能为空"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
address.setUserId(user.getId().intValue());
|
|
|
|
|
|
- if (address.getId() == null) {
|
|
|
- // 新增
|
|
|
+ if (id == null) {
|
|
|
if (service.save(address)) {
|
|
|
- renderJson(MyRet.ok("添加成功").setData(address));
|
|
|
+ renderJson(MyRet.ok("保存成功").setData(address));
|
|
|
} else {
|
|
|
- renderJson(MyRet.fail("添加失败"));
|
|
|
+ renderJson(MyRet.fail("保存失败"));
|
|
|
}
|
|
|
} else {
|
|
|
- // 修改
|
|
|
if (service.update(address)) {
|
|
|
- renderJson(MyRet.ok("修改成功").setData(address));
|
|
|
+ renderJson(MyRet.ok("更新成功").setData(address));
|
|
|
} else {
|
|
|
- renderJson(MyRet.fail("修改失败"));
|
|
|
+ renderJson(MyRet.fail("更新失败"));
|
|
|
}
|
|
|
}
|
|
|
}
|