我的商店分类和区域信息都只能显示一条。

来源:3-2 店铺注册之js实现

邢文凯888

2020-01-20 17:46:34

$(function () {
   //获取店铺信息和区域信息的控制器
   var initUrl = '/shopadmin/getshopinitinfo';
   //注册店铺的控制器
   var registerShopUrl = '/shopadmin/registershop';
   alert(initUrl);
   //初始化下面这个方法
   getshopinitinfo();

   //这个是获取店铺信息和区域信息的方法
   function getshopinitinfo() {
       $.getJSON(initUrl,function (data) {
           if(data.success){
               //商店分类信息
               var tempHtml = '';
               //所属区域信息
               var tempAreaHtml = '';
               //讲后台传入的map进行遍历,并传入前台(商品信息)
               data.shopCategoryList.map(function (items,index) {
                   tempHtml = '<option data-id="'+items.shopCategoryId+'">'
                       + items.shopCategoryName + '</option>';
               });
               //讲后台传入的map进行遍历,并传入前台(区域信息)
               data.areaList.map(function (items,index) {
                   tempAreaHtml = '<option data-id="'+items.areaId+'">'
                       + items.areaName + '</option>';
               });

               //进行传入
               $("#shop-category").html(tempHtml);
               $("#area").html(tempAreaHtml);
           }
       });
       $("#submit").click(function () {
           var shop = {};
           shop.shopName = $("#shop-name").val();
           shop.shopAddr = $("#shop-addr").val();
           shop.phone = $("#shop-phone").val();
           shop.shopDesc = $("#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.addend("shopImg",shopImg);
           formData.addend("shopstr",JSON.stringify(shop));
           $.ajax({
               url : registerShopUrl,
               type : 'post',
               contentType : false,
               proceesData : false,
               cache : false,
               success : function (data) {
                   if(data.success){
                       $.toast("提交成功!");
                   }else {
                       $.toast("提交失败!"+data.errMsg);
                   }
               }


           })
       });

   }


})



package com.xing.o2o.web.shopadmin;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xing.o2o.dto.ShopExecution;
import com.xing.o2o.entity.Area;
import com.xing.o2o.entity.PersonInfo;
import com.xing.o2o.entity.Shop;
import com.xing.o2o.entity.ShopCategory;
import com.xing.o2o.enums.ShopEnum;
import com.xing.o2o.service.AreaService;
import com.xing.o2o.service.ShopCategoryService;
import com.xing.o2o.service.Shopservice;
import com.xing.o2o.util.HttpServletRequestutil;
import com.xing.o2o.util.ImageUtil;
import com.xing.o2o.util.PathUtil;
import jdk.nashorn.internal.ir.RuntimeNode;
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 org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/shopadmin")
public class ShopManagermentController {
   @Autowired
   private Shopservice shopservice;

   @Autowired
   private ShopCategoryService shopCategoryService;


   @Autowired
   private AreaService areaService;

   @RequestMapping(value = "/registershop",method = RequestMethod.POST)
   @ResponseBody
   private Map<String,Object> registerShop(HttpServletRequest request){
       Map<String,Object> map = new HashMap<String, Object>();
       //接受并转换参数
       String shopStr = HttpServletRequestutil.getString(request, "shopStr");
       ObjectMapper objectMapper = new ObjectMapper();
       Shop shop =null;
       try {
           objectMapper.readValue(shopStr,Shop.class);
       }catch (Exception e){
           map.put("success",false);
           map.put("errMsg",e.getMessage());
           return map;
       }
       CommonsMultipartFile shopImg = null;
       CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(
               request.getSession().getServletContext());
       if(commonsMultipartResolver.isMultipart(request)){
           MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
           shopImg = (CommonsMultipartFile) multipartHttpServletRequest.getFile("shopImg");
       }else {
           map.put("success",false);
           map.put("errMsg","上传图片不能为空");
           return map;
       }
       //注册店铺

       if(shop!=null&&shopImg!=null){
           PersonInfo owner = new PersonInfo();
           owner.setUserId(1L);
           shop.setOwner(owner);
           ShopExecution shopExecution = null;
           try {
               shopExecution = shopservice.insert(shop, shopImg.getInputStream(),shopImg.getOriginalFilename());
               if(shopExecution.getState()== ShopEnum.SUCCESS.getState()){
                   map.put("success",true);
               }else{
                   map.put("success",false);
                   map.put("errMsg",shopExecution.getStateInfo());
               }
           } catch (IOException e) {
               map.put("success",false);
               map.put("errMsg","上传图片不能为空");
               return map;
           }

           return map;
       }else {
           map.put("success",false);
           map.put("errMsg","请输入店铺信息");
           return map;
       }

   }
   @RequestMapping(value = "/getshopinitinfo",method = RequestMethod.GET)
   @ResponseBody
   private Map<String,Object> getshopinitinfo(){
      Map<String,Object> modelMap = new HashMap<String, Object>();
       List<ShopCategory> shopCategoryList = new ArrayList<ShopCategory>();
       List<Area> areaList = new ArrayList<Area>();
       try {
           shopCategoryList = shopCategoryService.getShopCategorylist(new ShopCategory());
           areaList = areaService.getAreaist();
           modelMap.put("shopCategoryList",shopCategoryList);
           modelMap.put("areaList",areaList);
           modelMap.put("success",true);
       }catch (Exception e){
           modelMap.put("success",false);
           modelMap.put("essMasg",e.getMessage());
       }
       return modelMap;
   }

//    private static void InputStreamtoFile(InputStream ins, File file){
//        FileOutputStream os = null;
//        try {
//            os= new FileOutputStream(file);
//            int bytesRead = 0;
//            byte[] buffer = new byte[1024];
//            while ((bytesRead = ins.read(buffer))!=-1){
//                 os.write(buffer,0,bytesRead);
//            }
//        }catch (Exception e){
//            throw new RuntimeException("调用InputStreamtoFile产生异常"+e.getMessage());
//        }finally {
//          try {
//              if (os!=null){
//                  os.close();
//              }
//              if (ins != null) {
//
//                  ins.close();
//              }
//          }catch (Exception e){
//              throw new RuntimeException("关闭流产生错误");
//          }
//        }
//    }
}


<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xing.o2o.dao.AreaDao">
   <select id="selectAreaList" resultType="Area">
       SELECT area_id, area_name,
     priority, create_time, last_edit_time
     FROM tb_area
     ORDER BY priority
     DESC
   </select>



</mapper>


<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xing.o2o.dao.ShopCategoryDao">
   <select id="queryShopCategory" resultType="com.xing.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 not null
           </if>
           <if test="shopCategoryCondition.parent!=null">
               and parent_id=#{shopCategoryCondition.parent.shopCategoryId}
           </if>
       </where>
       order by
       priority desc
   </select>

</mapper>


<!DOCTYPE html>
<html>
<head>
   <meta charset="utf-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <title>SUI Mobile Demo</title>
   <meta name="description" content="MSUI: Build mobile apps with simple HTML, CSS, and JS components.">
   <meta name="author" content="阿里巴巴国际UED前端">
   <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">
   <meta name="format-detection" content="telephone=no">

   <!-- Google Web Fonts -->

   <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="apple-touch-icon-precomposed" href="/assets/img/apple-touch-icon-114x114.png">
   <script>
       //ga
   </script>
   <script>
       var _hmt = _hmt || [];
       (function() {
           var hm = document.createElement("script");
           hm.src = "//hm.baidu.com/hm.js?ba76f8230db5f616edc89ce066670710";
           var s = document.getElementsByTagName("script")[0];
           s.parentNode.insertBefore(hm, s);
       })();
   </script>

</head>
<body>
<div class="page-group">
   <div id="page-label-input" class="page">
       <header class="bar bar-nav">
           <a class="button button-link button-nav pull-left back" href="/demos/form">
               <span class="icon icon-left"></span>
               返回
           </a>
           <h1 class="title">商店信息</h1>
       </header>
       <div class="content">
           <div class="list-block">
               <ul>
                   <!-- Text inputs -->
                   <li>
                       <div class="item-content">
                           <div class="item-inner">
                               <div class="item-title label">商铺名称</div>
                               <div class="item-input">
                                   <input type="text" id="shop-name" placeholder="商铺名称">
                               </div>
                           </div>
                       </div>
                   </li>

                   <li>
                       <div class="item-content">
                           <div class="item-inner">
                               <div class="item-title label">商铺分类</div>
                               <div class="item-input">
                                   <select id="shop-category">
                                   </select>
                               </div>
                           </div>
                       </div>
                   </li>

                   <li>
                       <div class="item-content">
                           <div class="item-inner">
                               <div class="item-title label">所属区域</div>
                               <div class="item-input">
                                   <select id="area">
                                   </select>
                               </div>
                           </div>
                       </div>
                   </li>

                   <li>
                       <div class="item-content">
                           <div class="item-inner">
                               <div class="item-title label">详细地址</div>
                               <div class="item-input">
                                   <input type="text" id="shop-addr" placeholder="详细地址">
                               </div>
                           </div>
                       </div>
                   </li>

                   <li>
                       <div class="item-content">
                           <div class="item-inner">
                               <div class="item-title label">联系电话</div>
                               <div class="item-input">
                                   <input type="text" id="shop-phone" placeholder="联系电话">
                               </div>
                           </div>
                       </div>
                   </li>

                   <li>
                       <div class="item-content">
                           <div class="item-inner">
                               <div class="item-title label">缩略图</div>
                               <div class="item-input">
                                   <input type="file" id="shop-img">
                               </div>
                           </div>
                       </div>
                   </li>

                   <li class="align-top">
                       <div class="item-content">
                           <div class="item-inner">
                               <div class="item-title label">店铺简介</div>
                               <div class="item-input">
                                   <textarea id="shop-desc" placeholder="店铺简介"></textarea>
                               </div>
                           </div>
                       </div>
                   </li>
               </ul>
           </div>
           <div class="content-block">
               <div class="row">
                   <div class="col-50"><a href="#" class="button button-big button-fill button-danger">返回</a></div>
                   <div class="col-50"><a href="#" class="button button-big button-fill button-success" id="submit">提交</a></div>
               </div>
           </div>
       </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/shop/shopoperation.js" charset="UTF-8"></script>
</body>
</html>
我的商店分类和区域信息都只能显示一条。老师帮忙看看为什么,我里面有很多信息呀

http://img.mukewang.com/climg/5e2576f909a8e8d612800521.jpg


写回答

2回答

好帮手慕柯南

2020-01-20

同学你好!

1、=相当于是进行赋值操作。是完全重新赋值

2. +=相当于是之前的加上现在的,在重新赋值给变量。比如:html +="s",等价于:html=html+"s"

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

0

邢文凯888

提问者

2020-01-20

已经解决了,请问这个+=,和=区别是什么,为什么要用+=拼接

http://img.mukewang.com/climg/5e2578b5098a923907230270.jpg

0

0 学习 · 8263 问题

查看课程