Compare commits

...

4 Commits

Author SHA1 Message Date
刘宇航 6f2ae1a92f 注释
10 months ago
方兆鑫 6999138b9d Changes
10 months ago
方兆鑫 48effa5c18 Changes
10 months ago
方兆鑫 05367b1c2a Changes
10 months ago

@ -0,0 +1,99 @@
package com.wsk.controller;
import com.wsk.pojo.UserInformation;
import com.wsk.pojo.UserPassword;
import com.wsk.response.BaseResponse;
import com.wsk.service.UserInformationService;
import com.wsk.service.UserPasswordService;
import com.wsk.tool.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* Created by wsk1103 on 2017/5/9.
*/
@RestController
public class ForgetController {
@Resource
private UserPasswordService userPasswordService;
@Resource
private UserInformationService userInformationService;
@RequestMapping(value = "checkCode.do", method = {RequestMethod.POST, RequestMethod.GET})
public Map checkPhone(HttpServletRequest request, Model model,
@RequestParam String code, @RequestParam String token) {
Map<String, Integer> map = new HashMap<>();
String name = request.getParameter("name");
if (!StringUtils.getInstance().isNullOrEmpty(name)) {
request.getSession().setAttribute("name", name);
}
String checkCodeToken = (String) request.getSession().getAttribute("token");
if (StringUtils.getInstance().isNullOrEmpty(checkCodeToken) || !checkCodeToken.equals(token)) {
map.put("result", 0);
return map;
}
//验证码错误
if (!checkCodePhone(code, request)) {
map.put("result", 0);
return map;
}
map.put("result", 1);
return map;
}
//更新密码
@RequestMapping("updatePassword.do")
public BaseResponse updatePassword(HttpServletRequest request, Model model,
@RequestParam String password, @RequestParam String token) {
//防止重复提交
String updatePasswordToken = (String) request.getSession().getAttribute("token");
if (StringUtils.getInstance().isNullOrEmpty(updatePasswordToken) || !updatePasswordToken.equals(token)) {
return BaseResponse.fail();
}
String realPhone = (String) request.getSession().getAttribute("phone");
UserPassword userPassword = new UserPassword();
String newPassword = StringUtils.getInstance().getMD5(password);
int uid;
try {
uid = userInformationService.selectIdByPhone(realPhone);
if (uid == 0) {
return BaseResponse.fail();
}
} catch (Exception e) {
e.printStackTrace();
return BaseResponse.fail();
}
int id = userPasswordService.selectByUid(uid).getId();
userPassword.setId(id);
userPassword.setUid(uid);
userPassword.setModified(new Date());
userPassword.setPassword(newPassword);
int result;
try {
result = userPasswordService.updateByPrimaryKeySelective(userPassword);
} catch (Exception e) {
return BaseResponse.fail();
}
//更新失败
if (result != 1) {
return BaseResponse.fail();
}
UserInformation userInformation = userInformationService.selectByPrimaryKey(uid);
request.getSession().setAttribute("userInformation", userInformation);
return BaseResponse.success();
}
//check the phone`s code
private boolean checkCodePhone(String codePhone, HttpServletRequest request) {
String trueCodePhone = "12251103";
return codePhone.equals(trueCodePhone);
}
}

