8.12总结
店铺申请代码
ShopController
package com.helei.controller;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.helei.pojo.Code;
import com.helei.pojo.Shop;
import com.helei.pojo.User;
import com.helei.service.CodeService;
import com.helei.service.ShopService;
import com.helei.service.UserService;
import com.helei.util.JWTUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("/shop")
public class ShopController {
@Resource(name = "shopService")
private ShopService shopService;
@Resource(name = "codeService")
private CodeService codeService;
@Resource(name = "userService")
private UserService userService;
@RequestMapping("/getShopByUserId")
public @ResponseBody Map<String,Object> getShopByUserId(HttpServletRequest request){
DecodedJWT verify = (DecodedJWT)request.getAttribute("verify");
Map<String, Object> map = userService.checkJWT(verify);
if ((Integer)map.get("status") == 2){
return map;
}
User user = JWTUtils.parse(verify);
Shop shop = shopService.getByUserId(user.getUserId());
if (shop == null){
map.put("status",0);
} else {
map.put("shop",shop);
}
return map;
}
@RequestMapping("/checkShopName")
public @ResponseBody Map<String,Object> checkShopName(@RequestBody Map<String,Object> m){
Map<String,Object> map = new HashMap<>();
String shopName = (String)m.get("shopName");
Shop shop = shopService.getByShopName(shopName);
if (shop != null){
map.put("status",1);//表示已有店铺
} else {
map.put("status",0);//表示没有店铺
}
return map;
}
@RequestMapping("/startShop")
public @ResponseBody Map<String,Object> startShop(@RequestBody Map<String,Object> m){
//验证邮箱是否合法
User u = new User();
u.setEmail((String)m.get("email"));
User user = userService.getUserByUser(u);
Shop shop0 = shopService.getByUserId(user.getUserId());
//验证码是否正确
Code code = new Code();
code.setNum((Integer)m.get("num"));
code.setCheckCode((String)m.get("checkCode"));
code.setEmail((String)m.get("email"));
Map<String, Object> map = codeService.checkCode(code);
if ((Integer)map.get("status") != 1){
return map;
}
//该邮箱是否已有店铺
if (shop0 != null){
map.put("status",5);
return map;
}
//该店铺名称是否已存在
String shopName = (String)m.get("shopName");
Shop shop = shopService.getByShopName(shopName);
if (shop != null){
map.put("status",4);//表示已有店铺
return map;
}
shopService.saveShop(shopName,user.getUserId());
codeService.updateCodeStatus((Integer)map.get("checkId"));
return map;
}
}
Service
package com.helei.service.impl;
import com.helei.dao.ShopDao;
import com.helei.pojo.Shop;
import com.helei.service.ShopService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service("shopService")
public class ShopServiceImpl implements ShopService {
@Resource(name = "shopDao")
private ShopDao shopDao;
/**
* 通过用户id查找店铺
* @param userId
* @return
*/
@Override
public Shop getByUserId(Integer userId) {
return shopDao.getByUserId(userId);
}
/**
* 通过店铺名称查找店铺
* @param shopName
* @return
*/
@Override
public Shop getByShopName(String shopName) {
return shopDao.getByShopName(shopName);
}
/**
* 通过店铺名和用户id创建店铺
* @param shopName
*/
@Override
public void saveShop(String shopName,Integer userId) {
shopDao.saveShop(shopName,userId);
}
}
dao
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.helei.dao.ShopDao">
<!-- 通过userId查询店铺-->
<select id="getByUserId" parameterType="Integer" resultType="Shop">
select * from shop_information where userid=#{userId}
</select>
<!-- 通过店铺名称查找店铺-->
<select id="getByShopName" resultType="Shop" parameterType="String">
select * from shop_information where shopname=#{shopName}
</select>
<!-- 新建店铺-->
<insert id="saveShop" >
insert into shop_information (shopname,userid) values (#{shopName},#{userId})
</insert>
</mapper>