2024.12.18

package com.men.index;

import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.men.common.model.User;
import com.men.user.UserService;

/**
* @author mendianyu
*/
public class IndexController extends Controller
{

UserService service = new UserService();

//前台渲染登录界面
public void index()
{
render("index.html");
}

/**
* 用户登录验证
*/
@ActionKey("/login")
public void login()
{
//获取用户输入的账号和密码
String id = getPara("id");
String password = getPara("password");

//从数据库中获取数据
User userMassage = service.findUserByNameAndPassword(id);

//验证用户名和密码是否正确
if (userMassage != null)
{
if (password.equals(userMassage.getStr("password")))
{
setSessionAttr("loginUser", userMassage);
setSessionAttr("id", userMassage.getId());
redirect("/user/self");
} else
{
setAttr("errorMsg", "用户名或密码错误,请重新输入。");
render("index.html");
}
} else
{
setAttr("errorMsg", "该用户名不存在,请重新输入。");
render("index.html");
}
}

/**
* 用户退出
*/
@ActionKey("logout")
public void logout()
{
getSession().removeAttribute("loginUser");
render("index.html");
}

/**
* 用户注册
*/
@ActionKey("register")
public void register()
{
String id = getPara("id");
String password = getPara("password");
User user = service.findUserByNameAndPassword(id);
//用户名存在
if (user != null)
{
setAttr("error","该用户名已经存在,换一个吧");
render("register.html");
} else
{
User user1 = new User();
user1.setId(id);
user1.setPassword(password);
user1.setIsAdmin(0);
user1.setChecked(1);
user1.setTUsed(0);
user1.setTLast(5000);
user1.setPUsed(0);
user1.setPLast(50);
user1.save();
render("index.html");
}
}

}


package com.men.picture;

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.Picture;
import com.men.common.model.User;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Before(PictureInterceptor.class)
public class PictureController extends Controller
{

@Inject
PictureService service = new PictureService();

/**
* 主界面
*/
public void index()
{
render("/picture/convert.html");
}

//图片转换
public void convert()
{
String src1 = getPara("imageSrc");
String src2 = null;
if (src1 != null)
{
setAttr("imageSrc1", src1);
String base64 = src1.split(",")[1];

String url = "";
String url1 = getPara("url1");
String url2 = getPara("url2");
String urlType = "";
String type = "";
if (url1 != null)
{
url = url1;

} else
{
url = url2;
}
//获取url的最后四位,判断是什么转换类型
urlType = url.substring(url.length() - 4);
switch (urlType)
{
case "nime":
type = "人物动漫化";
break;
case "rize":
type = "黑白图像上色";
break;
case "haze":
type = "图像去雾";
break;
case "tore":
type = "拉伸图像修复";
break;
case "ance":
type = "图像清晰度增强";

break;
case "oire":
type = "图像去摩尔纹";
break;
case "pair":
type = "文档图片去底纹";
break;
case "oise":
type = "图像去噪";
break;
default:
break;
}

//System.out.println("url:" + url);
//System.out.println(base64);
//转换后的图片的base64编码
// System.out.println(service.getBase64Str(url, base64));
src2 = "data:image/jpeg;base64," + service.getBase64Str(url, base64);
setAttr("imageSrc2", src2);
//System.out.println(src2);

//保存到数据库
//获取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);

Picture picture = new Picture();
picture.setId(id);
picture.setTime(formattedDateTime);
picture.setType(type);
picture.setSrc1(src1);
picture.setSrc2(src2);
picture.save();

refreshPictureTimes();
}

render("/picture/convert.html");
}

/**
* 更新图片转换使用次数
*/
public void refreshPictureTimes()
{
String id = getSessionAttr("id");

Integer pUsed = service.findUserById(id).getPUsed() + 1;
Integer pLast = service.findUserById(id).getPLast() - 1;
User user = service.findUserById(id);
user.setPUsed(pUsed);
user.setPLast(pLast);
user.update();
}


public void list()
{
setAttr("list", service.paginate(getParaToInt(0, 1), 5));
render("/picture/list.html");
}

public void detail()
{
setAttr("details", service.findByTime(getPara()));
render("/picture/details.html");
}


public void search()
{
String url = "";
String url1 = getPara("url1");
String url2 = getPara("url2");
String urlType = "";
String type = "";
if (url1 != null)
{
url = url1;

} else
{
url = url2;
}
String date = getPara("date");
String id = getSessionAttr("id");
if ((url != null && !url.isEmpty()) || (date != null && !date.isEmpty()))
{
// 执行模糊查询操作
List<Record> pRecord = service.searchRecords(url, date, id);
setAttr("date", date);

// 将查询结果传递到页面
setAttr("pRecord", pRecord);
} else
{
//如果没有参数,显示所有翻译记录
List<Record> pRecord = service.searchRecords("", "", id);
setAttr("pRecord", pRecord);
}

// 渲染结果到页面
render("/picture/search.html");
}
}

