refactor(移动端): 代码优化

master
wayn 4 years ago
parent c0f4708081
commit 140a77330c

@ -1,8 +1,8 @@
package com.wayn.admin.api.controller.system;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wayn.admin.framework.config.WaynConfig;
import com.wayn.common.base.controller.BaseController;
import com.wayn.common.config.WaynConfig;
import com.wayn.common.constant.SysConstants;
import com.wayn.common.core.domain.system.Dict;
import com.wayn.common.core.service.system.IDictService;

@ -1,8 +1,8 @@
package com.wayn.admin.api.controller.system;
import com.wayn.admin.framework.config.WaynConfig;
import com.wayn.admin.framework.manager.upload.service.UploadService;
import com.wayn.admin.framework.security.service.TokenService;
import com.wayn.common.base.service.UploadService;
import com.wayn.common.config.WaynConfig;
import com.wayn.common.core.domain.system.User;
import com.wayn.common.core.model.LoginUserDetail;
import com.wayn.common.core.service.system.IUserService;

@ -1,8 +1,8 @@
package com.wayn.admin.api.controller.system;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wayn.admin.framework.config.WaynConfig;
import com.wayn.common.base.controller.BaseController;
import com.wayn.common.config.WaynConfig;
import com.wayn.common.constant.SysConstants;
import com.wayn.common.core.domain.system.Role;
import com.wayn.common.core.service.system.IRoleService;

@ -3,8 +3,8 @@ package com.wayn.admin.api.controller.system;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.imports.ExcelImportService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wayn.admin.framework.config.WaynConfig;
import com.wayn.common.base.controller.BaseController;
import com.wayn.common.config.WaynConfig;
import com.wayn.common.constant.SysConstants;
import com.wayn.common.core.domain.system.User;
import com.wayn.common.core.service.system.IRoleService;

@ -1,5 +1,6 @@
package com.wayn.admin.framework.config;
import com.wayn.common.config.WaynConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@ -1,5 +1,6 @@
package com.wayn.admin.framework.config;
import com.wayn.common.config.WaynConfig;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@ -1,7 +1,7 @@
package com.wayn.admin.api.controller.system;
package com.wayn.common.base.controller;
import com.wayn.admin.framework.config.WaynConfig;
import com.wayn.admin.framework.manager.upload.service.UploadService;
import com.wayn.common.base.service.UploadService;
import com.wayn.common.config.WaynConfig;
import com.wayn.common.exception.BusinessException;
import com.wayn.common.util.R;
import com.wayn.common.util.file.FileUploadUtil;
@ -18,6 +18,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Base64;
/**
*
@ -101,4 +102,27 @@ public class CommonController {
return R.error(e.getMessage());
}
}
/**
*
*/
@PostMapping("base64uploadFile")
@ResponseBody
public R base64uploadFile(String filename,
String base64content,
HttpServletRequest request) {
try {
byte[] decode = Base64.getDecoder().decode(base64content.substring(base64content.indexOf(IMAGE_BASE64_FLAG) + IMAGE_BASE64_FLAG.length()));
// 上传文件路径
String filePath = WaynConfig.getUploadDir();
String fileName = FileUploadUtil.uploadFile(decode, filename, filePath);
String fileUrl = uploadService.uploadFile(fileName);
return R.success().add("url", fileUrl).add("fileName", fileName);
} catch (Exception e) {
log.error(e.getMessage(), e);
return R.error(e.getMessage());
}
}
public static final String IMAGE_BASE64_FLAG = ";base64,";
}

@ -1,4 +1,4 @@
package com.wayn.admin.framework.manager.upload.service;
package com.wayn.common.base.service;
import com.qiniu.common.QiniuException;

@ -1,4 +1,4 @@
package com.wayn.admin.framework.manager.upload.service.impl;
package com.wayn.common.base.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.qiniu.common.QiniuException;
@ -6,8 +6,8 @@ import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.wayn.admin.framework.config.WaynConfig;
import com.wayn.admin.framework.manager.upload.service.UploadService;
import com.wayn.common.base.service.UploadService;
import com.wayn.common.config.WaynConfig;
import com.wayn.common.core.domain.tool.QiniuConfig;
import com.wayn.common.core.service.tool.IQiniuConfigService;
import com.wayn.common.exception.BusinessException;
@ -86,5 +86,4 @@ public class UploadServiceImpl implements UploadService {
return requestUrl + "/upload/" + fileName;
}
}
}

