校园商铺平台项目(10)-店铺注册 js实现 和 类别、区域获取

1 店铺注册

1.1 js 文件

校园商铺平台项目(10)-店铺注册 js实现 和 类别、区域获取_第1张图片

$(function () {
     
    var initUrl = '/o2o/shopadmin/getshopinitinfo';
    var registerShopUrl = 'o2o/shopadmin/registershop';

    alert(initUrl);
    getShopInitInfo();

    function getShopInitInfo() {
     
        $.getJSON(initUrl, function (data) {
     
            if (data.success) {
     
                var tempHtml = '';
                var tempAreaHtml = '';
                data.shopCategoryList.map(function (item, index) {
     
                    tempHtml += ' item.shopCategoryId + '">'
                        + item.shopCategoryName + '';
                });
                data.areaList.map(function (item, index) {
     
                        tempAreaHtml += ' item.areaId + '">'
                            + item.areaName + '';
                    }
                );
                $('#shop-category').html(tempHtml);
                $('#area').html(tempAreaHtml);
            }
        });

        $('#submit').click(function () {
     
            var shop = {
     };
            shop.shopNamme = $('#shop-name').val();
            shop.shopAddr = $('#shop-addr').val();
            shop.phone = $('#shop-phone').val();
            shop.desc = $('#shop-desc').val();
            shop.shopCategory = {
     
                shopCategoryId: $('#shop-category').find('option').not(function () {
     
                    return !this.selected;
                }).data('id')
            };
            shop.area = {
     
                areaId: $('#area').find('option').not(function () {
     
                    return !this.selected;
                }).data('id')
            };
            var shopImg = $('#shop-img')[0].files[0];
            var formData = new formData();
            formData.append('shopImg', shopImg);
            formData.append('shopStr', JSON.stringify(shop));

            // 提交后台
            $.ajax({
     
                url: registerShopUrl,
                type: 'POST',
                data: formData,
                contentType: false,
                processData: false,
                cache: false,
                success: function (data) {
     
                    if (data.success()) {
     
                        $.toast('提交成功!');
                    }else{
     
                        $.toast('提交失败!'+ data.errMsg);
                    }
                }
            });
        });

    }
})

1.2 引入 js

校园商铺平台项目(10)-店铺注册 js实现 和 类别、区域获取_第2张图片

2 店铺类别-ShopCategoryDao

package com.tzb.o2o.dao;

import com.tzb.o2o.entity.ShopCategory;
import org.apache.ibatis.annotations.Param;

import java.util.List;

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


  • ShopCategoryDao.xml


<mapper namespace="com.tzb.o2o.dao.ShopCategoryDao" >

    <select id="queryShopCategory" resultType="com.tzb.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.parent!=null">
             and parent_id = #{shopCategoryCondition.parent.shopCategoryId}
         if>
     where>
    ORDER BY
    priority DESC

    select>
mapper>

2.1 店铺类别dao 测试

在这里插入图片描述

package com.tzb.o2o.dao;

import com.tzb.o2o.BaseTest;
import com.tzb.o2o.entity.ShopCategory;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import static org.junit.Assert.assertEquals;

public class ShopCategoryDaoTest extends BaseTest {
     

    @Autowired
    private ShopCategoryDao shopCategoryDao;

    @Test
    public void testQueryShopCategory(){
     
        List<ShopCategory> shopCategoryList = shopCategoryDao.queryShopCategory(new ShopCategory());
        assertEquals(2, shopCategoryList.size());
        ShopCategory testCategory = new ShopCategory();
        ShopCategory parentCategory = new ShopCategory();
        parentCategory.setShopCategoryId(1L);
        testCategory.setParent(parentCategory);
        shopCategoryList = shopCategoryDao.queryShopCategory(testCategory);
        System.out.println(shopCategoryList);
    }
}

校园商铺平台项目(10)-店铺注册 js实现 和 类别、区域获取_第3张图片

3 店铺类别 Service

package com.tzb.o2o.service;

import com.tzb.o2o.entity.ShopCategory;

import java.util.List;

public interface IShopCategoryService {
     
    List<ShopCategory> getShopCategoryList(ShopCategory shopCategoryCondition);
}

package com.tzb.o2o.service.impl;

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

