项目二05
1.攻略新增
1.1.图片上传
OSS服务器
Bucket:文件存储
工具类
public class UploadUtil {
//阿里域名
public static final String ALI_DOMAIN = "https://wolf2w-54.oss-cn-guangzhou.aliyuncs.com/";
public static String uploadAli(MultipartFile file) throws Exception {
//生成文件名称
String uuid = UUID.randomUUID().toString();
String orgFileName = file.getOriginalFilename();//获取真实文件名称 xxx.jpg
String ext= "." + FilenameUtils.getExtension(orgFileName);//获取拓展名字.jpg
String fileName =uuid + ext;//xxxxxsfsasa.jpg
// Endpoint以杭州为例,其它Region请按实际情况填写。
String endpoint = "http://oss-cn-guangzhou.aliyuncs.com";
// 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,
// 请登录 https://ram.console.aliyun.com 创建。
String accessKeyId = "LTAI4FygtEGrZts8Q6rmgsgW";
String accessKeySecret = "IwYr0Ezr3YFbcMo0YteuSlbvaY7Sl1";
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId,accessKeySecret);
// 上传文件流。
ossClient.putObject("wolf2w-54", fileName, file.getInputStream());
// 关闭OSSClient。
ossClient.shutdown();
return ALI_DOMAIN + fileName;
}
}
图片上传路径
@RequestMapping("/uploadImg")
@ResponseBody
public Object uploadImg(MultipartFile pic) throws Exception {
//需要共享服务器
String path = UploadUtil.uploadAli(pic);
return path;
}
1.2.分类
数据结构
public class xxxx{
private String name;
private List<分类> 值;
}
表现层
@RequestMapping("/input")
public String input(Model model, Long id) {
//有id的实体类
List<CatalogVO> catalogs = strategyCatalogService.queryCatalogVOList();
model.addAttribute("catalogs",catalogs);
model.addAttribute("themes", strategyThemeService.list());
return "strategy/input";
}
实现
@Override
public List<CatalogVO> queryCatalogVOList() {
List<CatalogVO> vos = new ArrayList<>();
QueryWrapper<StrategyCatalog> wrapper = new QueryWrapper<>();
wrapper.select("dest_name", "group_concat(id) ids", "group_concat(name) names");
wrapper.groupBy("dest_name");
List<Map<String, Object>> maps = listMaps(wrapper);
for (Map<String, Object> map : maps) {
CatalogVO catalogVO = new CatalogVO();
catalogVO.setDestName(map.get("dest_name").toString());
String key = map.get("ids").toString();
String value = map.get("names").toString();
//---------关键
List<StrategyCatalog> list = create(key, value);
catalogVO.setStrategyCatalogList(list);
vos.add(catalogVO);
}
return vos;
}
create方法
private List<StrategyCatalog> create(String key, String value) {
List<StrategyCatalog> strategyCatalogList = new ArrayList<>();
String[] split1 = key.split(",");
String[] split2 = value.split(",");
for (int i = 0; i < split1.length; i++) {
StrategyCatalog strategyCatalog = new StrategyCatalog();
strategyCatalog.setId(Long.valueOf(split1[i]));
strategyCatalog.setName(split2[i]);
strategyCatalogList.add(strategyCatalog);
}
return strategyCatalogList;
}
1.3.富文本编辑器
下载插件
引入依赖
texterea
富文本的图片保存
1.4.保存和编辑
表现层
@RequestMapping("/input")
public String input(Model model, Long id) {
//有id的实体类
List<CatalogVO> catalogs = strategyCatalogService.queryCatalogVOList();
if(id!=null){
Strategy strategy = strategyService.getById(id);
StrategyContent content = strategyService.getContent(id);
strategy.setContent(content);
model.addAttribute("strategy",strategy);
}
model.addAttribute("catalogs",catalogs);
model.addAttribute("themes", strategyThemeService.list());
return "strategy/input";
}
实现层
因为打破了第三范式,维护冗余字段需要特别注意
@Override
public boolean saveOrUpdate(Strategy entity) {
//--------------封装目的地对象--------------------
//获取分类的id
Long catalogId = entity.getCatalogId();
//根据分类对象查询目的地id,根据id查询目的地对象
StrategyCatalog strategyCatalog = strategyCatalogService.getById(catalogId);
Long destId = strategyCatalog.getDestId();
Destination destination = destinationSerivce.getById(destId);
entity.setDestId(destination.getId());
entity.setDestName(destination.getName());
//---------------主题名称
StrategyTheme strategyTheme = strategyThemeService.getById(entity.getThemeId());
entity.setThemeName(strategyTheme.getName());
//---------------分类名称
entity.setCatalogName(strategyCatalog.getName());
//---------------是否国外
List<Destination> destinations = destinationSerivce.queryToastByParentId(entity.getDestId());
if(destinations !=null && destinations.size() >0){
Destination first = destinations.get(0);
if("中国".equals(first.getName())){
entity.setIsabroad(Strategy.ABROAD_NO);
}else{
entity.setIsabroad(Strategy.ABROAD_YES);
}
}
//---------------创建时间
//---------------各种统计数
//---------------文章内容处理
if(entity.getId()==null){
entity.setCreateTime(new Date());
entity.setReplynum(0);
entity.setFavornum(0);
entity.setViewnum(0);
entity.setThumbsupnum(0);
entity.setSharenum(0);
save(entity);
StrategyContent content = entity.getContent();
content.setId(entity.getId());
strategyContentMapper.insert(content);
}else{
updateById(entity);
StrategyContent content = entity.getContent();
content.setId(entity.getId());
strategyContentMapper.updateById(content);
}
return true;
}
2.前端目的地
目的地吐司
目的地概况
分类的详情页
目的地攻略
攻略的详情页
2.1.吐司
@GetMapping("/toasts")
public Object toasts(Long destId){
return JsonResult.success(destinationSerivce.queryToastByParentId(destId));
}
2.2.概况
数据结构:List
目的地分类,该分类下的文章(集合)
表现层
@GetMapping("/catalogs")
public Object catalogs(Long destId){
return JsonResult.success(strategyCatalogService.queryByDestId(destId));
}
实现层
@Override
public List<StrategyCatalog> queryByDestId(Long destId) {
QueryWrapper<StrategyCatalog> wrapper = new QueryWrapper<>();
wrapper.eq("dest_id", destId);
//.orderByDesc("viewnum")
// .last("limit 3");
List<StrategyCatalog> catalogs = list(wrapper);
//遍历每个分类集合下的文章集合对象
for (StrategyCatalog catalog : catalogs) {
List<Strategy> list = strategyService.queryByCatalogId(catalog.getId());
catalog.setStrategies(list);
}
return catalogs;
}
查询攻略集合
@Override
public List<Strategy> queryByCatalogId(Long id) {
QueryWrapper<Strategy> wrapper = new QueryWrapper<>();
wrapper.eq("catalog_id", id);
return list(wrapper);
}
2.3.概况详情
左边显示分类,右边显示该分类的所有文章,同时显示第一篇文章内容
表现层
@GetMapping("/content")
public Object content(Long id) {
return JsonResult.success(strategyService.getContent(id));
}
实现层
@Override
public StrategyContent getContent(Long id) {
return strategyContentMapper.selectById(id);
}
2.4.前三篇攻略
表现层
@GetMapping("/strategies/viewnumTop3")
public Object viewnumTop3(Long destId){
return JsonResult.success(strategyService.queryViewTop3(destId));
}
实现层
@Override
public List<Strategy> queryViewTop3(Long destId) {
QueryWrapper<Strategy> wrapper = new QueryWrapper<>();
wrapper.eq("dest_id", destId)
.orderByDesc("viewnum")
.last("limit 3");
return list(wrapper);
}
2.5.攻略详情
表现层
@GetMapping("/detail")
public Object detail(Long id) {
Strategy strategy = strategyService.getById(id);
StrategyContent content = strategyService.getContent(id);
strategy.setContent(content);
return JsonResult.success(strategy);
}
2.6.该目的地的所有攻略
攻略主题
@GetMapping("/themes")
public Object themes() {
List<StrategyTheme> strategyThemes = strategyThemeService.list();
return JsonResult.success(strategyThemes);
}