注:前提是使用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方法