给安卓端调用的短信发送接口demo

package com.js.ai.modules.pointwall.action;

import java.io.IOException;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.js.ai.common.utils.CacheUtils;
import com.js.ai.common.utils.StringUtils;
import com.js.ai.common.web.BaseController;
import com.js.ai.modules.pointwall.util.JsonHandle;
import com.js.ai.modules.pointwall.util.SysConfigUtil;
import com.js.ai.smssend.SendValidCode;

/**
 * 
 * @ClassName: SendSmsAction
 * @Description: 短信验证码 Controller
 * @author: 
 * @date: 
 */
@Controller
@RequestMapping(value = "${adminPath}/client")
public class SendSmsAction extends BaseController {
	private static Logger logger = Logger.getLogger(SendSmsAction.class);

	/**
	 * 
	 * @Title: sendSms
	 * @Description: 发送短信验证码
	 * @param request
	 * @param response
	 * @throws IOException
	 * 
	 * @return: void
	 */
	@RequestMapping(value = "sendSMS")
	@ResponseBody
	public String sendSms(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		String uuid = request.getParameter("uuid");// 获取客户端发送的手机唯一标识码
		String phone = request.getParameter("phone");// 获取客户端发送手机号码
		String phone_map = (String) CacheUtils.get(phone);// 根据手机号这个key取出对应的uuid值,限制同一个手机号五分钟内只能发送一次号码
		String uuid_map = (String) CacheUtils.get(uuid);// 取出uuid的值来限制一台手机五分钟内只能发送一条短息。
		HashMap<String, String> map = new HashMap<String, String>();
		int smsCode = 100000 + (int) (Math.random() * 899999);// 随机验证码
		String verificationCode = String.valueOf(smsCode);// 验证码
		if (StringUtils.isEmpty(uuid_map)) {// 第一次为空的时候则插入缓存中
			CacheUtils.put(uuid, uuid);// 用同一个uuid即作为key 也作为value
			if (StringUtils.isNotBlank(phone) && StringUtils.isEmpty(phone_map)) {
				CacheUtils.put(phone, phone);
				String result = SendValidCode.sendMessage(SysConfigUtil.SMS_ZH, SysConfigUtil.SMS_PW, phone,
						SysConfigUtil.SMS_QM, verificationCode, SysConfigUtil.SMS_WAY);
				logger.info("客户端第一次发送短信" + phone + ">>>>" + uuid);
			} else {// 即使同一个号码 在不同的手机上 也同样限制为在五分钟内只能 发送一次短信获取验证码
				map.put("sendSMS", "error");
				logger.info("当前在五分钟内不能重复发送短信" + phone + ">>>>" + uuid);
			}

		} else {

			if (uuid_map.equals(uuid)) {// 第二次之后进来,则判断uuid是否一样,如果是则代表五分钟内不能发送短信
				map.put("sendSMS", "error");
				logger.info("当前在五分钟内不能重复发送短信" + phone + "=====" + uuid);
			} else {
				if (StringUtils.isNotBlank(phone)) {// 法则发送信息,并重新赋值

					String result = SendValidCode.sendMessage(SysConfigUtil.SMS_ZH, SysConfigUtil.SMS_PW, phone,
							SysConfigUtil.SMS_QM, verificationCode, SysConfigUtil.SMS_WAY);

					CacheUtils.put(phone, phone);
					CacheUtils.put(uuid, uuid);// 用同一个uuid即作为key 也作为value
					logger.info("当前不在五分钟内允许发送短信");
				}

			}
		}

		map.put("Checkcode", verificationCode);
		String Checkcode = JsonHandle.toJson(map);
		return Checkcode;
	}
}

  

posted @ 2017-04-26 17:50  ATJAVA  阅读(306)  评论(0编辑  收藏  举报