SSM项目之商铺系统-前端展示系统之商铺类型列表和商铺列表展示的开发(十八)

我们现在开发前端展示系统的开发

我们先开发首页:

我们先进行预览:

首先最上面是轮播图 ,然后是一级商铺类别展示

我们分析轮播图,轮播图的轮播功能通过前台实现,我们后台要实现的就是存入轮播图的图片和信息

那么这个方法将会很简单,我们之前也创建了轮播图的表和实体类

接下来开始开发dao到controller的开发

通过接受一个HeadLine的实体类来完成了相应的查询,返回一个列表,因为可能有多个轮播图

mapper.xml:

    <!--返回可用的头条信息-->
    <select id="queryHeadLine"  resultType="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>

接受传来的headline参数进行条件查询,并返回结果

接下来service层:

serviceImpl:

@Service
public class HeadLineServiceImpl implements HeadLineService{
    @Autowired
    HeadLineDao headLineDao;

    @Override
    public List<HeadLine> queryHeadLineList(HeadLine headLineCondition) {
        return headLineDao.queryHeadLine(headLineCondition);
    }

}

没什么多余的处理直接调用dao层的方法获得轮播图列表

接下来看下商铺一级类别的展示功能实现:

DAO层:

之前已经实现过的方法

mapper.xml

这里是我们查询的mapper文件内容,我们在这里显示第一类别时就传入shopCategort为null,根据查询条件显示的就是我们的第一类别

 <select id="queryShopCategory" resultType="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 shopCategoryCondition.parent.shopCategoryId != null">
                    and parent_id=#{shopCategoryCondition.parent.shopCategoryId}
                </if>

            </where>
            ORDER BY priority
            DESC

    </select>

service:

serviceImpl:

并没有什么多余的操作

接下来看controller,这个controller直接交给前端轮播图和一级商铺的内容。

package storepro.web.front;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import storepro.dao.HeadLineDao;
import storepro.entity.HeadLine;
import storepro.entity.ShopCategory;
import storepro.service.HeadLineService;
import storepro.service.ShopCategoryService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//用于处理显示前台信息的controller
@Controller
@RequestMapping("/frontend")
public class MainPageController {
@Autowired
    ShopCategoryService shopCategoryService;
@Autowired
    HeadLineService headLineService;
//获取前端需要的头条信息和商品展示列表
@RequestMapping(value = "/listmainpage", method = RequestMethod.GET)
@ResponseBody
private Map<String,Object> listMainPageInfo(){
    Map<String,Object> modelMap=new HashMap<String,Object>();
    List<ShopCategory> shopCategoryList=new ArrayList<ShopCategory>();
    //先获取一级目录,当传入为空时获取一级目录
    try {
        shopCategoryList= shopCategoryService.getShopCategoryList(null);
        modelMap.put("shopCategoryList",shopCategoryList);
    }catch (Exception e){
        modelMap.put("success",false);
        modelMap.put("errMsg",e.getMessage());
    }
    //接下来存入头条
    try {
        HeadLine headLine = new HeadLine();
        headLine.setEnableStatus(1);//获取那些状态为1的头条
        List<HeadLine> headLineList = headLineService.queryHeadLineList(headLine);
        modelMap.put("headLineList", headLineList);
    }catch (Exception e){
        e.printStackTrace();
        modelMap.put("errMsg","取出头条失败");
    }
    modelMap.put("success", true);
    return  modelMap;
}
}

第一步:获取商铺一级类别列表,这里传入null参数才能返回一级列表

第二步:接受传入的头条

第三步:将前面的内容全部包装并送给前端

还要写一个controller返回到这个html页面

 view层:

<!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="/storepro/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">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="/storepro/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>
    <!--侧边栏 TODO   -->
    <div class="panel-overlay"></div>
    <div class="panel panel-right panel-reveal" id="panel-left-demo">
        <div class="content-block">
            <p>
                <a href="/storepro/frontend/myrecord" class="close-panel">消费记录</a>
            </p>
            <p>
                <a href="/storepro/frontend/mypoint" class="close-panel">我的积分</a>
            </p>
            <p>
                <a href="/storepro/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>

$(function() {
    // 定义访问后台获取头条列表以及一级商铺类别列表的URL
    var url = '/storepro/frontend/listmainpage';

    // 访问后台获取头条列表以及一级商铺类别
    $.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);
            // 设置轮播图轮换时间为1秒
            $(".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>';
            });
            $('.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 = '/storepro/frontend/shoplist?parentId=' + shopCategoryId;
        window.location.href = newUrl;
    });

});

.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%;
}

接下来我们完成店铺列表页的详情开发我们先看下店铺列表页的展示

当我们点击某个一级了类别时会显示其下的店铺种类列表和店铺详情,

这些就是店铺列表详情 ,我们可以通过点击一级商铺类别时显示所有一级店铺类别下的所有二级商铺类别,然后通过选择查出相应的店铺

注意我们还有一种情况

当点击全部商店时

 

我们要展示所有一级类别并且显示所有店铺

那么我们首先来实现点击一级类别或者全部商铺时展示的上方商铺类别的选择模块

