Pu Zhibing
4 天以前 0cc73a9ffcdd356e0b797e458b400acfb660da94
UserQYTTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/UserInfoServiceImpl.java
@@ -16,6 +16,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.qianyuntong.SMSUtil;
import com.stylefeng.guns.modular.system.warpper.LoginWarpper;
import com.stylefeng.guns.modular.taxi.model.PaymentRecord;
import com.stylefeng.guns.modular.taxi.service.IPaymentRecordService;
@@ -28,8 +29,8 @@
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.crypto.Cipher;
@@ -44,6 +45,7 @@
import java.security.spec.AlgorithmParameterSpec;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
@@ -58,7 +60,7 @@
    private UserActivityInviteMapper userActivityInviteMapper;
    @Autowired
    private RedisUtil redisUtil;
    private RedisTemplate redisTemplate;
    @Autowired
    private ALiSendSms aLiSendSms;
@@ -154,16 +156,17 @@
                templateCode = "SMS_154775431";//修改密码
                break;
        }
        String sData = aLiSendSms.sendSms(phone, "SMS_229715276", "{\"code\":\"" + authCode + "\"}");
        JSONObject jsonObject = JSON.parseObject(sData);
        String message = jsonObject.getString("Message");
        if(!"OK".equals(message)){
            System.err.println(message);
            return ResultUtil.error(message);
        }
//        String sData = aLiSendSms.sendSms(phone, "SMS_229715276", "{\"code\":\"" + authCode + "\"}");
//        JSONObject jsonObject = JSON.parseObject(sData);
//        String message = jsonObject.getString("Message");
//        if(!"OK".equals(message)){
//            System.err.println(message);
//            return ResultUtil.error(message);
//        }
        SMSUtil.sendVerifyCode(phone);
        String sms = "短信验证码【" + authCode + "】已发到您的手机,验证码将在5分钟后失效,请及时登录!";
        //发送验证码短信
        redisUtil.setStrValue(phone, authCode, 5 * 60);//设置五分钟过期
        redisTemplate.opsForValue().set(phone, authCode, 5, TimeUnit.MINUTES);//设置五分钟过期
        System.out.println("【" + phone + "】" + sms);
        return ResultUtil.success();
    }
@@ -178,7 +181,7 @@
     */
    @Override
    public boolean checkCaptcha(String phone, String code) throws Exception {
        Object value = redisUtil.getValue(phone);
        Object value = redisTemplate.opsForValue().get(phone);
        if("1234".equals(code)){
            return true;
        }
@@ -513,7 +516,7 @@
        }
        if(type == 2){//小程序登录
            String value = redisUtil.getValue("appletOpenId");
            String value = (String) redisTemplate.opsForValue().get("appletOpenId");
            JSONObject jsonObject = null;
            if(ToolUtil.isNotEmpty(value)){
                jsonObject = JSON.parseObject(value);
@@ -521,7 +524,7 @@
                jsonObject = new JSONObject();
            }
            jsonObject.put(userInfo.getId().toString(), userInfo.getAppletsOpenId());
            redisUtil.setStrValue("appletOpenId", jsonObject.toJSONString());
            redisTemplate.opsForValue().set("appletOpenId", jsonObject.toJSONString());
        }
        //调用单点登录的逻辑
@@ -650,7 +653,7 @@
        }
        if(type == 2){//小程序登录
            String value = redisUtil.getValue("appletOpenId");
            String value = (String) redisTemplate.opsForValue().get("appletOpenId");
            JSONObject jsonObject = null;
            if(ToolUtil.isNotEmpty(value)){
                jsonObject = JSON.parseObject(value);
@@ -658,7 +661,7 @@
                jsonObject = new JSONObject();
            }
            jsonObject.put(userInfo.getId().toString(), userInfo.getAppletsOpenId());
            redisUtil.setStrValue("appletOpenId", jsonObject.toJSONString());
            redisTemplate.opsForValue().set("appletOpenId", jsonObject.toJSONString());
        }
        //调用单点登录的逻辑
@@ -796,7 +799,7 @@
            this.updateById(userInfo1);
            if(ToolUtil.isNotEmpty(loginType) && loginType.equals("Applets")){//小程序登录后绑定手机号码
                String value = redisUtil.getValue("appletOpenId");
                String value = (String) redisTemplate.opsForValue().get("appletOpenId");
                JSONObject jsonObject = null;
                if(ToolUtil.isNotEmpty(value)){
                    jsonObject = JSON.parseObject(value);
@@ -805,7 +808,7 @@
                    jsonObject = new JSONObject();
                }
                jsonObject.put(userInfo1.getId().toString(), userInfo.getAppletsOpenId());
                redisUtil.setStrValue("appletOpenId", jsonObject.toJSONString());
                redisTemplate.opsForValue().set("appletOpenId", jsonObject.toJSONString());
            }
