Skip to content

Commit

Permalink
add: 扩展服务类 & fix: 启动自动建表
Browse files Browse the repository at this point in the history
  • Loading branch information
Wizzercn committed Feb 15, 2017
1 parent 9ca5e19 commit dec2e22
Show file tree
Hide file tree
Showing 9 changed files with 456 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private void initSysTask(NutConfig config, Dao dao) {
Sys_task task = new Sys_task();
task.setDisabled(true);
task.setName("测试任务");
task.setJobClass("cn.wizzer.common.quartz.job.TestJob");
task.setJobClass("cn.wizzer.app.web.commons.quartz.job.TestJob");
task.setCron("*/5 * * * * ?");
task.setData("{\"hi\":\"Wechat:wizzer | send red packets of support,thank u\"}");
task.setNote("微信号:wizzer | 欢迎发送红包以示支持,多谢。。");
Expand Down Expand Up @@ -157,7 +157,7 @@ private void initSysTask(NutConfig config, Dao dao) {
* @param dao
*/
private void initSysData(NutConfig config, Dao dao) {
Daos.createTablesInPackage(dao, "cn.wizzer.modules", false);
Daos.createTablesInPackage(dao, "cn.wizzer.app", false);
// 若必要的数据表不存在,则初始化数据库
if (0 == dao.count(Sys_user.class)) {
//初始化配置表
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cn.wizzer.app.web.commons.quartz.job;

import cn.wizzer.app.sys.modules.models.Sys_task;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
* Created by Wizzer.cn on 2015/6/27.
*/
@IocBean
public class TestJob implements Job {

private static final Log log = Logs.get();
@Inject
protected Dao dao;

public void execute(JobExecutionContext context) throws JobExecutionException {
JobDataMap data = context.getJobDetail().getJobDataMap();
String taskId = context.getJobDetail().getKey().getName();
String hi = data.getString("hi");
log.info("Test Job hi::" + hi);
dao.update(Sys_task.class, Chain.make("exeAt", (int) (System.currentTimeMillis() / 1000)).add("exeResult", "执行成功"), Cnd.where("id", "=", taskId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cn.wizzer.app.web.commons.services.email;

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;

/**
* Created by Wizzer on 2016/7/31.
*/
@IocBean
public class 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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cn.wizzer.app.web.commons.services.qrcode;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.view.HttpStatusView;

import java.util.HashMap;
import java.util.Map;

/**
* Created by Wizzer on 2016/7/31.
*/
@IocBean
public class QrcodeService {
private static final Log log = Logs.get();

protected MultiFormatWriter writer = new MultiFormatWriter();
public Object get(String data, int w, int h) {
if (Strings.isBlank(data))
return new HttpStatusView(404);
// 修正长宽
if (w < 1)
w = 256;
else if (w > 1024)
w = 1024;
if (h < 1)
h = 256;
else if (h > 1024)
h = 1024;
// 接受Base64编码,例如内容是中文
byte[] tmp = data.getBytes();
if (tmp != null)
data = new String(tmp);
try {
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix matrix = writer.encode(data, BarcodeFormat.QR_CODE, w, h, hints);
return MatrixToImageWriter.toBufferedImage(matrix);
} catch (WriterException e) {
// 生成失败,一般是文本太长,指定的尺寸放不下
log.debug("qrcode write fail", e);
return new HttpStatusView(500);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cn.wizzer.app.web.commons.services.wx;

import cn.wizzer.app.wx.modules.models.Wx_tpl_id;
import cn.wizzer.app.wx.modules.models.Wx_tpl_log;
import cn.wizzer.app.wx.modules.models.Wx_user;
import cn.wizzer.app.wx.modules.services.WxConfigService;
import cn.wizzer.app.wx.modules.services.WxTplIdService;
import cn.wizzer.app.wx.modules.services.WxTplLogService;
import cn.wizzer.app.wx.modules.services.WxUserService;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.weixin.bean.WxTemplateData;
import org.nutz.weixin.spi.WxApi2;
import org.nutz.weixin.spi.WxResp;

import java.util.Map;

/**
* Created by wizzer on 2016/8/6.
*/
@IocBean
public class TplService {
@Inject
private WxTplIdService wxTplIdService;
@Inject
private WxUserService wxUserService;
@Inject
private WxTplLogService wxTplLogService;
@Inject
private WxConfigService wxConfigService;

/**
* 通过模板编号发送模板消息,并记录到日志表
*
* @param wxid
* @param openid
* @param tplId
* @param url
* @param data
* @return
*/
public String send(String wxid, String openid, String tplId, String url, Map<String, WxTemplateData> data) {
WxApi2 wxApi2 = wxConfigService.getWxApi2(wxid);
Wx_tpl_id tpl = wxTplIdService.fetch(tplId);
if (tpl != null) {
WxResp wxResp = wxApi2.template_send(openid, tpl.getTemplate_id(), url, data);
Wx_user user = wxUserService.fetch(Cnd.where("openid", "=", openid));
Wx_tpl_log l = new Wx_tpl_log();
l.setWxid(wxid);
l.setOpenid(openid);
l.setNickname("");
if (user != null) {
l.setNickname(user.getNickname());
}
l.setContent(Json.toJson(data));
if (wxResp.errcode() == 0) {
l.setStatus(1);//发送成功
} else l.setStatus(2);//发送失败
Wx_tpl_log rl = wxTplLogService.insert(l);
return rl == null ? null : rl.getId();
}
return null;
}
}
Loading

0 comments on commit dec2e22

Please sign in to comment.