首頁 > 軟體

Java實戰之小米交易商城系統的實現

2022-04-06 19:03:43

一、專案簡述

環境設定:

Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支援)

專案技術:

JSP +Spring + SpringMVC + MyBatis + html+ css + JavaScript + JQuery + Ajax + layui+ maven等等。

二、效果圖

三、核心程式碼

使用者購物車控制層

@CrossOrigin
@RestController
@RequestMapping("carts")
public class CartController {
 
    @Autowired
    private RegLogServiceImp regLogServiceImp;
    @Autowired
    private GoodServiceImp goodServiceImp;
    @Autowired
    private CartServiceImp cartServiceImp;
 
    @PostMapping("/push")
    public CartResult putShop(@RequestBody pushCart pushcart) throws IOException {
 
        CartResult cartResult = new CartResult();
        boolean success = TokenUtil.verify(pushcart.getToken());
        if (success) {
            cartResult.setStatus(0);
            User user = regLogServiceImp.getUserByUserName(pushcart.getUsername().trim());
            String userid = user.getUserid();
            CartGood cartGood = goodServiceImp.getGoodByid(userid, pushcart.getProductId());
            if (cartGood == null) {
                int goodcount = 1;
                CartGoodInsert goodByidinsert = goodServiceImp.getGoodByidinsert(pushcart.getProductId());
 
                goodServiceImp.insCartGood(userid, pushcart.getProductId(), goodByidinsert.getName(),
                        goodByidinsert.getPrice(), goodcount, goodByidinsert.getSubtitle(), goodByidinsert.getProductStock(),
                        goodByidinsert.getStatus(), true);
            } else {
                goodServiceImp.updateCartGood(userid, cartGood.getCount() + 1, pushcart.getProductId(), (cartGood.getCount() + 1) * cartGood.getPrice());
            }
 
            cartProductVoList cartProductVoList = cartServiceImp.getCartProduct(userid);
 
            int cartTotalPrice = 0;
            boolean selectedAll = true;
            int cartTotalQuantity = 0;
            for (cartProduct cartProduct : cartProductVoList.getList()) {
                cartTotalPrice += cartProduct.getProductTotalPrice();
                if (!cartProduct.isProductSelected()) {
                    selectedAll = false;
                }
                cartTotalQuantity += cartProduct.getCount();
            }
 
            cartResult.setStatus(0);
            cartResult.setCartTotalPrice(cartTotalPrice);
            cartResult.setCartTotalQuantity(cartTotalQuantity);
            cartResult.setData(cartProductVoList);
            cartResult.setImageHost("www.mi.com");
            cartResult.setSelectedAll(selectedAll);
        } else {
            cartResult.setStatus(10);
        }
        return cartResult;
    }
    @GetMapping("/sum")
    public CartResult getCartSum(@RequestParam("username") String username) throws IOException {
        CartResult cartResult = new CartResult();
 
        User user = regLogServiceImp.getUserByUserName(username);
        String userid = user.getUserid();
        cartProductVoList cartProductVoList = cartServiceImp.getCartProduct(userid);
        if(cartProductVoList==null){
            cartResult.setStatus(0);
            cartResult.setCartTotalQuantity(0);
        }
        else {
            int cartTotalQuantity=0;
            for(cartProduct cartProduct:cartProductVoList.getList()){
                cartTotalQuantity+=cartProduct.getCount();
            }
            cartResult.setStatus(0);
            cartResult.setCartTotalQuantity(cartTotalQuantity);
        }
    return cartResult;
    }
    @RequestMapping("/del")
    public CartResult delCarts(@RequestParam("id") int goodid,
                               @RequestParam("username") String username) throws IOException {
        User user = regLogServiceImp.getUserByUserName(username);
        String userid = user.getUserid();
        goodServiceImp.delGoodByid(userid,goodid);
        CartResult cartResult = getCarts(username);
        return cartResult;
    }
 
