//SpringBoot是对SSM框架的进一步改进与封装,随着微服务时代的来临,使用多个子模块组建一个大的应用项目成为时下后端开发的趋势。
//数据库Dao层 使用Mybatis框架
@Repository
public interface ShopDao {
/**
* 返回查询条件的总数
* @param shopCondition
* @return
*/
int queryShopCount(@Param("shopCondition") Shop shopCondition);
/**
*
* 分页查询店铺
* 输入条件
* 店铺名(模糊查询)、店铺状态、店铺类别、区域Id、owner
*
* @param shopCondition
* @param rowIndex 第几行
* @param pageSize 取几行
* @return
*/
List<Shop> queryShopList(@Param("shopCondition") Shop shopCondition,
@Param("rowIndex") int rowIndex,
@Param("pageSize") int pageSize);
/***
* 新增店铺
* @param shop
* @return
*/
int insertSHop(Shop shop);
/***
* 更新店铺信息
* @param shop
* @return
*/
int updateShop(Shop shop);
/**
* 店铺信息查询
* @param shopId
* @return
*/
Shop queryByShopId(long shopId);
}
// 对应的ShopDao.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.swust.emporium.dao.ShopDao">
<!--
useGeneratedKeys 代表插入结束后是否自动获取数据表的自增主键值
keyColumn 代表数据表的主键是哪个属性
keyProperty 代表数据表的主键与实体类中哪个属性相对应
-->
<insert id="insertSHop" useGeneratedKeys="true" keyColumn="shop_id" keyProperty="shopId">
insert into
tb_shop(owner_id, area_id,shop_category_id, shop_name, shop_desc, shop_addr, phone,
shop_img, priority, create_time, last_edit_time, enable_status, advice)
values(
#{owner.userId},
#{area.areaId},
#{shopCategory.shopCategoryId},
#{shopName},
#{shopDesc},
#{shopAddr},
#{phone},
#{shopImg},
#{priority},
#{createTime},
#{lastEditTime},
#{enableStatus},
#{advice}
)
</insert>
<update id="updateShop" parameterType="com.swust.emporium.pojo.Shop">
<!--
动态SQL生成
实体类字段不为空 则更新数据库字段 注意逗号
<if test="xxx != null"> xxx_xxx = #{xxx}> </if>
-->
update tb_shop
<set>
<if test="shopName != null"> shop_name = #{shopName},</if>
<if test="shopDesc != null"> shop_desc = #{shopDesc},</if>
<if test="shopAddr != null"> shop_addr = #{shopAddr},</if>
<if test="phone != null"> phone = #{phone},</if>
<if test="shopImg != null"> shop_img = #{shopImg},</if>
<if test="priority != null"> priority = #{priority},</if>
<if test="lastEditTime != null"> last_edit_time = #{lastEditTime},</if>
<if test="enableStatus != null"> enable_status = #{enableStatus},</if>
<if test="advice != null"> advice = #{advice},</if>
<!--复合类型-->
<if test="shopCategory != null"> shop_category_id = #{shopCategory.shopCategoryId},</if>
<if test="area != null"> area_id = #{area.areaId}</if>
</set>
where shop_id = #{shopId}
</update>
<!--定义queryByShopId查询方法的返回值类型-->
<resultMap type="com.swust.emporium.pojo.Shop" id ="shopMap">
<!--定义主键Id 有且仅有一个-->
<id column="shop_id" property="shopId"></id>
<!--定义基本数据类型-->
<result column="shop_name" property="shopName"></result>
<result column="shop_desc" property="shopDesc"></result>
<result column="shop_addr" property="shopAddr"></result>
<result column="phone" property="phone"></result>
<result column="shop_img" property="shopImg"></result>
<result column="priority" property="priority"></result>
<result column="create_time" property="createTime"></result>
<result column="last_edit_time" property="lastEditTime"></result>
<result column="enable_status" property="enableStatus"></result>
<result column="advice" property="advice"></result>
<!--定义复合数据类型-->
<association property="area" column="area_id" javaType="com.swust.emporium.pojo.Area">
<id column="area_id" property="areaId"></id>
<result column="area_name" property="areaName"></result>
</association>
<association property="owner" column="owner" javaType="com.swust.emporium.pojo.PersonInfo">
<id column="user_id" property="userId"></id>
<result column="name" property="name"></result>
</association>
<association property="shopCategory" column="shop_category_id" javaType="com.swust.emporium.pojo.ShopCategory">
<id column="shop_category_id" property="shopCategoryId"></id>
<result column="shop_category_name" property="shopCategoryName"></result>
</association>
</resultMap>
<select id="queryByShopId" resultMap="shopMap" parameterType="Long">
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
s.area_id = a.area_id
and
s.shop_category_id = sc.shop_category_id
and
s.shop_id = #{shopId}
</select>
<!--分页查询功能-->
<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.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.shopName != null">
and s.shop_name like '%${shopCondition.shopName}%'
</if>
<!--实现状态信息条件查询功能-->
<if test="shopCondition.enableStatus">
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>
<!--通过Id连接三张表-->
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.area != null
and
shopCondition.area.areaId != null">
and s.area_id = #{shopCondition.area.areaId}
</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.shopName != null">
and s.shop_name like '%${shopCondition.shopName}%'
</if>
<!--实现状态信息条件查询功能-->
<if test="shopCondition.enableStatus">
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>
<!--通过Id连接三张表-->
and
s.area_id = a.area_id
and
s.shop_category_id = sc.shop_category_id
</where>
</select>
</mapper>
//业务逻辑层
//为了更加符合实际项目的内容需求,在原始的包+impl实现类的实际基础上,引入enum枚举类存储店铺的相关操作信息,使用dto下的ShopExecution对原始信息的包装,并设计专属于Shop操作的异常类。
// 枚举类
public enum ShopStateEnum {
CHECK(0,"审核中"),
OFFLINE(-1,"非法店铺"),
SUCCESS(1, "操作成功"),
PASS(2, "通过认证"),
INNER_ERROR(-1001, "内部系统错误"),
NULL_SHOPID(-1002,"shop为空"),
NULL_SHOP(-1003,"shop信息为空");
private int state;
private String stateInfo;
/**
* 枚举构造方法
* @param state
* @param stateInfo
*/
private ShopStateEnum(int state, String stateInfo){
this.state = state;
this.stateInfo = stateInfo;
}
/***
* 根据传入的state值返回对应的数据
* @param state
* @return
*/
public static ShopStateEnum stateEnum(int state){
for (ShopStateEnum stateEnum : ShopStateEnum.values()){
if (stateEnum.getState() == state){
return stateEnum;
}
}
return null;
}
public String getStateInfo(){
return this.stateInfo;
}
public int getState(){
return this.state;
}
}
//店铺操作表示符合操作信息描述
@Getter
@Setter
public class ShopExecution {
//结果状态
private int state;
//状态描述
private String stateInfo;
//店铺数量
private int count;
//操作的Shop 增删改
private Shop shop;
//shop列表 查询
private List<Shop> shopList;
public ShopExecution(){}
// 店铺操作失败的时候使用的构造器
public ShopExecution(ShopStateEnum shopStateEnum){
this.state = shopStateEnum.getState();
this.stateInfo = shopStateEnum.getStateInfo();
}
// 店铺操作成功的时候使用构的造器
public ShopExecution(ShopStateEnum shopStateEnum, Shop shop){
this.state = shopStateEnum.getState();
this.stateInfo = shopStateEnum.getStateInfo();
this.shop = shop;
}
// 店铺操作成功的时候使用的构造器
public ShopExecution(ShopStateEnum shopStateEnum, List<Shop> shopList){
this.state = shopStateEnum.getState();
this.stateInfo = shopStateEnum.getStateInfo();
this.shopList = shopList;
}
}
// 店铺操作异常类 生成随机序列化ID需要在类名上使用Alt+Enter快捷键
public class ShopOperationException extends RuntimeException{
//生成随机序列化ID
private static final long serialVersionUID = -2293712388036514485L;
public ShopOperationException(String msg){
super(msg);
}
}
// web层
// web层相对于SSM框架所对应的controller层 这里是实现前端与后端的连接层 我采用的admin和management对不同的访问请求进行处理,admin主要处理html页面跳转请求,management主要负责shopoperation.js文件传递来的AJAX业务访问请求
@Controller
@RequestMapping("/shopadmin")
public class ShopManagementController {
@Autowired
private ShopService shopService;
@Autowired
private ShopCategoryService shopCategoryService;
@Autowired
private AreaService areaService;
/***
* 获取初始信息
* @return
*/
@RequestMapping(value = "/getshopinitinfo", method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> getShopInitInfo(HttpServletRequest request){
Map<String, Object> modelMap = new HashMap<>();
List<ShopCategory> shopCategoryLists = new ArrayList<>();
List<Area> areaLists = new ArrayList<>();
try {
ShopCategoryExecution sce = shopCategoryService.getShopCategoryList(new ShopCategory());
shopCategoryLists = sce.getShopCategoryList();
areaLists = areaService.getAreaList();
modelMap.put("shopCategoryList", shopCategoryLists);
modelMap.put("areaList", areaLists);
modelMap.put("success", true);
}catch (Exception e){
modelMap.put("success", false);
modelMap.put("errMsg", e.getMessage());
return modelMap;
}
return modelMap;
}
/**
* 注册店铺
* @param request
* @return
*/
@RequestMapping(value = "/registershop", method = RequestMethod.POST)
@ResponseBody //将返回的map自动转换为Json格式
private Map<String, Object> registerShop(HttpServletRequest request){
Map<String,Object> modelMap = new HashMap<>();
// 校验验证码是否一致
if (CodeUtils.checkVerifyCode(request)){
modelMap.put("success", false);
modelMap.put("errMsg", "校验码不能为空或输入错误...");
return modelMap;
}
// 接收并转换响应参数 店铺信息 图片信息
String shopStr = HttpServletRequestUtils.getString(request, "shopStr");
ObjectMapper mapper = new ObjectMapper();
Shop shop = null;
try {
shop = mapper.readValue(shopStr, Shop.class);
} catch (IOException 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 = (CommonsMultipartFile)multipartHttpServletRequest.getFile("shopImg");
}else {
modelMap.put("success",false);
modelMap.put("errMsg","上传图片不能为空");
return modelMap;
}
// 注册店铺
if (shop != null && shopImg != null){
// PersonInfo owner = new PersonInfo();
PersonInfo owner = (PersonInfo) request.getSession().getAttribute("user"); // 从session域中获取店铺信息
// Session TODO ...
// owner.setUserId(1L);
shop.setOwner(owner);
String fileName = shopImg.getOriginalFilename();
ShopExecution se = null;
try {
ImageHolder imageHolder = new ImageHolder();
imageHolder.setImage(shopImg.getInputStream());
imageHolder.setImageName(fileName);
se = shopService.addShop(shop, imageHolder);
} catch (IOException e) {
modelMap.put("success",false);
modelMap.put("errMsg", e.getMessage());
return modelMap;
}
// 判断店铺是否创建成功 同时将店铺信息更新到session中
if (se.getState() == ShopStateEnum.CHECK.getState()){
modelMap.put("success",true);
// 将店铺保存到session域内
// unchecked 去除警告信息
@SuppressWarnings("unchecked")
List<Shop> shopList = (List<Shop>) request.getSession().getAttribute("shopList");
//是否是第一次创建店铺
if (shopList.size() == 0 || shopList == null){
shopList = new ArrayList<>();
}
shopList.add(se.getShop());
request.getSession().setAttribute("shopList", shopList);
return modelMap;
}else {
modelMap.put("success",false);
modelMap.put("errMsg", se.getStateInfo());
return modelMap;
}
}else {
modelMap.put("success",false);
modelMap.put("errMsg","请输入店铺信息");
return modelMap;
}
}
// /**
// * 将InputStream转换成file对象
// * 该方法已废弃
// * @param inputStream
// * @param file
// */
// private static void InputStramToFile(InputStream inputStream, File file){
// FileOutputStream os = null;
// try {
// os = new FileOutputStream(file);
// int bytesRead = 0;
// // 读取数据的buffer缓冲区
// byte[] buffer = new byte[1024];
// // 从输入流读取一些字节数,并将它们存储到缓冲区buffer中。
// while ((bytesRead = inputStream.read(buffer)) != -1){
// // 需要理解
// os.write(buffer,0,bytesRead);
// }
// } catch (Exception e) {
// throw new RuntimeException("调用InputStramToFile方法流解析异常 ==> " + e.getMessage());
// } finally {
// if (os != null){
// try {
// os.close();
// } catch (IOException e) {
// throw new RuntimeException("调用InputStramToFile方法关闭输出流异常 ==> " + e.getMessage());
// }
// }
// if (inputStream != null){
// try {
// inputStream.close();
// } catch (IOException e) {
// throw new RuntimeException("调用InputStramToFile方法关闭输入流异常 ==> " + e.getMessage());
// }
// }
// }
// }
/**
* 基于传入的shopId查询店铺信息
* @param request
* @return
*/
@RequestMapping(value = "/getshopbyid", method = RequestMethod.GET)
@ResponseBody
private Map<String, Object> getShopById(HttpServletRequest request){
Map<String, Object> modelMap = new HashMap<>();
Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
Long id = currentShop.getShopId();
System.out.println(id);
long shopId = HttpServletRequestUtils.getLong(request, "shopId");
if (shopId > -1){
try {
Shop shop = shopService.getByShopId(shopId);
List<Area> list = areaService.getAreaList();
modelMap.put("shop", shop);
modelMap.put("areaList", list);
modelMap.put("success", true);
return modelMap;
}catch (Exception e){
modelMap.put("success", false);
modelMap.put("errMsg", e.getMessage());
return modelMap;
}
}else {
modelMap.put("success",false);
modelMap.put("errMsg","店铺信息查询错误");
return modelMap;
}
}
/**
* 更新店铺信息
* @param request
* @return
*/
@RequestMapping(value = "/modifyshop", method = RequestMethod.POST)
@ResponseBody
private Map<String, Object> modifyShop(HttpServletRequest request){
Map<String, Object> modelMap = new HashMap<>();
// 校验码判断
if (CodeUtils.checkVerifyCode(request)){
modelMap.put("success", false);
modelMap.put("errMsg", "请输入正确的验证码");
return modelMap;
}
// 格式化shop对象
ObjectMapper mapper = new ObjectMapper();
// 获取基于Ajax请求传递过来的店铺信息参数
String shopStr = HttpServletRequestUtils.getString(request, "shopStr");
Shop shop = null;
try {
shop = mapper.readValue(shopStr, Shop.class);
} catch (IOException e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.getMessage());
return modelMap;
}
//判断是否具有图片上传信息流
HttpServletRequest httpServletRequest;
CommonsMultipartResolver commonsMultipartResolver = new
CommonsMultipartResolver(request.getSession().getServletContext());
CommonsMultipartFile shopImg = null;
if (commonsMultipartResolver.isMultipart(request)){
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
shopImg = (CommonsMultipartFile)multipartHttpServletRequest.getFile("shopImg");
}else {
modelMap.put("success", false);
modelMap.put("errMsg", "图片信息解析错误");
return modelMap;
}
ShopExecution shopExecution = null;
// 更新信息
if (shop != null && shop.getShopId() != null){
if (shopImg != null){
InputStream shopImgInputStream = null;
try {
shopImgInputStream = shopImg.getInputStream();
String fileName = shopImg.getOriginalFilename();
ImageHolder imageHolder = new ImageHolder();
imageHolder.setImageName(fileName);
imageHolder.setImage(shopImgInputStream);
shopExecution = shopService.modifyShop(shop, imageHolder);
} catch (IOException e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.getMessage());
return modelMap;
}
}else {
// 上传图片信息为空 选择只更新其余信息
try {
shopExecution = shopService.modifyShop(shop, null) ;
}catch (Exception e){
modelMap.put("success", false);
modelMap.put("errMsg", e.getMessage());
return modelMap;
}
}
}
// 判读店铺是否更新成功
if (shopExecution != null){
int state = shopExecution.getState();
String stateInfo = shopExecution.getStateInfo();
if (state == ShopStateEnum.SUCCESS.getState()){
modelMap.put("success", true);
return modelMap;
}else {
modelMap.put("success", false);
modelMap.put("errMsg","状态码返回异常");
return modelMap;
}
}else {
modelMap.put("success", false);
modelMap.put("errMsg","店铺更新异常");
return modelMap;
}
}
/***
* 店铺信息分页查询模块功能开发
* controller
* @param request
* @return
*/
@RequestMapping(value = "/getshoplist", method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> getShopList(HttpServletRequest request){
Map<String, Object> modelMap = new HashMap<>();
// TODO...这里有bug userLogin和personInfo的Id必须一致
// session中获取登陆信息
UserLogin userLogin = (UserLogin) request.getSession().getAttribute("userLogin");
int uid = userLogin.getUid();
PersonInfo user = new PersonInfo();
user.setUserId(new Long(uid));
request.getSession().setAttribute("user", user);
user = (PersonInfo) request.getSession().getAttribute("user");
Long userId = user.getUserId();
List<Shop> shopList = new ArrayList<>();
ShopExecution se = null;
try {
Shop shopCondition = new Shop();
shopCondition.setOwner(user);
se = shopService.getShopList(shopCondition, 0, 100);
modelMap.put("success", true);
modelMap.put("shopList", se.getShopList());
// 将shopList保存到session中
request.getSession().setAttribute("shopList",se.getShopList());
modelMap.put("user",user);
return modelMap;
}catch (Exception e){
modelMap.put("success", false);
modelMap.put("errMsg", e.getMessage());
return modelMap;
}
}
/***
* 获取店铺管理相关信息
* @param request
* @return
*/
@RequestMapping(value = "getshopmanagementinfo", method = RequestMethod.GET)
@ResponseBody
private Map<String, Object> getShopManagementInfo(HttpServletRequest request){
Map<String, Object> modelMap = new HashMap<>();
long shopId = HttpServletRequestUtils.getLong(request, "shopId");
if (shopId <= 0){
// 尝试从session中获取店铺Id信息
Object currenShopObj = request.getSession().getAttribute("currentShop");
if (currenShopObj == null){
// 重定向回店铺页面
modelMap.put("redirect", true);
// 访问Html页面
modelMap.put("url", "/emporium/shopadmin/shoplist");
}else {
Shop currenShop = (Shop) currenShopObj;
modelMap.put("redirect", false);
modelMap.put("shopId", currenShop.getShopId());
}
}else {
Shop current = new Shop();
current.setShopId(shopId);
request.getSession().setAttribute("currentShop", current);
modelMap.put("redirect", false);
}
return modelMap;
}
}
@Controller
@RequestMapping(value = "shopadmin", method = RequestMethod.GET)
public class ShopAdminController {
@RequestMapping(value = "/shopoperation")
public String shopOperation(){
return "shop/shopoperation";
}
@RequestMapping(value = "/shoplist")
public String shopList(){
return "shop/shoplist";
}
@RequestMapping(value = "/shopmanagement")
public String shopManagement(){
return "shop/shopmanagement";
}
}
// 前端主要使用Html+Css渲染页面,为快速开发,缩短研发周期,本项目使用 Sui Mobile进行开发设计,借助原始的模板引擎获取数据
// 对于前端所展示需要的数据,主要在页面加载时调用相应的javas文件获取后台数据
//对于get请求,我使用$.getJSON方法通过访问特定的URL获取数据,对于post请求选择使用Ajax请求获取对应的数据
// 获取初始信息填充到前端界面
// 当点击提交时通过Ajax将请求转发到后台
$(function (){
// 解析get请求的url上的shopId
var shopId = getQueryString('shopId');
// 如果shopId为空则证明是注册店铺否则代表修改店铺信息
var isEdit = shopId ? true : false;
// 完成信息初始化 localhost:8888 + URL
var initUrl = '/emporium/shopadmin/getshopinitinfo';
// 注册信息
var registerShopUrl = '/emporium/shopadmin/registershop';
// 根据店铺Id获取店铺信息
var shopInfoUrl = '/emporium/shopadmin/getshopbyid?shopId=' + shopId;
// 更新店铺信息
var editShopUrl = '/emporium/shopadmin/modifyshop';
if (!isEdit){
getShopInitInfo();
}else {
getShopInfo(shopId);
}
function getShopInfo(shopId){
$.getJSON(shopInfoUrl,function (data){
if (data.success){
var shop = data.shop;
// 根据查询到的值给html页面内控件赋值
$('#shop-name').val(shop.shopName);
$('#shop-addr').val(shop.shopAddr);
$('#shop-phone').val(shop.phone);
$('#shop-desc').val(shop.shopDesc);
// 将shopCategory的内容以option形式进行保存
var shopCategory = '<option data-id="'
+ shop.shopCategory.shopCategoryId + '"selected>'
+ shop.shopCategory.shopCategoryName + '</option>';
// 获取下拉列表
var tempAreahtml = '';
data.areaList.map(function (item, index){
tempAreahtml += '<option data-id="' + item.areaId
+ '">' + item.areaName + '</option>';
});
$('#shop-category').html(shopCategory);//控件绑定属性
// 设置店铺信息无法进行修改
$('#shop-category').attr('disabled','disabled');
$('#area').html(tempAreahtml);
// 默认选择
$('#area option[data-id="' + shop.area.areaId + "']").attr('selected',"selected");
}
});
}
function getShopInitInfo(){
$.getJSON(initUrl, function(data){
if (data.success){
var tempHtml = '';
var tempAreaHtml = '';
data.shopCategoryList.map(function (item,index){
tempHtml += '<option data-id="' + item.shopCategoryId + '">'
+item.shopCategoryName + '</option>>';
});
data.areaList.map(function (item,index){
tempAreaHtml += '<option data-id="' + item.areaId + '">'
+item.areaName + '</option>';
});
// 将信息发送到html文件
$('#shop-category').html(tempHtml);
$('#area').html(tempAreaHtml);
}
});
}
// 点击提交时 进行响应
$('#submit').click(function (){
var shop = {};
if (isEdit){
shop.shopId = shopId;
}
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.append('shopImg', shopImg);
formData.append('shopStr', JSON.stringify(shop));
// 验证码
var verifyCodeActual = $('#j_captcha').val();
if (!verifyCodeActual){
$.toast('请输入验证码!');
return ;
}
formData.append('verifyCodeActual', verifyCodeActual);
$.ajax({
url:isEdit?editShopUrl:registerShopUrl,
type:'POST',
data:formData,
contentType:false,
processData:false,
cache:false,
success:function (data){
if (data.success){
alert('success!');
$.toast('提交成功 !');
}else {
// alert('error!');
$.toast('提交失败' + data.errMsg);
}
// 每次执行ok后更换验证码
$('#captcha_img').click();
}
})
})
})