goupan
2024-04-25 bde332ae80fe754e0cfd1daa8a0095c62ce32a59
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
package cn.stylefeng.rest.modular.user.service;
 
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.utils.GeneralUtil;
import cn.stylefeng.roses.kernel.auth.api.pojo.auth.LoginRequest;
import cn.stylefeng.roses.kernel.auth.api.pojo.auth.LoginResponse;
import cn.stylefeng.roses.kernel.customer.api.exception.CustomerException;
import cn.stylefeng.roses.kernel.customer.api.exception.enums.CustomerExceptionEnum;
import cn.stylefeng.roses.kernel.customer.modular.entity.Customer;
import cn.stylefeng.roses.kernel.customer.modular.request.CustomerRequest;
import cn.stylefeng.roses.kernel.customer.modular.service.CustomerService;
import cn.stylefeng.roses.kernel.rule.enums.CustomerUserTypeEnum;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
 
/**
 * C端登录业务接口
 */
@Slf4j
@Service
public class CustomerLoginBizService {
 
    @Resource
    private CustomerService customerService;
 
    public LoginResponse phoneLogin(String phone, CustomerUserTypeEnum userType) {
        // 查询用户信息
        Wrapper wrapper = Wrappers.<Customer>lambdaQuery()
                .eq(Customer::getUserType, userType.getCode())
                .and(w -> w
                        .eq(Customer::getAccount, phone)
                        .or().eq(Customer::getTelephone, phone)
                );
        Customer customer = customerService.getOne(wrapper, false);
 
        // 账号不存在时,判断是否去注册
        assertAccountNoExistToReg(customer, userType, phone, null);
 
        if (customer == null) {
            // 可能已经注册的用户,再获取一次用户
            customer = customerService.getOne(wrapper, false);
        }
        // 用户登录信息
        LoginResponse loginResponse = customerService.wrapperLoginResponse(customer, new LoginRequest());
        return loginResponse;
    }
 
    public LoginResponse wxOpenIdLogin(String wxOpenId, String phone, String workerNo, CustomerUserTypeEnum userType) {
        // 查询用户信息
        Wrapper wrapper = Wrappers.<Customer>lambdaQuery()
                .eq(Customer::getUserType, userType.getCode())
                .eq(Customer::getWxOpenId, wxOpenId);
        Customer customer = customerService.getOne(wrapper, false);
 
        if (customer == null && StrUtil.isNotBlank(phone)) {
            // 判断手机号是否注册
            Wrapper phonewrapper = Wrappers.<Customer>lambdaQuery()
                    .eq(Customer::getUserType, userType.getCode())
                    .eq(Customer::getAccount, phone)
                    .last("LIMIT 1");
            customer = customerService.getOne(phonewrapper, false);
            if (customer != null) {
                // 更新微信openId
                customerService.update(
                        Wrappers.<Customer>lambdaUpdate()
                                .set(Customer::getWxOpenId, wxOpenId)
                                .eq(Customer::getCustomerId, customer.getCustomerId())
                );
            }
        }
 
        // 账号不存在时,判断是否去注册
        assertAccountNoExistToReg(customer, userType, phone, wxOpenId);
 
        if (customer == null) {
            // 可能已经注册的用户,再获取一次用户
            customer = customerService.getOne(wrapper, false);
        }
 
        if (StrUtil.isNotBlank(workerNo)) {
            // 绑定顾问岗位ID数据
            customerService.userBindClassConsultWorkerId(customer.getCustomerId(), workerNo);
        }
 
        // 用户登录信息
        LoginResponse loginResponse = customerService.wrapperLoginResponse(customer, new LoginRequest());
        return loginResponse;
    }
 
    /**
     * 判断(用户、工作人员)账号是否存在,(用户)不存在则去注册
     *
     * @param customer
     * @param userType
     * @param phone
     * @param wxOpenId
     */
    public void assertAccountNoExistToReg(Customer customer, CustomerUserTypeEnum userType, String phone, String wxOpenId) {
        if (customer == null && StrUtil.isNotBlank(phone)) {
            if (CustomerUserTypeEnum.USER.getCode().equals(userType.getCode())) {
                // 用户手机号登录,提示账号不存在时,直接注册
                CustomerRequest regRequest = new CustomerRequest();
                regRequest.setAccount(phone);
                regRequest.setEmail(GeneralUtil.generateEmail(phone));
                regRequest.setPassword("111111");
                regRequest.setNickName(GeneralUtil.generateNickName());
 
                // 扩展注册信息
                regRequest.setUserType(userType.getCode());
                regRequest.setTelephone(phone);
                regRequest.setWxOpenId(wxOpenId);
                regRequest.setShowId(GeneralUtil.generateShowId());
 
                // 用户注册
                customerService.reg(regRequest);
            } else {
                // 异常提示账号不存在
                throw new CustomerException(CustomerExceptionEnum.CANT_FIND_CUSTOMER, phone);
            }
        }
    }
}