@@ -871,7 +874,7 @@
            }else{
                key = requestHeader;
            }
            String value = redisUtil.getValue(key);
            String value = (String) redisTemplate.opsForValue().get(key);
            return null != value ? Integer.valueOf(value) : null;
        }else{
            return null;
@@ -931,7 +934,7 @@
            return ResultUtil.error("支付金额必须大于0元");
        }
        if(payType == 1){//微信支付
            String value = redisUtil.getValue("appletOpenId");
            String value = (String) redisTemplate.opsForValue().get("appletOpenId");
            String appletsOpenId = null;
            if(ToolUtil.isNotEmpty(value)){
                JSONObject jsonObject = JSON.parseObject(value);
@@ -1150,7 +1153,7 @@
        String openid = map.get("openid");
        String unionid = map.get("unionid");
        //将当前小程序openid存储到缓存中用后面的支付时使用
        String value = redisUtil.getValue("appletOpenId");
        String value = (String) redisTemplate.opsForValue().get("appletOpenId");
        JSONObject jsonObject = null;
        if(ToolUtil.isNotEmpty(value)){
            jsonObject = JSON.parseObject(value);
@@ -1158,7 +1161,7 @@
            jsonObject = new JSONObject();
        }
        jsonObject.put(userId.toString(), openid);
        redisUtil.setStrValue("appletOpenId", jsonObject.toJSONString());
        redisTemplate.opsForValue().set("appletOpenId", jsonObject.toJSONString());
        return ResultUtil.success();
    }
@@ -1348,33 +1351,22 @@
        return "JYX" + String.valueOf(num).substring(1);
    }
    /**
     * 获取token
     * @param userInfo
     * @param password
     * @return
     * 单点登录
     * @param id
     */
    private String getToken(UserInfo userInfo, String password,String type) throws Exception{
    private void singlePointLogin(Integer id,String type) throws Exception{
        if(ToolUtil.isEmpty(type))type="APP";
        //封装请求账号密码为shiro可验证的token
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(userInfo.getPhone(), password.toCharArray());
        String credentials = userInfo.getPassWord();
        ByteSource credentialsSalt = new Md5Hash(salt);
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
                new ShiroUser(), credentials, credentialsSalt, "");
        //校验用户账号密码
        HashedCredentialsMatcher md5CredentialsMatcher = new HashedCredentialsMatcher();
        md5CredentialsMatcher.setHashAlgorithmName(ShiroKit.hashAlgorithmName);
        md5CredentialsMatcher.setHashIterations(ShiroKit.hashIterations);
        boolean passwordTrueFlag = md5CredentialsMatcher.doCredentialsMatch(
                usernamePasswordToken, simpleAuthenticationInfo);
        String s = JwtTokenUtil.generateToken(String.valueOf(userInfo.getId()));
        redisUtil.setStrValue(s.substring(s.length() - 32), String.valueOf(userInfo.getId()), 180 * 24 * 60 * 60);
        redisUtil.setStrValue("USER_"+type+"_" + userInfo.getPhone(), s.substring(s.length() - 32));
        redisUtil.setStrValue("USER_"+type +"_"+ userInfo.getId(), s);
        return s;
        //开始验证当前账号是否在别处登录
        String value = (String) redisTemplate.opsForValue().get("USER_" + type + "_" + id);
        if(ToolUtil.isNotEmpty(value)) {//将另外设备上的强迫下线
            //开始清除redis中无效的数据
            UserInfo userInfo = userInfoMapper.selectById(id);
            String key = (String) redisTemplate.opsForValue().get("USER_" + type + "_" + userInfo.getPhone());
            redisTemplate.delete(key);//删除个人信息数据
            redisTemplate.delete("USER_" + type + "_" + userInfo.getPhone());//删除后台冻结相关缓存
            redisTemplate.delete("USER_" + type + "_" + id);//清除存储的token
        }
    }
@@ -1438,23 +1430,32 @@
        }
    }
    /**
     * 单点登录
     * @param id
     * 获取token
     * @param userInfo
     * @param password
     * @return
     */
    private void singlePointLogin(Integer id,String type) throws Exception{
    private String getToken(UserInfo userInfo, String password,String type) throws Exception{
        if(ToolUtil.isEmpty(type))type="APP";
        //开始验证当前账号是否在别处登录
        String value = redisUtil.getValue("USER_"+type+"_" + id);
        if(ToolUtil.isNotEmpty(value)){//将另外设备上的强迫下线
            //开始清除redis中无效的数据
            UserInfo userInfo = userInfoMapper.selectById(id);
            String key = redisUtil.getValue("USER_"+type+"_" + userInfo.getPhone());
            redisUtil.remove(key);//删除个人信息数据
            redisUtil.remove("USER_"+type+"_" + userInfo.getPhone());//删除后台冻结相关缓存
            redisUtil.remove("USER_"+type+"_" + id);//清除存储的token
        }
        //封装请求账号密码为shiro可验证的token
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(userInfo.getPhone(), password.toCharArray());
        String credentials = userInfo.getPassWord();
        ByteSource credentialsSalt = new Md5Hash(salt);
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
                new ShiroUser(), credentials, credentialsSalt, "");
        //校验用户账号密码
        HashedCredentialsMatcher md5CredentialsMatcher = new HashedCredentialsMatcher();
        md5CredentialsMatcher.setHashAlgorithmName(ShiroKit.hashAlgorithmName);
        md5CredentialsMatcher.setHashIterations(ShiroKit.hashIterations);
        boolean passwordTrueFlag = md5CredentialsMatcher.doCredentialsMatch(
                usernamePasswordToken, simpleAuthenticationInfo);
        String s = JwtTokenUtil.generateToken(String.valueOf(userInfo.getId()));
        redisTemplate.opsForValue().set(s.substring(s.length() - 32), String.valueOf(userInfo.getId()), 180, TimeUnit.DAYS);
        redisTemplate.opsForValue().set("USER_" + type + "_" + userInfo.getPhone(), s.substring(s.length() - 32));
        redisTemplate.opsForValue().set("USER_" + type + "_" + userInfo.getId(), s);
        return s;
    }
    @Override