package cn.sh.stc.sict.cloud.common.gateway.handler;

import cn.sh.stc.sict.cloud.common.core.util.R;
import cn.sh.stc.sict.cloud.common.core.util.SmsSendUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

/**
 * @Description 短信验证码获取类
 * @Author
 * @Date
 */
@Slf4j
@Component
@AllArgsConstructor
public class SmsCodeHandler implements HandlerFunction<ServerResponse> {
    private SmsSendUtil smsSendUtil;

    @Override
    public Mono<ServerResponse> handle(ServerRequest serverRequest) {
        String phone = serverRequest.queryParam("mobile").get();

        String result = smsSendUtil.sendSMS(phone, 1);

        return ServerResponse
                .status(HttpStatus.OK)
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .body(BodyInserters.fromObject(new R(result)));
    }
}
