luodangjia
2024-10-28 bcff6cd41a09c5b3f5db68db8b9dbb2f90fb78f3
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.ruoyi.account.service.impl;
 
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.alipay.api.response.AlipayUserUserinfoShareResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.account.api.model.TAppUser;
import com.ruoyi.account.mapper.TAppUserMapper;
import com.ruoyi.account.service.TAppUserService;
import com.ruoyi.account.service.TInviteUserService;
import com.ruoyi.account.util.RptUtils;
import com.ruoyi.account.wx.model.WeixinProperties;
import com.ruoyi.account.wx.pojo.AppletUserDecodeData;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.system.api.model.LoginUserApplet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
 
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author luodangjia
 * @since 2024-08-06
 */
@Service
public class TAppUserServiceImpl extends ServiceImpl<TAppUserMapper, TAppUser> implements TAppUserService {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private TInviteUserService inviteUserService;
    @Override
    public Map<String, Object> wxLogin(AppletUserDecodeData appletUserDecodeData,Long inviteUserId) {
        // 通过手机号查询用户,是否已存在手动导入用户,包含支付宝用户
        TAppUser appUser = this.getOne(Wrappers.lambdaQuery(TAppUser.class)
                .eq(TAppUser::getPhone, appletUserDecodeData.getPhoneNumber())
                .isNull(TAppUser::getWxOpenid)
                .last("LIMIT 1"));
        if(Objects.isNull(appUser)){
            // 先使用openId和当前手机号进行查询
            appUser = this.getOne(Wrappers.lambdaQuery(TAppUser.class)
                    .eq(TAppUser::getWxOpenid, appletUserDecodeData.getOpenId())
                    .eq(TAppUser::getPhone, appletUserDecodeData.getPhoneNumber())
                    .last("LIMIT 1"));
            if(Objects.isNull(appUser)){
                appUser = new TAppUser();
                appUser.setPhone(appletUserDecodeData.getPhoneNumber());
                appUser.setInviteUserId(inviteUserId);
            }
        }
        if(Objects.nonNull(appUser.getStatus())){
            switch (appUser.getStatus()){
                case 1:
                    break;
                case 2:
                    throw new ServiceException("账号被冻结,请联系管理员");
                case 3:
                    appUser = new TAppUser();
                    appUser.setPhone(appletUserDecodeData.getPhoneNumber());
                    break;
            }
        }
 
        appUser.setAvatar(StringUtils.hasLength(appUser.getAvatar())?appUser.getAvatar():"http://221.182.45.100:8090/2024-10-26/logo.png");
        appUser.setName(StringUtils.hasLength(appUser.getName())?appUser.getName():RptUtils.around(appletUserDecodeData.getPhoneNumber(),3,4));
        appUser.setCity(appletUserDecodeData.getCity());
        appUser.setProvince(appletUserDecodeData.getProvince());
        appUser.setWxOpenid(appletUserDecodeData.getOpenId());
        this.saveOrUpdate(appUser);
        if(Objects.nonNull(inviteUserId)){
            inviteUserService.saveInviteUser(appUser.getId(), inviteUserId);
        }
        return this.getUserInfo(appUser);
    }
 
    @Override
    public Map<String, Object> aliLogin(AlipaySystemOauthTokenResponse response,String phone,Long inviteUserId) {
        // 通过手机号查询用户,是否已存在手动导入用户,包含微信用户
        TAppUser appUser = this.getOne(Wrappers.lambdaQuery(TAppUser.class)
                .eq(TAppUser::getPhone, phone)
                .isNull(TAppUser::getAliOpenid)
                .last("LIMIT 1"));
        if(Objects.isNull(appUser)){
            // 先使用openId和当前手机号进行查询
            appUser = this.getOne(Wrappers.lambdaQuery(TAppUser.class)
                    .eq(TAppUser::getAliOpenid, response.getOpenId())
                    .eq(TAppUser::getPhone, phone)
                    .last("LIMIT 1"));
            if(Objects.isNull(appUser)){
                appUser = new TAppUser();
                appUser.setPhone(phone);
                appUser.setInviteUserId(inviteUserId);
            }
        }
        if(Objects.nonNull(appUser.getStatus())){
            switch (appUser.getStatus()){
                case 1:
                    break;
                case 2:
                    throw new ServiceException("账号被冻结,请联系管理员");
                case 3:
                    appUser = new TAppUser();
                    appUser.setPhone(phone);
                    break;
            }
        }
        appUser.setAliOpenid(response.getOpenId());
        appUser.setAvatar(StringUtils.hasLength(appUser.getAvatar())?appUser.getAvatar():"http://221.182.45.100:8090/2024-10-26/logo.png");
        appUser.setName(StringUtils.hasLength(appUser.getName())?appUser.getName():RptUtils.around(phone,3,4));
        this.saveOrUpdate(appUser);
        if(Objects.nonNull(inviteUserId)){
            inviteUserService.saveInviteUser(appUser.getId(), inviteUserId);
        }
        return this.getUserInfo(appUser);
    }
//    @Override
//    public Map<String, Object> aliLogin(AlipaySystemOauthTokenResponse response, AlipayUserInfoShareResponse userInfo) {
//        // 通过手机号查询用户,是否已存在手动导入用户,包含微信用户
//        TAppUser appUser = this.getOne(Wrappers.lambdaQuery(TAppUser.class)
//                .eq(TAppUser::getPhone, userInfo.getMobile())
//                .isNull(TAppUser::getAliOpenid)
//                .last("LIMIT 1"));
//        if(Objects.isNull(appUser)){
//            // 先使用openId和当前手机号进行查询
//            appUser = this.getOne(Wrappers.lambdaQuery(TAppUser.class)
//                    .eq(TAppUser::getAliOpenid, response.getOpenId())
//                    .eq(TAppUser::getPhone, userInfo.getMobile())
//                    .last("LIMIT 1"));
//            if(Objects.isNull(appUser)){
//                appUser = new TAppUser();
//                appUser.setPhone(userInfo.getMobile());
//            }
//        }
//        if(Objects.nonNull(appUser.getStatus())){
//            throwInfo(appUser.getStatus());
//        }
//        appUser.setAvatar(userInfo.getAvatar());
//        appUser.setCity(userInfo.getCity());
//        appUser.setName(userInfo.getNickName());
//        appUser.setProvince(userInfo.getProvince());
//        appUser.setAliOpenid(response.getOpenId());
//        this.saveOrUpdate(appUser);
//        return this.getUserInfo(appUser);
//    }
 
    @Override
    public Map<String, Object> getUserInfo(TAppUser appUser) {
        LoginUserApplet loginUserApplet = new LoginUserApplet();
        if(ObjectUtils.isNotNull(appUser)){
            loginUserApplet.setUserId(appUser.getId());
            loginUserApplet.setUserIdStr(appUser.getId().toString());
            loginUserApplet.setName(appUser.getName());
            loginUserApplet.setPhone(appUser.getPhone());
            loginUserApplet.setAvatar(appUser.getAvatar());
            loginUserApplet.setAddress(appUser.getProvince()+appUser.getCity());
        }
        Map<String, Object> tokenInfos = new HashMap<>();
        tokenInfos.put("token",tokenService.createTokenApplet(loginUserApplet));
        tokenInfos.put("info",loginUserApplet);
        return tokenInfos;
    }
 
    @Override
    public void throwInfo(Integer status){
        switch (status){
            case 2:
                throw new ServiceException("账号被冻结,请联系管理员");
            case 3:
//                throw new ServiceException("账号已注销,请重新注册使用");
            default:
                break;
        }
    }
}