@ -1,4 +1,4 @@
package com.wayn.admin.framework.config;
package com.wayn.common.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ -3,10 +3,12 @@ package com.wayn.common.util.file;
import com.wayn.common.exception.BusinessException;
import com.wayn.common.util.date.DateUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Objects;
@ -15,6 +17,14 @@ import java.util.Objects;
*/
public class FileUploadUtil {
/**
*
*
* @param file spring MultipartFile
* @param filePath
* @return
* @throws IOException
*/
public static String uploadFile(MultipartFile file, String filePath) throws IOException {
int fileNameLength = Objects.requireNonNull(file.getOriginalFilename()).length();
if (fileNameLength > 100) {
@ -26,7 +36,7 @@ public class FileUploadUtil {
extension = MimeTypeUtils.getExtension(Objects.requireNonNull(file.getContentType()));
}
String encodingFilename = FileUtils.encodingFilename(fileName);
fileName = DateUtils.datePath() + "/" + encodingFilename + "." + extension;
fileName = genNewFilename(encodingFilename, extension);
File desc = new File(filePath, fileName);
if (!desc.getParentFile().exists()) {
desc.getParentFile().mkdirs();
@ -37,4 +47,42 @@ public class FileUploadUtil {
file.transferTo(desc);
return fileName;
}
/**
* @param fileBytes base64
* @param fileName
* @param filePath
* @return
* @throws IOException
*/
public static String uploadFile(byte[] fileBytes, String fileName, String filePath) throws IOException {
int fileNameLength = Objects.requireNonNull(fileName).length();
if (fileNameLength > 100) {
throw new BusinessException("文件名称过长");
}
String encodingFilename = FileUtils.encodingFilename(fileName);
String extension = FilenameUtils.getExtension(fileName);
fileName = genNewFilename(encodingFilename, extension);
File desc = new File(filePath, fileName);
if (!desc.getParentFile().exists()) {
desc.getParentFile().mkdirs();
}
if (!desc.exists()) {
desc.createNewFile();
}
IOUtils.write(fileBytes, new FileOutputStream(desc));
return fileName;
}
/**
*
*
* @param encodingFilename
* @param extension
* @return
*/
private static String genNewFilename(String encodingFilename, String extension) {
return DateUtils.datePath() + "/" + encodingFilename + "." + extension;
}
}

@ -1,11 +1,15 @@
package com.wayn.mobile.api.controller;
import com.wayn.common.core.domain.shop.Member;
import com.wayn.common.core.service.shop.IMemberService;
import com.wayn.common.exception.BusinessException;
import com.wayn.common.util.R;
import com.wayn.common.util.ServletUtils;
import com.wayn.mobile.framework.security.LoginUserDetail;
import com.wayn.mobile.framework.security.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@ -16,9 +20,26 @@ public class UserController {
@Autowired
private TokenService tokenService;
@Autowired
private IMemberService iMemberService;
@GetMapping("info")
public R getInfo() {
LoginUserDetail loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
return R.success().add("info", loginUser.getMember());
}
@PostMapping("uploadAvatar")
public R uploadAvatar(String avatar) {
LoginUserDetail loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
Member member = loginUser.getMember();
member.setAvatar(avatar);
boolean update = iMemberService.updateById(member);
if (!update) {
throw new BusinessException("");
}
loginUser.setMember(member);
tokenService.refreshToken(loginUser);
return R.result(update).add("userInfo", member);
}
}

@ -13,6 +13,7 @@ import com.github.binarywang.wxpay.bean.result.BaseWxPayResult;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.wayn.common.config.WaynConfig;
import com.wayn.common.constant.SysConstants;
import com.wayn.common.core.domain.shop.*;
import com.wayn.common.core.domain.vo.OrderVO;
@ -31,7 +32,6 @@ import com.wayn.mobile.api.service.ICartService;
import com.wayn.mobile.api.service.IOrderService;
import com.wayn.mobile.api.task.OrderUnpaidTask;
import com.wayn.mobile.api.util.OrderSnGenUtil;
import com.wayn.mobile.framework.config.WaynConfig;
import com.wayn.mobile.framework.redis.RedisCache;
import com.wayn.mobile.framework.security.util.SecurityUtils;
import lombok.extern.slf4j.Slf4j;

@ -1,5 +1,6 @@
package com.wayn.mobile.framework.config;
import com.wayn.common.config.WaynConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@ -1,5 +1,6 @@
package com.wayn.mobile.framework.config;
import com.wayn.common.config.WaynConfig;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@ -1,91 +0,0 @@
package com.wayn.mobile.framework.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "wayn")
public class WaynConfig {
/**
*
*/
private static String uploadDir;
/**
*
*/
private static String name;
/**
*
*/
private static String version;
/**
*
*/
private static String email;
/**
*
*/
private static String adminUrl;
/**
*
*/
private static String mobileUrl;
public static String getUploadDir() {
return uploadDir;
}
public void setUploadDir(String uploadDir) {
WaynConfig.uploadDir = uploadDir;
}
public static String getDownloadPath() {
return getUploadDir() + "/download/";
}
public static String getAvatarPath() {
return getUploadDir() + "/avatar/";
}
public static String getName() {
return name;
}
public void setName(String name) {
WaynConfig.name = name;
}
public static String getVersion() {
return version;
}
public void setVersion(String version) {
WaynConfig.version = version;
}
public static String getEmail() {
return email;
}
public void setEmail(String email) {
WaynConfig.email = email;
}
public static String getAdminUrl() {
return adminUrl;
}
public void setAdminUrl(String adminUrl) {
WaynConfig.adminUrl = adminUrl;
}
public static String getMobileUrl() {
return mobileUrl;
}
public void setMobileUrl(String mobileUrl) {
WaynConfig.mobileUrl = mobileUrl;
}
}

@ -1,29 +0,0 @@
package com.wayn.mobile.framework.manager.upload.service;
import com.qiniu.common.QiniuException;
import java.io.IOException;
/**
* <p>
* Service
* </p>
*
* @package: com.xkcoding.upload.service
* @description: Service
* @author: yangkai.shen
* @date: Created in 2018/11/6 17:21
* @copyright: Copyright (c) 2018
* @version: V1.0
* @modified: yangkai.shen
*/
public interface UploadService {
/**
*
*
* @param filename
* @return 访
* @throws QiniuException
*/
String uploadFile(String filename) throws IOException;
}

@ -1,89 +0,0 @@
package com.wayn.mobile.framework.manager.upload.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.wayn.common.core.domain.tool.QiniuConfig;
import com.wayn.common.core.service.tool.IQiniuConfigService;
import com.wayn.common.exception.BusinessException;
import com.wayn.common.util.ServletUtils;
import com.wayn.common.util.file.FileUtils;
import com.wayn.common.util.file.QiniuUtil;
import com.wayn.common.util.http.HttpUtil;
import com.wayn.mobile.framework.config.WaynConfig;
import com.wayn.mobile.framework.manager.upload.service.UploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.File;
/**
* <p>
* Service
* </p>
*
* @package: com.xkcoding.upload.service.impl
* @description: Service
* @author: yangkai.shen
* @date: Created in 2018/11/6 17:22
* @copyright: Copyright (c) 2018
* @version: V1.0
* @modified: yangkai.shen
*/
@Service
@Slf4j
public class UploadServiceImpl implements UploadService {
@Autowired
private IQiniuConfigService iQiniuConfigService;
/**
*
*
* @param fileName
* @return 访
* @throws QiniuException
*/
@Override
public String uploadFile(String fileName) {
QiniuConfig qiniuConfig = iQiniuConfigService.getById(1);
if (qiniuConfig != null && qiniuConfig.getEnable()) {
File file = new File(WaynConfig.getUploadDir() + File.separator + fileName);
Configuration cfg = new Configuration(QiniuUtil.getRegion(qiniuConfig.getRegion()));
UploadManager uploadManager = new UploadManager(cfg);
Auth auth = Auth.create(qiniuConfig.getAccessKey(), qiniuConfig.getSecretKey());
String upToken = auth.uploadToken(qiniuConfig.getBucket());
Response response;
try {
response = uploadManager.put(file, file.getName(), upToken);
int retry = 0;
while (response.needRetry() && retry < 3) {
response = uploadManager.put(file, file.getName(), upToken);
retry++;
}
if (response.isOK()) {
JSONObject jsonObject = JSONObject.parseObject(response.bodyString());
String yunFileName = jsonObject.getString("key");
String yunFilePath = qiniuConfig.getHost() + "/" + yunFileName;
FileUtils.deleteQuietly(file);
log.info("【文件上传至七牛云】绝对路径:{}", yunFilePath);
return yunFilePath;
} else {
log.error("【文件上传至七牛云】失败,{}", JSONObject.toJSONString(response));
FileUtils.deleteQuietly(file);
throw new BusinessException("文件上传至七牛云失败");
}
} catch (QiniuException e) {
FileUtils.deleteQuietly(file);
throw new BusinessException("文件上传至七牛云失败");
}
} else {
String requestUrl = HttpUtil.getRequestContext(ServletUtils.getRequest());
return requestUrl + "/upload/" + fileName;
}
}
}
Loading…
Cancel
Save