package com.jpsoft.smart.modules.wechat.controller; import com.alibaba.fastjson.JSONObject; import com.jpsoft.smart.modules.base.entity.InformationInfo; import com.jpsoft.smart.modules.base.entity.OwnerInfo; import com.jpsoft.smart.modules.base.service.InformationInfoService; import com.jpsoft.smart.modules.base.service.OwnerInfoService; import com.jpsoft.smart.modules.common.dto.MessageResult; import com.jpsoft.smart.modules.common.service.impl.RedisServiceImpl; import com.jpsoft.smart.modules.common.utils.ApiUtil; import com.jpsoft.smart.modules.common.utils.Generator.CaptchaUtilA; import com.jpsoft.smart.modules.common.utils.Generator.CircleCaptchaA; import com.jpsoft.smart.modules.common.utils.SMSUtil; import com.jpsoft.smart.modules.common.utils.Sign; import com.jpsoft.smart.modules.common.utils.WeixinUtil; import com.jpsoft.smart.modules.pay.properties.WxPayProperties; import com.jpsoft.smart.modules.wechat.entity.AccessControl; import com.jpsoft.smart.modules.wechat.entity.AccessToken; import com.jpsoft.smart.modules.wechat.service.IAccessControlService; import com.jpsoft.smart.modules.wechat.vo.*; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import java.util.*; /** * @author 墨鱼_mo * @date 2019-12-3 15:08 */ @Slf4j @RequestMapping("/wechat") @RestController public class WxController { @Autowired private ApiUtil apiUtil; @Autowired private RedisServiceImpl redisService; @Autowired private OwnerInfoService ownerInfoService; @Autowired private WxPayProperties wxPayProperties; @Autowired private IAccessControlService accessControlService; @Autowired private InformationInfoService informationInfoService; @ApiOperation(value = "获取微信配置") @GetMapping(value = "/getConfig") public MessageResult getConfig(String url) { int code = 200; String message = "获取成功"; Object data = ""; boolean result = true; try { AccessToken token = WeixinUtil.getAccessToken(wxPayProperties.getAppId(), wxPayProperties.getAppSecret()); Map wxMap = Sign.sign(WeixinUtil.getJsAPI(token.getToken()), url); wxMap.put("appId", wxPayProperties.getAppId()); HashMap dataMap = new HashMap(); dataMap.put("wxConfig", wxMap); data = dataMap; } catch (Exception ex) { code = 500; result = false; message = "系统错误"; } return new MessageResult(result, message, data, code); } @ApiOperation(value = "获取微信code") @PostMapping(value = "findWechatUrl") public MessageResult findWechatUrl(@RequestBody String url) { String newpath = WeixinUtil.getCodeRequest(wxPayProperties.getAppId(), url, "snsapi_userinfo"); HashMap dataMap = new HashMap(); dataMap.put("wechatUrl", newpath); return new MessageResult(true, "获取成功", dataMap, 200); } @ApiOperation(value = "获取公众号用户信息") @GetMapping(value = "findUserInfo/{code}") public MessageResult findUserInfo(@PathVariable String code) { try { System.out.println(code); AccessToken at = WeixinUtil.getAccessToken(wxPayProperties.getAppId(), wxPayProperties.getAppSecret(), code); if (at != null && StringUtils.isNotBlank(at.getOpenid())) { String openId = at.getOpenid(); System.out.println("openId:" + openId); UserInfo userInfo = WeixinUtil.getUserInfo(openId, at.getToken()); HashMap dataMap = new HashMap(); dataMap.put("userInfo", userInfo); return new MessageResult(true, "获取微信信息成功", userInfo, 200); } else { return new MessageResult(false, "获取微信信息失败", "", 400); } } catch (Exception ex) { ex.printStackTrace(); return new MessageResult(false, "系统错误", "", 500); } } @GetMapping(value = "getAppId/{id}") @ResponseBody public MessageResult getAppId(@PathVariable("id") String id) { int code = 200; String message = "获取成功"; Object data = ""; boolean result = true; try { HashMap wxMap = new HashMap<>(); wxMap.put("appId", wxPayProperties.getAppId()); HashMap dataMap = new HashMap(); dataMap.put("wxConfig", wxMap); data = dataMap; } catch (Exception ex) { code = 500; message = "系统错误"; result = false; } return new MessageResult(result, message, data, code); } @GetMapping(value = "/findAccessControlByOpenId/{openId}") @ResponseBody public MessageResult findAccessControlByOpenId(@PathVariable(name = "openId", value = "") String openId) { int code = 200; String message = "查询成功"; Object data = ""; boolean result = true; if (StringUtils.isNotBlank(openId)) { AccessControl accessControl = accessControlService.findByOpenId(openId); if (accessControl == null) { code = 400; message = "用户信息不存在"; result = false; } else { OwnerInfo ownerInfo = ownerInfoService.get(accessControl.getOwnerId()); AccessControlVo accessControlVo = new AccessControlVo(); BeanUtils.copyProperties(accessControl,accessControlVo); accessControlVo.setName(ownerInfo.getName()); accessControlVo.setPhoneNum(ownerInfo.getTel()); HashMap dataMap = new HashMap(); dataMap.put("accessControl", accessControlVo); data = dataMap; } } else { code = 400; message = "用户信息不存在"; result = false; } return new MessageResult(result, message, data,code); } @ApiOperation(value = "获取门禁token") @PostMapping("/getApiToken") public MessageResult getApiToken() { boolean result = true; String message = ""; Object data = ""; try { String token = apiUtil.getApiToken(); if (StringUtils.isNotBlank(token)) { result = true; message = "获取token成功"; redisService.put("apiToken", "apiToken", token, 7200L); } } catch (Exception e) { e.printStackTrace(); result = false; message = "获取token失败"; } return new MessageResult(result, message, data, 200); } @ApiOperation(value = "公众号开门") @GetMapping(value = "/openDoor/{openId}") public MessageResult openDoor(@PathVariable(name = "openId", value = "") String openId) { int code = 200; String message = "开锁成功"; Object data = ""; if (StringUtils.isNotBlank(openId)) { AccessControl accessControl = accessControlService.findByOpenId(openId); if (accessControl == null) { code = 401; message = "请先绑定用户"; } else { String apiToken = (String) redisService.get("apiToken", "apiToken"); if (!apiUtil.testApiToken(apiToken, "215301")) { apiToken = apiUtil.getApiToken(); } JSONObject resultJSON = apiUtil.httpRequest("https://api.parkline.cc/api/devicecgi", apiToken, "01", "215301", "01", "", ""); String resultCode = resultJSON.getString("code"); if (!resultCode.equals("0")) { code = Integer.parseInt(resultCode); message = resultJSON.getString("msg"); } } } else { code = 400; message = "开锁失败"; } return new MessageResult(true, message, "", code); } @ApiOperation(value = "获取图片验证码信息") @GetMapping("/getVerifyPic") public MessageResult getVerifyPic(String openId, HttpServletResponse response) { int code = 200; String message = "获取验证码成功"; Object data = ""; boolean result = true; try { if (StringUtils.isBlank(openId)) { throw new Exception("openId为空"); } CircleCaptchaA captcha = CaptchaUtilA.createCircleCaptcha(200, 100, 4, 0); captcha.write(response.getOutputStream()); String verifyPic = captcha.getCode(); redisService.put("code", openId, verifyPic, 120); response.getOutputStream().close(); } catch (Exception e) { e.printStackTrace(); code = 500; message = e.getMessage(); result = false; } return new MessageResult(result, message, data, code); } @ApiOperation(value = "获取短信验证码") @PostMapping("/getVerifyCode") public MessageResult getVerifyCode(@RequestBody BindPhoneVo bindPhoneVo) { int code = 200; String message = "获取验证码成功"; Object data = ""; boolean result = true; try { if (StringUtils.isBlank(bindPhoneVo.getOpenId())) { throw new Exception("openId为空"); } if (StringUtils.isBlank(bindPhoneVo.getName()) || StringUtils.isBlank(bindPhoneVo.getPhoneNum())) { throw new Exception("业主姓名和手机号必填"); } if (StringUtils.isBlank(bindPhoneVo.getVerifyPicCode())) { throw new Exception("图片验证码为空"); } String verifyPicCode = (String) redisService.get("code", bindPhoneVo.getOpenId()); if (StringUtils.isBlank(verifyPicCode)) { throw new Exception("图片验证码过期"); } if (!verifyPicCode.equals(bindPhoneVo.getVerifyPicCode())) { throw new Exception("图片验证码错误"); } List ownerInfoList = ownerInfoService.findByTel(bindPhoneVo.getPhoneNum()); if (ownerInfoList.size()<=0){ throw new Exception("人员信息未录入,请联系物业公司"); } AccessControl accessControl = accessControlService.findByOwnerId(ownerInfoList.get(0).get("id").toString()); if (accessControl != null) { throw new Exception("此手机号已被绑定"); } String apiToken = (String) redisService.get("apiToken", "apiToken"); if (!apiUtil.testApiToken(apiToken, "215301")) { apiToken = apiUtil.getApiToken(); } JSONObject resultJSON = apiUtil.httpRequest("https://api.parkline.cc/api/facecgi", apiToken, "101", "", "", bindPhoneVo.getPhoneNum(), ""); if (!resultJSON.getString("code").equals("100101")) { code = resultJSON.getIntValue("code"); if (code == 100100) { message = "未找到业主信息,请联系物业"; } else { message = resultJSON.getString("msg"); } return new MessageResult(result, message, data, code); } else { JSONObject dataJSON = resultJSON.getJSONObject("data"); String name = dataJSON.getString("name"); if (!bindPhoneVo.getName().equals(name)) { throw new Exception("业主姓名有误"); } } String verifyCode = SMSUtil.generateNumberString(6); JSONObject verifyCodeJSON = new JSONObject(); verifyCodeJSON.put("code", verifyCode); MessageResult rmg = SMSUtil.send(bindPhoneVo.getPhoneNum(), "SMS_49390047", verifyCodeJSON.toString()); if (rmg.isResult()) { redisService.put("code", bindPhoneVo.getPhoneNum(), verifyCode, 120); } } catch (Exception e) { e.printStackTrace(); code = 400; message = e.getMessage(); result = false; } return new MessageResult(result, message, data, code); } @ApiOperation(value = "公众号绑定住户手机") @PostMapping(value = "/bindPhoneNumAndOpenId") public MessageResult bindPhoneNumAndOpenId(@RequestBody BindPhoneVo bindPhoneVo) { int code = 200; String message = "绑定成功"; Object data = ""; boolean result = true; try { if (StringUtils.isBlank(bindPhoneVo.getPhoneNum()) || StringUtils.isBlank(bindPhoneVo.getOpenId()) || StringUtils.isBlank(bindPhoneVo.getVerifyCode())) { throw new Exception("参数错误"); } // else { String verifyCode = (String) redisService.get("code", bindPhoneVo.getPhoneNum()); if (StringUtils.isBlank(verifyCode)) { throw new Exception("验证码过期"); } if (!verifyCode.equals(bindPhoneVo.getVerifyCode())) { throw new Exception("验证码错误"); } AccessControl accessControl1 = accessControlService.findByOpenId(bindPhoneVo.getOpenId()); if (accessControl1 != null) { throw new Exception("此微信号已绑定手机号"); } String apiToken = (String) redisService.get("apiToken", "apiToken"); if (!apiUtil.testApiToken(apiToken, "215301")) { apiToken = apiUtil.getApiToken(); } JSONObject resultJSON = apiUtil.httpRequest("https://api.parkline.cc/api/facecgi", apiToken, "101", "", "", bindPhoneVo.getPhoneNum(), ""); if (!resultJSON.getString("code").equals("100101")) { code = resultJSON.getIntValue("code"); System.out.println(code); if (code == 100100) { message = "未找到业主信息,请联系物业"; } else { message = resultJSON.getString("msg"); } } else { net.sf.json.JSONObject dataJSON = net.sf.json.JSONObject.fromObject(resultJSON.getString("data")); List ownerInfoList = ownerInfoService.findByTel(bindPhoneVo.getPhoneNum()); AccessControl accessControl2 = new AccessControl(); accessControl2.setId(UUID.randomUUID().toString()); accessControl2.setOwnerId(ownerInfoList.get(0).get("id").toString()); accessControl2.setOpenId(bindPhoneVo.getOpenId()); accessControl2.setBindTime(new Date()); accessControl2.setBindFace(false); accessControl2.setCreateTime(new Date()); accessControlService.save(accessControl2); } // } } catch (Exception e) { log.error(e.getMessage(), e); code = 400; message = e.getMessage(); result = false; } return new MessageResult(result, message, data, code); } @ApiOperation(value = "微信端上传住户人脸") @PostMapping(value = "/bindFace") public MessageResult bindFace(@RequestBody BindFaceVo bindFaceVo) { int code = 200; String message = "提交成功"; Object data = ""; boolean result = true; List ownerList = ownerInfoService.findByTel(bindFaceVo.getPhoneNum()); if (ownerList.size() <= 0) { return new MessageResult(false, "未找到用户信息,请联系物业", data, 400); } AccessControl accessControl = accessControlService.findByOwnerId(ownerList.get(0).get("id").toString()); if (accessControl == null) { return new MessageResult(false, "请先进行微信绑定", data, 400); } if (StringUtils.isBlank(bindFaceVo.getPhoneNum()) || StringUtils.isBlank(bindFaceVo.getFiledata())) { code = 400; message = "参数错误"; return new MessageResult(false, message, data, code); } String apiToken = (String) redisService.get("apiToken", "apiToken"); if (!apiUtil.testApiToken(apiToken, "215301")) { apiToken = apiUtil.getApiToken(); } JSONObject resultJSON = apiUtil.httpRequest("https://api.parkline.cc/api/facecgi", apiToken, "300", "215301", "01", bindFaceVo.getPhoneNum(), bindFaceVo.getFiledata()); System.out.println("返回码:"+resultJSON.getInteger("code")+"-------------"); if (!resultJSON.getString("code").equals("100101")) { code = resultJSON.getInteger("code"); if (code == 100100) { message = "未找到业主信息,请联系物业"; } else { message = resultJSON.getString("msg"); } } else if (resultJSON.getString("code").equals("1")) { accessControl.setBindFace(true); accessControlService.update(accessControl); } return new MessageResult(result, message, "", code); } @ApiOperation(value = "微信端报修或投诉列表") @PostMapping(value = "/reportAndRepairsList") @ResponseBody public MessageResult reportAndRepairsList(@RequestBody RepairsVo repairsVo) { int code = 200; String message = "查询成功"; Object data = ""; boolean result = true; if (StringUtils.isBlank(repairsVo.getOpenId())) { code = 400; message = "openId为空"; return new MessageResult(false, message, "", code); } if (repairsVo.getType() == null) { return new MessageResult(false, "类型不能为空", "", 400); } AccessControl accessControl = accessControlService.findByOpenId(repairsVo.getOpenId()); if (accessControl == null) { return new MessageResult(false, "此微信还未绑定住户", "", 400); } List informationInfoList = informationInfoService.findByOwnerIdAndType(accessControl.getOwnerId(),repairsVo.getType()); data = informationInfoList; return new MessageResult(result, message, data, code); } @ApiOperation(value = "微信端提交报修或投诉") @PostMapping(value = "/reportAndRepairs") @ResponseBody public MessageResult reportAndRepairs(@RequestBody RepairsVo repairsVo) { int code = 200; String message = "提交成功"; Object data = ""; boolean result = true; try{ if (StringUtils.isBlank(repairsVo.getOpenId()) || StringUtils.isBlank(repairsVo.getContent())) { throw new Exception("参数错误"); } if (repairsVo.getType() == null) { throw new Exception("类型不能为空"); } AccessControl accessControl = accessControlService.findByOpenId(repairsVo.getOpenId()); if (accessControl == null) { throw new Exception("此微信还未绑定住户"); } InformationInfo informationInfo = new InformationInfo(); informationInfo.setId(UUID.randomUUID().toString()); informationInfo.setDelFlag(false); informationInfo.setCreateBy(repairsVo.getOpenId()); informationInfo.setCreateTime(new Date()); informationInfo.setStatus("0"); informationInfo.setContent(repairsVo.getContent()); informationInfo.setOwnerId(accessControl.getOwnerId()); informationInfo.setType(repairsVo.getType()); informationInfoService.insert(informationInfo); }catch (Exception e){ e.printStackTrace(); log.error(e.getMessage(),e); message = e.getMessage(); result = false; code = 400; } return new MessageResult(result, message, data, code); } @ApiOperation(value = "根据报修或投诉id获取内容") @GetMapping(value = "/reportAndRepairsDetail") @ResponseBody public MessageResult reportAndRepairsDetail(String id) { int code = 200; String message = "查询成功"; Object data = ""; boolean result = true; try{ if (id == null) { throw new Exception("id为空"); } InformationInfo informationInfo = informationInfoService.get(id); data = informationInfo; }catch (Exception e){ e.printStackTrace(); log.error(e.getMessage(),e); message = e.getMessage(); code = 400; result = false; } return new MessageResult(result, message, data, code); } }