package com.ruoyi.system.service.impl;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.ruoyi.common.core.domain.entity.ShopUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.config.AliConfig;
import com.ruoyi.system.service.AliConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class AliConfigServiceImpl implements AliConfigService {
    @Autowired
    private RedisCache redisCache;


    @Override
    public String sendMsg(ShopUser shopUser) {
        String phone = shopUser.getPhone();
        if (phone == null || phone.isEmpty()) {
            return "手机号不能为空";
        }
        // 1. 生成6位验证码
        String code =   AliConfig.sendMsg(shopUser);
        if (code != null){
        // 2. 存入Redis，5分钟有效
        redisCache.setCacheObject("sms_code:" + phone, code, 5, java.util.concurrent.TimeUnit.MINUTES);
        }
        return code;
    }
}
