package com.template.controller; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.template.annotation.PassToken; import com.template.api.LoginControllerAPI; import com.template.common.utils.*; import com.template.config.WxAuthorConfig; import com.template.config.WxOpenidConfig; import com.template.model.enumModel.eIdentityTypeStatu; import com.template.model.enumModel.eSchool; import com.template.model.enumModel.eStatu; import com.template.model.pojo.*; import com.template.model.request.*; import com.template.model.result.CommonResult; import com.template.model.result.Wx_user; import com.template.model.vo.*; import com.template.services.*; import io.swagger.annotations.ApiModelProperty; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.configurationprocessor.json.JSONObject; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.awt.image.BufferedImage; import java.io.IOException; import java.math.BigDecimal; import java.math.BigInteger; import java.net.URLEncoder; import java.util.*; /** * @Author: binguo * @Date: 2023/7/5 星期三 9:28 * @Description: com.template.controller * @Version: 1.0 */ @RestController public class LoginController implements LoginControllerAPI { @Autowired private WxOpenidConfig wxOpenidConfig; @Resource private WelcomeOrgService welcomeOrgService; @Resource private WelcomeFamilyService welcomeFamilyService; @Resource private WelcomeStudentService welcomeStudentService; @Resource private WelcomeAccountService welcomeAccountService; @Resource private WelcomeAccompanyService welcomeAccompanyService; @Resource private WelcomePaySettingService welcomePaySettingService; @Resource private WelcomeArriveSettingService welcomeArriveSettingService; private static Logger logger = LoggerFactory.getLogger(LoginController.class); /** * 查看系统版本号 * * @return */ @Override @PassToken public CommonResult queryReduce() { return CommonResult.ok("当前系统版本为:1V"); } /** * 登录接口 * * @param loginRequest account 账号 * password 密码 * @return */ @Override @PassToken public CommonResult Login(@RequestBody loginRequest loginRequest, BindingResult bindingResult) { if (loginRequest == null) { return CommonResult.fail("请传递参数"); } if (bindingResult.hasErrors()) { String st = paramUtils.getParamError(bindingResult); return CommonResult.fail(st); } WelcomeAccount result = welcomeAccountService.getDataByAccount(loginRequest.getAccount()); if (result == null) { return CommonResult.fail("账号不存在"); } if (result.getStatus()!=1) { return CommonResult.fail("账号已冻结"); } String encPassword = AesUtils.encrypt(loginRequest.getPassword()); if (!encPassword.equals(result.getPassword())) { return CommonResult.fail("密码错误"); } TokenDateVo tokenDate = JWTUtil.getToken("", result.getId(), null); String token = tokenDate == null ? "" : tokenDate.getToken(); Date expireTime = tokenDate == null ? new Date() : tokenDate.getExpireTime(); LoginVO login = new LoginVO(); login.setToken(token); login.setTokenTtl(TimeExchange.DateToString(expireTime, "yyyy-MM-dd HH:mm:ss")); login.setUserName(result.getName()); login.setAccountId(result.getId()); return CommonResult.ok("200", "登录成功", login); } /** * 修改密码 * * @param cpr oldPassword 旧密码 * newPassword 新密码 * confirmPassword 确认密码 * @param userId * @param bindingResult * @return */ @Override @Transactional(rollbackFor = {Exception.class}) public CommonResult ChangePassword(String userId, changePasswordRequest cpr, BindingResult bindingResult) throws Exception { ChangePasswordVO results = new ChangePasswordVO(); if (bindingResult.hasErrors()) { String st = paramUtils.getParamError(bindingResult); return CommonResult.fail(st); } WelcomeAccount result = welcomeAccountService.getManageById(userId); if (result == null) { return CommonResult.fail("账号不存在"); } if (!AesUtils.encrypt(cpr.getOldPassword()).equals(result.getPassword())) { return CommonResult.fail("原密码错误!"); } result.setPassword(AesUtils.encrypt(cpr.getNewPassword())); int updateData = welcomeAccountService.updateWelcomeAccount(result); return updateData > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败"); } @Override @PassToken @Transactional(rollbackFor = {Exception.class}) public CommonResult Openid(String wxcode, String urlstr, String state, HttpServletResponse response) throws Exception { logger.info("微校授权:" + wxcode + ";redirect_uri:" + urlstr + ";H5:" + state); System.out.println("微校授权:" + wxcode); Gson gson = new Gson(); String tokenUrl = "https://open.wecard.qq.com/connect/oauth2/token"; Map tokenParams = new HashMap<>(); tokenParams.put("wxcode", wxcode); tokenParams.put("app_key", wxOpenidConfig.getAppid()); tokenParams.put("app_secret", wxOpenidConfig.getAppkey()); tokenParams.put("grant_type", wxOpenidConfig.getGranttype()); tokenParams.put("redirect_uri", state); // H5 // wecode换取token String respon = HttpsClient.post(tokenUrl, tokenParams); System.out.println("微校授权2:" + respon); if (!StringUtils.hasText(respon)) { System.out.println("微校授权异常信息:respon为空" + respon); return CommonResult.fail("微校授权异常信息"); } HashMap tokenMap = gson.fromJson(respon, new TypeToken>() { }.getType()); String accessToken = (String) tokenMap.get("access_token"); // token换取用户信息 String userInfoUrl = "https://open.wecard.qq.com/connect/oauth/get-user-info"; Map userInfoParam = new HashMap<>(); userInfoParam.put("access_token", accessToken); String userinfo = HttpsClient.post(userInfoUrl, userInfoParam); String card_number = null; String user_name = null; String phone = null; String college = null; int idenType = 0; int gender = 0; String profession = null; String campus = null; String id_card = null; String classStr = null; System.out.println("微校授权获取用户信息:" + userinfo); Wx_user userinfos = gson.fromJson(userinfo, new TypeToken() { }.getType()); System.out.println("微校授权获取用户信息类别:" + userinfos.getIdentity_type()); try { card_number = userinfos.getCard_number(); user_name = userinfos.getName(); phone = userinfos.getTelephone(); college = userinfos.getCollege(); classStr = userinfos.getClassStr(); idenType = userinfos.getIdentity_type() == 1 || userinfos.getIdentity_type() == 6 ? 1 : 2;//1:学生 2:非学生 gender = userinfos.getGender(); profession = userinfos.getProfession(); campus = userinfos.getCampus(); id_card = userinfos.getId_card(); } catch (Exception e) { System.out.println("微校授权异常信息:" + e.getMessage()); response.sendRedirect(wxOpenidConfig.getIp() + "/#/pages/404/404/?message=" + URLEncoder.encode("获取用户信息失败", "UTF-8")); return CommonResult.fail(e.getMessage()); } if (card_number == null || card_number.equals("")) { response.sendRedirect(wxOpenidConfig.getIp() + "/#/pages/404/404/?message=" + URLEncoder.encode("获取用户信息失败", "UTF-8")); return CommonResult.fail("卡号为空,授权失败"); } //根据卡号查询repair_user表中的用户信息 WelcomeStudent student = welcomeStudentService.getDataByIdcard(id_card); //取消授权的身份验证 谁都能进 //if (identity_type != 4 && user == null) { // return CommonResult.fail("非法权限,授权失败"); //} System.out.println("微校授权校区:" + campus); Integer studentId = 0; if (student == null) { System.out.println("微校授权失败,学生信息新增异常:" + user_name + "" + card_number); response.sendRedirect(wxOpenidConfig.getIp() + "/#/pages/404/404/?message=" + URLEncoder.encode("用户在系统中不存在,请联系管理员", "UTF-8")); throw new Exception("微校授权失败!"); // try { // student = new WelcomeStudent(); // student.setAdmissNum(card_number); // student.setName(user_name); // student.setPhone(phone); // student.setSchool(campus); // student.setSchoolId(campus == null ? 0 : eSchool.stringOf(campus)); // student.setSex(gender == 1 ? "男" : "女"); // student.setCollege(college); // student.setMajor(profession); // student.setIdenType(idenType); // student.setClassstr(classStr); // student.setCardId(id_card);//身份证号 // int num = welcomeStudentService.insertWelcomeStudent(student); // if (num <= 0) { // System.out.println("微校授权失败,学生信息新增异常:" + user_name + "" + card_number); // response.sendRedirect(wxOpenidConfig.getIp() + "/#/pages/404/404/?message=" + URLEncoder.encode("用户新增异常", "UTF-8")); // throw new Exception("微校授权失败!"); // } // studentId = num; // } catch (Exception e) { // System.out.println("微校授权异常信息:" + e.getMessage()); // response.sendRedirect(wxOpenidConfig.getIp() + "/#/pages/404/404/?message=" + URLEncoder.encode("获取用户信息失败", "UTF-8")); // throw new Exception("微校授权失败!"); // } } else { //更新微校获取的年纪信息 student.setAdmissNum(card_number); student.setName(user_name); student.setPhone(phone); student.setSchool(campus); student.setSchoolId(campus == null ? 0 : eSchool.stringOf(campus)); student.setSex(gender == 1 ? "男" : "女"); // student.setCollege(college); // student.setMajor(profession); student.setIdenType(idenType); //student.setClassstr(classStr); student.setCardId(id_card);//身份证号 if(!(student.getIsPay() != null && student.getIsPay().intValue() == 1)){ List payInfos = WelcomePayController.queryStudentPayInfo(card_number,TimeExchange.getYear()); if(payInfos != null && payInfos.size() > 0){ List paySettings = welcomePaySettingService.queryPaySettings(campus); if(paySettings != null && paySettings.size() > 0){ for (WelcomePaySetting pay:paySettings) { BigDecimal money = pay.getPayAmount(); if(pay.getMethod().equals("全部")){ BigDecimal totalSj = new BigDecimal(BigInteger.ZERO); for (JsonPayVo jpv:payInfos){ totalSj = totalSj.add(jpv.getSJJE()); } if(totalSj.compareTo(money) >= 0){ student.setIsPay(1); } } Optional ojpv = payInfos.stream().filter(e -> e.getSFXMMC().equals(pay.getMethod())).findFirst(); if(ojpv != null && ojpv.isPresent()){ if(ojpv.get().getSJJE().compareTo(money) >= 0){ student.setIsPay(1); } } } }else{ student.setIsPay(0); } }else{ student.setIsPay(0); } } int num = welcomeStudentService.updateWelcomeStudent(student); if (num <= 0) { System.out.println("微校授权失败,用户信息新增异常:" + user_name + "" + card_number); response.sendRedirect(wxOpenidConfig.getIp() + "/#/pages/404/404/?message=" + URLEncoder.encode("用户更新异常", "UTF-8")); throw new Exception("微校授权失败!"); } studentId = student.getId(); } wxLoginVo wlv = new wxLoginVo(); List orgs = welcomeOrgService.queryDatas(college, profession, classStr); if (orgs != null && orgs.size() > 0) { for (WelcomeOrg org : orgs) { if (org.getName().equals(college)) { wlv.setCollegeId(org.getId()); wlv.setCollege(college); } if (org.getName().equals(profession)) { wlv.setMajorId(org.getId()); wlv.setMajor(profession); } if (org.getName().equals(classStr)) { wlv.setClassstrId(org.getId()); wlv.setClassstr(classStr); } } } wlv.setId(studentId); wlv.setAdmissNum(card_number); wlv.setName(user_name); wlv.setPicture(student.getPicture()); wlv.setCardId(id_card); if(StringUtils.hasText(id_card)){ if(StringUtils.hasText(student.getSex())){ wlv.setSex(student.getSex()); }else{ String sex = StrUtils.getGender(id_card); wlv.setSex(sex); } //家庭成员 List fvs = new ArrayList<>(); List familys = welcomeFamilyService.getManageByCardId(student.getCardId()); if (familys != null && familys.size() > 0) { for (WelcomeFamily wf : familys) { FamilyVo fv = new FamilyVo(); fv.setId(wf.getId()); fv.setFamilyShip(wf.getFamilyShip()); fv.setName(wf.getName()); fv.setPhone(wf.getPhone()); fv.setWorkUnit(wf.getWorkUnit()); fvs.add(fv); } } wlv.setFvs(fvs); //陪同人员 List avs = new ArrayList<>(); List accompanys = welcomeAccompanyService.getManageByCardId(student.getCardId()); if (accompanys != null && accompanys.size() > 0) { for (WelcomeAccompany ac : accompanys) { AccompanyVo av = new AccompanyVo(); av.setId(ac.getId()); av.setName(ac.getName()); av.setPhone(ac.getPhone()); avs.add(av); } } wlv.setAvs(avs); //时间段 List atvs = new ArrayList<>(); List wass = welcomeArriveSettingService.queryCheckDatas(); if (wass != null && wass.size() > 0) { for (WelcomeArriveSetting was : wass) { ArriveTimeVo atv = new ArriveTimeVo(); atv.setId(was.getId()); atv.setStartTime(was.getStartTime()); atv.setEndTime(was.getEndTime()); atv.setTimeStr(was.getStartTime() + "-" + was.getEndTime()); atv.setIsCheck((student.getArriveTimeId() != null && was.getId().equals(student.getArriveTimeId())) ? 1 : 0); atvs.add(atv); } } wlv.setAtvs(atvs); } wlv.setSchool(campus); wlv.setSchoolId(campus == null ? 0 : eSchool.stringOf(campus)); //籍贯 wlv.setOprovinceId(student.getOprovinceId()); wlv.setOprovince(student.getOprovince()); wlv.setOcityId(student.getOcityId()); wlv.setOcity(student.getOcity()); wlv.setOdistrictId(student.getOdistrictId()); wlv.setOdistrict(student.getOdistrict()); wlv.setProvinceId(student.getProvinceId()); wlv.setProvince(student.getProvince()); wlv.setCityId(student.getCityId()); wlv.setCity(student.getCity()); wlv.setDistrictId(student.getDistrictId()); wlv.setDistrict(student.getDistrict()); wlv.setIsPay(student.getIsPay()); wlv.setPhone(student.getPhone().replace("(+86)", "")); wlv.setCollege(college == null ? "微校获取不到院校" : college); long expired = 1000 * 60 * 60 * 24 * 365; TokenDateVo token = JWTUtil.getToken(id_card, student.getId(), expired); wlv.setToken(token.getToken()); System.out.println("微校授权成功:" + user_name + "" + card_number); response.sendRedirect(wxOpenidConfig.getIp() + "/#/pages/index/index/?urlstr=" + urlstr + "&token=" + token.getToken() + "&type=" + idenType); return CommonResult.ok(wlv); } @Override @PassToken public CommonResult mobileLogin(mobileLoginRequest mlr, BindingResult bindingResult) { if (mlr == null) { return CommonResult.fail("请传递参数"); } if (bindingResult.hasErrors()) { String st = paramUtils.getParamError(bindingResult); return CommonResult.fail(st); } WelcomeStudent student = welcomeStudentService.getDataByIdcardOrNum(mlr.getAdmissNum(), mlr.getCardId()); if (student == null) { return CommonResult.fail("录取号或身份证错误,登录失败!"); } if(StringUtils.hasText(student.getCardId())){ if(StringUtils.hasText(student.getSex())){ student.setSex(student.getSex()); }else{ String sex =StrUtils.getGender(student.getCardId()); student.setSex(sex); } } //家庭成员 List fvs = new ArrayList<>(); List familys = welcomeFamilyService.getManageByCardId(student.getCardId()); if (familys != null && familys.size() > 0) { for (WelcomeFamily wf : familys) { FamilyVo fv = new FamilyVo(); fv.setId(wf.getId()); fv.setFamilyShip(wf.getFamilyShip()); fv.setName(wf.getName()); fv.setPhone(wf.getPhone()); fv.setWorkUnit(wf.getWorkUnit()); fvs.add(fv); } } student.setFvs(fvs); //陪同人员 List avs = new ArrayList<>(); List accompanys = welcomeAccompanyService.getManageByCardId(student.getCardId()); if (accompanys != null && accompanys.size() > 0) { for (WelcomeAccompany ac : accompanys) { AccompanyVo av = new AccompanyVo(); av.setId(ac.getId()); av.setName(ac.getName()); av.setPhone(ac.getPhone()); avs.add(av); } } student.setAvs(avs); //时间段 List atvs = new ArrayList<>(); List wass = welcomeArriveSettingService.queryCheckDatas(); if (wass != null && wass.size() > 0) { for (WelcomeArriveSetting was : wass) { ArriveTimeVo atv = new ArriveTimeVo(); atv.setId(was.getId()); atv.setStartTime(was.getStartTime()); atv.setEndTime(was.getEndTime()); atv.setTimeStr(was.getStartTime() + "-" + was.getEndTime()); atv.setIsCheck((student.getArriveTimeId() != null && was.getId().equals(student.getArriveTimeId())) ? 1 : 0); atvs.add(atv); } } student.setAtvs(atvs); //region 查询缴费 if(!(student.getIsPay() != null && student.getIsPay().intValue() == 1)){//未缴费的情况下去查 if(StringUtils.hasText(student.getAdmissNum()) && StringUtils.hasText(student.getSchool())){ List paySettings = welcomePaySettingService.queryPaySettings(student.getSchool()); if(paySettings != null && paySettings.size() > 0){ String year = TimeExchange.getYear(); String payResult = HtPayUtils.getDataTwo(student.getAdmissNum(),year); if(StringUtils.hasText(payResult)){ //缴费判定 }else{ student.setIsPay(0); } }else{ student.setIsPay(0); } } } //endregion if(!(student.getIsPay() != null && student.getIsPay().intValue() == 1)){ List payInfos = WelcomePayController.queryStudentPayInfo(student.getAdmissNum(),TimeExchange.getYear()); if(payInfos != null && payInfos.size() > 0){ List paySettings = welcomePaySettingService.queryPaySettings(student.getSchool()); if(paySettings != null && paySettings.size() > 0){ for (WelcomePaySetting pay:paySettings) { BigDecimal money = pay.getPayAmount(); if(pay.getMethod().equals("全部")){ BigDecimal totalSj = new BigDecimal(BigInteger.ZERO); for (JsonPayVo jpv:payInfos){ totalSj = totalSj.add(jpv.getSJJE()); } if(totalSj.compareTo(money) >= 0){ student.setIsPay(1); } } Optional ojpv = payInfos.stream().filter(e -> e.getSFXMMC().equals(pay.getMethod())).findFirst(); if(ojpv != null && ojpv.isPresent()){ if(ojpv.get().getSJJE().compareTo(money) >= 0){ student.setIsPay(1); } } } }else{ student.setIsPay(0); } }else{ student.setIsPay(0); } } long expired = 1000 * 60 * 60 * 24 * 365; TokenDateVo token = JWTUtil.getToken(student.getCardId(), student.getId(), expired); student.setToken(token.getToken()); return CommonResult.ok(student); } @Override @PassToken public CommonResult writeCarInfo() { com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject(); String ukey = "6VMZEC5C6HZM7EO8"; com.alibaba.fastjson.JSONObject datas = new com.alibaba.fastjson.JSONObject(); datas.put("car_number", "赣A0AY39"); datas.put("begin_time", "2025-06-16 10:00:00"); datas.put("end_time", "2025-06-16 18:00:00"); datas.put("mobile", "18279193722"); // 生成带签名的字符串并使用MD5生成签名,然后转大写 String sign = datas.toJSONString() + "key=" + ukey; sign = CreateSign1.MD5(sign).toUpperCase(); json.put("service_name", "visitor_sync"); json.put("sign", sign); json.put("park_id", "10033845"); json.put("data", datas); String msg = HttpsClient.sendJson("http://istparking.sciseetech.com/public/visitor/do", json); return CommonResult.ok(msg); } @Override public CommonResult payResult(payResultRequest mlr) { logger.info("支付回调信息,mlr参数:" + JSON.toJSON(mlr)); return CommonResult.ok(); } }