注册店铺

来源:6-4 店铺信息编辑之前端实现

我总是那么的乖

2020-03-01

我在测试用id传入修改店铺后(是成功的),又测试了一下注册店铺,发现不能注册店铺了,但是之前4-14的时候还是可以注册成功
图片描述
这里看着像是说owner_id
图片描述
是不是因为我的.js代码修改错了?(店铺我不想要图片、地址、类型)

$(function() {
// 从URL里获取shopId参数的值
var shopId = getQueryString(‘shopId’);
// 由于店铺注册和编辑使用的是同一个页面,
// 该标识符用来标明本次是添加还是编辑操作true对店铺进行更新,false添加店铺
var isEdit = shopId ? true : false;
// 注册店铺的URL
var registerShopUrl = ‘/o2o/shopadmin/registershop’;
// 编辑店铺前需要获取店铺信息,这里为获取当前店铺信息的URL
var shopInfoUrl = “/o2o/shopadmin/getshopbyid?shopId=” + shopId;
// 编辑店铺信息的URL
var editShopUrl = ‘/o2o/shopadmin/modifyshop’;
// 判断是编辑操作还是注册操作
if (isEdit) {
getShopInfo(shopId);
}

// 通过店铺Id获取店铺信息
function getShopInfo(shopId) {
	$.getJSON(shopInfoUrl, function(data) {
		if (data.success) {
			// 若访问成功,则依据后台传递过来的店铺信息为表单元素赋值
			var shop = data.shop;
			$('#shop-name').val(shop.shopName);
			$('#shop-phone').val(shop.phone);
			$('#shop-desc').val(shop.shopDesc);
		}
	});
}

// 提交按钮的事件响应,分别对店铺注册和编辑操作做不同响应
$('#submit').click(function() {
	// 创建shop对象
	var shop = {};
	if (isEdit) {
		// 若属于编辑,则给shopId赋值
		shop.shopId = shopId;
	}
	// 获取表单里的数据并填充进对应的店铺属性中
	shop.shopName = $('#shop-name').val();
	shop.phone = $('#shop-phone').val();
	shop.shopDesc = $('#shop-desc').val();

	// 将shop json对象转成字符流保存至表单对象key为shopStr的的键值对里
	var formData=new FormData();
	formData.append('shopStr', JSON.stringify(shop));
	// 获取表单里输入的验证码
	var verifyCodeActual = $('#j_captcha').val();
	if (!verifyCodeActual) {
		$.toast('请输入验证码!');
		return;
	}
	formData.append('verifyCodeActual', verifyCodeActual);
	
	// 将数据提交至后台处理相关操作
	$.ajax({
		url : (isEdit ? editShopUrl : registerShopUrl),
		type : 'POST',
		data : formData,
		contentType : false,
		processData : false,
		cache : false,
		success : function(data) {
			if (data.success) {
				$.toast('提交成功!');
			} else {
				$.toast('提交失败!' + data.errMsg);
			}
			// 点击验证码图片的时候,注册码会改变
			$('#captcha_img').click();
		}
	});
});

})
这里这样写可以吗
这里这样写可以吗

