vue post删除 兼容批量删除和单个删除
核心
const idsTemp = row.personId ? [row.personId] : this.ids
完整代码
// 删除
handleDelete(row) {
const idsTemp = row.personId ? [row.personId] : this.ids
this.$confirm('删除后无法恢复, 确定吗?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
this.loading = true
api.deleteByIds(idsTemp).then(res => {
this.loading = false
if (res.code !== 200) {
this.$message.error(res.msg)
return
}
this.$message.success(res.msg)
this.getByPage()
})
}).catch(() => {
this.msgError('删除已取消')
this.loading = false
})
},
// 删除
deleteByIds(ids) {
return request({
url: `/${groupName}/deleteByIds`,
method: 'post',
data: { ids }
})
},
/**
* 删除
*
* @param dto
* @return
*/
@PostMapping("deleteByIds")
@PreAuthorize("hasAuthority('sys:delete')")
@ApiOperation("删除员工")
@Log(title = "删除员工", businessType = BusinessType.DELETE)
public Result<?> deleteByIds(@RequestBody IdsDto dto) {
return service.deleteByIds(dto.getIds());
}
/**
* 删除
*
* @param ids
* @return
*/
Result<?> deleteByIds(List<Long> ids);
/**
* 删除
*
* @param ids
* @return
*/
@Override
public Result<?> deleteByIds(List<Long> ids) {
if (CollectionUtils.isEmpty(ids)) {
return Result.fail("传入ids为空");
}
// 验证并删除
String currentName = CurrentUserUtil.getCurrentUserInfo().getName() + "-删";
int rows = 0;
Person person;
for(Long id: ids){
// 查询数据
person = mapper.selectById(id);
if (ObjectUtils.isEmpty(person)) {
return Result.fail("未查询到有效的数据信息");
}
// 记录删除人
person.setUpdateBy(currentName);
person.setUpdateTime(new Date());
mapper.updateById(person);
// 删除
rows += mapper.deleteById(id);
}
return Result.toAjax(rows);
}