@ -0,0 +1,241 @@
package com.wsk.controller;
import com.wsk.bean.ShopInformationBean;
import com.wsk.pojo.*;
import com.wsk.service.*;
import com.wsk.tool.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by wsk1103 on 2017/5/11.
*/
@Controller
public class HomeController {
@Resource
private ShopInformationService shopInformationService;
@Resource
private SpecificeService specificeService;
@Resource
private ClassificationService classificationService;
@Resource
private AllKindsService allKindsService;
@Resource
private ShopContextService shopContextService;
@RequestMapping(value = {"/", "/home.do"})
public String home(HttpServletRequest request, Model model) {
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
// if user login,the session will have the "userInformation"
if (!StringUtils.getInstance().isNullOrEmpty(userInformation)) {
model.addAttribute("userInformation", userInformation);
} else {
userInformation = new UserInformation();
model.addAttribute("userInformation", userInformation);
}
//一般形式进入首页
try {
List<ShopInformation> shopInformations = selectTen(1, 5);
List<ShopInformationBean> list = new ArrayList<>();
int counts = getShopCounts();
model.addAttribute("shopInformationCounts", counts);
String stringBuffer;
for (ShopInformation shopInformation : shopInformations) {
stringBuffer = getSortName(shopInformation.getSort());
ShopInformationBean shopInformationBean = new ShopInformationBean();
shopInformationBean.setId(shopInformation.getId());
shopInformationBean.setName(shopInformation.getName());
shopInformationBean.setLevel(shopInformation.getLevel());
shopInformationBean.setPrice(shopInformation.getPrice().doubleValue());
shopInformationBean.setRemark(shopInformation.getRemark());
shopInformationBean.setSort(stringBuffer);
shopInformationBean.setQuantity(shopInformation.getQuantity());
shopInformationBean.setUid(shopInformation.getUid());
shopInformationBean.setTransaction(shopInformation.getTransaction());
shopInformationBean.setImage(shopInformation.getImage());
list.add(shopInformationBean);
}
model.addAttribute("shopInformationBean", list);
} catch (Exception e) {
e.printStackTrace();
return "page/login_page";
}
return "index";
}
//进入商城
@RequestMapping(value = "/mall_page.do")
public String mallPage(HttpServletRequest request, Model model) {
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
userInformation = new UserInformation();
model.addAttribute("userInformation", userInformation);
} else {
model.addAttribute("userInformation", userInformation);
}
try {
List<ShopInformation> shopInformations = selectTen(1, 12);
List<ShopInformationBean> list = new ArrayList<>();
int counts = getShopCounts();
model.addAttribute("shopInformationCounts", counts);
String sortName;
for (ShopInformation shopInformation : shopInformations) {
int sort = shopInformation.getSort();
sortName = getSortName(sort);
ShopInformationBean shopInformationBean = new ShopInformationBean();
shopInformationBean.setId(shopInformation.getId());
shopInformationBean.setName(shopInformation.getName());
shopInformationBean.setLevel(shopInformation.getLevel());
shopInformationBean.setRemark(shopInformation.getRemark());
shopInformationBean.setPrice(shopInformation.getPrice().doubleValue());
shopInformationBean.setSort(sortName);
shopInformationBean.setQuantity(shopInformation.getQuantity());
shopInformationBean.setTransaction(shopInformation.getTransaction());
shopInformationBean.setUid(shopInformation.getUid());
shopInformationBean.setImage(shopInformation.getImage());
list.add(shopInformationBean);
}
model.addAttribute("shopInformationBean", list);
} catch (Exception e) {
e.printStackTrace();
return "page/login_page";
}
return "page/mall_page";
}
//通过分类的第三层id获取全名
private String getSortName(int sort) {
StringBuilder stringBuffer = new StringBuilder();
Specific specific = selectSpecificBySort(sort);
int cid = specific.getCid();
Classification classification = selectClassificationByCid(cid);
int aid = classification.getAid();
AllKinds allKinds = selectAllKindsByAid(aid);
stringBuffer.append(allKinds.getName());
stringBuffer.append("-");
stringBuffer.append(classification.getName());
stringBuffer.append("-");
stringBuffer.append(specific.getName());
// System.out.println(sort);
return stringBuffer.toString();
}
//获得分类中的第一层
@RequestMapping(value = "/getAllKinds.do")
@ResponseBody
public List<AllKinds> getAllKind() {
return getAllKinds();
}
//获得分类中的第二层通过第一层的id
@RequestMapping(value = "/getClassification.do", method = RequestMethod.POST)
@ResponseBody
public List<Classification> getClassificationByAid(@RequestParam int id) {
return selectAllClassification(id);
}
//通过第二层的id获取对应的第三层
@RequestMapping(value = "/getSpecific.do")
@ResponseBody
public List<Specific> getSpecificByCid(@RequestParam int id) {
return selectAllSpecific(id);
}
//get the shops counts
@RequestMapping(value = "/getShopsCounts.do")
@ResponseBody
public Map getShopsCounts() {
Map<String, Integer> map = new HashMap<>();
int counts = 0;
try {
counts = shopInformationService.getCounts();
} catch (Exception e) {
e.printStackTrace();
map.put("counts", counts);
return map;
}
map.put("counts", counts);
return map;
}
@RequestMapping(value = "/getShops.do")
@ResponseBody
public List getShops(@RequestParam int start) {
List<ShopInformation> list = new ArrayList<>();
try {
int end = 12;
list = selectTen(start, end);
} catch (Exception e) {
e.printStackTrace();
return list;
}
return list;
}
//获取商品,分页,一次性获取end个
private List<ShopInformation> selectTen(int start, int end) {
Map map = new HashMap();
map.put("start", (start - 1) * end);
map.put("end", end);
List<ShopInformation> list = shopInformationService.selectTen(map);
return list;
}
//获取最详细的分类,第三层
private Specific selectSpecificBySort(int sort) {
return specificeService.selectByPrimaryKey(sort);
}
//获得第二层分类
private Classification selectClassificationByCid(int cid) {
return classificationService.selectByPrimaryKey(cid);
}
//获得第一层分类
private AllKinds selectAllKindsByAid(int aid) {
return allKindsService.selectByPrimaryKey(aid);
}
//获得第一层所有
private List<AllKinds> getAllKinds() {
return allKindsService.selectAll();
}
//根据第一层的id获取该层下的第二层
private List<Classification> selectAllClassification(int aid) {
return classificationService.selectByAid(aid);
}
//根据第二层的id获取其对应的第三层所有id
private List<Specific> selectAllSpecific(int cid) {
return specificeService.selectByCid(cid);
}
//获得商品总页数
private int getShopCounts() {
return shopInformationService.getCounts();
}
//获得商品留言总页数
private int getShopContextCounts(int sid) {
return shopContextService.getCounts(sid);
}
//获得商品留言10条
private List<ShopContext> selectShopContextBySid(int sid, int start) {
return shopContextService.findById(sid, (start - 1) * 10);
}
}

