SSM到Spring Boot-從零開發校園商鋪平九(前端展示系統)

首頁輪播圖

HeadLineDao.java

public interface HeadLineDao {
    /**
     * 根據傳入的查詢條件(頭條名查詢頭條)
     */
    List<HeadLine> queryHeadLine(@Param("headLineCondition")HeadLine
            headLineCondition);
}

HeadLineDao.xml

<?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.imooc.o2o.dao.HeadLineDao">
    <select id="queryHeadLine"
            resultType="com.imooc.o2o.entity.HeadLine">
        select
        line_id,
        line_name,
        line_link,
        line_img,
        priority,
        enable_status,
        create_time,
        last_edit_time
        from
        tb_head_line
        <where>
            <if test="headLineCondition.enableStatus!=null">
                and enable_status =#{headLineCondition.enableStatus}
            </if>
        </where>
        order by
        priority desc
    </select>
</mapper>

HeadLineService

public interface HeadLineSevice {
    /**
     * 根據傳入的條件返回指定的頭條列表
     */
    List<HeadLine> getHeadLine(HeadLine headLineCondition)throws IOException;
}

 HeadLineServiceImpl

@Service
public class HeadLineServiceImpl implements HeadLineSevice {
    @Autowired
    private HeadLineDao headLineDao;

    public List<HeadLine> getHeadLine(HeadLine headLindCondition)throws IOException{
        return headLineDao.queryHeadLine(headLindCondition);
    }

店鋪類別展示

ShopCategoryDao

public interface ShopCategoryDao {
    List<ShopCategory> queryShopCategory(@Param("shopCategoryCondition") ShopCategory shopCategoryCondition);
}

shopCategoryDao.xml

    <select id="queryShopCategory"
            resultType="com.imooc.o2o.entity.ShopCategory">
        select
        shop_category_id,
        shop_category_name,
        shop_category_desc,
        shop_category_img,
        priority,
		create_time,
		last_edit_time,
		parent_id
		FROM
		tb_shop_category
		<where>
		    <if test="shopCategoryCondition==null">
                and parent_id is null
            </if>
		    <if test="shopCategoryCondition!=null">
                and parent_id is not null
            </if>
            <if
                    test="shopCategoryCondition!=null and shopCategoryCondition.parent!=null">
                and parent_id = #{shopCategoryCondition.parent.shopCategoryId}
            </if>
        </where>
        order by
        priority desc
    </select>

 ShopCategoryService

public interface ShopCategoryService {
    List<ShopCategory> getShopCategoryList(ShopCategory shopCategoryCondition) throws IOException;
}

ShopCategoryServiceImpl

package com.imooc.o2o.service.impl;

import com.imooc.o2o.dao.ShopCategoryDao;
import com.imooc.o2o.entity.ShopCategory;
import com.imooc.o2o.service.ShopCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class ShopCategoryServiceImpl implements ShopCategoryService {

    @Autowired
    private ShopCategoryDao shopCategoryDao;

    /**
     * 根據查詢條件獲取ShopCategory列表
     * @param shopCategoryCondition
     * @return
     * @throws IOException
     */
    @Override
    public List<ShopCategory> getShopCategoryList(ShopCategory shopCategoryCondition) throws IOException {
        return shopCategoryDao.queryShopCategory(shopCategoryCondition);
    }
}

 MainPageController

@Controller
@RequestMapping("/frontend")
public class MainPageController {
    @Autowired
    private ShopCategoryService shopCategoryService;

    @Autowired
    private HeadLineSevice headLineSevice;

    /**
     * 初始化前端展示系統的主頁信息,包括獲取一級店鋪類別列表以及頭條列表
     *
     */
    @RequestMapping(value = "/listmainpageinfo",method = RequestMethod.GET)
    @ResponseBody
    private Map<String,Object> listMainPageInfo(){
        Map<String,Object> modelMap = new HashMap<String, Object>();
        List<ShopCategory> shopCategoryList =new ArrayList<ShopCategory>();

        try{
            //獲取一級店鋪類別列表(即parentId爲空的ShopCategory)
            shopCategoryList = shopCategoryService.getShopCategoryList(null);
            modelMap.put("shopCategoryList",shopCategoryList);
        }catch (Exception e){
            modelMap.put("success",false);
            modelMap.put("errMsg",e.getMessage());
            return modelMap;
        }
        List<HeadLine> headLineList = new ArrayList<HeadLine>();

        try{
            //獲取狀態爲可用(1)的頭條列表
            HeadLine headLineCondition= new HeadLine();
            headLineCondition.setEnableStatus(1);
            headLineList= headLineSevice.getHeadLine(headLineCondition);
            modelMap.put("headLineList",headLineList);
        }catch (Exception e){
            modelMap.put("success",false);
            modelMap.put("errMsg",e.getMessage());
            return modelMap;
        }
        modelMap.put("success",true);
        return modelMap;


    }
}