import java.util.List;

@Service
public class ShopCatergoryServiceImpl implements IShopCategoryService {
     

    @Autowired
    private ShopCategoryDao shopCategoryDao;

    @Override
    public List<ShopCategory> getShopCategoryList(ShopCategory shopCategoryCondition) {
     
        return shopCategoryDao.queryShopCategory(shopCategoryCondition);
    }
}

4 修改店铺管理 Controller校园商铺平台项目(10)-店铺注册 js实现 和 类别、区域获取_第4张图片

@Controller
@RequestMapping("shopadmin")
public class ShopManagementController {
     

    @Autowired
    private ShopService shopService;

    @Autowired
    private IShopCategoryService shopCategoryService;

    @Autowired
    private AreaService areaService;

    @RequestMapping(value = "/getshopinitinfo", method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> getShopInitInfo() {
     
        Map<String, Object> modelMap = new HashMap<>();
        List<ShopCategory> shopCategoryList = new ArrayList<>();
        List<Area> areaList = new ArrayList<>();

        try {
     
            // 仅选出 parent_id 不为空的类别
            // 传入一个新的对象,符合条件1 test="shopCategoryCondition != null"
            shopCategoryList = shopCategoryService.getShopCategoryList(new ShopCategory());
            areaList = areaService.getAreaList();
            modelMap.put("shopCategoryList",shopCategoryList);
            modelMap.put("areaList",areaList);
            modelMap.put("success",true);
        } catch (Exception e) {
     
            modelMap.put("success",false);
            modelMap.put("errMsg", e.getMessage());
        }

        return modelMap;
    }


    @RequestMapping(value = "registershop", method = RequestMethod.POST)
    @ResponseBody
    private Map<String, Object> registerShop(HttpServletRequest request) {
     

        Map<String, Object> modelMap = new HashMap<>();

        // 1.接收并转化相应的参数,包括店铺信息和图片信息
        // shopStr,和前端约定好的变量
        String shopStr = HttpServletRequestUtil.getString(request, "shopStr");
        ObjectMapper mapper = new ObjectMapper();
        Shop shop = null;

        try {
     
            shop = mapper.readValue(shopStr, Shop.class);
        } catch (Exception e) {
     
            modelMap.put("success", false);
            modelMap.put("errMsg", e.getMessage());
            return modelMap;
        }

        CommonsMultipartFile shopImg = null;
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(
                request.getSession().getServletContext()
        );
        // 如果有上传的文件流
        if (commonsMultipartResolver.isMultipart(request)) {
     
            MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
            // shopImg ,和前段约定好的变量
            shopImg = (CommonsMultipartFile) multipartHttpServletRequest.getFile("shopImg");
        } else {
     
            modelMap.put("success", false);
            modelMap.put("errMsg", "上传图片不能为空");
            return modelMap;
        }

        // 2. 注册店铺
        if (null != shop && null != shopImg) {
     
            PersonInfo owner = new PersonInfo();

            // session TODO
            owner.setUserId(1L);
            shop.setOwner(owner);

            ShopExecution se = null;
            try {
     
                se = shopService.addShop(shop, shopImg.getInputStream(), shopImg.getOriginalFilename());
                if (se.getState() == ShopStateEnum.CHECK.getState()) {
     
                    modelMap.put("success", true);
                } else {
     
                    modelMap.put("success", false);
                    modelMap.put("errMsg", se.getStateInfo());
                }
            } catch (IOException e) {
     
                modelMap.put("success", false);
                modelMap.put("errMsg", e.getMessage());
                return modelMap;
            }
            return modelMap;
        } else {
     
            modelMap.put("success", false);
            modelMap.put("errMsg", "请输入店铺信息");
            return modelMap;
        }
        // 3.返回结果
    }
}

5 测试

校园商铺平台项目(10)-店铺注册 js实现 和 类别、区域获取_第5张图片
校园商铺平台项目(10)-店铺注册 js实现 和 类别、区域获取_第6张图片


http://localhost:8080/o2o/shopadmin/shopoperation
校园商铺平台项目(10)-店铺注册 js实现 和 类别、区域获取_第7张图片

你可能感兴趣的:(#,校园商铺平台项目,校园商铺,spring,mybatis)