doum
3 天以前 3c7399c25c0f35c8aa7cb6af1935e31d1a3f0102
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
package com.doumee.service.business.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.doumee.config.jwt.JwtTokenUtil;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.utils.Constants;
import com.doumee.dao.business.YwCustomerMapper;
import com.doumee.dao.business.model.YwCustomer;
import com.doumee.dao.system.dto.LoginPhoneDTO;
import com.doumee.service.business.YwCustomerH5AuthService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Collections;
import java.util.Date;
 
@Service
public class YwCustomerH5AuthServiceImpl implements YwCustomerH5AuthService {
 
    @Autowired
    private YwCustomerMapper ywCustomerMapper;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String loginByPhone(LoginPhoneDTO dto) {
        YwCustomer customer = findActiveByPhone(dto.getPhone());
        bindOpenId(customer, dto.getOpenid());
        touchLogin(customer);
        return jwtTokenUtil.generateToken(toLoginUserInfo(customer));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String loginByCustomerId(Integer customerId) {
        YwCustomer customer = requireActiveCustomer(customerId);
        touchLogin(customer);
        return jwtTokenUtil.generateToken(toLoginUserInfo(customer));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String loginByOpenId(String openId) {
        if (StringUtils.isBlank(openId)) {
            return null;
        }
        YwCustomer customer = ywCustomerMapper.selectOne(new QueryWrapper<YwCustomer>().lambda()
                .eq(YwCustomer::getIsdeleted, Constants.ZERO)
                .eq(YwCustomer::getOpenid, openId.trim())
                .last(" limit 1 "));
        if (customer == null) {
            return null;
        }
        assertCustomerEnabled(customer);
        touchLogin(customer);
        return jwtTokenUtil.generateToken(toLoginUserInfo(customer));
    }
 
    @Override
    public LoginUserInfo buildLoginUserInfo(Integer customerId) {
        return toLoginUserInfo(requireActiveCustomer(customerId));
    }
 
    private YwCustomer findActiveByPhone(String phone) {
        if (StringUtils.isBlank(phone)) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "手机号不能为空");
        }
        YwCustomer customer = ywCustomerMapper.selectOne(new QueryWrapper<YwCustomer>().lambda()
                .eq(YwCustomer::getIsdeleted, Constants.ZERO)
                .eq(YwCustomer::getPhone, phone.trim())
                .last(" limit 1 "));
        if (customer == null) {
            throw new BusinessException(ResponseStatus.ACCOUNT_INCORRECT.getCode(), "商户不存在或未注册");
        }
        assertCustomerEnabled(customer);
        return customer;
    }
 
    private YwCustomer requireActiveCustomer(Integer customerId) {
        if (customerId == null) {
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        YwCustomer customer = ywCustomerMapper.selectById(customerId);
        if (customer == null || Constants.equalsInteger(customer.getIsdeleted(), Constants.ONE)) {
            throw new BusinessException(ResponseStatus.ACCOUNT_INCORRECT.getCode(), "商户不存在或未注册");
        }
        assertCustomerEnabled(customer);
        return customer;
    }
 
    private void assertCustomerEnabled(YwCustomer customer) {
        if (customer.getStatus() != null && Constants.equalsInteger(customer.getStatus(), Constants.ONE)) {
            throw new BusinessException(ResponseStatus.NO_ALLOW_LOGIN.getCode(), "商户账号已禁用");
        }
    }
 
    private void bindOpenId(YwCustomer customer, String openid) {
        if (StringUtils.isBlank(openid)) {
            return;
        }
        ywCustomerMapper.update(null, new UpdateWrapper<YwCustomer>().lambda()
                .set(YwCustomer::getOpenid, null)
                .eq(YwCustomer::getOpenid, openid.trim())
                .ne(YwCustomer::getId, customer.getId()));
        customer.setOpenid(openid.trim());
    }
 
    private void touchLogin(YwCustomer customer) {
        Date now = new Date();
        customer.setLastLoginDate(now);
        customer.setEditDate(now);
        customer.setLoginNum((customer.getLoginNum() == null ? 0 : customer.getLoginNum()) + 1);
        ywCustomerMapper.updateById(customer);
    }
 
    private LoginUserInfo toLoginUserInfo(YwCustomer customer) {
        LoginUserInfo loginUserInfo = new LoginUserInfo();
        loginUserInfo.setCustomerId(customer.getId());
        loginUserInfo.setId(customer.getId());
        loginUserInfo.setH5UserType(LoginUserInfo.H5_USER_CUSTOMER);
        loginUserInfo.setRealname(customer.getName());
        loginUserInfo.setMobile(customer.getPhone());
        loginUserInfo.setUsername("customer_" + customer.getId());
        loginUserInfo.setSource(LoginUserInfo.SOURCE_H5_CUSTOMER);
        loginUserInfo.setRoles(Collections.singletonList("h5_customer"));
        loginUserInfo.setPermissions(Collections.emptyList());
        return loginUserInfo;
    }
}