AppService.java
5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.ruoyi.service;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.driver.service.impl.DriverServiceImpl;
import com.ruoyi.equipment.domain.Equipment;
import com.ruoyi.equipment.mapper.EquipmentMapper;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.pojo.request.HeartPackageVo;
import com.ruoyi.pojo.response.ApplicationResponseVo;
import com.ruoyi.upgrade.domain.VersionUpgrade;
import com.ruoyi.upgrade.mapper.VersionUpgradeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.Paths;
import java.util.Date;
import java.util.Objects;
/**
* 应用service
*/
@Service
public class AppService {
@Value("${api.apk.path}")
String apkPath;
@Autowired
private EquipmentMapper equipmentMapper;
@Autowired
private ServerConfig serverConfig;
@Autowired
private VersionUpgradeMapper versionUpgradeMapper;
public ApplicationResponseVo checkVersionNum(Integer versionNum, String deviceId) {
boolean result = DriverServiceImpl.doCheckDevice(deviceId);
if (result) {
return getApplicationResponseVoByDeviceId(versionNum);
} else {
return getApplicationResponseVo(versionNum);
}
}
private ApplicationResponseVo getApplicationResponseVoByDeviceId(Integer versionNum) {
// 比对版本 需要更新 判断是否需要强制更新
VersionUpgrade vu = versionUpgradeMapper.queryLatestVersionNum(1);
// 无需更新
if (!Objects.isNull(vu) && vu.getVersionCode().equals(versionNum)) {
return ApplicationResponseVo.ApplicationResponseVoBuild().appNoUpdate(versionNum);
}
// 更新判断
return ApplicationResponseVo.ApplicationResponseVoBuild().appForceUpdate(vu.getVersionCode(), vu.getApkUrl());
}
private ApplicationResponseVo getApplicationResponseVo(Integer versionNum) {
// 比对版本 需要更新 判断是否需要强制更新
VersionUpgrade vu = versionUpgradeMapper.queryLatestVersionNum(2);
// 无需更新
if (!Objects.isNull(vu) && vu.getVersionCode().equals(versionNum)) {
return ApplicationResponseVo.ApplicationResponseVoBuild().appNoUpdate(versionNum);
}
// 更新判断
return ApplicationResponseVo.ApplicationResponseVoBuild().appForceUpdate(vu.getVersionCode(), vu.getApkUrl());
}
public void downloadApk(String url, HttpServletResponse response) throws IOException {
// 把文件写入响应体
String realFileName = url.substring(url.indexOf("_") + 1);
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
FileUtils.setAttachmentResponseHeader(response, realFileName);
FileUtils.writeBytes(RuoYiConfig.getUploadPath() + url, response.getOutputStream());
}
public AjaxResult uploadApk(MultipartFile file) throws IOException {
// 校验文件
checkFile(file);
// 上传文件路径
String filePath = RuoYiConfig.getUploadPath() + apkPath;
// 上传并返回新文件名称
String fileName = "app.apk";
// 获取相对路径
String absPath = FileUploadUtils.getAbsoluteFile(filePath, fileName).getAbsolutePath();
// 上传文件
file.transferTo(Paths.get(absPath));
fileName = FileUploadUtils.getPathFileName(filePath, fileName);
// 返回url
String url = serverConfig.getUrl() + fileName;
AjaxResult ajax = AjaxResult.success();
ajax.put("url", url);
ajax.put("fileName", fileName);
ajax.put("newFileName", FileUtils.getName(fileName));
ajax.put("originalFilename", file.getOriginalFilename());
return ajax;
}
private void checkFile(MultipartFile file) {
if (StringUtils.isEmpty(file.getOriginalFilename())) {
throw new RuntimeException("不是apk文件请重新上传");
}
if (!file.getOriginalFilename().endsWith(".apk")) {
throw new RuntimeException("不是apk文件请重新上传");
}
}
public void checkAppHeart(HeartPackageVo vo) {
Equipment equipment = new Equipment();
equipment.setLastHeartRes(new Date());
equipment.setDeviceId(vo.getDeviceId());
equipment.setRemark("版本号:" + vo.getVersionNum());
equipmentMapper.updateEquipmentByDeviceId(equipment);
}
public AjaxResult uploadApkOther(MultipartFile file) throws IOException {
// 校验文件
checkFile(file);
// 上传文件路径
String filePath = RuoYiConfig.getUploadPath() + apkPath + "/other";
// 上传并返回新文件名称
String fileName = "app.apk";
// 获取相对路径
String absPath = FileUploadUtils.getAbsoluteFile(filePath, fileName).getAbsolutePath();
// 上传文件
file.transferTo(Paths.get(absPath));
fileName = FileUploadUtils.getPathFileName(filePath, fileName);
// 返回url
String url = serverConfig.getUrl() + fileName;
AjaxResult ajax = AjaxResult.success();
ajax.put("url", url);
ajax.put("fileName", fileName);
ajax.put("newFileName", FileUtils.getName(fileName));
ajax.put("originalFilename", file.getOriginalFilename());
return ajax;
}
}