大作业
package com.men.translate; import com.jfinal.aop.Interceptor; import com.jfinal.aop.Invocation; public class TranslateInterceptor implements Interceptor { @Override public void intercept(Invocation inv) {
package com.men.translate; import com.jfinal.aop.Before; import com.jfinal.aop.Inject; import com.jfinal.core.Controller; import com.jfinal.plugin.activerecord.Record; import com.men.common.model.Translate; import com.men.common.model.User; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.List; /** * @author mendianyu */ @Before(TranslateInterceptor.class) public class TranslateController extends Controller { @Inject TranslateService service = new TranslateService(); public void index() { render("/translate/translation.html"); } /** * 翻译文本并将结果存到数据库 */ public void fanyi() { //从输入获取参数 String from = getPara("from"); String to = getPara("to"); String q = getPara("q"); //调用翻译方法 String result = service.textTrans(from, to, q); //用于将内容显示回界面 setAttr("q", q); setAttr("result", result); //获取session中保存的id String id = getSessionAttr("id"); // 获取当前时间 LocalDateTime currentDateTime = LocalDateTime.now(); // 定义日期时间格式 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH:mm:ss"); // 将当前时间格式化为字符串 String formattedDateTime = currentDateTime.format(formatter); Translate translate = new Translate(); translate.setId(id); translate.setTime(formattedDateTime); translate.setFrom1(from); translate.setTo1(to); translate.setQ(q); translate.setResult(result); translate.save(); refreshTranslateTimes(); render("/translate/translation.html"); } /** * 更新翻译使用字符量 */ public void refreshTranslateTimes() { String q = getAttr("q"); //忽略空格和标点符号 int amounts = q.replaceAll("[\\s\\p{P}]", "").length(); String id = getSessionAttr("id"); Integer tUsed = service.findUserById(id).getTUsed() + amounts; Integer tLast = service.findUserById(id).getTLast() - amounts; User user = service.findUserById(id); user.setTUsed(tUsed); user.setTLast(tLast); user.update(); } /** * 翻译记录列表 */ public void list() { setAttr("list", service.paginate(getParaToInt(0, 1), 5)); render("/translate/list.html"); } /** * 翻译记录具体内容 */ public void detail() { setAttr("details", service.findByTime(getPara())); render("/translate/details.html"); } /** * 模糊查询 */ public void search() { String from = getPara("from"); String to = getPara("to"); String date = getPara("date"); String id = getSessionAttr("id"); if ((from != null && !from.isEmpty()) || (to != null && !to.isEmpty()) || (date != null && !date.isEmpty())) { // 执行模糊查询操作 List<Record> translations = service.searchRecords(from, to, date, id); setAttr("date", date); // 将查询结果传递到页面 setAttr("translations", translations); } else { //如果没有参数,显示所有翻译记录 List<Record> allTranslations = service.searchRecords("", "", "", id); setAttr("translations", allTranslations); } // 渲染结果到页面 render("/translate/search.html"); } }
package com.men.translate; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import com.jfinal.plugin.activerecord.Db; import com.jfinal.plugin.activerecord.Page; import com.jfinal.plugin.activerecord.Record; import com.men.common.HttpUtil; import com.men.common.model.Translate; import com.men.common.model.User; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author mendianyu */ public class TranslateService { private Translate dao = new Translate().dao(); private User udao = new User().dao(); public String textTrans(String from, String to, String q) { // 请求url String url = "https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1"; try { Map<String, Object> map = new HashMap<>(); map.put("from", from); map.put("to", to); map.put("q", q); String param = com.alibaba.fastjson2.JSON.toJSONString(map); String accessToken = "24.d093c94cec01f789c67706665690eb9a.2592000.1703815127.282335-43853160"; String result = HttpUtil.post(url, accessToken, "application/json", param); //将HTTP响应的JSON字符串解析为一个JSONObject对象 JSONObject jsonObject = com.alibaba.fastjson2.JSON.parseObject(result); //从解析后的JSON对象中获取名为 "result" 的子对象 JSONObject resultObject = jsonObject.getJSONObject("result"); //从"result"对象中获取名为"trans_result"的JSON数组 JSONArray transResultArray = resultObject.getJSONArray("trans_result"); //从transResultArray数组中获取第一个翻译结果 JSONObject transResultObject = transResultArray.getJSONObject(0); String dstValue = transResultObject.getString("dst"); //输出翻译结果 System.out.println("翻译结果为: " + dstValue); return dstValue; } catch (Exception e) { e.printStackTrace(); } return null; } public Page<Translate> paginate(int pageNumber, int pageSize) { return dao.paginate(pageNumber, pageSize, "select *", "from translate order by time desc"); } /** * 根据时间查找记录 */ public Translate findByTime(String time) { return dao.findFirst("select * from translate where time = ? ", time); } /** * 模糊查询 */ public List<Record> searchRecords(String from, String to, String date, String id) { String sql = "select * from translate where (from1 like ? or to1 like ? or time like ?) and id=? order by id desc"; date = "%" + date + "%"; List<Record> translations = Db.find(sql, from, to, date, id); return translations; } /** * 根据id查找用户 */ public User findUserById(String id) { return udao.findFirst("select * from user where id = ? ", id); } }
package com.men.translate; import com.jfinal.core.Controller; import com.jfinal.validate.Validator; /** * @author mendianyu */ public class TranslateValidator extends Validator { @Override protected void validate(Controller controller) { } @Override protected void handleError(Controller controller) { } }
System.out.println("Before invoking " + inv.getActionKey()); inv.invoke(); System.out.println("After invoking " + inv.getActionKey()); } }