 frontend/index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>我的生活</title>
    <meta name="viewport" content="initial-scale=1, maximum-scale=1">
    <link rel="shortcut icon" href="/favicon.ico">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="stylesheet"
          href="//g.alicdn.com/msui/sm/0.6.2/css/sm.min.css">
    <link rel="stylesheet"
          href="//g.alicdn.com/msui/sm/0.6.2/css/sm-extend.min.css">
    <link rel="stylesheet" href="../resources/css/frontend/index/index.css">
</head>
<body>
<div class="page-group">
    <div class="page">
        <header class="bar bar-nav">
            <!-- <a class="button button-link button-nav pull-left" href="/demos/card" data-transition='slide-out'>
                  <span class="icon icon-left"></span>
                  返回
              </a> -->
            <h1 class="title">My School O2O</h1>
        </header>
        <nav class="bar bar-tab">
            <a class="tab-item active" href="#"> <span
                    class="icon icon-home"></span> <span class="tab-label">首頁</span>
            </a> <a class="tab-item" href="#" id='me'> <span class="icon icon-me"></span>
            <span class="tab-label">我</span>
        </a>
        </nav>
        <div class="content">
            <!-- 這裏是頁面內容區 -->
            <div class="swiper-container index-banner" data-space-between='10'>
                <div class="swiper-wrapper">
                    <!-- <div class="swiper-slide img-wrap">
                            <img class="banner-img" src="//gqianniu.alicdn.com/bao/uploaded/i4//tfscom/i1/TB1n3rZHFXXXXX9XFXXXXXXXXXX_!!0-item_pic.jpg_320x320q60.jpg" alt="">
                        </div>
                        <div class="swiper-slide img-wrap">
                            <img class="banner-img" src="//gqianniu.alicdn.com/bao/uploaded/i4//tfscom/i4/TB10rkPGVXXXXXGapXXXXXXXXXX_!!0-item_pic.jpg_320x320q60.jpg" alt="">
                        </div>
                        <div class="swiper-slide img-wrap">
                            <img class="banner-img" src="//gqianniu.alicdn.com/bao/uploaded/i4//tfscom/i1/TB1kQI3HpXXXXbSXFXXXXXXXXXX_!!0-item_pic.jpg_320x320q60.jpg" alt="">
                        </div> -->
                </div>
                <div class="swiper-pagination"></div>
            </div>
            <div class='total-shop-button'>
                <a href="/frontend/shoplist" external>全部商店</a>
            </div>
            <div class="row">
                <!-- <div class="col-50 shop-classify">
                        <div class='word'>
                            <p class='shop-title'>本期推薦</p>
                            <p class='shop-desc'>近期相關活動、新款上市、旅遊資訊</p>
                        </div>
                        <div class='shop-classify-img-warp'>
                            <img class='shop-img' src="static/index/display13.png">
                        </div>
                    </div> -->
            </div>
        </div>
    </div>
    <!--側邊欄  -->
    <div class="panel-overlay"></div>
    <div class="panel panel-right panel-reveal" id="panel-left-demo">
        <div class="content-block">
            <p>
                <a href="/frontend/myrecord" class="close-panel">消費記錄</a>
            </p>
            <p>
                <a href="/frontend/mypoint" class="close-panel">我的積分</a>
            </p>
            <p>
                <a href="/frontend/pointrecord" class="close-panel">積分兌換記錄</a>
            </p>
            <!-- Click on link with "close-panel" class will close panel -->
        </div>
    </div>
</div>



<script type='text/javascript'
        src='//g.alicdn.com/sj/lib/zepto/zepto.min.js' charset='utf-8'></script>
<script type='text/javascript'
        src='//g.alicdn.com/msui/sm/0.6.2/js/sm.min.js' charset='utf-8'></script>
<script type='text/javascript'
        src='//g.alicdn.com/msui/sm/0.6.2/js/sm-extend.min.js' charset='utf-8'></script>
<script type='text/javascript' src='../resources/js/frontend/index.js'
        charset='utf-8'></script>
</body>
</html>

frontend/index.js

$(function() {
    //定義訪問後臺,獲取頭條列表以及一級類別列表的URL
    var url = '/frontend/listmainpageinfo';
    //訪問後臺,獲取頭條列表以及一級類別列表
    $.getJSON(url, function (data) {
        if (data.success) {
            //獲取後臺傳遞過來的頭條列表
            var headLineList = data.headLineList;
            var swiperHtml = '';
            //遍歷頭條列表,並拼接出輪播圖組
            headLineList.map(function (item, index) {
                swiperHtml += ''
                    + '<div class="swiper-slide img-wrap">'
                    +      '<img class="banner-img" src="'+ item.lineImg +'" alt="'+ item.lineName +'">'
                    + '</div>';
            });
            //將輪播圖組賦值給前端HTML控件
            $('.swiper-wrapper').html(swiperHtml);
            //設定輪播圖輪換時間爲3秒
            $(".swiper-container").swiper({
                autoplay: 1000,
                //用戶對輪播圖進行操作時,是否自動停止autoplay
                autoplayDisableOnInteraction: false
            });
            //獲取後臺傳遞過來的大類列表
            var shopCategoryList = data.shopCategoryList;
            var categoryHtml = '';
            //遍歷大類列表,拼接出兩兩(col-50)一行的類別
            shopCategoryList.map(function (item, index) {
                categoryHtml += ''
                    +  '<div class="col-50 shop-classify" data-category='+ item.shopCategoryId +'>'
                    +      '<div class="word">'
                    +          '<p class="shop-title">'+ item.shopCategoryName +'</p>'
                    +          '<p class="shop-desc">'+ item.shopCategoryDesc +'</p>'
                    +      '</div>'
                    +      '<div class="shop-classify-img-warp">'
                    +          '<img class="shop-img" src="'+ item.shopCategoryImg +'">'
                    +      '</div>'
                    +  '</div>';
            });
            //將拼接好的類別賦值給前端HTML控件進行展示
            $('.row').html(categoryHtml);
        }
    });
    //若點擊我的,則顯示側欄
    $('#me').click(function () {
        $.openPanel('#panel-left-demo');
    });

    $('.row').on('click', '.shop-classify', function (e) {
        var shopCategoryId = e.currentTarget.dataset.category;
        var newUrl = '/myo2o/frontend/shoplist?parentId=' + shopCategoryId;
        window.location.href = newUrl;
    });

});

frontend/css/index.css

.index-banner {
height: 35%;
padding-bottom: 0.4rem;
}
.img-wrap {
overflow: hidden;
}
.banner-img {
width: 100%;
height: 100%;
}
.total-shop-button {
height: 1.5rem;
line-height: 1.5rem;
padding-left: 0.85rem;
margin-bottom: 0.4rem;
position: relative;
cursor: pointer;
}
.total-shop-button:before {
content: '';
display: inline-block;
position: absolute;
left: 0;
width: 0.15rem;
height: 1.5rem;
background-color: #0894ec;
}
.shop-classify {
height: 3.3rem;
padding: 0.2rem;
cursor: pointer;
}
.shop-classify > .word {
width: 65%;
height: 100%;
overflow: hidden;
float: left;
}
.shop-classify > .word > p {
margin: 0;
}
.shop-classify > .word > .shop-title {
margin: 0;
font-size: 0.8rem;
}
.shop-classify > .word > .shop-desc {
margin: 0;
font-size: 0.4rem;
}
.shop-classify > .shop-classify-img-warp {
width: 30%;
height: 100%;
margin-left: 0.2rem;
display: inline-block;
}
.shop-classify > .shop-classify-img-warp > .shop-img {
width: 100%;
height: 100%;
}

店鋪列表和詳情展示

shopDao