@Controller
@RequestMapping("/shopadmin") //controller的返回路径
public class ShopManagementController {
@Autowired
private ShopService shopService;

@RequestMapping(value = "/getshopbyid", method = RequestMethod.GET)
@ResponseBody
private Map<String, Object> getShopById(HttpServletRequest request) {
	Map<String, Object> modelMap = new HashMap<String, Object>();
	Long shopId = HttpServletRequestUtil.getLong(request, "shopId");
	if (shopId > -1) {
		try {
			Shop shop = shopService.getByShopId(shopId);
			modelMap.put("shop", shop);
			modelMap.put("success", true);
		} catch (Exception e) {
			modelMap.put("success", false);
			modelMap.put("errMsg", e.toString());
		}
	} else {
		modelMap.put("success", false);
		modelMap.put("errMsg", "empty shopId");
	}
	return modelMap;
}


// registershop的返回路径
@RequestMapping(value = "/registershop", method = RequestMethod.POST)
@ResponseBody // 将传入进来的请求信息解析出来,通过json转换为实体类
private Map<String, Object> registerShop(HttpServletRequest request) {
	Map<String, Object> modelMap = new HashMap<String, Object>();
	if (!CodeUtil.checkVerifyCode(request)) {
		modelMap.put("success", false);
		modelMap.put("errMsg", "输入了错误的验证码");
		return modelMap;
	}
	// 1.接收并转化相应的参数,包括店铺信息
	String shopStr = HttpServletRequestUtil.getString(request, "shopStr");
	ObjectMapper mapper = new ObjectMapper();
	Shop shop = null;
	try {
		shop = mapper.readValue(shopStr, Shop.class);
	} catch (Exception e) {
		modelMap.put("success", false);
		modelMap.put("errMsg", e.getMessage());
		return modelMap;
	}
	// 2.注册店铺 ,将转化过来的实例
	if (shop != null ) {
		PersonInfo owner = (PersonInfo) request.getSession().getAttribute("user");
		shop.setOwner(owner);
		ShopExecution se;
		try {
			se = shopService.addShop(shop);
			if (se.getState() == ShopStateEnum.CHECK.getState()) {
				modelMap.put("success", true);
				// 该用户可以操作的店铺列表
				@SuppressWarnings("unchecked")
				List<Shop> shopList = (List<Shop>) request.getSession().getAttribute("shopList");
				if (shopList == null || shopList.size() == 0) {
					shopList = new ArrayList<Shop>();
				}
				shopList.add(se.getShop());
				request.getSession().setAttribute("shopList", shopList);
			} else {
				modelMap.put("success", false);
				modelMap.put("errMsg", se.getStateInfo());
			}
		} catch (ShopOperationException e) {
			modelMap.put("success", false);
			modelMap.put("errMsg", e.getMessage());
		}
		return modelMap;
	} else {
		modelMap.put("success", false);
		modelMap.put("errMsg", "请输入店铺信息");
		return modelMap;
	}
}

@RequestMapping(value = "/modifyshop", method = RequestMethod.POST)
@ResponseBody
private Map<String, Object> modifyShop(HttpServletRequest request) {
	Map<String, Object> modelMap = new HashMap<String, Object>();
	if (!CodeUtil.checkVerifyCode(request)) {
		modelMap.put("success", false);
		modelMap.put("errMsg", "输入了错误的验证码");
		return modelMap;
	}
	// 1.接收并转化相应的参数,包括店铺信息以及图片信息
	String shopStr = HttpServletRequestUtil.getString(request, "shopStr");
	ObjectMapper mapper = new ObjectMapper();
	Shop shop = null;
	try {
		shop = mapper.readValue(shopStr, Shop.class);
	} catch (Exception e) {
		modelMap.put("success", false);
		modelMap.put("errMsg", e.getMessage());
		return modelMap;
	}
	// 2.修改店铺信息
	if (shop != null && shop.getShopId() != null) {
		ShopExecution se;
		try {
			se=shopService.modifyShop(shop);
			if (se.getState() == ShopStateEnum.SUCCESS.getState()) {
				modelMap.put("success", true);
			} else {
				modelMap.put("success", false);
				modelMap.put("errMsg", se.getStateInfo());
			}
		} catch (ShopOperationException e) {
			modelMap.put("success", false);
			modelMap.put("errMsg", e.getMessage());
		}
		return modelMap;
	} else {
		modelMap.put("success", false);
		modelMap.put("errMsg", "请输入店铺Id");
		return modelMap;
	}
}

}

之前都是好好好,今天加了这个注册修改就不行…

写回答

1回答

翔仔

2020-03-02

同学好,直接看代码没法精确定位错误,但是我看异常的关键在于你的第一个截图,但是没截全,主要是和owner_id没传相关,需要传入owner_id才可以,在实现登录功能之前,我们会写死一个owner_id,请确保owner_id存在于tb_person_info 也就是user_id存在才可以。创建店铺的时候,需要给店铺赋值上owner_id

0
1
我总是那么的乖
找到问题了,因为缺一个登录的程序,没有session传过来,就没有ower,之前4-14的ower是写死的,所以到6-4的时候在去试一下注册就会over
2020-03-02
共1条回复

Java双版本(SSM到SpringBoot)校园商铺全栈开发

SSM商铺V1.0,解决毕设痛点;SpringBoot商铺V2.0,满足工作刚需

5113 学习 · 8144 问题

查看课程