梳理思路:

底层实现就是通过获得前端的查询条件,处理后交给dao层,通过条件获得商铺列表

dao层和service层都实现过了

我们直接讲解controller层:

  /*
     *    当我们点击某个一级类别时,我们进入到商铺列表的时候,上面还有一排供选择二级商铺类别,我们的任务就是显示这个
     *    还有就是直接点击全部,会显示所有商铺类别
     *    还需要展示地区,供选择
     * */

    @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;
        try {
            if (parentId != -1) {//如果获取的shopCategoryId不等于-1,-1为点击全部时的显示
                ShopCategory shopCategory = new ShopCategory();//
                ShopCategory shopCategoryParent = new ShopCategory();
                shopCategoryParent.setShopCategoryId(parentId);
                shopCategory.setParent(shopCategoryParent);
                //调用方法,查找相应的二级类别
                shopCategoryList = shopCategoryService.getShopCategoryList(shopCategory);//只传入一级类别的id即可,
                modelMap.put("shopCategoryList", shopCategoryList);//存入信息

            } else {
                shopCategoryList = shopCategoryService.getShopCategoryList(null);//获取所有商品类别
                modelMap.put("shopCategoryList", shopCategoryList);//存入信息
            }
        } catch (ShopOperationException e) {
            modelMap.put("success", false);
            modelMap.put("errMsg", e.getMessage());
            return modelMap;
        }
        //获取所有地区供查询
        List<Area> areaList = null;
        try {
            areaList = areaService.getAreaList();
            modelMap.put("areaList", areaList);//存入信息
        } catch (Exception e) {
            modelMap.put("success", false);
            modelMap.put("errMsg", e.getMessage());
        }
        modelMap.put("success", true);//成功标识
        return modelMap;
    }

我们需要的条件只有一个就是点击的商铺类别的父类的id

我们前端设置如果点击全部商品为-1,因为这个需要特殊处理

第一步:判断parentId,不为-1我们则获取相应的点击的条件查询

第二步:如果是parentId为-1,我们则传入条件为null,这样出查询出来的条件就是全部一级店铺类别显示在上方

第三步:查出当前商铺类别可选的商铺区域信息,供前端显示

因为还加入了新html页面我们需要控制路由转发至这个页面


view层与下面的方法相同我们一起完成

接下来我们来完成当点击一个店铺类或者相应的条件时展示的相应的店铺列表

思路梳理:

当我们点击商铺类别或者选择区域时我们显示相应类别下的商铺列表

分几种情况

当我们点击全部商品时:我们无条件查询

当我们点击一级类别时:我们通过查询parentId相等的店铺

当我们点击二级类别或者其他限定条件时:根据相应的条件进行查询

方法就是查询商铺列表,之前实现过了

我们在查询商铺列表的查询语句中的判定条件多加了一个

<!--分页查询店铺列表 ,可选条件有店铺名,店铺状态,店铺类别,区域id,owner-->
   <select id="queryShopList" resultMap="shopMap" >
         	SELECT
			s.shop_id,
			s.shop_name,
			s.shop_desc,
			s.shop_addr,
			s.phone,
			s.shop_img,
			s.priority,
			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.area!=null and shopCondition.area.areaId!=null">
                and s.area_id=#{shopCondition.area.areaId}
            </if>
            <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>
        <!--查出当前商铺同类型的商铺-->
            <!--比如美食下面有两个种类,是大排档和火锅,当我们在主页点击美食这个图片的时候我们要显示大排档和火锅下的所有商铺
            -->
            <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>
            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>
      <!--查出当前商铺同类型的商铺-->
            <!--比如美食下面有两个种类,是大排档和火锅,当我们在主页点击美食这个图片的时候我们要显示大排档和火锅下的所有商铺
            -->
            <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>

 这句说明就是点击一级类别查询底下的所有商铺类别

controller层实现:

/*
     * 根据传入的条件查询店铺
     * */
    @RequestMapping(value = "/listshops", method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> listShops(HttpServletRequest request) throws UnsupportedEncodingException {
        Map<String, Object> modelMap = new HashMap<String, Object>();
      //先获取前台传来的数据()
        //获取页码
        int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");
        int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
        try {
            if ((pageIndex > -1) && (pageSize > -1)){
            //获取组合查询条件
            long parentId = HttpServletRequestUtil.getLong(request, "parentId");
            long shopCategoryId = HttpServletRequestUtil.getLong(request, "shopCategoryId");
            int areaId = HttpServletRequestUtil.getInt(request, "areaId");
            String shopName=HttpServletRequestUtil.getString(request, "shopName");


            // 封装查询条件

            Shop shopCondition = compactShopCondition4Search(parentId, shopCategoryId, areaId, shopName);
            // 调用service层提供的方法,获取条件查询的语句
            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");
        }
    }catch (Exception e){
        modelMap.put("success" ,false);
        modelMap.put("errMsg" ,e.getMessage());
    }
        return modelMap;
    }

    private Shop compactShopCondition4Search(long parentId, long shopCategoryId, int areaId, String shopName) throws UnsupportedEncodingException {
        Shop shopCondition=new Shop();
        if (parentId>-1){
            ShopCategory shopCategory=new ShopCategory();
            ShopCategory shopCategoryParent=new ShopCategory();
            shopCategoryParent.setShopCategoryId(parentId);
            shopCategory.setParent(shopCategoryParent);
            shopCondition.setShopCategory(shopCategory);
        }
        //判断shopCategoryId如果这个参数有值,代表我们通过二级商铺类型查找店铺
        if (shopCategoryId>-1){
            ShopCategory shopCategory=new ShopCategory();
            shopCategory.setShopCategoryId(shopCategoryId);
            shopCondition.setShopCategory(shopCategory);
        }

        //判断areaid是否有值
        if (areaId>-1){
            Area area =new Area();
            area.setAreaId(areaId);
            shopCondition.setArea(area);

        }
        //判断shopName是否有值
        if (shopName!=null){
            shopName= new String(shopName.getBytes("ISO-8859-1"), "UTF-8");
            shopCondition.setShopName(shopName);
        }
        // 查询状态为审核通过的商铺
        shopCondition.setEnableStatus(1);
        return  shopCondition;
    }
}

