liurunyu
2 天以前 1cf88d43994ec7ec403319032a9d118b39fe3571
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.dy.sso.busi;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dy.pipIrrGlobal.daoBa.BaCaptchaMapper;
import com.dy.pipIrrGlobal.daoBa.BaUserMapper;
import com.dy.pipIrrGlobal.pojoBa.BaUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
import java.util.Map;
 
@Slf4j
@Service
public class SsoSv {
    private BaUserMapper baUserMapper;
 
    @Autowired
    private void setBaUserMapper(BaUserMapper baUserMapper){
        this.baUserMapper = baUserMapper ;
    }
 
    @Autowired
    private BaCaptchaMapper baCaptchaMapper;
 
    /**
     * 需要BaUserMapper.xml
     * @param uuid 给登录成功的用户赋值其token
     * @param phone 用户手机号
     * @param password 用户密码
     * @return 登录成功用户
     */
    //当未注解@Transactional时,会输出日志:SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@46727a0c] was not registered for synchronization because synchronization is not active
    @Transactional
    @Cacheable(cacheNames=CacheConstants.cacheNames, key="'" + CacheConstants.loginUserKeyPrefix + "' + #uuid", sync=true)
    public BaUser loginWithMapperXml(String uuid, String phone, String password){
        return this.baUserMapper.login(phone, password) ;
    }
 
    /**
     * 不需要 BaUserMapper.xml
     * @param uuid 给登录成功的用户赋值其token
     * @param phone 用户手机号
     * @param password 用户密码
     * @return 登录成功用户
     */
    //当未注解@Transactional时,会输出日志:SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@46727a0c] was not registered for synchronization because synchronization is not active
    @Transactional
    @Cacheable(cacheNames=CacheConstants.cacheNames, key="'" + CacheConstants.loginUserKeyPrefix + "' + #uuid", sync=true)
    public BaUser loginWithOutMapperXml(String uuid, String phone, String password){
        QueryWrapper<BaUser> qw = new QueryWrapper<>();
        //QueryWrapper<DemoMp> queryWrapper = Wrappers.<DemoMp>query() ;
        qw.select("id", "name", "phone", "orgTag", "supperAdmin")
                .eq("disabled", 0)
                .eq("deleted", 0)
                .eq("phone", phone)
                .eq("password", password);
        return this.baUserMapper.selectOne(qw) ;
    }
 
    @CacheEvict(cacheNames=CacheConstants.cacheNames, key="'" + CacheConstants.loginUserKeyPrefix + "' + #uuid")
    public void logout(String uuid){
    }
 
    @Cacheable(cacheNames=CacheConstants.cacheNames, key="'" + CacheConstants.loginUserKeyPrefix + "' + #uuid")
    public BaUser getByUuid(String uuid){
        //此方法目的是直接从缓存中读取,如果缓存无此值,说明数据被清除了,返回null值,需要重新登录
        return null ;
    }
 
    /**
     * 判断手机号是否存在
     * @return
     */
    public boolean existPhone(String phone){
        Long count = baUserMapper.countPhone(phone) ;
        if(count == null || count == 0){
            return false ;
        }
        return true ;
    }
 
    /**
     * 得到所有用户手机号
     * @return
     */
    public List<String> getPhones(){
        return baUserMapper.getPhones() ;
    }
 
    /**
     * 依据验证码token获取验证字符
     * @param token
     * @return
     */
    public Map getCodeByToken(String token) {
        return baCaptchaMapper.getCodeByToken(token);
    }
}