    @RequestMapping("/getcarts")
    public CartResult getCarts (@RequestParam("username") String username) throws IOException {
        CartResult cartResult = new CartResult();
        User user = regLogServiceImp.getUserByUserName(username);
        String userid = user.getUserid();
        cartProductVoList cartProductVoList = cartServiceImp.getCartProduct(userid);
        if(cartProductVoList==null){
            cartResult.setStatus(0);
            cartResult.setCartTotalQuantity(0);
            cartResult.setSelectedAll(false);
            cartResult.setImageHost("www.mi.com");
            cartResult.setCartTotalQuantity(0);
        }
        else {
            int carttotalPrice=0;
            boolean selectedall=true;
            int carttotalQuantity=0;
            for(cartProduct cartProduct:cartProductVoList.getList()){
                if(cartProduct.isProductSelected())
                    carttotalPrice+=cartProduct.getProductTotalPrice();
                if(!cartProduct.isProductSelected()){
                    selectedall=false;
                    }
                    carttotalQuantity+=cartProduct.getCount();
                }
 
                cartResult.setStatus(0);
                cartResult.setCartTotalPrice(carttotalPrice);
                cartResult.setCartTotalQuantity(carttotalQuantity);
                cartResult.setData(cartProductVoList);
                cartResult.setImageHost("www.mi.com");
                cartResult.setSelectedAll(selectedall);
            }
            return cartResult;
        }
 
        @GetMapping("/updatecart")
        public CartResult getCarts (
                @RequestParam("goodid") int  goodid,
                @RequestParam("username") String username,
                @RequestParam("quantity") int count,
                @RequestParam("selected") boolean selected
        ) throws IOException {
            User user = regLogServiceImp.getUserByUserName(username);
            String userid = user.getUserid();
 
            CartGood goodByid = goodServiceImp.getGoodByid(userid, goodid);
            goodServiceImp.updaCartNumSel(userid,goodid,count,selected,goodByid.getPrice()*count);
            CartResult carts = getCarts(username);
            return carts;
        }
 
        @GetMapping("/selectAll")
        public CartResult SelectAllCart(@RequestParam("username") String username) throws IOException {
            User user = regLogServiceImp.getUserByUserName(username);
            String userid = user.getUserid();
            goodServiceImp.updaCartSelect(userid);
            CartResult cartResult = getCarts(username);
            return cartResult;
        }
 
        @GetMapping("/unSelectAll")
        public CartResult unSelectAllCart(@RequestParam("username") String username) throws IOException {
            User user = regLogServiceImp.getUserByUserName(username);
            String userid = user.getUserid();
            goodServiceImp.updaCartunSelect(userid);
            CartResult cartResult = getCarts(username);
            cartResult.setCartTotalQuantity(0);
            cartResult.setCartTotalPrice(0);
            return cartResult;
    }
}

後臺登入控制層

@CrossOrigin
@RestController
@RequestMapping("Login")
public class LoginController {
 
    @Autowired
    private LoginServiceImp loginServiceImp;
 
    Map<String, String> keyMap = RSAUtils.createKeys(512);
//    String  publicKey = keyMap.get("publicKey");
//    String  privateKey = keyMap.get("privateKey");
//    private String publicKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIS-9P6aiWK-mNIJxz0R_XXIbHM21pS_ZncpQcE8T5Mu6P6sJfoi5Cw6S4jcSqxNbRs5FSUc59GAkU1Ek9onrz8CAwEAAQ";
    private String privateKey = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAhL70_pqJYr6Y0gnHPRH9dchsczbWlL9mdylBwTxPky7o_qwl-iLkLDpLiNxKrE1tGzkVJRzn0YCRTUST2ievPwIDAQABAkAviOHaOiRwQ6gdrnq2F7cXcknDiCOW6GMc0J1HQP2Nyn52HKtc9E9Ailm-eQesTw6P-qTsvHMyfuVYz8fLhfkhAiEAvhvoG2Dz6qSN6IHeuPboJP7YHK9zTt6M_NGCJKAenasCIQCywU7eStITcHFTRDjjShH0CfO-vR6q-zgWQPjtNI_YvQIgY8UNiODW4bu-ZdlSY8vghRnhnRxO7YNKhpwFKueFacUCIQCZ-P65ZVNs4ScMZTBnP48VWVk2S-rVto0KRi7lC6h4NQIgEa_GhBO6U2p6G5cGAR25ZiQVH9dFnrMVyPxY7P7-WsA";
 
    @PostMapping("/login")
    public LoginResult Login(@RequestBody User adminUser) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException {
        User user = loginServiceImp.getUserByUserName(adminUser.getUsername());
        LoginResult result = new LoginResult();
        LoginData loginData = new LoginData();
 
        if (user == null) {
            result.setMsg("無此使用者,請重新輸入正確使用者名稱");
            result.setStatus(2008);
        } else {
            if (RSAUtils.privateDecrypt(user.getPwd(), RSAUtils.getPrivateKey(privateKey)).equals(adminUser.getPwd())){
                result.setStatus(0);
                String token = TokenUtil.token(user.getId().trim());
                loginData.setToken(token);
                result.setMsg("登入成功");
                result.setStatus(1010);
                result.setData(loginData);
            }else{
                result.setStatus(2007);
                result.setMsg("密碼錯誤");
            }
        }
        return result;
    }
}

