目录
1,补充wx/ExpressController,并配置application.properties
3,设置UserFilter对index.html的访问进行拦截
完整项目在这里实时更新<( ̄︶ ̄)↗[GO!]:
GitHub - GoodbyeFirefly/ExpressManagementSystem: 用GitHub一步步记录编码过程,实现一个相对完整的快递管理项目https://github.com/GoodbyeFirefly/ExpressManagementSystemhttps://github.com/GoodbyeFirefly/ExpressManagementSystem
1,补充wx/ExpressController,并配置application.properties
package com.xxy.wx.controller;
import com.xxy.bean.BootstrapTableExpress;
import com.xxy.bean.Express;
import com.xxy.bean.Message;
import com.xxy.bean.User;
import com.xxy.mvc.ResponseBody;
import com.xxy.service.ExpressService;
import com.xxy.service.UserService;
import com.xxy.util.DateFormatUtil;
import com.xxy.util.JSONUtil;
import com.xxy.util.UserUtil;
import sun.plugin2.message.JavaScriptBaseMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
public class ExpressController {
@ResponseBody("/wx/findExpressByUserPhone.do")
public String findByUserPhone(HttpServletRequest request, HttpServletResponse response) {
User wxUser = UserUtil.getWxUser(request.getSession());
String userphone = wxUser.getUserphone();
List<Express> expresses = ExpressService.findByUserPhone(userphone);
List<BootstrapTableExpress> list2 = new ArrayList<>();
for(Express e:expresses){
String inTime = DateFormatUtil.format(e.getIntime());
String outTime = e.getOuttime()==null?"未出库":DateFormatUtil.format(e.getOuttime());
String status = e.getStatus()==0?"待取件":"已取件";
String code = e.getCode()==null?"已取件":e.getCode();
BootstrapTableExpress e2 = new BootstrapTableExpress(e.getId(),e.getNumber(),e.getUsername(),e.getUserphone(),e.getCompany(),code,inTime,outTime,status,e.getSysPhone());
list2.add(e2);
}
Message msg = new Message();
if (expresses.size() == 0) {
msg.setStatus(-1);
} else {
msg.setStatus(0);
// 从对象列表中挑选出status为0的快递对象,并按入库时间进行排序
Stream<BootstrapTableExpress> status0Express = list2.stream().filter(express -> {
if (express.getStatus().equals("待取件")) {
return true;
} else {
return false;
}
}).sorted((o1,o2) -> {
long o1Time = DateFormatUtil.toTime(o1.getIntime());
long o2Time = DateFormatUtil.toTime(o2.getIntime());
return (int)(o1Time-o2Time);
});
Stream<BootstrapTableExpress> status1Express = list2.stream().filter(express -> {
if (express.getStatus().equals("已取件")) {
return true;
} else {
return false;
}
}).sorted((o1,o2) -> {
long o1Time = DateFormatUtil.toTime(o1.getIntime());
long o2Time = DateFormatUtil.toTime(o2.getIntime());
return (int)(o1Time-o2Time);
});
Object[] s0 = status0Express.toArray();
Object[] s1 = status1Express.toArray();
Map data = new HashMap<>();
data.put("status0",s0);
data.put("status1",s1);
msg.setData(data);
}
return JSONUtil.toJSON(msg);
}
}
2,在DateFormatUtil中添加字符串转时间的方法
该方法用于将字符串形式的日期转换为可对比的格式,方便快递日期的对比排序
3,设置UserFilter对index.html的访问进行拦截
这里暂时只针对index.html网页进行拦截
package com.xxy.wx.filter;
import com.xxy.bean.User;
import com.xxy.util.UserUtil;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter({"/index.html"})
public class UserFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
HttpSession session = ((HttpServletRequest) req).getSession();
User wxUser = UserUtil.getWxUser(session);
if (wxUser != null)
chain.doFilter(req, resp);
else
((HttpServletResponse) resp).sendRedirect("login.html");
}
public void init(FilterConfig config) throws ServletException {
}
}
4,修改expressList.html
这里暂时还没有用到前端框架,添加div组件较为繁琐;
此外,要注意区分回调函数中data的具体含义;
生成的二维码作用主要有两大类:标识快递,标识用户。所以在跳转页面时需要加上type;
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1">
<style type="text/css">
body{
margin: 0;
padding: 0;
background-color: #eeeeee;
font-family: "楷体";
}
.item{
margin: 20px;
margin-bottom: 50px;
}
.imte_top{
text-align: center;
}
.imte_top>span{
font-size:14px;
display: inline-block;
padding: 5px 12px;
background-color: #ddd;
color:#fff;
border-radius: 5px;
}
.item_content{
background-color: #fff;
padding: 25px 15px;
margin-top: 6px;
}
.item_content_top_1{
font-size: 24px;
font-weight: bold;
}
.item_content_top_2{
color:#eee;
font-size: 14px;
margin: 5px 0px;
}
.item_content_top_3{
margin: 10px 0px;
}
.item_content_top_4{
margin: 10px 0px;
}
.item_content_bottom>a{
text-decoration: none;
color:#33e;
}
</style>
</head>
<body>
<div class="content" id="status0content"></div>
<div class="content" id="status1content"></div>
</body>
<script src="js/jquery.min.js"></script>
<script src="layer/layer.js"></script>
<script>
$(function () {
$.getJSON("/wx/findExpressByUserPhone.do", null, function (data) {
// [{"id":19,"number":"116","username":"温迪","userphone":"18856359611","company":"京东快递","code":"133009","intime":"2021-09-09 11:01:34","outtime":"未出库","status":"待取件","sysPhone":"18888888888"}]
var data0 = data.data.status0;
for (i = 0; i < data0.length; i++) {
var item = "\t\t<div class=\"item\">\n" +
"\t\t\t<div class=\"imte_top\"><span>" + data0[i].intime + "</span></div>\n" +
"\t\t\t<div class=\"item_content\">\n" +
"\t\t\t\t<div class=\"item_content_top\">\n" +
"\t\t\t\t\t<div class=\"item_content_top_1\">取件通知</div>\n" +
"\t\t\t\t\t<div class=\"item_content_top_2\">" + data0[i].intime + "</div>\n" +
"\t\t\t\t\t<div class=\"item_content_top_3\">您有一个包裹到e栈了!</div>\n" +
"\t\t\t\t\t<div class=\"item_content_top_4\">\n" +
"\t\t\t\t\t\t取件码:<span style=\"color:#05a\">" + data0[i].code + "</span><br>\n" +
"\t\t\t\t\t\t快递公司:" + data0[i].company + "<br>\n" +
"\t\t\t\t\t\t运单号码:" + data0[i].number + "<br>\n" +
"\t\t\t\t\t\t站点电话:" + data0[i].sysPhone + "<br>\n" +
"\t\t\t\t\t</div>\n" +
"\t\t\t\t</div>\n" +
"\t\t\t\t<hr>\n" +
"\t\t\t\t<div class=\"item_content_bottom\">\n" +
"\t\t\t\t\t<a href=\"/wx/createQRCode.do?type=express&code=" + data0[i].code + "\">二维码</a>\n" +
"\t\t\t\t</div>\n" +
"\t\t\t</div>\n" +
"\t\t</div>";
$("#status0content").append($(item));
}
// [{"id":16,"number":"113","username":"温迪","userphone":"18856359611","company":"京东快递","code":"133006","intime":"2021-09-02 10:59:47","outtime":"2021-09-04 10:59:52","status":"已取件","sysPhone":"18888888888"}]
var data1 = data.data.status1;
for(i=0;i<data1.length;i++){
var item = "<div class=\"item\">\n" +
"\t\t\t<div class=\"imte_top\"><span>"+data1[i].outtime+"</span></div>\n" +
"\t\t\t<div class=\"item_content\">\n" +
"\t\t\t\t<div class=\"item_content_top\">\n" +
"\t\t\t\t\t<div class=\"item_content_top_1\">取件成功通知</div>\n" +
"\t\t\t\t\t<div class=\"item_content_top_2\">"+data1[i].outtime+"</div>\n" +
"\t\t\t\t\t<div class=\"item_content_top_3\">您有一个包裹从e栈取出了!</div>\n" +
"\t\t\t\t\t<div class=\"item_content_top_4\">\n" +
"\t\t\t\t\t\t快递公司:"+data1[i].company+"<br>\n" +
"\t\t\t\t\t\t运单号码:"+data1[i].number+"<br>\n" +
"\t\t\t\t\t\t站点电话:"+data1[i].sysPhone+"<br>\n" +
"\t\t\t\t\t</div>\n" +
"\t\t\t\t</div>\n" +
"\t\t\t\t<hr>\n" +
"\t\t\t\t<div class=\"item_content_bottom\">\n" +
"\t\t\t\t\t<a href=\"#\">有疑问 ? 点了也没用</a>\n" +
"\t\t\t\t</div>\n" +
"\t\t\t</div>\n" +
"\t\t</div>";
$("#status1content").append($(item));
}
});
});
</script>
</html>
章节汇总在这里(づ ̄3 ̄)づ╭❤~@&再见萤火虫&【07-项目训练】
对学习Java感兴趣的同学欢迎加入QQ学习交流群:1126298731
有问题欢迎提问,大家一起在学习Java的路上打怪升级!(o゜▽゜)o☆[BINGO!]