QrcodeService.java 17 KB

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