QrcodeService.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. package com.yihu.wlyy.service.common;
  2. import com.yihu.wlyy.entity.organization.Hospital;
  3. import com.yihu.wlyy.entity.address.Town;
  4. import com.yihu.wlyy.entity.doctor.profile.Doctor;
  5. import com.yihu.wlyy.repository.address.TownDao;
  6. import com.yihu.wlyy.repository.doctor.DoctorDao;
  7. import com.yihu.wlyy.repository.organization.HospitalDao;
  8. import com.yihu.wlyy.service.BaseService;
  9. import com.yihu.wlyy.util.HttpUtil;
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.json.JSONObject;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.stereotype.Service;
  14. import org.springframework.transaction.annotation.Transactional;
  15. import java.io.*;
  16. import java.net.HttpURLConnection;
  17. import java.net.URL;
  18. import java.net.URLEncoder;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import java.util.zip.ZipEntry;
  22. import java.util.zip.ZipOutputStream;
  23. /**
  24. * Created by lyr on 2016/08/10.
  25. */
  26. @Service
  27. @Transactional
  28. public class QrcodeService extends BaseService {
  29. @Autowired
  30. private DoctorDao doctorDao;
  31. @Autowired
  32. private HospitalDao hospitalDao;
  33. @Autowired
  34. private TownDao townDao;
  35. /**
  36. * 所有医生二维码生成
  37. *
  38. * @param token
  39. * @return
  40. * @throws Exception
  41. */
  42. public boolean makeDoctorQrCode(String hospital, String token) throws Exception {
  43. // 查找所有医生
  44. Iterable<Doctor> doctors = null;
  45. if (StringUtils.isNotEmpty(hospital)) {
  46. doctors = doctorDao.findHospitalDoctors(hospital);
  47. } else {
  48. doctors = doctorDao.findAllDoctors();
  49. }
  50. String path = QrcodeService.class.getResource("/").getPath().replace("/WEB-INF/classes/", "")
  51. + File.separator + "qrcode";
  52. for (Doctor doctor : doctors) {
  53. // 二维码内容
  54. String content = "qr_" + doctor.getCode() + "_" + doctor.getName();
  55. // 二维码图片文件名
  56. String fileName = doctor.getMobile();
  57. if (StringUtils.isEmpty(fileName)) {
  58. continue;
  59. }
  60. // 通过微信接口生成医生二维码
  61. makeQrcodeFromWeiXin(content, fileName.replaceAll("\r\n", ""), path, token);
  62. doctor.setQrcode(fileName + ".png");
  63. }
  64. return true;
  65. }
  66. /**
  67. * 生成社区医院二维码
  68. *
  69. * @param hospital
  70. * @param token
  71. * @return
  72. * @throws Exception
  73. */
  74. public boolean makeHospitalQrcode(String hospital, String token) throws Exception {
  75. // 查找所有医生
  76. List<Hospital> hospitals = new ArrayList<>();
  77. if (StringUtils.isNotEmpty(hospital)) {
  78. Hospital hos = hospitalDao.findByCode(hospital);
  79. hospitals.add(hos);
  80. } else {
  81. hospitals = hospitalDao.findAllSqHospital();
  82. }
  83. String path = QrcodeService.class.getResource("/").getPath().replace("/WEB-INF/classes/", "")
  84. + File.separator + "qrcode";
  85. for (Hospital hos : hospitals) {
  86. // 二维码内容
  87. String content = "hs_" + hos.getCode() + "_" + hos.getName();
  88. // 二维码图片文件名
  89. String fileName = hos.getCode();
  90. if (StringUtils.isEmpty(fileName)) {
  91. continue;
  92. }
  93. // 通过微信接口生成医生二维码
  94. makeQrcodeFromWeiXin(content, fileName.replaceAll("\r\n", ""), path, token);
  95. }
  96. return true;
  97. }
  98. /**
  99. * 生成区二维码
  100. *
  101. * @param town
  102. * @param token
  103. * @return
  104. * @throws Exception
  105. */
  106. public boolean makeTownQrcode(String town, String token) throws Exception {
  107. try {
  108. Town twn = townDao.findByCode(town);
  109. if (twn != null) {
  110. // 二维码内容
  111. String content = "tw_" + twn.getCode() + "_" + twn.getName();
  112. // 二维码图片文件名
  113. String fileName = twn.getCode();
  114. String path = QrcodeService.class.getResource("/").getPath().replace("/WEB-INF/classes/", "")
  115. + File.separator + "qrcode";
  116. // 通过微信接口生成区二维码
  117. makeQrcodeFromWeiXin(content, fileName.replaceAll("\r\n", ""), path, token);
  118. return true;
  119. } else {
  120. throw new Exception("can not find town info");
  121. }
  122. } catch (Exception e) {
  123. e.printStackTrace();
  124. return false;
  125. }
  126. }
  127. /**
  128. * 社区二维码生成打包为zip
  129. *
  130. * @param token
  131. * @return
  132. * @throws Exception
  133. */
  134. public File downloadHospitalQrCodes(String area, String token) throws Exception {
  135. // 查找所有医生
  136. List<Hospital> Hospitals = null;
  137. String zipFileName = "hospital_qrcode";
  138. if (StringUtils.isEmpty(area)) {
  139. Hospitals = hospitalDao.findAllSqHospital();
  140. } else {
  141. Hospitals = hospitalDao.findAreaSqHospital(area);
  142. }
  143. String path = QrcodeService.class.getResource("/").getPath().replace("/WEB-INF/classes/", "")
  144. + File.separator + "qrcode_download";
  145. File file = new File(path);
  146. // 删除文件夹、文件
  147. if (file.exists()) {
  148. File[] files = file.listFiles();
  149. if (files != null && files.length > 0) {
  150. for (File f : files) {
  151. f.delete();
  152. }
  153. }
  154. file.delete();
  155. }
  156. if (Hospitals != null) {
  157. for (Hospital hos : Hospitals) {
  158. // 二维码内容
  159. String content = "hs_" + hos.getCode() + "_" + hos.getName();
  160. String hosLevel = "";
  161. if (hos.getLevel() == 1) {
  162. hosLevel = "医院";
  163. } else if (hos.getLevel() == 2) {
  164. hosLevel = "社区";
  165. }
  166. // 二维码图片文件名
  167. String fileName = hos.getName() + "_" + hos.getCode() + "_" + hosLevel;
  168. if (StringUtils.isEmpty(fileName)) {
  169. continue;
  170. }
  171. // 通过微信接口生成医生二维码
  172. makeQrcodeFromWeiXin(content, fileName.replaceAll("\r\n", ""), path, token);
  173. }
  174. File zipFile = new File(path.replace("qrcode_download", "") + zipFileName + ".zip");
  175. if (zipFile.exists()) {
  176. zipFile.delete();
  177. }
  178. // 打包文件夹
  179. if (fileToZip(path, path.replace("qrcode_download", ""), zipFileName)) {
  180. return new File(path.replace("qrcode_download", "") + zipFileName + ".zip");
  181. } else {
  182. return null;
  183. }
  184. } else {
  185. return null;
  186. }
  187. }
  188. /**
  189. * 医生二维码生成打包为zip
  190. *
  191. * @param token
  192. * @return
  193. * @throws Exception
  194. */
  195. public File downloadDoctorQrCodes(String hospital, String token) throws Exception {
  196. // 查找所有医生
  197. Iterable<Doctor> doctors = null;
  198. String zipFileName = "doctor_qrcode";
  199. if (StringUtils.isNotEmpty(hospital)) {
  200. doctors = doctorDao.findHospitalDoctors(hospital);
  201. } else {
  202. doctors = doctorDao.findAllDoctors();
  203. }
  204. String path = QrcodeService.class.getResource("/").getPath().replace("/WEB-INF/classes/", "")
  205. + File.separator + "qrcode_download";
  206. File file = new File(path);
  207. // 删除文件夹、文件
  208. if (file.exists()) {
  209. File[] files = file.listFiles();
  210. if (files != null && files.length > 0) {
  211. for (File f : files) {
  212. f.delete();
  213. }
  214. }
  215. file.delete();
  216. }
  217. if (doctors != null) {
  218. for (Doctor doctor : doctors) {
  219. if (StringUtils.isNotEmpty(hospital)) {
  220. zipFileName = doctor.getHospitalName();
  221. }
  222. // 二维码内容
  223. String content = "qr_" + doctor.getCode() + "_" + doctor.getName();
  224. String doctorLevel = "";
  225. if (doctor.getLevel() == 1) {
  226. doctorLevel = "专科医生";
  227. } else if (doctor.getLevel() == 2) {
  228. doctorLevel = "全科医生";
  229. } else if (doctor.getLevel() == 3) {
  230. doctorLevel = "健康管理师";
  231. }
  232. else{
  233. doctorLevel = "未知";
  234. }
  235. // 二维码图片文件名
  236. String fileName = doctor.getName() + "_" + doctor.getMobile() + "_" + doctorLevel;
  237. if (StringUtils.isEmpty(fileName)) {
  238. continue;
  239. }
  240. // 通过微信接口生成医生二维码
  241. makeQrcodeFromWeiXin(content, fileName.replaceAll("\r\n", ""), path, token);
  242. }
  243. File zipFile = new File(path.replace("qrcode_download", "") + zipFileName + ".zip");
  244. if (zipFile.exists()) {
  245. zipFile.delete();
  246. }
  247. // 打包文件夹
  248. if (fileToZip(path, path.replace("qrcode_download", ""), zipFileName)) {
  249. return new File(path.replace("qrcode_download", "") + zipFileName + ".zip");
  250. } else {
  251. return null;
  252. }
  253. } else {
  254. return null;
  255. }
  256. }
  257. /**
  258. * 区二维码生成打包为zip
  259. *
  260. * @param city
  261. * @param token
  262. * @return
  263. * @throws Exception
  264. */
  265. public File downLoadTownQrcodes(String city, String token) throws Exception {
  266. // 查找所有医生
  267. Iterable<Town> towns = null;
  268. String zipFileName = "town_qrcode";
  269. if (StringUtils.isNotEmpty(city)) {
  270. towns = townDao.findByCity(city);
  271. } else {
  272. towns = townDao.findAll();
  273. }
  274. String path = QrcodeService.class.getResource("/").getPath().replace("/WEB-INF/classes/", "")
  275. + File.separator + "qrcode_download";
  276. File file = new File(path);
  277. // 删除文件夹、文件
  278. if (file.exists()) {
  279. File[] files = file.listFiles();
  280. if (files != null && files.length > 0) {
  281. for (File f : files) {
  282. f.delete();
  283. }
  284. }
  285. file.delete();
  286. }
  287. if (towns != null) {
  288. for (Town town : towns) {
  289. if (StringUtils.isNotEmpty(city)) {
  290. zipFileName = town.getCity();
  291. }
  292. // 二维码内容
  293. String content = "tw_" + town.getCode() + "_" + town.getName();
  294. // 二维码图片文件名
  295. String fileName = town.getName() + "_" + town.getCode();
  296. if (StringUtils.isEmpty(fileName)) {
  297. continue;
  298. }
  299. // 通过微信接口生成医生二维码
  300. makeQrcodeFromWeiXin(content, fileName.replaceAll("\r\n", ""), path, token);
  301. }
  302. File zipFile = new File(path.replace("qrcode_download", "") + zipFileName + ".zip");
  303. if (zipFile.exists()) {
  304. zipFile.delete();
  305. }
  306. // 打包文件夹
  307. if (fileToZip(path, path.replace("qrcode_download", ""), zipFileName)) {
  308. return new File(path.replace("qrcode_download", "") + zipFileName + ".zip");
  309. } else {
  310. return null;
  311. }
  312. } else {
  313. return null;
  314. }
  315. }
  316. /**
  317. * 打包文件夹
  318. *
  319. * @param sourcePath
  320. * @param zipPath
  321. * @param zipName
  322. * @return
  323. * @throws Exception
  324. */
  325. public boolean fileToZip(String sourcePath, String zipPath, String zipName) throws Exception {
  326. File sourceFile = new File(sourcePath);
  327. File zipFile = new File(zipPath + File.separator + zipName + ".zip");
  328. File[] files = sourceFile.listFiles();
  329. FileOutputStream fos = new FileOutputStream(zipFile);
  330. ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos));
  331. if (files != null) {
  332. byte[] bufs = new byte[1024 * 10];
  333. for (int i = 0; i < files.length; i++) {
  334. // 创建ZIP实体,并添加进压缩包
  335. ZipEntry zipEntry = new ZipEntry(files[i].getName());
  336. zos.putNextEntry(zipEntry);
  337. // 读取待压缩的文件并写进压缩包里
  338. FileInputStream fis = new FileInputStream(files[i]);
  339. BufferedInputStream bis = new BufferedInputStream(fis, 1024 * 10);
  340. int read = 0;
  341. while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
  342. zos.write(bufs, 0, read);
  343. }
  344. bis.close();
  345. fis.close();
  346. }
  347. }
  348. zos.close();
  349. fos.close();
  350. return true;
  351. }
  352. /**
  353. * 生成某个医生的二维码
  354. *
  355. * @param doctor
  356. * @param token
  357. * @return
  358. * @throws Exception
  359. */
  360. public boolean makeDoctorQrcode(String doctor, String token) throws Exception {
  361. Doctor doc = doctorDao.findByCode(doctor);
  362. if (doc != null) {
  363. // 二维码内容
  364. String content = "qr_" + doc.getCode() + "_" + doc.getName();
  365. // 二维码图片文件名
  366. String fileName = doc.getMobile();
  367. String path = QrcodeService.class.getResource("/").getPath().replace("/WEB-INF/classes/", "")
  368. + File.separator + "qrcode";
  369. // 通过微信接口生成医生二维码
  370. makeQrcodeFromWeiXin(content, fileName.replaceAll("\r\n", ""), path, token);
  371. doc.setQrcode(fileName + ".png");
  372. return true;
  373. } else {
  374. throw new Exception("找不到对应医生信息!");
  375. }
  376. }
  377. /**
  378. * 从微信生成二维码并下载到本地
  379. *
  380. * @param content
  381. * @param fileName
  382. * @param path
  383. * @param token
  384. * @throws IOException
  385. */
  386. public String makeQrcodeFromWeiXin(String content, String fileName, String path, String token) throws Exception {
  387. try {
  388. String token_url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + token;
  389. String params = "{\"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"" + content + "\"}}}";
  390. String result = HttpUtil.sendPost(token_url, params);
  391. if (!StringUtils.isEmpty(result)) {
  392. JSONObject json = new JSONObject(result);
  393. if (json.has("ticket")) {
  394. String file = path + File.separator + fileName + ".png";
  395. // 保存路径
  396. File pathFile = new File(path);
  397. // 保存文件
  398. File outputFile = new File(file);
  399. // 路径不存在则创建
  400. if (!pathFile.exists()) {
  401. pathFile.mkdir();
  402. }
  403. // 文件已存在则删除
  404. if (outputFile.exists()) {
  405. outputFile.delete();
  406. }
  407. // 请求输入流
  408. InputStream inputStream = null;
  409. // 二维码图片输出流
  410. FileOutputStream outputStream = new FileOutputStream(file);
  411. // 下载二维码图片
  412. URL urlGet = new URL("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="
  413. + URLEncoder.encode(json.get("ticket").toString(), "UTF-8"));
  414. HttpURLConnection connection = (HttpURLConnection) urlGet.openConnection();
  415. connection.connect();
  416. inputStream = connection.getInputStream();
  417. // 保存图片
  418. byte[] data = new byte[1024];
  419. int len = 0;
  420. while ((len = inputStream.read(data)) != -1) {
  421. outputStream.write(data, 0, len);
  422. }
  423. if (inputStream != null) {
  424. inputStream.close();
  425. }
  426. if (outputStream != null) {
  427. outputStream.close();
  428. }
  429. return file;
  430. } else {
  431. throw new Exception("请求微信生成二维码失败!");
  432. }
  433. } else {
  434. throw new Exception("请求微信生成二维码失败!");
  435. }
  436. } catch (Exception e) {
  437. e.printStackTrace();
  438. throw new Exception(e.getMessage());
  439. }
  440. }
  441. }