jeesite框架使用创蓝获取短信验证码实现手机号注册

注:前提是使用jeesite框架

1. 先启动项目,在系统管理中找到系统参数中的参数设置

     把账号自助-是否开启用户注册功能(sys.account,registerUser)的参数键值修改为true

2. 修改登录页面:sysLogin.html

        把忘记密码和注册账号的按钮打开

3. 修改registerUser.html

  当时打开注册之后,点击注册账号,页面会错乱,不正常显示

  我就修改了registerUser.html页

4. 写了一个MsgService接口
public interface MsgService {
    public String send(String mobile,String content);
}
5. 写一个SmsSendService类实现MsgService接口
import com.jeesite.modules.msg.service.MsgService;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class SmsSendService extends BaseService implements MsgService {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	private String smsSendUrl = "创蓝接口的url";

	private String smsAccount = "你所申请的创蓝账号";

	private String smsPassword = "你所申请的创蓝密码";

	@Override
	public String send(String mobile, String content) {
		this.logger.info("-------------短信发送开始执行--------------");
		HttpClient client = new HttpClient(new HttpClientParams(), new SimpleHttpConnectionManager(true));
		GetMethod method = new GetMethod();
		try {
			URI base = new URI(smsSendUrl, false);
			method.setURI(new URI(base, "HttpBatchSendSM", false));
			method.setQueryString(new NameValuePair[]{
					new NameValuePair("account", smsAccount),
					new NameValuePair("pswd", smsPassword),
					new NameValuePair("mobile", mobile),
					new NameValuePair("needstatus", String.valueOf(true)),
					new NameValuePair("msg", content),
					new NameValuePair("extno", null),
			});
			int result = client.executeMethod(method);
			if (result == HttpStatus.SC_OK) {
				InputStream in = method.getResponseBodyAsStream();
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				byte[] buffer = new byte[1024];
				int len = 0;
				while ((len = in.read(buffer)) != -1) {
					baos.write(buffer, 0, len);
				}
				this.logger.info("-------------短信发送返回结果" + URLDecoder.decode(baos.toString(), "UTF-8"));
				return URLDecoder.decode(baos.toString(), "UTF-8");

			} else {
				throw new Exception("HTTP ERROR Status: " + method.getStatusCode() + ":" + method.getStatusText());
			}
		}catch (Exception e)
		{
			e.printStackTrace();
			this.logger.info("-------------短信发送执行报错----" + e.getMessage());
			this.logger.info("-------------短信发送执行报错----" + e.toString());
			return e.getMessage();
		}finally {
			method.releaseConnection();
		}
	}
}

注:这里注意导包的时候不要导错了

5. 修改AccountController

  需要将原本的SmsUtils.send()方法注释掉,在这个类的开头注入我们写的SmsSendService,调用SmsSendService中的send方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值