WxController.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. package com.jpsoft.smart.modules.wechat.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.jpsoft.smart.modules.base.entity.InformationInfo;
  4. import com.jpsoft.smart.modules.base.entity.OwnerInfo;
  5. import com.jpsoft.smart.modules.base.service.InformationInfoService;
  6. import com.jpsoft.smart.modules.base.service.OwnerInfoService;
  7. import com.jpsoft.smart.modules.common.dto.MessageResult;
  8. import com.jpsoft.smart.modules.common.service.impl.RedisServiceImpl;
  9. import com.jpsoft.smart.modules.common.utils.ApiUtil;
  10. import com.jpsoft.smart.modules.common.utils.Generator.CaptchaUtilA;
  11. import com.jpsoft.smart.modules.common.utils.Generator.CircleCaptchaA;
  12. import com.jpsoft.smart.modules.common.utils.SMSUtil;
  13. import com.jpsoft.smart.modules.common.utils.Sign;
  14. import com.jpsoft.smart.modules.common.utils.WeixinUtil;
  15. import com.jpsoft.smart.modules.pay.properties.WxPayProperties;
  16. import com.jpsoft.smart.modules.wechat.entity.AccessControl;
  17. import com.jpsoft.smart.modules.wechat.entity.AccessToken;
  18. import com.jpsoft.smart.modules.wechat.service.IAccessControlService;
  19. import com.jpsoft.smart.modules.wechat.vo.*;
  20. import io.swagger.annotations.ApiOperation;
  21. import lombok.extern.slf4j.Slf4j;
  22. import org.apache.commons.lang3.StringUtils;
  23. import org.springframework.beans.BeanUtils;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.web.bind.annotation.*;
  26. import javax.servlet.http.HttpServletResponse;
  27. import java.util.*;
  28. /**
  29. * @author 墨鱼_mo
  30. * @date 2019-12-3 15:08
  31. */
  32. @Slf4j
  33. @RequestMapping("/wechat")
  34. @RestController
  35. public class WxController {
  36. @Autowired
  37. private ApiUtil apiUtil;
  38. @Autowired
  39. private RedisServiceImpl redisService;
  40. @Autowired
  41. private OwnerInfoService ownerInfoService;
  42. @Autowired
  43. private WxPayProperties wxPayProperties;
  44. @Autowired
  45. private IAccessControlService accessControlService;
  46. @Autowired
  47. private InformationInfoService informationInfoService;
  48. @ApiOperation(value = "获取微信配置")
  49. @GetMapping(value = "/getConfig")
  50. public MessageResult getConfig(String url) {
  51. int code = 200;
  52. String message = "获取成功";
  53. Object data = "";
  54. boolean result = true;
  55. try {
  56. AccessToken token = WeixinUtil.getAccessToken(wxPayProperties.getAppId(), wxPayProperties.getAppSecret());
  57. Map<String, String> wxMap = Sign.sign(WeixinUtil.getJsAPI(token.getToken()), url);
  58. wxMap.put("appId", wxPayProperties.getAppId());
  59. HashMap<String, Object> dataMap = new HashMap<String, Object>();
  60. dataMap.put("wxConfig", wxMap);
  61. data = dataMap;
  62. } catch (Exception ex) {
  63. code = 500;
  64. result = false;
  65. message = "系统错误";
  66. }
  67. return new MessageResult(result, message, data, code);
  68. }
  69. @ApiOperation(value = "获取微信code")
  70. @PostMapping(value = "findWechatUrl")
  71. public MessageResult findWechatUrl(@RequestBody String url) {
  72. String newpath = WeixinUtil.getCodeRequest(wxPayProperties.getAppId(), url, "snsapi_userinfo");
  73. HashMap<String, Object> dataMap = new HashMap<String, Object>();
  74. dataMap.put("wechatUrl", newpath);
  75. return new MessageResult(true, "获取成功", dataMap, 200);
  76. }
  77. @ApiOperation(value = "获取公众号用户信息")
  78. @GetMapping(value = "findUserInfo/{code}")
  79. public MessageResult findUserInfo(@PathVariable String code) {
  80. try {
  81. System.out.println(code);
  82. AccessToken at = WeixinUtil.getAccessToken(wxPayProperties.getAppId(), wxPayProperties.getAppSecret(), code);
  83. if (at != null && StringUtils.isNotBlank(at.getOpenid())) {
  84. String openId = at.getOpenid();
  85. System.out.println("openId:" + openId);
  86. UserInfo userInfo = WeixinUtil.getUserInfo(openId, at.getToken());
  87. HashMap<String, Object> dataMap = new HashMap<String, Object>();
  88. dataMap.put("userInfo", userInfo);
  89. return new MessageResult(true, "获取微信信息成功", userInfo, 200);
  90. } else {
  91. return new MessageResult(false, "获取微信信息失败", "", 400);
  92. }
  93. } catch (Exception ex) {
  94. ex.printStackTrace();
  95. return new MessageResult(false, "系统错误", "", 500);
  96. }
  97. }
  98. @GetMapping(value = "getAppId/{id}")
  99. @ResponseBody
  100. public MessageResult getAppId(@PathVariable("id") String id) {
  101. int code = 200;
  102. String message = "获取成功";
  103. Object data = "";
  104. boolean result = true;
  105. try {
  106. HashMap<String, Object> wxMap = new HashMap<>();
  107. wxMap.put("appId", wxPayProperties.getAppId());
  108. HashMap<String, Object> dataMap = new HashMap<String, Object>();
  109. dataMap.put("wxConfig", wxMap);
  110. data = dataMap;
  111. } catch (Exception ex) {
  112. code = 500;
  113. message = "系统错误";
  114. result = false;
  115. }
  116. return new MessageResult(result, message, data, code);
  117. }
  118. @GetMapping(value = "/findAccessControlByOpenId/{openId}")
  119. @ResponseBody
  120. public MessageResult findAccessControlByOpenId(@PathVariable(name = "openId", value = "") String openId) {
  121. int code = 200;
  122. String message = "查询成功";
  123. Object data = "";
  124. boolean result = true;
  125. if (StringUtils.isNotBlank(openId)) {
  126. AccessControl accessControl = accessControlService.findByOpenId(openId);
  127. if (accessControl == null) {
  128. code = 400;
  129. message = "用户信息不存在";
  130. result = false;
  131. } else {
  132. OwnerInfo ownerInfo = ownerInfoService.get(accessControl.getOwnerId());
  133. AccessControlVo accessControlVo = new AccessControlVo();
  134. BeanUtils.copyProperties(accessControl,accessControlVo);
  135. accessControlVo.setName(ownerInfo.getName());
  136. accessControlVo.setPhoneNum(ownerInfo.getTel());
  137. HashMap<String, Object> dataMap = new HashMap<String, Object>();
  138. dataMap.put("accessControl", accessControlVo);
  139. data = dataMap;
  140. }
  141. } else {
  142. code = 400;
  143. message = "用户信息不存在";
  144. result = false;
  145. }
  146. return new MessageResult(result, message, data,code);
  147. }
  148. @ApiOperation(value = "获取门禁token")
  149. @PostMapping("/getApiToken")
  150. public MessageResult getApiToken() {
  151. boolean result = true;
  152. String message = "";
  153. Object data = "";
  154. try {
  155. String token = apiUtil.getApiToken();
  156. if (StringUtils.isNotBlank(token)) {
  157. result = true;
  158. message = "获取token成功";
  159. redisService.put("apiToken", "apiToken", token, 7200L);
  160. }
  161. } catch (Exception e) {
  162. e.printStackTrace();
  163. result = false;
  164. message = "获取token失败";
  165. }
  166. return new MessageResult(result, message, data, 200);
  167. }
  168. @ApiOperation(value = "公众号开门")
  169. @GetMapping(value = "/openDoor/{openId}")
  170. public MessageResult openDoor(@PathVariable(name = "openId", value = "") String openId) {
  171. int code = 200;
  172. String message = "开锁成功";
  173. Object data = "";
  174. if (StringUtils.isNotBlank(openId)) {
  175. AccessControl accessControl = accessControlService.findByOpenId(openId);
  176. if (accessControl == null) {
  177. code = 401;
  178. message = "请先绑定用户";
  179. } else {
  180. String apiToken = (String) redisService.get("apiToken", "apiToken");
  181. if (!apiUtil.testApiToken(apiToken, "215301")) {
  182. apiToken = apiUtil.getApiToken();
  183. }
  184. JSONObject resultJSON = apiUtil.httpRequest("https://api.parkline.cc/api/devicecgi", apiToken, "01", "215301", "01", "", "");
  185. String resultCode = resultJSON.getString("code");
  186. if (!resultCode.equals("0")) {
  187. code = Integer.parseInt(resultCode);
  188. message = resultJSON.getString("msg");
  189. }
  190. }
  191. } else {
  192. code = 400;
  193. message = "开锁失败";
  194. }
  195. return new MessageResult(true, message, "", code);
  196. }
  197. @ApiOperation(value = "获取图片验证码信息")
  198. @GetMapping("/getVerifyPic")
  199. public MessageResult getVerifyPic(String openId, HttpServletResponse response) {
  200. int code = 200;
  201. String message = "获取验证码成功";
  202. Object data = "";
  203. boolean result = true;
  204. try {
  205. if (StringUtils.isBlank(openId)) {
  206. throw new Exception("openId为空");
  207. }
  208. CircleCaptchaA captcha = CaptchaUtilA.createCircleCaptcha(200, 100, 4, 0);
  209. captcha.write(response.getOutputStream());
  210. String verifyPic = captcha.getCode();
  211. redisService.put("code", openId, verifyPic, 120);
  212. response.getOutputStream().close();
  213. } catch (Exception e) {
  214. e.printStackTrace();
  215. code = 500;
  216. message = e.getMessage();
  217. result = false;
  218. }
  219. return new MessageResult(result, message, data, code);
  220. }
  221. @ApiOperation(value = "获取短信验证码")
  222. @PostMapping("/getVerifyCode")
  223. public MessageResult getVerifyCode(@RequestBody BindPhoneVo bindPhoneVo) {
  224. int code = 200;
  225. String message = "获取验证码成功";
  226. Object data = "";
  227. boolean result = true;
  228. try {
  229. if (StringUtils.isBlank(bindPhoneVo.getOpenId())) {
  230. throw new Exception("openId为空");
  231. }
  232. if (StringUtils.isBlank(bindPhoneVo.getName()) || StringUtils.isBlank(bindPhoneVo.getPhoneNum())) {
  233. throw new Exception("业主姓名和手机号必填");
  234. }
  235. if (StringUtils.isBlank(bindPhoneVo.getVerifyPicCode())) {
  236. throw new Exception("图片验证码为空");
  237. }
  238. String verifyPicCode = (String) redisService.get("code", bindPhoneVo.getOpenId());
  239. if (StringUtils.isBlank(verifyPicCode)) {
  240. throw new Exception("图片验证码过期");
  241. }
  242. if (!verifyPicCode.equals(bindPhoneVo.getVerifyPicCode())) {
  243. throw new Exception("图片验证码错误");
  244. }
  245. List<Map> ownerInfoList = ownerInfoService.findByTel(bindPhoneVo.getPhoneNum());
  246. if (ownerInfoList.size()<=0){
  247. throw new Exception("人员信息未录入,请联系物业公司");
  248. }
  249. AccessControl accessControl = accessControlService.findByOwnerId(ownerInfoList.get(0).get("id").toString());
  250. if (accessControl != null) {
  251. throw new Exception("此手机号已被绑定");
  252. }
  253. String apiToken = (String) redisService.get("apiToken", "apiToken");
  254. if (!apiUtil.testApiToken(apiToken, "215301")) {
  255. apiToken = apiUtil.getApiToken();
  256. }
  257. JSONObject resultJSON = apiUtil.httpRequest("https://api.parkline.cc/api/facecgi", apiToken, "101", "", "", bindPhoneVo.getPhoneNum(), "");
  258. if (!resultJSON.getString("code").equals("100101")) {
  259. code = resultJSON.getIntValue("code");
  260. if (code == 100100) {
  261. message = "未找到业主信息,请联系物业";
  262. } else {
  263. message = resultJSON.getString("msg");
  264. }
  265. return new MessageResult(result, message, data, code);
  266. } else {
  267. JSONObject dataJSON = resultJSON.getJSONObject("data");
  268. String name = dataJSON.getString("name");
  269. if (!bindPhoneVo.getName().equals(name)) {
  270. throw new Exception("业主姓名有误");
  271. }
  272. }
  273. String verifyCode = SMSUtil.generateNumberString(6);
  274. JSONObject verifyCodeJSON = new JSONObject();
  275. verifyCodeJSON.put("code", verifyCode);
  276. MessageResult rmg = SMSUtil.send(bindPhoneVo.getPhoneNum(), "SMS_49390047", verifyCodeJSON.toString());
  277. if (rmg.isResult()) {
  278. redisService.put("code", bindPhoneVo.getPhoneNum(), verifyCode, 120);
  279. }
  280. } catch (Exception e) {
  281. e.printStackTrace();
  282. code = 400;
  283. message = e.getMessage();
  284. result = false;
  285. }
  286. return new MessageResult(result, message, data, code);
  287. }
  288. @ApiOperation(value = "公众号绑定住户手机")
  289. @PostMapping(value = "/bindPhoneNumAndOpenId")
  290. public MessageResult bindPhoneNumAndOpenId(@RequestBody BindPhoneVo bindPhoneVo) {
  291. int code = 200;
  292. String message = "绑定成功";
  293. Object data = "";
  294. boolean result = true;
  295. try {
  296. if (StringUtils.isBlank(bindPhoneVo.getPhoneNum()) || StringUtils.isBlank(bindPhoneVo.getOpenId()) || StringUtils.isBlank(bindPhoneVo.getVerifyCode())) {
  297. throw new Exception("参数错误");
  298. }
  299. // else {
  300. String verifyCode = (String) redisService.get("code", bindPhoneVo.getPhoneNum());
  301. if (StringUtils.isBlank(verifyCode)) {
  302. throw new Exception("验证码过期");
  303. }
  304. if (!verifyCode.equals(bindPhoneVo.getVerifyCode())) {
  305. throw new Exception("验证码错误");
  306. }
  307. AccessControl accessControl1 = accessControlService.findByOpenId(bindPhoneVo.getOpenId());
  308. if (accessControl1 != null) {
  309. throw new Exception("此微信号已绑定手机号");
  310. }
  311. String apiToken = (String) redisService.get("apiToken", "apiToken");
  312. if (!apiUtil.testApiToken(apiToken, "215301")) {
  313. apiToken = apiUtil.getApiToken();
  314. }
  315. JSONObject resultJSON = apiUtil.httpRequest("https://api.parkline.cc/api/facecgi", apiToken, "101", "", "", bindPhoneVo.getPhoneNum(), "");
  316. if (!resultJSON.getString("code").equals("100101")) {
  317. code = resultJSON.getIntValue("code");
  318. System.out.println(code);
  319. if (code == 100100) {
  320. message = "未找到业主信息,请联系物业";
  321. } else {
  322. message = resultJSON.getString("msg");
  323. }
  324. } else {
  325. net.sf.json.JSONObject dataJSON = net.sf.json.JSONObject.fromObject(resultJSON.getString("data"));
  326. List<Map> ownerInfoList = ownerInfoService.findByTel(bindPhoneVo.getPhoneNum());
  327. AccessControl accessControl2 = new AccessControl();
  328. accessControl2.setId(UUID.randomUUID().toString());
  329. accessControl2.setOwnerId(ownerInfoList.get(0).get("id").toString());
  330. accessControl2.setOpenId(bindPhoneVo.getOpenId());
  331. accessControl2.setBindTime(new Date());
  332. accessControl2.setBindFace(false);
  333. accessControl2.setCreateTime(new Date());
  334. accessControlService.save(accessControl2);
  335. }
  336. // }
  337. } catch (Exception e) {
  338. log.error(e.getMessage(), e);
  339. code = 400;
  340. message = e.getMessage();
  341. result = false;
  342. }
  343. return new MessageResult(result, message, data, code);
  344. }
  345. @ApiOperation(value = "微信端上传住户人脸")
  346. @PostMapping(value = "/bindFace")
  347. public MessageResult bindFace(@RequestBody BindFaceVo bindFaceVo) {
  348. int code = 200;
  349. String message = "提交成功";
  350. Object data = "";
  351. boolean result = true;
  352. List<Map> ownerList = ownerInfoService.findByTel(bindFaceVo.getPhoneNum());
  353. if (ownerList.size() <= 0) {
  354. return new MessageResult(false, "未找到用户信息,请联系物业", data, 400);
  355. }
  356. AccessControl accessControl = accessControlService.findByOwnerId(ownerList.get(0).get("id").toString());
  357. if (accessControl == null) {
  358. return new MessageResult(false, "请先进行微信绑定", data, 400);
  359. }
  360. if (StringUtils.isBlank(bindFaceVo.getPhoneNum()) || StringUtils.isBlank(bindFaceVo.getFiledata())) {
  361. code = 400;
  362. message = "参数错误";
  363. return new MessageResult(false, message, data, code);
  364. }
  365. String apiToken = (String) redisService.get("apiToken", "apiToken");
  366. if (!apiUtil.testApiToken(apiToken, "215301")) {
  367. apiToken = apiUtil.getApiToken();
  368. }
  369. JSONObject resultJSON = apiUtil.httpRequest("https://api.parkline.cc/api/facecgi", apiToken, "300", "215301", "01", bindFaceVo.getPhoneNum(), bindFaceVo.getFiledata());
  370. System.out.println("返回码:"+resultJSON.getInteger("code")+"-------------");
  371. if (!resultJSON.getString("code").equals("100101")) {
  372. code = resultJSON.getInteger("code");
  373. if (code == 100100) {
  374. message = "未找到业主信息,请联系物业";
  375. } else {
  376. message = resultJSON.getString("msg");
  377. }
  378. } else if (resultJSON.getString("code").equals("1")) {
  379. accessControl.setBindFace(true);
  380. accessControlService.update(accessControl);
  381. }
  382. return new MessageResult(result, message, "", code);
  383. }
  384. @ApiOperation(value = "微信端报修或投诉列表")
  385. @PostMapping(value = "/reportAndRepairsList")
  386. @ResponseBody
  387. public MessageResult reportAndRepairsList(@RequestBody RepairsVo repairsVo) {
  388. int code = 200;
  389. String message = "查询成功";
  390. Object data = "";
  391. boolean result = true;
  392. if (StringUtils.isBlank(repairsVo.getOpenId())) {
  393. code = 400;
  394. message = "openId为空";
  395. return new MessageResult(false, message, "", code);
  396. }
  397. if (repairsVo.getType() == null) {
  398. return new MessageResult(false, "类型不能为空", "", 400);
  399. }
  400. AccessControl accessControl = accessControlService.findByOpenId(repairsVo.getOpenId());
  401. if (accessControl == null) {
  402. return new MessageResult(false, "此微信还未绑定住户", "", 400);
  403. }
  404. List<InformationInfo> informationInfoList = informationInfoService.findByOwnerIdAndType(accessControl.getOwnerId(),repairsVo.getType());
  405. data = informationInfoList;
  406. return new MessageResult(result, message, data, code);
  407. }
  408. @ApiOperation(value = "微信端提交报修或投诉")
  409. @PostMapping(value = "/reportAndRepairs")
  410. @ResponseBody
  411. public MessageResult reportAndRepairs(@RequestBody RepairsVo repairsVo) {
  412. int code = 200;
  413. String message = "提交成功";
  414. Object data = "";
  415. boolean result = true;
  416. try{
  417. if (StringUtils.isBlank(repairsVo.getOpenId()) || StringUtils.isBlank(repairsVo.getContent())) {
  418. throw new Exception("参数错误");
  419. }
  420. if (repairsVo.getType() == null) {
  421. throw new Exception("类型不能为空");
  422. }
  423. AccessControl accessControl = accessControlService.findByOpenId(repairsVo.getOpenId());
  424. if (accessControl == null) {
  425. throw new Exception("此微信还未绑定住户");
  426. }
  427. InformationInfo informationInfo = new InformationInfo();
  428. informationInfo.setId(UUID.randomUUID().toString());
  429. informationInfo.setDelFlag(false);
  430. informationInfo.setCreateBy(repairsVo.getOpenId());
  431. informationInfo.setCreateTime(new Date());
  432. informationInfo.setStatus("0");
  433. informationInfo.setContent(repairsVo.getContent());
  434. informationInfo.setOwnerId(accessControl.getOwnerId());
  435. informationInfo.setType(repairsVo.getType());
  436. informationInfoService.insert(informationInfo);
  437. }catch (Exception e){
  438. e.printStackTrace();
  439. log.error(e.getMessage(),e);
  440. message = e.getMessage();
  441. result = false;
  442. code = 400;
  443. }
  444. return new MessageResult(result, message, data, code);
  445. }
  446. @ApiOperation(value = "根据报修或投诉id获取内容")
  447. @GetMapping(value = "/reportAndRepairsDetail")
  448. @ResponseBody
  449. public MessageResult reportAndRepairsDetail(String id) {
  450. int code = 200;
  451. String message = "查询成功";
  452. Object data = "";
  453. boolean result = true;
  454. try{
  455. if (id == null) {
  456. throw new Exception("id为空");
  457. }
  458. InformationInfo informationInfo = informationInfoService.get(id);
  459. data = informationInfo;
  460. }catch (Exception e){
  461. e.printStackTrace();
  462. log.error(e.getMessage(),e);
  463. message = e.getMessage();
  464. code = 400;
  465. result = false;
  466. }
  467. return new MessageResult(result, message, data, code);
  468. }
  469. }