From 75a2623173bcc4a235aa1f99f7ef28519186160b Mon Sep 17 00:00:00 2001 From: yanghb <yanghb> Date: 星期五, 21 四月 2023 11:24:25 +0800 Subject: [PATCH] 代码调整 --- UserTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/UserInfoServiceImpl.java | 178 +++++++++++++++++++++++++++++++++++++++------------------- 1 files changed, 119 insertions(+), 59 deletions(-) diff --git a/UserTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/UserInfoServiceImpl.java b/UserTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/UserInfoServiceImpl.java index ebb62b3..55f2f97 100644 --- a/UserTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/UserInfoServiceImpl.java +++ b/UserTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/UserInfoServiceImpl.java @@ -14,6 +14,7 @@ import com.stylefeng.guns.modular.system.model.*; import com.stylefeng.guns.modular.system.service.*; import com.stylefeng.guns.modular.system.util.*; +import com.stylefeng.guns.modular.system.util.httpClinet.HttpClientUtil; import com.stylefeng.guns.modular.system.warpper.LoginWarpper; import com.stylefeng.guns.modular.taxi.model.PaymentRecord; import com.stylefeng.guns.modular.taxi.service.IPaymentRecordService; @@ -31,6 +32,7 @@ import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.math.BigDecimal; +import java.text.SimpleDateFormat; import java.util.*; @@ -114,6 +116,10 @@ private String salt = "&a.s"; + private Map<String, Long> loginTime = new HashMap<>();//登录时间 + + private Map<String, Integer> loginFailures = new HashMap<>();//登录失败次数 + /** * 获取短信验证码 @@ -147,7 +153,7 @@ templateCode = "SMS_154775431";//修改密码 break; } - String sData = aLiSendSms.sendSms(phone, "SMS_229715276", "{\"code\":\"" + authCode + "\"}"); + String sData = aLiSendSms.sendSms(phone, templateCode, "{\"code\":\"" + authCode + "\"}"); JSONObject jsonObject = JSON.parseObject(sData); String message = jsonObject.getString("Message"); if(!"OK".equals(message)){ @@ -188,15 +194,33 @@ */ @Override public ResultUtil<LoginWarpper> captchaLogin(String phone, String code, String registIp, String registAreaCode,String loginType) throws Exception { + phone = AESUtil.decrypt(phone);//对电话号码进行解密 + + Integer frequency = loginFailures.get(phone); + Long time = loginTime.get(phone); + if(null != time && System.currentTimeMillis() < (time + 1800000) && null != frequency && frequency >= 5){ + return ResultUtil.error("暂时无法登录,请30分钟后重试!"); + } + if(null != time && System.currentTimeMillis() >= (time + 1800000)){ + frequency = 0; + loginFailures.put(phone, frequency); + } boolean b = this.checkCaptcha(phone, code); if(!b){ + frequency = (null == frequency ? 0 : frequency) + 1; + loginFailures.put(phone, frequency); + if(frequency == 1){ + loginTime.put(phone, System.currentTimeMillis()); + } return ResultUtil.error("验证码无效"); } - UserInfo userInfo = userInfoMapper.queryByPhone(phone); + String encrypt = AESUtil.encrypt(phone);//对电话号码进行加密 + + UserInfo userInfo = userInfoMapper.queryByPhone(encrypt); if(null == userInfo){ userInfo = new UserInfo(); - userInfo.setPhone(phone); + userInfo.setPhone(encrypt); userInfo.setPassWord(ShiroKit.md5("", salt)); userInfo.setNickName(this.getDefaultName()); userInfo.setRegistIp(registIp); @@ -241,7 +265,6 @@ loginWarpper.setAppid(UUIDUtil.getRandomCode()); loginWarpper.setEmergencyContact(userInfo.getEmergencyContact()); loginWarpper.setEmergencyContactNumber(userInfo.getEmergencyContactNumber()); - loginWarpper.setPhone(2); smsrecordService.saveData(1, phone, code, "短信验证码【" + code + "】已发到您的手机,验证码将在5分钟后失效,请及时登录!"); return ResultUtil.success(loginWarpper); @@ -260,14 +283,17 @@ } System.out.println( response); JSONObject json = JSONObject.parseObject(response); - if(!json.getBoolean("success")){ + if(null == json || !json.getBoolean("success")){ return ResultUtil.error("登录失败"); } String phone = json.getJSONObject("data").getString("mobile"); - UserInfo userInfo = userInfoMapper.queryByPhone(phone); + + String encrypt = AESUtil.encrypt(phone);//对电话号码进行加密 + + UserInfo userInfo = userInfoMapper.queryByPhone(encrypt); if(null == userInfo){ userInfo = new UserInfo(); - userInfo.setPhone(phone); + userInfo.setPhone(encrypt); userInfo.setPassWord(ShiroKit.md5("", salt)); userInfo.setNickName(this.getDefaultName()); userInfo.setRegistIp(registIp); @@ -331,9 +357,9 @@ incomeService.saveData(2, driver.getId(), 1, Integer.valueOf(String.valueOf(map.get("id"))), null, Double.valueOf(String.valueOf(map.get("money")))); } } - driver.setActivityMoney(bigDecimal.add(new BigDecimal(null == driver.getActivityMoney() ? 0 : driver.getActivityMoney())).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue()); - driver.setLaveActivityMoney(bigDecimal.add(new BigDecimal(null == driver.getLaveActivityMoney() ? 0 : driver.getLaveActivityMoney())).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue()); - driver.setBalance(bigDecimal.add(new BigDecimal(null == driver.getBalance() ? 0 : driver.getBalance())).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue()); + driver.setActivityMoney(bigDecimal.add(new BigDecimal(driver.getActivityMoney())).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue()); + driver.setLaveActivityMoney(bigDecimal.add(new BigDecimal(driver.getLaveActivityMoney())).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue()); + driver.setBalance(bigDecimal.add(new BigDecimal(driver.getBalance())).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue()); driverMapper.updateById(driver); Map<String, Date> date = dateUtil.getStartAndEndDate(new Date()); @@ -407,7 +433,19 @@ */ @Override public ResultUtil<LoginWarpper> userLogin(String phone, String password,String loginType) throws Exception { - UserInfo userInfo = this.queryByPhone(phone); + Integer frequency = loginFailures.get(phone); + Long time = loginTime.get(phone); + if(null != time && System.currentTimeMillis() < (time + 1800000) && null != frequency && frequency >= 5){ + return ResultUtil.error("暂时无法登录,请30分钟后重试!"); + } + if(null != time && System.currentTimeMillis() >= (time + 1800000)){ + frequency = 0; + loginFailures.put(phone, frequency); + } + + String encrypt = AESUtil.encrypt(phone);//对电话号码进行加密 + + UserInfo userInfo = this.queryByPhone(encrypt); if(null == userInfo){ return ResultUtil.error("账号无效"); } @@ -415,6 +453,11 @@ return ResultUtil.error("账号被冻结"); } if(!ShiroKit.md5(password, salt).equals(userInfo.getPassWord())){ + frequency = (null == frequency ? 0 : frequency) + 1; + loginFailures.put(phone, frequency); + if(frequency == 1){ + loginTime.put(phone, System.currentTimeMillis()); + } return ResultUtil.error("密码错误"); } @@ -445,6 +488,7 @@ @Override public ResultUtil<LoginWarpper> wxLogin(Integer type, String openid, String unionid, String jscode, String registIp, String registAreaCode, Integer sex, String nickName, String avatar,String loginType) throws Exception { + UserInfo userInfo = null; if(type == 2){//小程序 if(ToolUtil.isEmpty(jscode)){ @@ -500,7 +544,15 @@ if(userInfo.getState() == 2){ return ResultUtil.error("账号被冻结"); } - + Integer frequency = loginFailures.get(userInfo.getPhone()); + Long time = loginTime.get(userInfo.getPhone()); + if(null != time && System.currentTimeMillis() < (time + 1800000) && null != frequency && frequency >= 5){ + return ResultUtil.error("暂时无法登录,请30分钟后重试!"); + } + if(null != time && System.currentTimeMillis() >= (time + 1800000)){ + frequency = 0; + loginFailures.put(userInfo.getPhone(), frequency); + } if(type == 2){//小程序登录 String value = redisUtil.getValue("appletOpenId"); JSONObject jsonObject = null; @@ -538,11 +590,16 @@ */ @Override public ResultUtil forgetPassword(String phone, String code, String password) throws Exception { + phone = AESUtil.decrypt(phone);//对电话号码进行解密 + boolean b = this.checkCaptcha(phone, code); if(!b){ return ResultUtil.error("验证码无效"); } - UserInfo userInfo = this.queryByPhone(phone); + + String encrypt = AESUtil.encrypt(phone);//对电话号码进行加密 + + UserInfo userInfo = this.queryByPhone(encrypt); if(null == userInfo){ return ResultUtil.error("手机号码未注册"); } @@ -569,12 +626,20 @@ */ @Override public ResultUtil bindingPhone(Integer uid, String phone, String code,String loginType) throws Exception { + if(ToolUtil.isEmpty(phone)){ + return ResultUtil.error("手机号不能为空"); + } + phone = AESUtil.decrypt(phone);//对电话号码进行解密 + boolean b = this.checkCaptcha(phone, code); if(!b){ return ResultUtil.error("验证码无效"); } UserInfo userInfo = this.selectById(uid); - UserInfo userInfo1 = userInfoMapper.queryByPhone(phone); + + String encrypt = AESUtil.encrypt(phone);//对电话号码进行加密 + + UserInfo userInfo1 = userInfoMapper.queryByPhone(encrypt); if(null != userInfo1){ userInfo1.setOpenId(userInfo.getOpenId()); userInfo1.setUnionid(userInfo.getUnionid()); @@ -584,7 +649,7 @@ userInfo1.setNickName(userInfo.getNickName()); this.updateById(userInfo1); - if(ToolUtil.isNotEmpty(loginType) && loginType.equals("Applets")){//小程序登录后绑定手机号码 + if(loginType.equals("Applets")){//小程序登录后绑定手机号码 String value = redisUtil.getValue("appletOpenId"); JSONObject jsonObject = null; if(ToolUtil.isNotEmpty(value)){ @@ -632,7 +697,7 @@ return ResultUtil.success(loginWarpper); } - userInfo.setPhone(phone); + userInfo.setPhone(encrypt); userInfo.setUpdateTime(new Date()); userInfo.setUpdateUser(userInfo.getId()); this.updateById(userInfo); @@ -676,7 +741,8 @@ */ @Override public Map<String, Object> queryUserInfo(Integer uid) throws Exception { - return userInfoMapper.queryUserInfo(uid, null); + Map<String, Object> map = userInfoMapper.queryUserInfo(uid, null); + return map; } @@ -719,43 +785,24 @@ if(money.compareTo(0D) <= 0){ return ResultUtil.error("支付金额必须大于0元"); } - if(payType == 1){//微信支付 - String value = redisUtil.getValue("appletOpenId"); - String appletsOpenId = null; - if(ToolUtil.isNotEmpty(value)){ - JSONObject jsonObject = JSON.parseObject(value); - appletsOpenId = jsonObject.getString(uid.toString()); - }else{ - appletsOpenId = userInfo.getAppletsOpenId(); - } - Integer integer = paymentRecordService.saveData(2, uid, 1, null, null, 1, money, "", 1);//添加预支付数据 - String app = type == 1 ? "APP" : "JSAPI"; - return payMoneyUtil.weixinpay("余额充值", uid.toString(), integer.toString(), money.toString(), "/base/wxCancelUserBalance", app, userInfo.getAppletsOpenId()); -// Map<String, String> map = icbcPayUtil.placeAnOrder(integer.toString(), 9, 5, uid.toString(), "余额充值", money, callbackPath + "/base/wxCancelUserBalance", uid.toString(), type, appletsOpenId); -// if(map.get("code").equals("200")){ -// PaymentRecord paymentRecord = paymentRecordService.selectById(integer); -// paymentRecord.setCode(map.get("order_id")); -// paymentRecordService.updateById(paymentRecord); -// return ResultUtil.success(map.get("data")); -// }else{ -// return ResultUtil.error(map.get("msg"), ""); -// } + Integer integer = paymentRecordService.saveData(2, uid, 1, null, null, payType, money, "", 1);//添加预支付数据 + SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS"); + String out_trade_no = sdf.format(new Date()) + integer; + if(payType == 1){//微信支付 + String appletsOpenId = ""; + String tradeType = "APP"; + if(type == 3){ + appletsOpenId = userInfo.getAppletsOpenId(); + tradeType = "JSAPI"; + if(ToolUtil.isEmpty(userInfo.getAppletsOpenId())){ + return ResultUtil.error("支付异常,请重新授权登录小程序"); + } + } + return payMoneyUtil.weixinpay("余额充值", uid.toString(), out_trade_no, money.toString(), callbackPath + "/base/wxCancelUserBalance", tradeType, appletsOpenId); } if(payType == 2){//支付宝支付 - Integer integer = paymentRecordService.saveData(2, uid, 1, null, null, 2, money, "", 1);//添加预支付数据 - - return payMoneyUtil.alipay("余额充值", "余额充值", uid.toString(), integer.toString(), money.toString(), "/base/aliCancelUserBalance"); - -// Map<String, String> map = icbcPayUtil.placeAnOrder(integer.toString(), 10, 5, uid.toString(), "余额充值", money, callbackPath + "/base/aliCancelUserBalance", uid.toString(), type, null); -// if(map.get("code").equals("200")){ -// PaymentRecord paymentRecord = paymentRecordService.selectById(integer); -// paymentRecord.setCode(map.get("order_id")); -// paymentRecordService.updateById(paymentRecord); -// return ResultUtil.success(map.get("data")); -// }else{ -// return ResultUtil.error(map.get("msg"), ""); -// } + return payMoneyUtil.alipay("完成订单", uid.toString(), out_trade_no, money.toString(), callbackPath + "/base/aliCancelUserBalance"); } return ResultUtil.success(); } @@ -771,16 +818,20 @@ */ @Override public ResultUtil updatePhone(String code, String phone, Integer uid) throws Exception { + phone = AESUtil.decrypt(phone);//解密 boolean b = this.checkCaptcha(phone, code); if(!b){ return ResultUtil.error("验证码无效"); } UserInfo userInfo = this.selectById(uid); - UserInfo userInfo1 = userInfoMapper.queryByPhone(phone); + + String encrypt = AESUtil.encrypt(phone);//对手机号进行加密 + + UserInfo userInfo1 = userInfoMapper.queryByPhone(encrypt); if(null != userInfo1){ return ResultUtil.error("手机号已被注册"); } - userInfo.setPhone(phone); + userInfo.setPhone(encrypt); this.updateById(userInfo); // smsrecordService.saveData(2, phone, code, "短信验证码【" + code + "】已发到您的手机,验证码将在5分钟后失效,请及时登录!"); @@ -894,7 +945,7 @@ // map.put("clientSecret", "JZrYSBvtVlIRbnsnoJuaqheRWAEHcEJVgeAeIHAUojwUwZIAVzSpAWxgozhfPbcq"); map.put("grantType", "authorization_code"); map.put("authCode", authCode); - String get = httpClientUtil.pushHttpRequset("GET", path + "/api/oauth/accessToken", map, null, ""); + String get = httpClientUtil.pushHttpRequset("GET", path + "/api/oauth/accessToken", map, null, "").getData(); System.err.print("获取accessToken:" + get); JSONObject jsonObject = JSON.parseObject(get); Boolean success = jsonObject.getBoolean("success"); @@ -908,7 +959,7 @@ if(null == userInfo){ return ResultUtil.error("获取用户信息失败"); } - return this.captchaLogin(userInfo.get("phone"), "1234", null, registAreaCode, loginType); + return this.captchaLogin(AESUtil.encrypt(userInfo.get("phone")), "1234", null, registAreaCode, loginType); }else{ return ResultUtil.error(jsonObject.getString("msg")); } @@ -944,7 +995,12 @@ // String path = "https://open.test.brightcns.cn";//测试环境 Map<String, Object> map = new HashMap<>(); map.put("accessToken", accessToken); - String get = httpClientUtil.pushHttpRequset("GET", path + "/api/userinfo", map, null, ""); + String get = null; + try { + get = httpClientUtil.pushHttpRequset("GET", path + "/api/userinfo", map, null, "").getData(); + } catch (Exception e) { + e.printStackTrace(); + } System.err.print("获取用户信息:" + get); JSONObject jsonObject = JSON.parseObject(get); Boolean success = jsonObject.getBoolean("success"); @@ -1111,7 +1167,7 @@ */ public String getDefaultName(){ int num = this.selectCount(new EntityWrapper<UserInfo>().ne("flag", 3)) + 1000001; - return "JYX" + String.valueOf(num).substring(1); + return "OK" + String.valueOf(num).substring(1); } @@ -1124,8 +1180,12 @@ private String getToken(UserInfo userInfo, String password,String type) throws Exception{ if(ToolUtil.isEmpty(type))type="APP"; //封装请求账号密码为shiro可验证的token - UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(userInfo.getPhone(), password.toCharArray()); String credentials = userInfo.getPassWord(); + if(ToolUtil.isEmpty(password)){ + password = "123456"; + credentials = ShiroKit.md5(password, salt); + } + UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(userInfo.getPhone(), password.toCharArray()); ByteSource credentialsSalt = new Md5Hash(salt); SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo( new ShiroUser(), credentials, credentialsSalt, ""); @@ -1149,7 +1209,7 @@ * @param userInfo * @throws Exception */ - private synchronized void addCoupon(UserInfo userInfo) throws Exception{ + public synchronized void addCoupon(UserInfo userInfo) throws Exception{ //添加优惠券 List<Map<String, Object>> list = userActivityRegisteredService.query(userInfo.getCompanyId()); int num = 0; -- Gitblit v1.7.1