後臺商品管理控制層

@CrossOrigin
@RestController
@RequestMapping("Good")
public class GoodController {
 
    @Autowired
    private GoodServiceImp goodServiceImp;
 
    @GetMapping("/getGood")
    private GoodResult getGood(@RequestParam("pageIndex") int pageIndex,
                               @RequestParam("pageSize") int pageSize,
                               @RequestParam("name") String goodname
                               ) throws IOException {
        GoodResult goodResult = goodServiceImp.getGood(pageIndex,pageSize,goodname);
        return goodResult;
    }
 
    @GetMapping("/getGoodById")
    private GoodResult getGoodById(@RequestParam("category") int pageIndex,
                               @RequestParam("pageSize") int pageSize
    ) throws IOException {
        GoodResult goodResult = goodServiceImp.getGoodById(pageIndex,pageSize);
        return goodResult;
    }
    @RequestMapping("/getGoodByName")
    private GoodResult getGoodByName(@RequestParam("name") String goodname) throws IOException {
        GoodResult goodResult = goodServiceImp.getGoodByName(goodname);
        return goodResult;
    }
 
    @RequestMapping("/updGood")
    private GoodResult getGoodByName(@RequestParam("good_id") int goodid,
                                     @RequestParam("name") String goodname,
                                     @RequestParam("newhot") int newhot) throws IOException {
        GoodResult goodResult = goodServiceImp.updGood(goodid,goodname,newhot);
        return goodResult;
    }
 
 
    @GetMapping("/getGoodPicById")
    private GoodPicResult getGoodPicById(@RequestParam("category") int pageIndex,
                                   @RequestParam("pageSize") int pageSize
    ) throws IOException {
        GoodPicResult goodpicResult = goodServiceImp.getGoodPic(pageIndex,pageSize);
        return goodpicResult;
    }
    @RequestMapping("/getGoodPicByName")
    private GoodPicResult getGoodPicByName(@RequestParam("name") String goodname) throws IOException {
//        int categor = Integer.parseInt(category);
        GoodPicResult goodResult = goodServiceImp.getGoodPicByName(goodname);
        return goodResult;
    }
 
 
    @PostMapping("/pushGood")
    private GoodPushResult pushGood(@RequestBody good good) throws IOException {
        GoodPushResult goodPushResult = goodServiceImp.getGoodPush(good);
        return goodPushResult;
    }
 
    @PostMapping("/delGood")
    private void delGood(@RequestBody good good) throws IOException {
          goodServiceImp.delGood(good);
    }
 
    @PostMapping("/delMulGood")
    private void delMulGood(@RequestBody List<good> goods) throws IOException {
        goodServiceImp.delMulGood(goods);
    }
 
    @GetMapping("/getGoodPic")
    private GoodPicResult getGoodPic(@RequestParam("pageIndex") int pageIndex,
                                     @RequestParam("pageSize") int pageSize) throws IOException {
        GoodPicResult goodPicResult = goodServiceImp.getGoodPic(pageIndex,pageSize);
        return goodPicResult;
    }
 
    @PostMapping("/updGoodPic")
    private void upGoodPic(@RequestBody GoodPic goodPic) throws IOException {
        int goodid= goodPic.getGood_id();
        String icon= goodPic.getIcon();
        String imageHead = goodPic.getImageHead();
        String imageFoot = goodPic.getImageFoot();
 
        goodServiceImp.updGoodPic(goodid,icon,1);
        goodServiceImp.updGoodPic(goodid,imageHead,2);
        goodServiceImp.updGoodPic(goodid,imageFoot,3);
    }
}

後臺品類控制層

@CrossOrigin
@RestController
@RequestMapping("category")
public class CategoryController {
 
    @Autowired
    private CategServiceImp categServiceImp;
    @GetMapping("/getCategory")
        private CateResult getCategory(@RequestParam("pageIndex") int pageIndex,
                                    @RequestParam("pageSize") int pageSize) throws IOException {
        CateResult cateResult = categServiceImp.getCategory(pageIndex, pageSize);
        return cateResult;
    }
 
    @PostMapping("/updateCategory")
    private void updateCategory(@RequestBody Category category) throws IOException {
        categServiceImp.updCategory(category.getName(),category.getCategoryId());
    }
}

以上就是Java實戰之小米交易商城系統的實現的詳細內容,更多關於Java交易商城系統的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com