Commit 6f8bedd4 authored by Menethil's avatar Menethil
Browse files

修改StoreService

parent 23b1821a
...@@ -97,8 +97,8 @@ public class AdminGoodsController { ...@@ -97,8 +97,8 @@ public class AdminGoodsController {
try { try {
//将生成的分享图片地址写入数据库 //将生成的分享图片地址写入数据库
qCodeService.createGoodShareImage(goods.getId().toString(), goods.getPicUrl(), goods.getName()); String url = qCodeService.createGoodShareImage(goods.getId().toString(), goods.getPicUrl(), goods.getName());
goods.setShareUrl(qCodeService.getShareImageUrl(goods.getId().toString())); goods.setShareUrl(url);
// 商品基本信息表litemall_goods // 商品基本信息表litemall_goods
goodsService.updateById(goods); goodsService.updateById(goods);
...@@ -194,8 +194,8 @@ public class AdminGoodsController { ...@@ -194,8 +194,8 @@ public class AdminGoodsController {
goodsService.add(goods); goodsService.add(goods);
//将生成的分享图片地址写入数据库 //将生成的分享图片地址写入数据库
qCodeService.createGoodShareImage(goods.getId().toString(), goods.getPicUrl(), goods.getName()); String url = qCodeService.createGoodShareImage(goods.getId().toString(), goods.getPicUrl(), goods.getName());
goods.setShareUrl(qCodeService.getShareImageUrl(goods.getId().toString())); goods.setShareUrl(url);
goodsService.updateById(goods); goodsService.updateById(goods);
// 商品规格表litemall_goods_specification // 商品规格表litemall_goods_specification
......
...@@ -2,25 +2,18 @@ package org.linlinjava.litemall.admin.web; ...@@ -2,25 +2,18 @@ package org.linlinjava.litemall.admin.web;
import org.linlinjava.litemall.admin.annotation.LoginAdmin; import org.linlinjava.litemall.admin.annotation.LoginAdmin;
import org.linlinjava.litemall.core.storage.StorageService; import org.linlinjava.litemall.core.storage.StorageService;
import org.linlinjava.litemall.core.util.CharUtil;
import org.linlinjava.litemall.core.util.ResponseUtil; import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order; import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort; import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallStorage; import org.linlinjava.litemall.db.domain.LitemallStorage;
import org.linlinjava.litemall.db.service.LitemallStorageService; import org.linlinjava.litemall.db.service.LitemallStorageService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -35,29 +28,13 @@ public class AdminStorageController { ...@@ -35,29 +28,13 @@ public class AdminStorageController {
@Autowired @Autowired
private LitemallStorageService litemallStorageService; private LitemallStorageService litemallStorageService;
private String generateKey(String originalFilename){
int index = originalFilename.lastIndexOf('.');
String suffix = originalFilename.substring(index);
String key = null;
LitemallStorage storageInfo = null;
do{
key = CharUtil.getRandomString(20) + suffix;
storageInfo = litemallStorageService.findByKey(key);
}
while(storageInfo != null);
return key;
}
@GetMapping("/list") @GetMapping("/list")
public Object list(@LoginAdmin Integer adminId, public Object list(@LoginAdmin Integer adminId,
String key, String name, String key, String name,
@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "10") Integer limit, @RequestParam(defaultValue = "10") Integer limit,
@Sort @RequestParam(defaultValue = "add_time") String sort, @Sort @RequestParam(defaultValue = "add_time") String sort,
@Order @RequestParam(defaultValue = "desc") String order){ @Order @RequestParam(defaultValue = "desc") String order) {
List<LitemallStorage> storageList = litemallStorageService.querySelective(key, name, page, limit, sort, order); List<LitemallStorage> storageList = litemallStorageService.querySelective(key, name, page, limit, sort, order);
int total = litemallStorageService.countSelective(key, name, page, limit, sort, order); int total = litemallStorageService.countSelective(key, name, page, limit, sort, order);
Map<String, Object> data = new HashMap<>(); Map<String, Object> data = new HashMap<>();
...@@ -69,40 +46,21 @@ public class AdminStorageController { ...@@ -69,40 +46,21 @@ public class AdminStorageController {
@PostMapping("/create") @PostMapping("/create")
public Object create(@LoginAdmin Integer adminId, @RequestParam("file") MultipartFile file) throws IOException { public Object create(@LoginAdmin Integer adminId, @RequestParam("file") MultipartFile file) throws IOException {
if(adminId == null){ if (adminId == null) {
return ResponseUtil.unlogin(); return ResponseUtil.unlogin();
} }
String originalFilename = file.getOriginalFilename(); String originalFilename = file.getOriginalFilename();
InputStream inputStream = null; storageService.store(file.getInputStream(), file.getSize(), file.getContentType(), originalFilename);
try { return ResponseUtil.ok();
inputStream = file.getInputStream();
} catch (IOException e) {
e.printStackTrace();
return ResponseUtil.badArgumentValue();
}
String key = generateKey(originalFilename);
storageService.store(file.getInputStream(), file.getSize(), file.getContentType(), key);
String url = storageService.generateUrl(key);
LitemallStorage storageInfo = new LitemallStorage();
storageInfo.setName(originalFilename);
storageInfo.setSize((int)file.getSize());
storageInfo.setType(file.getContentType());
storageInfo.setAddTime(LocalDateTime.now());
storageInfo.setModified(LocalDateTime.now());
storageInfo.setKey(key);
storageInfo.setUrl(url);
litemallStorageService.add(storageInfo);
return ResponseUtil.ok(storageInfo);
} }
@PostMapping("/read") @PostMapping("/read")
public Object read(@LoginAdmin Integer adminId, @NotNull Integer id) { public Object read(@LoginAdmin Integer adminId, @NotNull Integer id) {
if(adminId == null){ if (adminId == null) {
return ResponseUtil.unlogin(); return ResponseUtil.unlogin();
} }
LitemallStorage storageInfo = litemallStorageService.findById(id); LitemallStorage storageInfo = litemallStorageService.findById(id);
if(storageInfo == null){ if (storageInfo == null) {
return ResponseUtil.badArgumentValue(); return ResponseUtil.badArgumentValue();
} }
return ResponseUtil.ok(storageInfo); return ResponseUtil.ok(storageInfo);
...@@ -110,7 +68,7 @@ public class AdminStorageController { ...@@ -110,7 +68,7 @@ public class AdminStorageController {
@PostMapping("/update") @PostMapping("/update")
public Object update(@LoginAdmin Integer adminId, @RequestBody LitemallStorage litemallStorage) { public Object update(@LoginAdmin Integer adminId, @RequestBody LitemallStorage litemallStorage) {
if(adminId == null){ if (adminId == null) {
return ResponseUtil.unlogin(); return ResponseUtil.unlogin();
} }
litemallStorageService.update(litemallStorage); litemallStorageService.update(litemallStorage);
...@@ -119,7 +77,7 @@ public class AdminStorageController { ...@@ -119,7 +77,7 @@ public class AdminStorageController {
@PostMapping("/delete") @PostMapping("/delete")
public Object delete(@LoginAdmin Integer adminId, @RequestBody LitemallStorage litemallStorage) { public Object delete(@LoginAdmin Integer adminId, @RequestBody LitemallStorage litemallStorage) {
if(adminId == null){ if (adminId == null) {
return ResponseUtil.unlogin(); return ResponseUtil.unlogin();
} }
litemallStorageService.deleteByKey(litemallStorage.getKey()); litemallStorageService.deleteByKey(litemallStorage.getKey());
......
...@@ -24,7 +24,7 @@ public class QCodeService { ...@@ -24,7 +24,7 @@ public class QCodeService {
private StorageService storageService; private StorageService storageService;
public void createGrouponShareImage(String goodName, String goodPicUrl, LitemallGroupon groupon) { public String createGrouponShareImage(String goodName, String goodPicUrl, LitemallGroupon groupon) {
try { try {
//创建该商品的二维码 //创建该商品的二维码
File file = wxMaService.getQrcodeService().createWxaCodeUnlimit("groupon," + groupon.getId(), "pages/index/index"); File file = wxMaService.getQrcodeService().createWxaCodeUnlimit("groupon," + groupon.getId(), "pages/index/index");
...@@ -33,7 +33,9 @@ public class QCodeService { ...@@ -33,7 +33,9 @@ public class QCodeService {
byte[] imageData = drawPicture(inputStream, goodPicUrl, goodName, SystemConfig.getMallName()); byte[] imageData = drawPicture(inputStream, goodPicUrl, goodName, SystemConfig.getMallName());
ByteArrayInputStream inputStream2 = new ByteArrayInputStream(imageData); ByteArrayInputStream inputStream2 = new ByteArrayInputStream(imageData);
//存储分享图 //存储分享图
storageService.store(inputStream2, imageData.length, "image/jpeg", getKeyName(groupon.getId().toString())); String url = storageService.store(inputStream2, imageData.length, "image/jpeg", getKeyName(groupon.getId().toString()));
return url;
} catch (WxErrorException e) { } catch (WxErrorException e) {
e.printStackTrace(); e.printStackTrace();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
...@@ -43,6 +45,8 @@ public class QCodeService { ...@@ -43,6 +45,8 @@ public class QCodeService {
} catch (FontFormatException e) { } catch (FontFormatException e) {
e.printStackTrace(); e.printStackTrace();
} }
return "";
} }
...@@ -53,9 +57,9 @@ public class QCodeService { ...@@ -53,9 +57,9 @@ public class QCodeService {
* @param goodPicUrl * @param goodPicUrl
* @param goodName * @param goodName
*/ */
public void createGoodShareImage(String goodId, String goodPicUrl, String goodName) { public String createGoodShareImage(String goodId, String goodPicUrl, String goodName) {
if (!SystemConfig.isAutoCreateShareImage()) if (!SystemConfig.isAutoCreateShareImage())
return; return "";
try { try {
//创建该商品的二维码 //创建该商品的二维码
...@@ -65,7 +69,9 @@ public class QCodeService { ...@@ -65,7 +69,9 @@ public class QCodeService {
byte[] imageData = drawPicture(inputStream, goodPicUrl, goodName, SystemConfig.getMallName()); byte[] imageData = drawPicture(inputStream, goodPicUrl, goodName, SystemConfig.getMallName());
ByteArrayInputStream inputStream2 = new ByteArrayInputStream(imageData); ByteArrayInputStream inputStream2 = new ByteArrayInputStream(imageData);
//存储分享图 //存储分享图
storageService.store(inputStream2, imageData.length, "image/jpeg", getKeyName(goodId)); String url = storageService.store(inputStream2, imageData.length, "image/jpeg", getKeyName(goodId));
return url;
} catch (WxErrorException e) { } catch (WxErrorException e) {
e.printStackTrace(); e.printStackTrace();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
...@@ -75,10 +81,8 @@ public class QCodeService { ...@@ -75,10 +81,8 @@ public class QCodeService {
} catch (FontFormatException e) { } catch (FontFormatException e) {
e.printStackTrace(); e.printStackTrace();
} }
}
public String getShareImageUrl(String goodId) { return "";
return storageService.generateUrl(getKeyName(goodId));
} }
private String getKeyName(String goodId) { private String getKeyName(String goodId) {
......
...@@ -16,7 +16,7 @@ public interface Storage { ...@@ -16,7 +16,7 @@ public interface Storage {
* @param inputStream 文件输入流 * @param inputStream 文件输入流
* @param contentLength 文件长度 * @param contentLength 文件长度
* @param contentType 文件类型 * @param contentType 文件类型
* @param keyName 文件索引 * @param keyName 文件名
*/ */
void store(InputStream inputStream, long contentLength, String contentType, String keyName); void store(InputStream inputStream, long contentLength, String contentType, String keyName);
......
package org.linlinjava.litemall.core.storage; package org.linlinjava.litemall.core.storage;
import org.linlinjava.litemall.core.util.CharUtil;
import org.linlinjava.litemall.db.domain.LitemallStorage;
import org.linlinjava.litemall.db.service.LitemallStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Path; import java.nio.file.Path;
import java.time.LocalDateTime;
import java.util.stream.Stream; import java.util.stream.Stream;
/** /**
...@@ -13,6 +18,8 @@ import java.util.stream.Stream; ...@@ -13,6 +18,8 @@ import java.util.stream.Stream;
public class StorageService { public class StorageService {
private String active; private String active;
private Storage storage; private Storage storage;
@Autowired
private LitemallStorageService litemallStorageService;
public String getActive() { public String getActive() {
return active; return active;
...@@ -32,13 +39,44 @@ public class StorageService { ...@@ -32,13 +39,44 @@ public class StorageService {
/** /**
* 存储一个文件对象 * 存储一个文件对象
* @param inputStream 文件输入流 *
* @param inputStream 文件输入流
* @param contentLength 文件长度 * @param contentLength 文件长度
* @param contentType 文件类型 * @param contentType 文件类型
* @param keyName 文件索引名 * @param fileName 文件索引名
*/ */
public void store(InputStream inputStream, long contentLength, String contentType, String keyName) { public String store(InputStream inputStream, long contentLength, String contentType, String fileName) {
storage.store(inputStream, contentLength, contentType, keyName); String key = generateKey(fileName);
storage.store(inputStream, contentLength, contentType, key);
String url = generateUrl(key);
LitemallStorage storageInfo = new LitemallStorage();
storageInfo.setName(fileName);
storageInfo.setSize((int) contentLength);
storageInfo.setType(contentType);
storageInfo.setAddTime(LocalDateTime.now());
storageInfo.setModified(LocalDateTime.now());
storageInfo.setKey(key);
storageInfo.setUrl(url);
litemallStorageService.add(storageInfo);
return url;
}
private String generateKey(String originalFilename) {
int index = originalFilename.lastIndexOf('.');
String suffix = originalFilename.substring(index);
String key = null;
LitemallStorage storageInfo = null;
do {
key = CharUtil.getRandomString(20) + suffix;
storageInfo = litemallStorageService.findByKey(key);
}
while (storageInfo != null);
return key;
} }
public Stream<Path> loadAll() { public Stream<Path> loadAll() {
......
...@@ -27,14 +27,14 @@ public class LitemallGrouponService { ...@@ -27,14 +27,14 @@ public class LitemallGrouponService {
public List<LitemallGroupon> queryMyGroupon(Integer userId) { public List<LitemallGroupon> queryMyGroupon(Integer userId) {
LitemallGrouponExample example = new LitemallGrouponExample(); LitemallGrouponExample example = new LitemallGrouponExample();
example.or().andUserIdEqualTo(userId).andCreatorUserIdEqualTo(userId).andGrouponIdEqualTo(0).andDeletedEqualTo(false); example.or().andUserIdEqualTo(userId).andCreatorUserIdEqualTo(userId).andGrouponIdEqualTo(0).andDeletedEqualTo(false).andPayedEqualTo(true);
example.orderBy("add_time desc"); example.orderBy("add_time desc");
return mapper.selectByExample(example); return mapper.selectByExample(example);
} }
public List<LitemallGroupon> queryMyJoinGroupon(Integer userId) { public List<LitemallGroupon> queryMyJoinGroupon(Integer userId) {
LitemallGrouponExample example = new LitemallGrouponExample(); LitemallGrouponExample example = new LitemallGrouponExample();
example.or().andUserIdEqualTo(userId).andGrouponIdNotEqualTo(0).andDeletedEqualTo(false); example.or().andUserIdEqualTo(userId).andGrouponIdNotEqualTo(0).andDeletedEqualTo(false).andPayedEqualTo(true);
example.orderBy("add_time desc"); example.orderBy("add_time desc");
return mapper.selectByExample(example); return mapper.selectByExample(example);
} }
...@@ -47,7 +47,7 @@ public class LitemallGrouponService { ...@@ -47,7 +47,7 @@ public class LitemallGrouponService {
public List<LitemallGroupon> queryJoiners(Integer id) { public List<LitemallGroupon> queryJoiners(Integer id) {
LitemallGrouponExample example = new LitemallGrouponExample(); LitemallGrouponExample example = new LitemallGrouponExample();
example.or().andGrouponIdEqualTo(id).andDeletedEqualTo(false); example.or().andGrouponIdEqualTo(id).andDeletedEqualTo(false).andPayedEqualTo(true);
example.orderBy("add_time desc"); example.orderBy("add_time desc");
return mapper.selectByExample(example); return mapper.selectByExample(example);
} }
...@@ -70,7 +70,7 @@ public class LitemallGrouponService { ...@@ -70,7 +70,7 @@ public class LitemallGrouponService {
*/ */
public int countGroupon(Integer grouponId) { public int countGroupon(Integer grouponId) {
LitemallGrouponExample example = new LitemallGrouponExample(); LitemallGrouponExample example = new LitemallGrouponExample();
example.or().andGrouponIdEqualTo(grouponId).andDeletedEqualTo(false); example.or().andGrouponIdEqualTo(grouponId).andDeletedEqualTo(false).andPayedEqualTo(true);
return (int) mapper.countByExample(example); return (int) mapper.countByExample(example);
} }
......
...@@ -628,8 +628,8 @@ public class WxOrderController { ...@@ -628,8 +628,8 @@ public class WxOrderController {
//仅当发起者才创建分享图片 //仅当发起者才创建分享图片
if (groupon.getGrouponId() == 0) { if (groupon.getGrouponId() == 0) {
qCodeService.createGrouponShareImage(grouponRules.getGoodsName(), grouponRules.getPicUrl(), groupon); String url = qCodeService.createGrouponShareImage(grouponRules.getGoodsName(), grouponRules.getPicUrl(), groupon);
groupon.setShareUrl(qCodeService.getShareImageUrl(groupon.getId().toString())); groupon.setShareUrl(url);
} }
groupon.setPayed(true); groupon.setPayed(true);
grouponService.update(groupon); grouponService.update(groupon);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment