这是我dao.js的内容
var ioc = {
conf : {
type : "org.nutz.ioc.impl.PropertiesProxy",
fields : {
paths : ["custom/"]
}
},
dataSource : {
factory : "$conf#make",
args : ["com.alibaba.druid.pool.DruidDataSource", "db."],
type : "com.alibaba.druid.pool.DruidDataSource",
events : {
create : "init",
depose : 'close'
}
},
dao : {
type : "org.nutz.dao.impl.NutDao",
args : [{refer:"dataSource"}]
}
};
EmailService接口里的内容
package net.wendal.nutzbook.service;
public interface EmailService {
boolean send(String to, String subject, String html);
}
实现类的内容
package net.wendal.nutzbook.service.impl;
import net.wendal.nutzbook.service.EmailService;
import org.apache.commons.mail.HtmlEmail;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
@IocBean(name="emailService")//把当前类注入到IOC中
public class EmailServiceImpl implements EmailService{
private static final Log log = Logs.get();
@Inject("refer:$ioc")
protected Ioc ioc;
public boolean send(String to, String subject, String html) {
try {
HtmlEmail email = ioc.get(HtmlEmail.class);
email.setSubject(subject);
email.setHtmlMsg(html);
email.addTo(to);
email.buildMimeMessage();
email.sendMimeMessage();
return true;
} catch (Throwable e) {
log.info("send email fail", e);
return false;
}
}
}
UserProfileModule类中添加的方法
@At("/active/mail")
@POST
public Object activeMail(@Attr(scope=Scope.SESSION, value="me")int userId, HttpServletRequest req) {
NutMap re = new NutMap();
UserProfile profile = get(userId);
if (Strings.isBlank(profile.getEmail())) {
return re.setv("ok", false).setv("msg", "你还没有填邮箱啊!");
}
String token = String.format("%s,%s,%s", userId, profile.getEmail(), System.currentTimeMillis());
token = Toolkit._3DES_encode(emailKEY, token.getBytes());
String url = req.getRequestURL() + "?token=" + token;
String html = "<div>如果无法点击,请拷贝一下链接到浏览器中打开<p/>验证链接 %s</div>";
html = String.format(html, url, url);
try {
boolean ok = emailService.send(profile.getEmail(), "XXX 验证邮件 by Nutzbook", html);
if (!ok) {
return re.setv("ok", false).setv("msg", "发送失败");
}
} catch (Throwable e) {
log.debug("发送邮件失败", e);
return re.setv("ok", false).setv("msg", "发送失败");
}
return re.setv("ok", true);
}
@Filters // 不需要先登录,很明显...
@At("/active/mail")
@GET
@Ok("raw") // 为了简单起见,这里直接显示验证结果就好了
public String activeMailCallback(@Param("token")String token, HttpSession session) {
if (Strings.isBlank(token)) {
return "请不要直接访问这个链接!!!";
}
if (token.length() < 10) {
return "非法token";
}
try {
token = Toolkit._3DES_decode(emailKEY, Toolkit.hexstr2bytearray(token));
if (token == null)
return "非法token";
String[] tmp = token.split(",", 3);
if (tmp.length != 3 || tmp[0].length() == 0 || tmp[1].length() == 0 || tmp[2].length() == 0)
return "非法token";
long time = Long.parseLong(tmp[2]);
if (System.currentTimeMillis() - time > 10*60*1000) {
return "该验证链接已经超时";
}
int userId = Integer.parseInt(tmp[0]);
Cnd cnd = Cnd.where("userId", "=", userId).and("email", "=", tmp[1]);
int re = dao.update(UserProfile.class, Chain.make("emailChecked", true), cnd);
if (re == 1) {
return "验证成功";
}
return "验证失败!!请重新验证!!";
} catch (Throwable e) {
log.debug("检查token时出错", e);
return "非法token";
}
}