package com.men.picture;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;

public class PictureInterceptor implements Interceptor {

@Override
public void intercept(Invocation inv)
{
System.out.println("Before invoking " + inv.getActionKey());
inv.invoke();
System.out.println("After invoking " + inv.getActionKey());
}
}
package com.men.picture;

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.Picture;
import com.men.common.model.User;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class PictureService {

private Picture dao = new Picture().dao();
private User udao = new User().dao();

// 百度API Key和Secret Key
private static final String API_KEY = "SP3mEK87MwBGnxXsppHgDMt3";
private static final String SECRET_KEY = "M5AyIxOWiFuKb16qgW8MdrRNtq7UBdBH";


public Page<Picture> paginate(int pageNumber, int pageSize) {
return dao.paginate(pageNumber, pageSize, "select *", "from picture order by time desc");
}

public Picture findByTime(String time) {
return dao.findFirst("select * from picture where time = ? ", time);
}

public String getBase64Str(String url, String src) {
try {
// 获取access_token
String accessToken = getAccessToken();
if (accessToken == null) {
return null;
}

// 对图片src进行URL编码
String imgParam = URLEncoder.encode(src, "UTF-8");
String param = "image=" + imgParam;

// 发送POST请求并获取返回结果
String result = HttpUtil.post(url, accessToken, "application/x-www-form-urlencoded", param);

// 解析返回的JSON结果
JSONObject jsonObject = com.alibaba.fastjson2.JSON.parseObject(result);
String base64Str = "";

// 根据API返回的字段获取base64编码
if (jsonObject.getString("image") != null) {
base64Str = jsonObject.getString("image");
} else if (jsonObject.getString("result") != null) {
base64Str = jsonObject.getString("result");
} else if (jsonObject.getString("image_processed") != null) {
base64Str = jsonObject.getString("image_processed");
}

return base64Str;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 获取百度API的access_token
* @return access_token
*/
private String getAccessToken() {
String url = "https://aip.baidubce.com/oauth/2.0/token";
String params = "grant_type=client_credentials&client_id=" + API_KEY + "&client_secret=" + SECRET_KEY;

try {
// 使用application/x-www-form-urlencoded类型发送请求
String result = HttpUtil.post(url, "", "application/x-www-form-urlencoded", params);

// 解析返回的JSON并提取access_token
JSONObject jsonObject = com.alibaba.fastjson2.JSON.parseObject(result);
return jsonObject.getString("access_token");

} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 模糊查询,支持按特效类型(effectType)和日期(date)进行查询
*/
public List<Record> searchRecords(String effectType, String date, String id) {
String sql = "select * from picture where id = ? ";

if (effectType != null && !effectType.isEmpty()) {
sql += " AND type like ? ";
}

if (date != null && !date.isEmpty()) {
sql += " AND time like ? ";
}

sql += " order by time desc"; // 根据时间降序排列

List<Object> params = new ArrayList<>();
params.add(id);

if (effectType != null && !effectType.isEmpty()) {
params.add("%" + effectType + "%");
}

if (date != null && !date.isEmpty()) {
params.add("%" + date + "%");
}
// 查询
return Db.find(sql, params.toArray());
}

/**
* 根据id查找用户
*/
public User findUserById(String id) {
return udao.findFirst("select * from user where id = ? ", id);
}
}
package com.men.picture;

import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;

/**
* @author mendianyu
*/
public class PictureValidator extends Validator
{

@Override
protected void validate(Controller controller)
{

}

@Override
protected void handleError(Controller controller)
{

}
}
posted @   我也不想的  阅读(2)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示