puzhibing
2023-02-26 8883d654f52171657b62aaec85027f29ef0a4dd0
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.supersavedriving.user.modular.system.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.supersavedriving.user.core.shiro.ShiroKit;
import com.supersavedriving.user.core.shiro.ShiroUser;
import com.supersavedriving.user.core.util.JwtTokenUtil;
import com.supersavedriving.user.core.util.ToolUtil;
import com.supersavedriving.user.modular.system.dao.AppUserMapper;
import com.supersavedriving.user.modular.system.model.AppUser;
import com.supersavedriving.user.modular.system.service.IAppUserService;
import com.supersavedriving.user.modular.system.util.RedisUtil;
import com.supersavedriving.user.modular.system.util.ResultUtil;
import com.supersavedriving.user.modular.system.util.weChat.WXCore;
import com.supersavedriving.user.modular.system.util.weChat.WeChatUtil;
import com.supersavedriving.user.modular.system.util.weChat.model.Code2Session;
import com.supersavedriving.user.modular.system.warpper.SignInToRegister;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.Date;
 
 
/**
 * 用户
 */
@Service
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> implements IAppUserService {
 
    @Autowired
    private WeChatUtil weChatUtil;
 
    @Autowired
    private RedisUtil redisUtil;
 
    private final String salt = "s5d1";
 
 
 
    @Override
    public ResultUtil<String> appUserLogin(String jscode) throws Exception {
        Code2Session code2Session = weChatUtil.code2Session(jscode);
        if(code2Session.getErrcode() != 0){
            return ResultUtil.error(code2Session.getErrmsg());
        }
        String openid = code2Session.getOpenid();
        AppUser appUser = this.selectOne(new EntityWrapper<AppUser>().eq("openid", openid).ne("status", 3));
        if(null == appUser){
            return ResultUtil.error("无效的账号");
        }
        if(appUser.getStatus() == 2){
            return ResultUtil.error("账号被冻结");
        }
        String token = getToken(appUser);
        if(ToolUtil.isEmpty(token)){
            return ResultUtil.error("获取身份凭证失败");
        }
        return ResultUtil.success(token);
    }
 
 
    /**
     * 获取身份凭证
     * @return
     */
    public String getToken(AppUser appUser){
        //封装请求账号密码为shiro可验证的token
        String phone = appUser.getPhone();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(phone, phone.toCharArray());
 
        String credentials = ShiroKit.md5(phone, salt);
        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);
 
        if (passwordTrueFlag) {
            String token = JwtTokenUtil.generateToken(phone);
            String key = token;
            if(token.length() > 16){
                key = token.substring(token.length() - 16);
            }
            redisUtil.setStrValue(key, appUser.getId().toString(), 7 * 24 * 60 * 60);
            return token;
        }
        return null;
    }
 
 
    /**
     * 微信授权注册登录
     * @param signInToRegister
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil signInToRegister(SignInToRegister signInToRegister) throws Exception {
        if(ToolUtil.isEmpty(signInToRegister.getJscode())){
            return ResultUtil.paranErr("jscode");
        }
        if(ToolUtil.isEmpty(signInToRegister.getEncryptedDataPhone())){
            return ResultUtil.paranErr("encryptedDataPhone");
        }
        if(ToolUtil.isEmpty(signInToRegister.getIvPhone())){
            return ResultUtil.paranErr("ivPhone");
        }
        Code2Session code2Session = weChatUtil.code2Session(signInToRegister.getJscode());
        if(code2Session.getErrcode() != 0){
            return ResultUtil.error(code2Session.getErrmsg());
        }
        String openid = code2Session.getOpenid();
        String session_key = code2Session.getSession_key();
        String decrypt = WXCore.decrypt(signInToRegister.getEncryptedDataPhone(), session_key, signInToRegister.getIvPhone());
        if(ToolUtil.isEmpty(decrypt)){
            return ResultUtil.error("获取手机号失败");
        }
        JSONObject phone = JSON.parseObject(decrypt);
        String purePhoneNumber = phone.getString("purePhoneNumber");
        AppUser appUser = this.selectOne(new EntityWrapper<AppUser>().eq("phone", purePhoneNumber).ne("status", 3));
        if(null == appUser){
            appUser = new AppUser();
            appUser.setNickname("亲爱的用户");
            appUser.setPhone(purePhoneNumber);
            appUser.setOpenid(openid);
            appUser.setUnionid(code2Session.getUnionid());
            appUser.setAccountBalance(0D);
            appUser.setStatus(1);
            appUser.setCreateTime(new Date());
            appUser.setIsException(1);
            appUser.setInviterId(signInToRegister.getInviterId());
            appUser.setInviterType(signInToRegister.getInviterType());
            this.insert(appUser);
        }
        if(appUser.getStatus() == 2){
            return ResultUtil.error("账号被冻结");
        }
        String token = getToken(appUser);
        if(ToolUtil.isEmpty(token)){
            return ResultUtil.error("获取身份凭证失败");
        }
        return ResultUtil.success(token);
    }
}