我们要着重说拼装查询条件的方法:

我们这里需要置查询条件为空才能查所有商铺,因为在mapper文件中的判断中如果一个condition对象的需要查询条件的属性都为空那么会直接无条件查询(看mapper文件可知)

还有点击一级类别时,此时只能设置一个店铺类别id为空的但是parent为一级类别的对象传入才能查出(看mapper文件可知)

最后就是我们是使用GET方法获取数据,然而当我通过shopName查询时得到了乱码,我们需要进一步设置

先获得前端传来的字符,此时是乱码

然后通过 shopName= new String(shopName.getBytes("ISO-8859-1"), "UTF-8");字符格式转换为utf-8格式

商铺列表和商铺类别列表的View层:

<!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/shoplist.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="/storepro/frontend/index" external> <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-right-demo">
    <div class="content-block">
        <p>
            <a href="/storepro/local/accountbind?usertype=1" class="close-panel">绑定帐号</a>
        </p>
        <p>
            <a href="/storepro/local/changepsw?usertype=1" class="close-panel">修改密码</a>
        </p>
        <p>
            <a href="/storepro/frontend/myrecord" class="close-panel">消费记录</a>
        </p>
        <p>
            <a href="/storepro/frontend/mypoint" class="close-panel">我的积分</a>
        </p>
        <p>
            <a href="/storepro/frontend/pointrecord" class="close-panel">兑换记录</a>
        </p>
        <p>
            <a href="#" usertype="1" class="close-panel" id="log-out">登出系统</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/common.js'
        charset='utf-8'></script>
<script type='text/javascript'
        src='../resources/js/frontend/shoplist.js' charset='utf-8'></script>
<script type='text/javascript' src='../resources/js/local/login.js'
        charset='utf-8'></script>
</body>
</html>

$(function() {
    var loading = false;
    // 分页允许返回的最大条数,超过此数则禁止访问后台
    var maxItems = 999;
    // 一页返回的最大条数
    var pageSize = 3;
    // 获取店铺列表的URL
    var listUrl = '/storepro/frontend/listshops';
    // 获取店铺类别列表以及区域列表的URL
    var searchDivUrl = '/storepro/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);
    /**
     * 获取店铺类别列表以及区域列表信息
     *
     * @returns
     */
    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>';
                        // 遍历店铺类别列表,拼接出a标签集
                        shopCategoryList
                            .map(function(item, index) {
                                html += '<a href="#" class="button" data-category-id='
                                    + item.shopCategoryId
                                    + '>'
                                    + item.shopCategoryName
                                    + '</a>';
                            });
                        // 将拼接好的类别标签嵌入前台的html组件里
                        $('#shoplist-search-div').html(html);
                        var selectOptions = '<option value="">全部街道</option>';
                        // 获取后台返回过来的区域信息列表
                        var areaList = data.areaList;
                        // 遍历区域信息列表,拼接出option标签集
                        areaList.map(function(item, index) {
                            selectOptions += '<option value="'
                                + item.areaId + '">'
                                + item.areaName + '</option>';
                        });
                        // 将标签集添加进area列表里
                        $('#area-search').html(selectOptions);
                    }
                });
    }

    /**
     * 获取分页展示的店铺列表信息
     *
     * @param pageSize
     * @param pageIndex
     * @returns
     */
    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>';
                });
                // 将卡片集合添加到目标HTML组件里
                $('.list-div').append(html);
                // 获取目前为止已显示的卡片总数,包含之前已经加载的
                var total = $('.list-div .card').length;
                // 若总数达到跟按照此查询条件列出来的总数一致,则停止后台的加载
                if (total >= maxItems) {
                    // 隐藏提示符
                    $('.infinite-scroll-preloader').hide();
                } else {
                    $('.infinite-scroll-preloader').show();
                }
                // 否则页码加1,继续load出新的店铺
                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 = '/storepro/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('change', 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-right-demo');
    });

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

.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;
}

猜你喜欢

转载自blog.csdn.net/Sunmeok/article/details/81478422
今日推荐