Newer
Older
newfiber-termite / newfiber-system / newfiber-system-sms / src / main / java / com / newfiber / system / controller / SmsEndpoint.java
package com.newfiber.system.controller;


import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson2.JSONObject;
import com.newfiber.common.core.web.controller.BaseController;
import com.newfiber.common.core.web.domain.Result;
import com.newfiber.system.builder.SmsBuilder;
import com.newfiber.system.domain.model.SmsCode;
import com.newfiber.system.domain.model.SmsData;
import com.newfiber.system.domain.model.SmsResponse;
import io.swagger.annotations.Api;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 短信服务端点
 *
 * @author Chill
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sms/endpoint")
@Api(value = "短信服务端点", tags = "短信服务端点")
public class SmsEndpoint extends BaseController {

	/**
	 * 短信服务构建类
	 */
	private final SmsBuilder smsBuilder;

	public static final String PARAM_KEY = "code";
	public static final String SEND_SUCCESS = "短信发送成功";
	public static final String SEND_FAIL = "短信发送失败";
	public static final String VALIDATE_SUCCESS = "短信校验成功";
	public static final String VALIDATE_FAIL = "短信校验失败";

	//================================= 短信服务校验 =================================

	/**
	 * 短信验证码发送
	 *
	 * @param phone 手机号
	 */
	@SneakyThrows
	@PostMapping("/send-validate")
	public Result<Object> sendValidate(String code, @RequestParam String phone) {
		Map<String, String> params = new HashMap<>(1);
		params.put(PARAM_KEY, String.valueOf(RandomUtil.randomInt(1000, 9999)));

		SmsCode smsCode = smsBuilder.template(code).sendValidate(new SmsData(params).setKey(PARAM_KEY), phone);
		if(smsCode.isSuccess()){
			logger.info(String.format("验证码发送成功:smsCode:%s | phone:%s", JSONObject.toJSONString(smsCode), phone));
		}
		return smsCode.isSuccess() ? success(smsCode) : error(SEND_FAIL);
	}

	/**
	 * 校验短信
	 *
	 * @param smsCode 短信校验信息
	 */
	@SneakyThrows
	@PostMapping("/validate-message")
	public Result<String> validateMessage(String smsCode, String phone, String captcha) {
		boolean validate = smsBuilder.template(smsCode).validateMessage(new SmsCode(phone, captcha));
		return validate ? success(VALIDATE_SUCCESS) : error(VALIDATE_FAIL);
	}

	//========== 通用短信自定义发送(支持自定义params参数传递, 推荐用于测试, 不推荐用于生产环境) ==========

	/**
	 * 发送信息
	 *
	 * @param params 自定义短信参数
	 * @param phones 手机号集合
	 */
	@SneakyThrows
	@PostMapping("/send-message")
	@SuppressWarnings("unchecked")
	public Result<String> sendMessage(@RequestParam String code, @RequestParam String params, @RequestParam String phones) {
		SmsData smsData = new SmsData(JSONObject.parseObject(params, HashMap.class));
		return send(code, smsData, phones);
	}

	//========== 指定短信服务发送(可根据各种场景自定拓展定制, 损失灵活性增加安全性, 推荐用于生产环境) ==========

	/**
	 * 短信通知
	 *
	 * @param phones 手机号集合
	 */
	@SneakyThrows
	@PostMapping("/send-notice")
	public Result<String> sendNotice(@RequestParam String phones) {
		Map<String, String> params = new HashMap<>(3);
		params.put("title", "通知标题");
		params.put("content", "通知内容");
		params.put("date", "通知时间");
		SmsData smsData = new SmsData(params);
		return send(smsData, phones);
	}

	//================================= 通用短信发送接口 =================================

	/**
	 * 通用短信发送接口
	 *
	 * @param smsData 短信内容
	 * @param phones  手机号列表
	 * @return 是否发送成功
	 */
	private Result<String> send(SmsData smsData, String phones) {
		SmsResponse response = smsBuilder.template().sendMessage(smsData, Arrays.asList(phones.split(",")));
		if(response.isSuccess()){
			logger.info(String.format("短信发送成功:smsData:%s | phone:%s", JSONObject.toJSONString(smsData), phones));
		}
		return response.isSuccess() ? success(SEND_SUCCESS) : error(SEND_FAIL);
	}

	/**
	 * 通用短信发送接口
	 *
	 * @param smsCode    资源编号
	 * @param smsData 短信内容
	 * @param phones  手机号列表
	 * @return 是否发送成功
	 */
	private Result<String> send(String smsCode, SmsData smsData, String phones) {
		SmsResponse response = smsBuilder.template(smsCode).sendMessage(smsData, Arrays.asList(phones.split(",")));
		logger.info(String.format("短信发送%s:response:%s | code:%s | smsData:%s | phone:%s", response.isSuccess() ? "成功" : "失败",
			JSONObject.toJSONString(response), smsCode, JSONObject.toJSONString(smsData), phones));
		return response.isSuccess() ? success(SEND_SUCCESS) : error(SEND_FAIL);
	}

}