123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634 |
- 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<String, String> wxMap = Sign.sign(WeixinUtil.getJsAPI(token.getToken()), url);
- wxMap.put("appId", wxPayProperties.getAppId());
- HashMap<String, Object> dataMap = new HashMap<String, Object>();
- 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<String, Object> dataMap = new HashMap<String, Object>();
- 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<String, Object> dataMap = new HashMap<String, Object>();
- 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<String, Object> wxMap = new HashMap<>();
- wxMap.put("appId", wxPayProperties.getAppId());
- HashMap<String, Object> dataMap = new HashMap<String, Object>();
- 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<String, Object> dataMap = new HashMap<String, Object>();
- 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<Map> 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<Map> 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<Map> 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<InformationInfo> 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);
- }
- }
|