    /**
     * 分頁查詢店鋪,可輸入的條件有,店鋪名(模糊)查詢,店鋪狀態,店鋪類別,區域id,owner
     * @param shopCondition
     * @param roeIndex 從第幾行開始取數據
     * @param pageSize 返回的條數
     * @return
     */
    List<Shop> queryShopList(@Param("shopCondition")Shop shopCondition,
                             @Param("rowIndex")int roeIndex,
                             @Param("pageSize")int pageSize);

    /**
     * 返回queryShopList總數
     * @param shopCondition
     * @return
     */
    int queryShopCount(@Param("shopCondition")Shop shopCondition);

shopDao.xml

<select id="queryShopList" resultMap="shopMap">
        SELECT
        s.shop_id,
        s.shop_name,
        s.shop_desc,
        s.shop_addr,
        s.phone,
        s.shop_img,
        s.create_time,
        s.last_edit_time,
        s.enable_status,
        s.advice,
        a.area_id,
        a.area_name,
        sc.shop_category_id,
        sc.shop_category_name
        FROM
        tb_shop s,
        tb_area a,
        tb_shop_category sc
        <where>
            <if test="shopCondition.shopCategory!=null
				 and shopCondition.shopCategory.shopCategoryId!=null">
                and s.shop_category_id =
                #{shopCondition.shopCategory.shopCategoryId}
            </if>
            <!-- 取出父類類別下的店鋪  -->
            <if test="shopCondition.shopCategory != null and shopCondition.shopCategory.parent != null
		and shopCondition.shopCategory.parent.shopCategoryId != null">
                and s.shop_category_id in (SELECT shop_category_id FROM tb_shop_category
                WHERE parent_id = #{shopCondition.shopCategory.parent.shopCategoryId})
            </if>
            <if test="shopCondition.area!=null
				 and shopCondition.area.areaId!=null">
                and s.area_id =
                #{shopCondition.area.areaId}
            </if>
            <!-- 寫like語句的時候 一般都會寫成 like '% %' 在mybatis裏面寫就是應該是 like '%${name} %' 而不是
    '%#{name} %' ${name} 是不帶單引號的,而#{name} 是帶單引號的 -->
            <if test="shopCondition.shopName!=null">
                and s.shop_name like '%${shopCondition.shopName}%'
            </if>
            <if test="shopCondition.enableStatus!=null">
                and s.enable_status = #{shopCondition.enableStatus}
            </if>

            <if test="shopCondition.owner!=null and shopCondition.owner.userId!=null">
                and s.owner_id=#{shopCondition.owner.userId}
            </if>
            and
            s.area_id=a.area_id
            and
            s.shop_category_id=sc.shop_category_id
        </where>
        ORDER BY
        s.priority DESC
        LIMIT #{rowIndex},#{pageSize};
    </select>

    <select id="queryShopCount" resultType="int">
        SELECT
        count(1)
        FROM
        tb_shop s,
        tb_area a,
        tb_shop_category sc
        <where>
            <if test="shopCondition.shopCategory!=null
				 and shopCondition.shopCategory.shopCategoryId!=null">
                and s.shop_category_id =
                #{shopCondition.shopCategory.shopCategoryId}
            </if>
            <!-- 取出父類類別下的店鋪  -->
            <if test="shopCondition.shopCategory != null and shopCondition.shopCategory.parent != null
		and shopCondition.shopCategory.parent.shopCategoryId != null">
                and s.shop_category_id in (SELECT shop_category_id FROM tb_shop_category
                WHERE parent_id = #{shopCondition.shopCategory.parent.shopCategoryId})
            </if>
            <if test="shopCondition.area!=null
				 and shopCondition.area.areaId!=null">
                and s.area_id =
                #{shopCondition.area.areaId}
            </if>
            <!-- 寫like語句的時候 一般都會寫成 like '% %' 在mybatis裏面寫就是應該是 like '%${name} %' 而不是
    '%#{name} %' ${name} 是不帶單引號的,而#{name} 是帶單引號的 -->
            <if test="shopCondition.shopName!=null">
                and s.shop_name like '%${shopCondition.shopName}%'
            </if>
            <if test="shopCondition.enableStatus!=null">
                and s.enable_status = #{shopCondition.enableStatus}
            </if>
            <if test="shopCondition.owner!=null and shopCondition.owner.userId!=null">
                and s.owner_id=#{shopCondition.owner.userId}
            </if>
            and
            s.area_id=a.area_id
            and
            s.shop_category_id=sc.shop_category_id
        </where>
    </select>

 shopServiceImpl

@Override
    public ShopExecution getShopList(Shop shopCondition, int pageIndex, int pageSize) {
        //得到從第幾列開始
        int rowIndex = PageCalculator.calculateRowIndex(pageIndex,pageSize);
        List<Shop> shopList = shopDao.queryShopList(shopCondition,rowIndex,
                pageSize);
        //獲取總數
        int count = shopDao.queryShopCount(shopCondition);
        ShopExecution se = new ShopExecution();
        if(shopList!=null){
            se.setShopList(shopList);
            se.setCount(count);
        }else{
            se.setState(ShopStateEnum.INNER_ERROR.getState());
        }
        return se;
    }

ShopListController

@Controller
@RequestMapping("/frontend")
public class ShopListController {

    @Autowired
    private AreaService areaService;

    @Autowired
    private ShopCategoryService shopCategoryService;

    @Autowired
    private ShopService shopService;

    /**
     * 返回商品列表中的ShopCategory列表(二級或者一級),一級區域信息列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/listshopspageinfo", method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> listShopsPageInfo(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        long parentId = HttpServletRequestUtil.getLong(request, "parentId");
        List<ShopCategory> shopCategoryList = null;
        if (parentId != -1) {
            // 如果parentId存在,則取出該一級ShopCategory下的二級ShopCategory列表
            try {
                ShopCategory shopCategoryCondition = new ShopCategory();
                ShopCategory parent = new ShopCategory();
                parent.setShopCategoryId(parentId);
                shopCategoryCondition.setParent(parent);
                shopCategoryList = shopCategoryService.getShopCategoryList(shopCategoryCondition);
            } catch (Exception e) {
                modelMap.put("success", false);
                modelMap.put("errMsg", e.getMessage());
            }
        } else {
            // 如果parentId不存在,則取出所有一級ShopCategory(用戶首頁選擇的是全部商店列表)
            try {
                shopCategoryList = shopCategoryService.getShopCategoryList(null);
            } catch (Exception e) {
                modelMap.put("success", false);
                modelMap.put("errMsg", e.getMessage());
            }
        }
        modelMap.put("shopCategoryList", shopCategoryList);
        List<Area> areaList = null;
        try {
            //取出區域信息
            areaList = areaService.getAreaList();
            modelMap.put("areaList", areaList);
            modelMap.put("success", true);
            return modelMap;
        } catch (Exception e) {
            modelMap.put("success", false);
            modelMap.put("errMsg", e.getMessage());
        }
        return modelMap;
    }

    /**
     * 獲取指定查詢條件下的店鋪列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/listshops", method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> listShops(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        //獲取頁碼
        int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");
        //獲取一頁需要顯示的數據條數
        int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
        //非空判斷
        if ((pageIndex > -1) && (pageSize > -1)) {
            // 獲取一級類別id
            long parentId = HttpServletRequestUtil.getLong(request, "parentId");
            // 獲取特定二級類別id
            long shopCategoryId = HttpServletRequestUtil.getLong(request, "shopCategoryId");
            // 獲取區域id
            int areaId = HttpServletRequestUtil.getInt(request, "areaId");
            // 模糊查詢的名字
            String shopName = HttpServletRequestUtil.getString(request, "shopName");
            //獲取組合之後的查詢條件
            Shop shopCondition = compactShopCondition4Search(parentId, shopCategoryId, areaId, shopName);
            //根據查詢條件和分頁信息獲取店鋪列表,並返回總數
            ShopExecution se = shopService.getShopList(shopCondition, pageIndex, pageSize);
            modelMap.put("shopList", se.getShopList());
            modelMap.put("count", se.getCount());
            modelMap.put("success", true);
        } else {
            modelMap.put("success", false);
            modelMap.put("errMsg", "empty pageSize or pageIndex");
        }

        return modelMap;
    }

    /**
     * 拼接查詢組合條件
     *
     * @param parentId
     * @param shopCategoryId
     * @param areaId
     * @param shopName
     * @return
     */
    private Shop compactShopCondition4Search(long parentId, long shopCategoryId, int areaId, String shopName) {
        Shop shopCondition = new Shop();
        if (parentId != -1L) {
            // 查詢某個一級ShopCategory下面的所有二級ShopCategory裏面的店鋪列表
            ShopCategory childCategory = new ShopCategory();
            ShopCategory parentCategory = new ShopCategory();
            parentCategory.setShopCategoryId(parentId);
            childCategory.setParent(parentCategory);
            shopCondition.setShopCategory(childCategory);
        }
        if (shopCategoryId != -1L) {
            // 查詢某個二級ShopCategory下面的店鋪列表
            ShopCategory shopCategory = new ShopCategory();
            shopCategory.setShopCategoryId(shopCategoryId);
            shopCondition.setShopCategory(shopCategory);
        }
        if (areaId != -1L) {
            // 查詢某個區域id下的店鋪列表
            Area area = new Area();
            area.setAreaId(areaId);
            shopCondition.setArea(area);
        }

        if (shopName != null && !shopName.equalsIgnoreCase("null")) {
            shopCondition.setShopName(shopName);
        }
        // 展示審覈通過的店鋪
        shopCondition.setEnableStatus(1);
        return shopCondition;
    }
}

ShopDetailController

@Controller
@RequestMapping("/frontend")
public class ShopDetailController {

    @Autowired
    private ShopService shopService;

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 獲取店鋪信息以及店鋪下的信息商品類別信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/listShopDetailPageInfo", method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> listShopDetailPageInfo(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        long shopId = HttpServletRequestUtil.getLong(request, "shopId");
        Shop shop = null;
        List<ProductCategory> productCategoryList = null;
        if (shopId != -1) {
            shop = shopService.getByShopId(shopId);
            productCategoryList = productCategoryService.getProductCategoryList(shopId);
            modelMap.put("shop", shop);
            modelMap.put("productCategoryList", productCategoryList);
            modelMap.put("success", true);
        } else {
            modelMap.put("success", false);
            modelMap.put("errMsg", "empty shopId");
        }
        return modelMap;
    }

    @RequestMapping(value = "/listProductsByShop", method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> listProductsByShop(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");
        int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
        long shopId = HttpServletRequestUtil.getLong(request, "shopId");
        if ((pageIndex > -1) && (pageSize > -1) && (shopId > -1)) {
            long productCategoryId = HttpServletRequestUtil.getLong(request, "productCategoryId");
            String productName = HttpServletRequestUtil.getString(request, "productName");
            Product productCondition = compactProductCondition4Search(shopId, productCategoryId, productName);
            ProductExecution pe = productService.getProductList(productCondition, pageIndex, pageSize);
            modelMap.put("productList", pe.getProductList());
            modelMap.put("count", pe.getCount());
            modelMap.put("success", true);
        } else {
            modelMap.put("success", false);
            modelMap.put("errMsg", "empty pageSize or pageIndex or shopId");
        }
        return modelMap;
    }

    /**
     * 組合查詢條件
     *
     * @param shopId
     * @param productCategoryId
     * @param productName
     * @return
     */
    private Product compactProductCondition4Search(long shopId, long productCategoryId, String productName) {
        Product productCondition = new Product();
        Shop shop = new Shop();
        shop.setShopId(shopId);
        productCondition.setShop(shop);
        if (productCategoryId != -1L) {
            ProductCategory productCategory = new ProductCategory();
            productCategory.setProductCategoryId(productCategoryId);
            productCondition.setProductCategory(productCategory);
        }
        if (productName != null && !productName.equalsIgnoreCase("null")) {
            productCondition.setProductName(productName);
        }
        productCondition.setEnableStatus(1);
        return productCondition;
    }
}

shop-list.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商店列表</title>
    <meta name="viewport" content="initial-scale=1, maximum-scale=1">
    <link rel="shortcut icon" href="/favicon.ico">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="stylesheet"
          href="//g.alicdn.com/msui/sm/0.6.2/css/sm.min.css">
    <link rel="stylesheet"
          href="//g.alicdn.com/msui/sm/0.6.2/css/sm-extend.min.css">
    <link rel="stylesheet" href="../resources/css/frontend/shop-list.css">
</head>
<body>
<div class="page-group">
    <div class="page">
        <header class="bar bar-nav">
            <a class="button button-link button-nav pull-left" external
               href="index" data-transition='slide-out'> <span
                    class="icon icon-left"></span> 返回
            </a>
            <h1 class="title">商店列表</h1>
        </header>
        <div class="bar bar-header-secondary">
            <div class="searchbar">
                <a class="searchbar-cancel">取消</a>
                <!-- 搜索欄 -->
                <div class="search-input">
                    <label class="icon icon-search" for="search"></label> <input
                        type="search" id='search' placeholder='輸入關鍵字...' />
                </div>
            </div>
        </div>
        <nav class="bar bar-tab">
            <a class="tab-item" href="/o2o/frontend/index"> <span
                    class="icon icon-home"></span> <span class="tab-label">首頁</span>
            </a> <a class="tab-item" href="#" id="me"> <span
                class="icon icon-me"></span> <span class="tab-label">我</span>
        </a>
        </nav>
        <div class="content infinite-scroll infinite-scroll-bottom"
             data-distance="100">
            <!-- 類別列表展示區 -->
            <div class="shoplist-button-div" id="shoplist-search-div">
                <!-- <a href="#" class="button">所有貨物</a>
                    <a href="#" class="button">吃的</a>
                    <a href="#" class="button">喝的</a>
                    <a href="#" class="button">Usual Button 1</a>
                    <a href="#" class="button">Usual Button 1</a>
                    <a href="#" class="button">Usual Button 1</a> -->
            </div>
            <div class="select-wrap">
                <!-- 區域列表展示區 -->
                <select class="select" id="area-search"></select>
            </div>
            <!-- 店鋪列表 -->
            <div class="list-div shop-list">
                <!-- <div class="card">
                        <div class="card-header">傳統火鍋店</div>
                        <div class="card-content">
                            <div class="list-block media-list">
                                <ul>
                                    <li class="item-content">
                                        <div class="item-media">
                                            <img src="http://gqianniu.alicdn.com/bao/uploaded/i4//tfscom/i3/TB10LfcHFXXXXXKXpXXXXXXXXXX_!!0-item_pic.jpg_250x250q60.jpg" width="44">
                                        </div>
                                        <div class="item-inner">
                                            <div class="item-subtitle"></div>
                                        </div>
                                    </li>
                                </ul>
                            </div>
                        </div>
                        <div class="card-footer">
                            <span>2015/01/15</span>
                            <span>5 評論</span>
                        </div>
                    </div> -->
            </div>
            <div class="infinite-scroll-preloader">
                <div class="preloader"></div>
            </div>
        </div>
    </div>
</div>
<!--側邊欄  -->
<div class="panel-overlay"></div>
<div class="panel panel-right panel-reveal" id="panel-left-demo">
    <div class="content-block">
        <p>
            <a href="/o2o/frontend/myRecord" class="close-panel">消費記錄</a>
        </p>
        <p>
            <a href="/o2o/frontend/myPoint" class="close-panel">我的積分</a>
        </p>
        <p>
            <a href="/o2o/frontend/pointRecord" class="close-panel">積分兌換記錄</a>
        </p>
        <!-- Click on link with "close-panel" class will close panel -->
    </div>
</div>

<script type='text/javascript'
        src='//g.alicdn.com/sj/lib/zepto/zepto.min.js' charset='utf-8'></script>
<script type='text/javascript'
        src='//g.alicdn.com/msui/sm/0.6.2/js/sm.min.js' charset='utf-8'></script>
<script type='text/javascript'
        src='//g.alicdn.com/msui/sm/0.6.2/js/sm-extend.min.js' charset='utf-8'></script>
<script type='text/javascript' src='../resources/js/common/commonutil.js'
        charset='utf-8'></script>
<script type='text/javascript'
        src='../resources/js/frontend/shop-list.js' charset='utf-8'></script>
</body>
</html>

shop-detail.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商店詳情</title>
    <meta name="viewport" content="initial-scale=1, maximum-scale=1">
    <link rel="shortcut icon" href="/favicon.ico">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="stylesheet"
          href="//g.alicdn.com/msui/sm/0.6.2/css/sm.min.css">
    <link rel="stylesheet"
          href="//g.alicdn.com/msui/sm/0.6.2/css/sm-extend.min.css">
    <link rel="stylesheet" href="../resources/css/frontend/shop-detail.css">
</head>
<body>
<div class="page-group">
    <div class="page">
        <header class="bar bar-nav">
            <a class="button button-link button-nav pull-left" href="#"
               onClick="javascript :history.back(-1);" data-transition='slide-out'>
                <span class="icon icon-left"></span> 返回
            </a>
            <h1 class="title" id="shop-name">店鋪詳情</h1>
        </header>
        <nav class="bar bar-tab">
            <a class="tab-item" href="/o2o/frontend/index"> <span
                    class="icon icon-home"></span> <span class="tab-label">首頁</span>
            </a> <a class="tab-item" href="#" id="me"> <span
                class="icon icon-me"></span> <span class="tab-label">我</span>
        </a>
        </nav>
        <div class="content infinite-scroll infinite-scroll-bottom"
             data-distance="100">
            <!-- 這裏是頁面內容區 -->
            <div class="shop-detail-dev">
                <div class="card">
                    <div valign="bottom"
                         class="card-header color-white no-border no-padding">
                        <img class='card-cover' id="shop-cover-pic" src="" alt="">
                    </div>
                    <div class="card-content">
                        <div class="card-content-inner">
                            <p class="color-gray">
                                <span id="shop-update-time"></span>
                            </p>
                            <p id="shop-name"></p>
                            <p id="shop-desc"></p>
                            <p id="shop-addr"></p>
                            <p id="shop-phone"></p>
                        </div>
                    </div>
                    <div class="card-footer">
                        <!-- <a href="#" class="link">贊</a> -->
                        <!-- <a href="#" class="link">更多</a> -->
                    </div>
                </div>
            </div>
            <div class="shopdetail-button-div" id="shopdetail-button-div">
                <!-- <a href="#" class="button">所有貨物</a>
                    <a href="#" class="button">吃的</a>
                    <a href="#" class="button">喝的</a>
                    <a href="#" class="button">Usual Button 1</a>
                    <a href="#" class="button">Usual Button 1</a>
                    <a href="#" class="button">Usual Button 1</a> -->
            </div>
            <!-- 搜索區 -->
            <div class="detail-search">
                <div class="searchbar">
                    <a class="searchbar-cancel">取消</a>
                    <div class="search-input">
                        <label class="icon icon-search" for="search"></label> <input
                            type="search" id='search' placeholder='輸入關鍵字...' />
                    </div>
                </div>
            </div>
            <div class="list-div">
                <!-- <div class="card">
                        <div class="card-header">傳統火鍋店</div>
                        <div class="card-content">
                            <div class="list-block media-list">
                                <ul>
                                    <li class="item-content">
                                        <div class="item-media">
                                            <img src="http://gqianniu.alicdn.com/bao/uploaded/i4//tfscom/i3/TB10LfcHFXXXXXKXpXXXXXXXXXX_!!0-item_pic.jpg_250x250q60.jpg" width="44">
                                        </div>
                                        <div class="item-inner">
                                            <div class="item-subtitle"></div>
                                        </div>
                                    </li>
                                </ul>
                            </div>
                        </div>
                        <div class="card-footer">
                            <span>2015/01/15</span>
                            <span>5 評論</span>
                        </div>
                    </div> -->
            </div>
            <div class="infinite-scroll-preloader">
                <div class="preloader"></div>
            </div>
        </div>
    </div>

    <!--側邊欄  -->
    <div class="panel-overlay"></div>
    <div class="panel panel-right panel-reveal" id="panel-left-demo">
        <div class="content-block">
            <p>
                <a href="/o2o/frontend/myRecord" class="close-panel">消費記錄</a>
            </p>
            <p>
                <a href="/o2o/frontend/myPoint" class="close-panel">我的積分</a>
            </p>
            <p>
                <a href="/o2o/frontend/pointRecord" class="close-panel">積分兌換記錄</a>
            </p>
            <!-- Click on link with "close-panel" class will close panel -->
        </div>
    </div>
</div>


<script type='text/javascript'
        src='//g.alicdn.com/sj/lib/zepto/zepto.min.js' charset='utf-8'></script>
<script type='text/javascript'
        src='//g.alicdn.com/msui/sm/0.6.2/js/sm.min.js' charset='utf-8'></script>
<script type='text/javascript'
        src='//g.alicdn.com/msui/sm/0.6.2/js/sm-extend.min.js' charset='utf-8'></script>
<script type='text/javascript'
        src='../resources/js/common/commonutil.js' charset='utf-8'></script>
<script type='text/javascript'
        src='../resources/js/frontend/shop-detail.js' charset='utf-8'></script>
</body>
</html>

shop-list.js

$(function() {
    var loading = false;
    // 允許返回的最大條數
    var maxItems = 999;
    // 一頁的返回最大條數
    var pageSize = 10;
    // 獲取店鋪列表的URL
    var listUrl = '/frontend/listshops';
    // 獲取店鋪裏誒包及區域列表的URL
    var searchDivUrl = '/frontend/listshopspageinfo';
    // 頁碼
    var pageNum = 1;
    //從地址欄url裏嘗試獲取parent shop category id
    var parentId = getQueryString('parentId');
    var areaId = '';
    var shopCategoryId = '';
    var shopName = '';

    getSearchDivData();

    // 預先加載10條
    addItems(pageSize, pageNum);

    function getSearchDivData() {
        // 如果傳入了parentId,則取出一級類別下面的所有二級類別
        var url = searchDivUrl + '?' + 'parentId=' + parentId;
        $
            .getJSON(
                url,
                function(data) {
                    if (data.success) {
                        var shopCategoryList = data.shopCategoryList;
                        var html = '';
                        html += '<a href="#" class="button" data-category-id=""> 全部類別  </a>';
                        shopCategoryList
                            .map(function(item, index) {
                                html += '<a href="#" class="button" data-category-id='
                                    + item.shopCategoryId
                                    + '>'
                                    + item.shopCategoryName
                                    + '</a>';
                            });
                        $('#shoplist-search-div').html(html);
                        var selectOptions = '<option value="">全部街道</option>';
                        var areaList = data.areaList;
                        areaList.map(function(item, index) {
                            selectOptions += '<option value="'
                                + item.areaId + '">'
                                + item.areaName + '</option>';
                        });
                        $('#area-search').html(selectOptions);
                    }
                });
    }

    function addItems(pageSize, pageIndex) {
        // 拼接查詢的URL,賦空值默認就去掉這個條件的限制,有值就代表按照這個條件篩選
        var url = listUrl + '?' + 'pageIndex=' + pageIndex + '&pageSize='
            + pageSize + '&parentId=' + parentId + '&areaId=' + areaId
            + '&shopCategoryId=' + shopCategoryId + '&shopName=' + shopName;
        // 避免重複訪問
        loading = true;
        $.getJSON(url, function(data) {
            if (data.success) {
                maxItems = data.count;
                var html = '';
                data.shopList.map(function(item, index) {
                    html += '' + '<div class="card" data-shop-id="'
                        + item.shopId + '">' + '<div class="card-header">'
                        + item.shopName + '</div>'
                        + '<div class="card-content">'
                        + '<div class="list-block media-list">' + '<ul>'
                        + '<li class="item-content">'
                        + '<div class="item-media">' + '<img src="'
                        + item.shopImg + '" width="44">' + '</div>'
                        + '<div class="item-inner">'
                        + '<div class="item-subtitle">' + item.shopDesc
                        + '</div>' + '</div>' + '</li>' + '</ul>'
                        + '</div>' + '</div>' + '<div class="card-footer">'
                        + '<p class="color-gray">'
                        + new Date(item.lastEditTime).Format("yyyy-MM-dd")
                        + '更新</p>' + '<span>點擊查看</span>' + '</div>'
                        + '</div>';
                });
                $('.list-div').append(html);
                // 獲取目前爲止以顯示的總數,包含之前加載的
                var total = $('.list-div .card').length;
                // 如果總數和按照次查詢條件列出來的總數一直,則停止加載
                if (total >= maxItems) {
                    // 隱藏加載提示符
                    $('.infinite-scroll-preloader').hide();
                } else {
                    // 顯示加載提示符
                    $('.infinite-scroll-preloader').show();
                }

                // 否則頁碼+1, 繼續加載
                pageNum += 1;
                loading = false;
                $.refreshScroller();
            }
        });
    }

    // 下滑屏幕自動進行分頁搜索
    $(document).on('infinite', '.infinite-scroll-bottom', function() {
        if (loading)
            return;
        addItems(pageSize, pageNum);
    });

    // 顯示店鋪詳情頁
    $('.shop-list').on('click', '.card', function(e) {
        var shopId = e.currentTarget.dataset.shopId;
        window.location.href = '/frontend/shopdetail?shopId=' + shopId;
    });

    // 選擇新的店鋪類別後,重置頁碼,清空原來的店鋪列表重新加載新的搜索結果
    $('#shoplist-search-div').on(
        'click',
        '.button',
        function(e) {
            if (parentId) {// 如果傳遞過來的是一個父類下的子類
                shopCategoryId = e.target.dataset.categoryId;
                // 如果之前選定了別的category,移除其選定效果,改成選新的
                if ($(e.target).hasClass('button-fill')) {
                    $(e.target).removeClass('button-fill');
                    shopCategoryId = '';
                } else {
                    $(e.target).addClass('button-fill').siblings()
                        .removeClass('button-fill');
                }
                // 由於查詢條件發生改變,清空店鋪列表再進行查詢
                $('.list-div').empty();
                // 重置頁碼
                pageNum = 1;
                addItems(pageSize, pageNum);
            } else {// 如果傳遞過來的父類爲空,則按照父類查詢
                parentId = e.target.dataset.categoryId;
                if ($(e.target).hasClass('button-fill')) {
                    $(e.target).removeClass('button-fill');
                    parentId = '';
                } else {
                    $(e.target).addClass('button-fill').siblings()
                        .removeClass('button-fill');
                }
                // 由於查詢條件發生改變,清空店鋪列表再進行查詢
                $('.list-div').empty();
                // 重置頁碼
                pageNum = 1;
                addItems(pageSize, pageNum);
                parentId = '';
            }
        });

    // 查詢名字發生變化,重置頁碼,清空列表重新顯示查詢結果
    $('#search').on('input', function(e) {
        shopName = e.target.value;
        $('.list-div').empty();
        pageNum = 1;
        addItems(pageSize, pageNum);
    });

    // 查詢區域信息發生變化,重置頁碼,清空列表重新顯示查詢結果
    $('#area-search').on('change', function() {
        areaId = $('#area-search').val();
        $('.list-div').empty();
        pageNum = 1;
        addItems(pageSize, pageNum);
    });

    // 打開右邊側欄
    $('#me').click(function() {
        $.openPanel('#panel-left-demo');
    });

    // 初始化
    $.init();
});

shop-detail.js

$(function() {
    var loading = false;
    // 分頁允許最大條數
    var maxItems = 20;
    // 默認一頁返回的商品數
    var pageSize = 3;

    var listUrl = '/frontend/listproductsbyshop';

    var pageNum = 1;
    var shopId = getQueryString('shopId');
    var productCategoryId = '';
    var productName = '';

    var searchDivUrl = '/frontend/listshopdetailpageinfo?shopId=' + shopId;

    getSearchDivData();
    addItems(pageSize, pageNum);

    function getSearchDivData() {
        var url = searchDivUrl;
        $
            .getJSON(
                url,
                function(data) {
                    if (data.success) {
                        var shop = data.shop;
                        $('#shop-cover-pic').attr('src', shop.shopImg);
                        $('#shop-update-time').html(
                            new Date(shop.lastEditTime)
                                .Format("yyyy-MM-dd"));
                        $('#shop-name').html(shop.shopName);
                        $('#shop-desc').html(shop.shopDesc);
                        $('#shop-addr').html(shop.shopAddr);
                        $('#shop-phone').html(shop.phone);

                        // 獲取店鋪商品類別列表
                        var productCategoryList = data.productCategoryList;
                        var html = '';
                        productCategoryList
                            .map(function(item, index) {
                                html += '<a href="#" class="button" data-product-search-id='
                                    + item.productCategoryId
                                    + '>'
                                    + item.productCategoryName
                                    + '</a>';
                            });
                        $('#shopdetail-button-div').html(html);
                    }
                });
    }

    function addItems(pageSize, pageIndex) {
        // 拼接URL
        var url = listUrl + '?' + 'pageIndex=' + pageIndex + '&pageSize='
            + pageSize + '&productCategoryId=' + productCategoryId
            + '&productName=' + productName + '&shopId=' + shopId;
        loading = true;
        $.getJSON(url, function(data) {
            if (data.success) {
                maxItems = data.count;
                var html = '';
                data.productList.map(function(item, index) {
                    html += '' + '<div class="card" data-product-id='
                        + item.productId + '>'
                        + '<div class="card-header">' + item.productName
                        + '</div>' + '<div class="card-content">'
                        + '<div class="list-block media-list">' + '<ul>'
                        + '<li class="item-content">'
                        + '<div class="item-media">' + '<img src="'
                        + item.imgAddr + '" width="44">' + '</div>'
                        + '<div class="item-inner">'
                        + '<div class="item-subtitle">' + item.productDesc
                        + '</div>' + '</div>' + '</li>' + '</ul>'
                        + '</div>' + '</div>' + '<div class="card-footer">'
                        + '<p class="color-gray">'
                        + new Date(item.lastEditTime).Format("yyyy-MM-dd")
                        + '更新</p>' + '<span>點擊查看</span>' + '</div>'
                        + '</div>';
                });
                //將卡片集合添加到目標HTML組件中
                $('.list-div').append(html);
                var total = $('.list-div .card').length;
                if (total >= maxItems) {
                    // 隱藏加載提示符
                    $('.infinite-scroll-preloader').hide();
                } else {
                    // 顯示加載提示符
                    $('.infinite-scroll-preloader').show();
                }
                pageNum += 1;
                loading = false;
                $.refreshScroller();
            }
        });
    }

    // 下滑屏幕自動分頁搜索
    $(document).on('infinite', '.infinite-scroll-bottom', function() {
        if (loading) {
            return;
        }
        addItems(pageSize, pageNum);
    });

    $('#shopdetail-button-div').on(
        'click',
        '.button',
        function(e) {
            productCategoryId = e.target.dataset.productSearchId;
            if (productCategoryId) {
                if ($(e.target).hasClass('button-fill')) {
                    $(e.target).removeClass('button-fill');
                    productCategoryId = '';
                } else {
                    $(e.target).addClass('button-fill').siblings()
                        .removeClass('button-fill');
                }
                $('.list-div').empty();
                pageNum = 1;
                addItems(pageSize, pageNum);
            }
        });

    $('.list-div').on(
        'click',
        '.card',
        function(e) {
            var productId = e.currentTarget.dataset.productId;
            window.location.href = '/frontend/productdetail?productId='
                + productId;
        });

    $('#search').on('change', function(e) {
        productName = e.target.value;
        $('.list-div').empty();
        pageNum = 1;
        addItems(pageSize, pageNum);
    });

    $('#me').click(function() {
        $.openPanel('#panel-left-demo');
    });
    $.init();
});

commonutil.js

function changeVerifyCode(img) {
    img.src="../Kaptcha?"+Math.floor(Math.random()*100);
}

function getQueryString(name){
    var reg = new RegExp("(^|&)"+name+"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null){
        return decodeURIComponent(r[2]);
    }
    return '';
}

Date.prototype.Format = function(fmt) {
    var o = {
        "M+" : this.getMonth() + 1, // 月份
        "d+" : this.getDate(), // 日
        "h+" : this.getHours(), // 小時
        "m+" : this.getMinutes(), // 分
        "s+" : this.getSeconds(), // 秒
        "q+" : Math.floor((this.getMonth() + 3) / 3), // 季度
        "S" : this.getMilliseconds()
        // 毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
            .substr(4 - RegExp.$1.length));
    for ( var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k])
                : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

shop-list.css

.infinite-scroll-preloader {
    margin-top: -5px;
}

.shoplist-button-div {
    margin: 0 .3rem;
}

.shoplist-button-div>.button {
    width: 30%;
    height: 1.5rem;
    line-height: 1.5rem;
    display: inline-block;
    margin: 1%;
    overflow: hidden;
}

.select-wrap {
    margin: 0 .5rem;
}

.select {
    border: 1px solid #0894ec;
    color: #0894ec;
    background-color: #efeff4;
    width: 100%;
    height: 1.5rem;
    font-size: .7rem;
}

shop-detail.css

.detail-search {
    height: 2.2rem;
    padding-right: .5rem;
    padding-left: .5rem;
    background-color: #f7f7f8;
}

.infinite-scroll-preloader {
    margin-top: -5px;
}

.shopdetail-button-div {
    margin: 0 .3rem;
}

.shopdetail-button-div>.button {
    width: 30%;
    height: 1.5rem;
    line-height: 1.5rem;
    display: inline-block;
    margin: 1%;
    overflow: hidden;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章