<template>
<el-dialog
model-value="dialogVisible"
:title="dialogTitle"
width="30%"
@close="handleClose"
>
<el-form
ref="formRef"
:model="form"
:rules="rules"
label-width="100px"
>
<el-form-item label="大类名称" prop="name">
<el-input v-model="form.name"/>
</el-form-item>
<el-form-item label="大类描述" prop="remark">
<el-input v-model="form.remark" type="textarea" :rows="4"/>
</el-form-item>
</el-form>
<template
<span class="dialog-footer">
<el-button @click="handleClose">取消</el-button>
<el-button type="primary" @click="handleConfirm">确认</el-button>
</span>
</template>
</el-dialog>
</template>
<script setup>
import {defineEmits, defineProps, ref, watch} from "vue";
import axios from "@/util/axios";
import { ElMessage} from "element-plus";
const props=defineProps({
id:{
type:Number,
default:-1,
required:true
},
dialogTitle:{
type:String,
default:'',
required:true
}
})
const form=ref({
id:-1,
name:"",
remark:""
})
const rules=ref({
name:[
{required:true,message:'请输入商品大类名称'}
],
remark:[
{required:true,message:'请输入商品大类描述 '}
]
})
const formRef=ref(null)
const initFormData=async(id)=>{
const res=await axios.get("admin/bigType/"+id);
form.value=res.data.bigType;
}
watch(
()=>props.id,
()=>{
let id=props.id;
if(id!=-1){
initFormData(id)
}
}
)
//定义父组件事件
const emits=defineEmits(['update:modelValue','initBigTypeList'])
const handleClose=()=>{
emits('update:modelValue',false)
}
const handleConfirm=()=>{
formRef.value.validate(async(valid)=>{
if(valid){
let result=await axios.post("admin/bigType/save",form.value);
let data=result.data;
if(data.code==0){
ElMessage.success("执行成功!")
formRef.value.resetFields()
emits("initBigTypeList")
handleClose()
}else{
ElMessage.error(data.msg)
}
}else{
console.log("fail")
return false
}
})
}
</script>
<style scoped>
</style>
<template>
<el-card>
<el-row :gutter="20" class="header">
<el-col :span="7">
<el-input placeholder="请输入商品大类名称..." v-model="queryForm.query" clearable></el-input>
</el-col>
<el-button type="primary" :icon="Search" @click="initBigTypeList">搜索</el-button>
<el-button type="primary" :icon="DocumentAdd" @click="handleDialogValue()">添加商品大类</el-button>
</el-row>
<el-table :data="tableData" stripe style="width: 100%">
<el-table-column prop="id" label="#ID" width="80"/>
<el-table-column prop="name" label="商品大类名称" width="200"/>
<el-table-column prop="image" label="商品大类图片" width="200">
<template v-slot="scope">
<img :src="getServerUrl()+'/image/bigType/'+scope.row.image" width="80" height="80"/>
</template>
</el-table-column>
<el-table-column prop="remark" label="商品大类描述"/>
<el-table-column prop="action" label="操作" width="300" fixed="right">
<template v-slot="scope">
<el-button type="success" @click="handleDialogValue(scope.row.id)">更换图片</el-button>
<el-button type="primary" :icon="Edit" @click="handleDialogValue(scope.row.id)"/>
<el-button type="danger" :icon="Delete" @click="handleDelete(scope.row.id)"/>
</template>
</el-table-column>
</el-table>
<el-pagination
v-model:currentPage="queryForm.pageNum"
v-model:page-size="queryForm.pageSize"
:page-size="[10,20,30,40,50]"
layout="total,sizes,prev,pager,next,jumper"
:total="total"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
</el-card>
<Dialog v-model="dialogVisible" :id="id" :dialogTitle="dialogTitle" @initBigTypeList="initBigTypeList"></Dialog>
</template>
<script setup>
import { Search ,Delete,Edit,DocumentAdd} from '@element-plus/icons-vue'
import { ref } from 'vue'
import axios,{getServerUrl} from '@/util/axios'
import Dialog from './components/dialog'
const queryForm=ref({
query:'',
pageNum:1,
pageSize:10
})
const total=ref(0)
const tableData=ref([])
const id=ref(-1)
const dialogTitle=ref('')
const dialogVisible=ref(false)
const initBigTypeList=async()=>{
const res=await axios.post("admin/bigType/list",queryForm.value)
tableData.value=res.data.bigTypeList;
total.value=res.data.total;
}
initBigTypeList();
const handleSizeChange=(pageSize)=>{
queryForm.value.pageNum=1;
queryForm.value.pageSize=pageSize;
initBigTypeList();
}
const handleCurrentChange=(pageNum)=>{
queryForm.value.pageNum=pageNum;
initBigTypeList();
}
const handleDialogValue=(bigTypeId)=>{
if(bigTypeId){
id.value=bigTypeId;
dialogTitle.value="商品大类修改"
}else{
id.value=-1;
dialogTitle.value="商品大类添加"
}
dialogVisible.value=true
}
</script>
<style lang="scss" scoped>
.header{
padding-bottom: 16px;
box-sizing: border-box;
}
.el-pagination{
padding-top: 15px;
box-sizing: border-box;
}
</style>
package com.java1234.controller.admin;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.java1234.entity.*;
import com.java1234.service.IBigTypeService;
import com.java1234.service.ISmallTypeService;
import com.java1234.util.DateUtil;
import com.java1234.util.StringUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 后台管理-商品大类Controller控制器
*/
@RestController
@RequestMapping("/admin/bigType")
public class AdminBigTypeController {
@Autowired
private IBigTypeService bigTypeService;
@Autowired
private ISmallTypeService smallTypeService;
@Value("${bigTypeImagesFilePath}")
private String bigTypeImagesFilePath;
/**
* 根据条件分页查询商品大类信息
* @param pageBean
* @return
*/
@RequestMapping("/list")
public R list(@RequestBody PageBean pageBean){
System.out.println(pageBean);
String query=pageBean.getQuery().trim();
Page<BigType> page=new Page<>(pageBean.getPageNum(),pageBean.getPageSize());
Page<BigType> pageResult = bigTypeService.page(page, new QueryWrapper<BigType>().like(StringUtil.isNotEmpty(query), "name", query));
Map<String,Object> map=new HashMap<>();
map.put("bigTypeList",pageResult.getRecords());
map.put("total",pageResult.getTotal());
return R.ok(map);
}
/**
* 添加或修改
* @param bigType
* @return
*/
@RequestMapping("/save")
public R save(@RequestBody BigType bigType){
if(bigType.getId()==null || bigType.getId() == -1){
bigTypeService.save(bigType);
}else{
bigTypeService.saveOrUpdate(bigType);
}
return R.ok();
}
/**
* 根据id查询
* @param id
* @return
*/
@GetMapping("/{id}")
public R findById(@PathVariable(value = "id")Integer id){
BigType bigType = bigTypeService.getById(id);
Map<String,Object> map=new HashMap<>();
map.put("bigType",bigType);
return R.ok(map);
}
/**
* 删除
* @param id
* @return
*/
@GetMapping("/delete/{id}")
public R delete(@PathVariable(value = "id")Integer id){
//加个判断 大类下面如果有小类 返回报错提示
if(smallTypeService.count(new QueryWrapper<SmallType>().eq("bigTypeId",id))>0){
return R.error(500,"大类下面有小类信息不能删除");
}else{
bigTypeService.removeById(id);
return R.ok();
}
}
/**
* 上传商品大类图片
* @param file
* @return
* @throws Exception
*/
@RequestMapping("/uploadImage")
public Map<String,Object> uploadImage(MultipartFile file)throws Exception{
Map<String,Object> resultMap=new HashMap<>();
if(!file.isEmpty()){
//获取文件名
String originalFilename = file.getOriginalFilename();
String suffixName=originalFilename.substring(originalFilename.lastIndexOf("."));
String newFileName= DateUtil.getCurrentDateStr() + suffixName;
FileUtils.copyInputStreamToFile(file.getInputStream(),new File(bigTypeImagesFilePath+newFileName));
resultMap.put("code",0);
resultMap.put("msg","上传成功");
Map<String,Object> dataMap=new HashMap<>();
dataMap.put("title",newFileName);
dataMap.put("src","/image/bigType/"+newFileName);
resultMap.put("dat",dataMap);
}
return resultMap;
}
}