@ -0,0 +1,79 @@
package com.wsk.controller.webSocket;
import com.wsk.tool.SaveSession;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import java.io.IOException;
import java.util.*;
/**
* Created by wsk1103 on 2017/5/22.
*/
public class ChatWebSocketHandler extends TextWebSocketHandler {
private final static List<WebSocketSession> sessions = Collections.synchronizedList(new ArrayList<WebSocketSession>());
//接收文本消息,并发送出去
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) {
// System.out.println(session.getId()+":send....");
// chatTextMessageHandler(message.getPayload());
try {
// super.handleTextMessage(session, message);
// System.out.println(session.getId()+" :"+message.getPayload() + " " + new Date());
String m = message.getPayload();
String[] wsk = m.split(",");
String phone = wsk[0];
long time = Long.parseLong(wsk[1]);
String action = wsk[2];
if (action.equals("start")){
session.sendMessage(new TextMessage("success"));
SaveSession.getInstance().save(phone,time);
return;
}
boolean b = SaveSession.getInstance().isHave(phone,time);
if (b) {
if (session.isOpen()) {
session.sendMessage(new TextMessage("error"));
}
} else {
if (session.isOpen())
session.sendMessage(new TextMessage("success"));
}
} catch (Exception e) {
e.printStackTrace();
try {
session.sendMessage(new TextMessage("error"));
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
//连接建立后处理
@SuppressWarnings("unchecked")
@Override
public void afterConnectionEstablished(WebSocketSession session) {
sessions.add(session);
//处理离线消息
}
//抛出异常时处理
@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
if (session.isOpen()) {
session.close();
}
sessions.remove(session);
}
//连接关闭后处理
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) {
sessions.remove(session);
}
}
Loading…
Cancel
Save