diff --git a/yudao-module-accounting/yudao-module-accounting-biz/pom.xml b/yudao-module-accounting/yudao-module-accounting-biz/pom.xml
index 43394b44..3ccef5c1 100644
--- a/yudao-module-accounting/yudao-module-accounting-biz/pom.xml
+++ b/yudao-module-accounting/yudao-module-accounting-biz/pom.xml
@@ -90,6 +90,12 @@
1.7.3-snapshot
compile
+
+ cn.iocoder.boot
+ yudao-module-setting-biz
+ 1.7.3-snapshot
+ compile
+
diff --git a/yudao-module-accounting/yudao-module-accounting-biz/src/main/java/cn/iocoder/yudao/module/accounting/controller/admin/invoices/InvoicesController.java b/yudao-module-accounting/yudao-module-accounting-biz/src/main/java/cn/iocoder/yudao/module/accounting/controller/admin/invoices/InvoicesController.java
index fd3483fb..1f24e784 100644
--- a/yudao-module-accounting/yudao-module-accounting-biz/src/main/java/cn/iocoder/yudao/module/accounting/controller/admin/invoices/InvoicesController.java
+++ b/yudao-module-accounting/yudao-module-accounting-biz/src/main/java/cn/iocoder/yudao/module/accounting/controller/admin/invoices/InvoicesController.java
@@ -10,6 +10,7 @@ import cn.iocoder.yudao.module.accounting.dal.dataobject.invoices.Converter;
import cn.iocoder.yudao.module.accounting.enums.AccountingStatusEnum;
import cn.iocoder.yudao.module.bs.utils.BaiduOcrHandler;
import cn.iocoder.yudao.module.infra.service.file.FileService;
+import cn.iocoder.yudao.module.setting.service.passwords.PasswordsService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import liquibase.pro.packaged.A;
@@ -60,6 +61,8 @@ public class InvoicesController {
private InvoicesService invoicesService;
@Resource
private FileService fileService;
+ @Resource
+ private PasswordsService passwordsService;
@PostMapping("/create")
@Operation(summary = "创建发票")
@@ -111,6 +114,16 @@ public class InvoicesController {
return success(InvoicesConvert.INSTANCE.convertPage(pageResult));
}
+ @GetMapping("/downloadXml")
+ @Operation(summary = "获得发票分页Xml")
+ @PreAuthorize("@ss.hasPermission('accounting:invoices:query')")
+ public void downloadXml(@Valid InvoicesPageReqVO pageVO,HttpServletResponse response) throws IOException {
+ PageResult pageResult = invoicesService.getInvoicesPage(pageVO);
+ PageResult invoicesRespVOPageResult = InvoicesConvert.INSTANCE.convertPage(pageResult);
+ String jsonString = JSONObject.toJSONString(invoicesRespVOPageResult);
+ passwordsService.jsonToXmlConverter(response,pageVO.getInputPassword(),jsonString);
+ }
+
@GetMapping("/export-excel")
@Operation(summary = "导出发票 Excel")
@PreAuthorize("@ss.hasPermission('accounting:invoices:export')")
diff --git a/yudao-module-accounting/yudao-module-accounting-biz/src/main/java/cn/iocoder/yudao/module/accounting/controller/admin/invoices/vo/InvoicesPageReqVO.java b/yudao-module-accounting/yudao-module-accounting-biz/src/main/java/cn/iocoder/yudao/module/accounting/controller/admin/invoices/vo/InvoicesPageReqVO.java
index eea59a1d..b045df54 100644
--- a/yudao-module-accounting/yudao-module-accounting-biz/src/main/java/cn/iocoder/yudao/module/accounting/controller/admin/invoices/vo/InvoicesPageReqVO.java
+++ b/yudao-module-accounting/yudao-module-accounting-biz/src/main/java/cn/iocoder/yudao/module/accounting/controller/admin/invoices/vo/InvoicesPageReqVO.java
@@ -208,4 +208,7 @@ public class InvoicesPageReqVO extends PageParam {
@Schema(description = "发票地址")
private String fileUrl;
+
+ @Schema(description = "密码")
+ String inputPassword;
}
diff --git a/yudao-module-setting/yudao-module-setting-biz/src/main/java/cn/iocoder/yudao/module/setting/service/passwords/PasswordsServiceImpl.java b/yudao-module-setting/yudao-module-setting-biz/src/main/java/cn/iocoder/yudao/module/setting/service/passwords/PasswordsServiceImpl.java
index 31109172..b478d09f 100644
--- a/yudao-module-setting/yudao-module-setting-biz/src/main/java/cn/iocoder/yudao/module/setting/service/passwords/PasswordsServiceImpl.java
+++ b/yudao-module-setting/yudao-module-setting-biz/src/main/java/cn/iocoder/yudao/module/setting/service/passwords/PasswordsServiceImpl.java
@@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.setting.service.passwords;
+import cn.iocoder.yudao.module.setting.tools.JsonToXml;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhyd.oauth.log.Log;
import org.apache.http.HttpResponse;
@@ -15,8 +16,10 @@ import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import java.io.IOException;
+import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.*;
@@ -56,8 +59,6 @@ public class PasswordsServiceImpl implements PasswordsService {
wrapper.orderByDesc("create_time").last("LIMIT 1");
PasswordsDO passwordsDO = passwordsMapper.selectOne(wrapper);
String passwordValueMD5 = passwordsDO.getPasswordValue();
- // 获取PrintWriter对象
- PrintWriter out = httpResponse.getWriter();
httpResponse.setCharacterEncoding("UTF-8");
httpResponse.setContentType("application/json");
if (passwordValueMD5 == null) {
@@ -65,25 +66,34 @@ public class PasswordsServiceImpl implements PasswordsService {
json2.put("code", "500");
json2.put("msg", "password is null!");
httpResponse.setStatus(500);
+ PrintWriter out = httpResponse.getWriter(); // 获取PrintWriter对象
out.println(json2.toString());
out.flush();
return null; // 返回XML字符串
} else {
boolean passwordValid = this.isPasswordValid(inputPassword, passwordValueMD5);
- if (passwordValid) {//将json字符串转化为JSONObject对象
- JSONObject jsonObject = new JSONObject(json);
- //将JSONObject对象转化为xml字符串
- String xml = XML.toString(jsonObject);
- httpResponse.setStatus(200); // 设置响应状态码为200
- httpResponse.setHeader("Content-Type", ContentType.APPLICATION_XML.toString());
- out.println(xml);
- return xml; // 返回XML字符串
+ if (passwordValid) {
+ //1.设置文件下载的response响应格式
+ String fileType = "xml"; //文件类型
+ httpResponse.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("元数据." + fileType, "UTF-8"));
+ httpResponse.setContentType("multipart/form-data");
+ httpResponse.setCharacterEncoding("UTF-8");
+ String jsonToXml = JsonToXml.jsonToXml(json);
+ //3.将内容转为byte[]格式
+ byte[] data2 = jsonToXml.getBytes("UTF-8");
+ //4.将内容写入响应流
+ OutputStream out2 = httpResponse.getOutputStream();
+ out2.write(data2);
+ out2.flush();
+ out2.close();
+ return jsonToXml;
} else {
JSONObject json2 = new JSONObject();
json2.put("code", "500");
json2.put("msg", "passwordError!");
httpResponse.setStatus(500);
+ PrintWriter out = httpResponse.getWriter(); // 获取PrintWriter对象
out.println(json2.toString());
out.flush();
return null;
@@ -91,6 +101,8 @@ public class PasswordsServiceImpl implements PasswordsService {
}
}
+
+
@Override
public Long createPasswords(PasswordsCreateReqVO createReqVO) {
// 插入
diff --git a/yudao-module-setting/yudao-module-setting-biz/src/main/java/cn/iocoder/yudao/module/setting/tools/JsonToXml.java b/yudao-module-setting/yudao-module-setting-biz/src/main/java/cn/iocoder/yudao/module/setting/tools/JsonToXml.java
new file mode 100644
index 00000000..2a1042bb
--- /dev/null
+++ b/yudao-module-setting/yudao-module-setting-biz/src/main/java/cn/iocoder/yudao/module/setting/tools/JsonToXml.java
@@ -0,0 +1,92 @@
+package cn.iocoder.yudao.module.setting.tools;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * @author ysd
+ * @date 2021年08月03日 9:33
+ */
+public class JsonToXml {
+ /**
+ * Json to xml string.
+ *
+ * @param json the json
+ * @return the string
+ */
+ public static String jsonToXml(String json){
+ try {
+ StringBuffer buffer = new StringBuffer();
+ buffer.append("");
+ JSONObject jObj = JSON.parseObject(json);
+ jsonToXmlstr(jObj,buffer);
+ return buffer.toString();
+ } catch (Exception e) {
+ e.printStackTrace();
+ return "";
+ }
+ }
+
+
+ /**
+ * Json to xmlstr string.
+ *
+ * @param jObj the j obj
+ * @param buffer the buffer
+ * @return the string
+ */
+ public static String jsonToXmlstr(JSONObject jObj, StringBuffer buffer) {
+ Set> se = jObj.entrySet();
+ for (Iterator> it = se.iterator(); it.hasNext(); ) {
+ Map.Entry en = it.next();
+ if (en.getValue() instanceof JSONObject) {
+ buffer.append("<" + en.getKey() + ">");
+ JSONObject jo = (JSONObject) en.getValue();
+ jsonToXmlstr(jo, buffer);
+ buffer.append("" + en.getKey() + ">");
+ } else if (en.getValue() instanceof JSONArray) {
+ JSONArray jarray = (JSONArray) en.getValue();
+ for (int i = 0; i < jarray.size(); i++) {
+ buffer.append("<" + en.getKey() + ">");
+ JSONObject jsonobject = jarray.getJSONObject(i);
+ jsonToXmlstr(jsonobject, buffer);
+ buffer.append("" + en.getKey() + ">");
+ }
+ } else if (en.getValue().getClass().getName().equals("java.lang.String")) {
+ buffer.append("<" + en.getKey() + ">" + encodeXmlString((String) en.getValue()));
+ buffer.append("" + en.getKey() + ">");
+ }
+ }
+ return buffer.toString();
+ }
+
+ public static String encodeXmlString(String input) {
+ StringBuilder out = new StringBuilder();
+ for (int i = 0; i < input.length(); i++) {
+ char c = input.charAt(i);
+ if (c <= 0xD7FF || c >= 0xE000) {
+ out.append(c);
+ } else {
+ out.append("").append(Integer.toHexString(c)).append(";");
+ }
+ }
+ return out.toString();
+ }
+
+ /**
+ * The entry point of application.
+ *
+ * @param args the input arguments
+ */
+ public static void main(String[] args) {
+
+ String xmlstr = jsonToXml("{\"class\": {\"student\": [{\"age\": \"18\",\"gender\": \"男\",\"name\": \"张三\",\"qk\": [{\"q1\": \"001\",\"q2\": \"002\",\"q3\": \"003\"},{\"q1\": \"001\",\"q2\": \"002\",\"q3\": \"003\"}]},{\"age\": \"17\",\"gender\": \"男\",\"name\": \"李四\",\"qk\": {\"q1\": \"005\",\"q2\": \"006\",\"q3\": \"007\"}},{\"age\": \"19\",\"gender\": \"女\",\"name\": \"王五\",\"qk\": {\"q1\": \"008\",\"q2\": \"009\",\"q3\": \"010\"}}]}}");
+ System.out.println(xmlstr);
+ }
+
+}