SmsCodeServiceImpl.java 1.62 KB
package com.meishu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.meishu.common.exception.HttpException;
import com.meishu.dto.sms.SendVerifyCodeDTO;
import com.meishu.model.SmsCodeDO;
import com.meishu.mapper.SmsCodeMapper;
import com.meishu.model.UserRoleDO;
import com.meishu.service.SmsCodeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meishu.service.UserRoleService;
import com.meishu.util.ConstantUtils;
import com.meishu.util.RandomUtil;
import com.meishu.util.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 短信验证码 服务实现类
 * </p>
 *
 * @author Tuyp
 * @since 2021-04-25
 */
@Service
public class SmsCodeServiceImpl extends ServiceImpl<SmsCodeMapper, SmsCodeDO> implements SmsCodeService {

    @Autowired
    private SMSUtils smsUtils;

    @Autowired
    private UserRoleService userRoleService;

    @Override
    public void sendVerifyCode(SendVerifyCodeDTO sendVerifyCodeDTO) {
        UserRoleDO userRoleDO =userRoleService.getOne(new QueryWrapper<UserRoleDO>()
                .lambda()
                .eq(UserRoleDO::getPhone, sendVerifyCodeDTO.getPhone()));
        if(userRoleDO == null) {
            throw new HttpException(70012);
        }

        String code = RandomUtil.getRandomCode(6);
        smsUtils.send(sendVerifyCodeDTO.getPhone(), code);

        SmsCodeDO smsCodeDO = new SmsCodeDO();
        smsCodeDO.setCode(code);
        smsCodeDO.setTelephone(sendVerifyCodeDTO.getPhone());
        this.baseMapper.insert(smsCodeDO);
    }
}