发表于: 2017-07-12 23:23:30

3 1258


今天完成的事:

1、将登陆接口的cookie部分加上了并测试OK。(计划总是赶不上变化,昨天本计划将后台管理的接口写完的)

完成了以下逻辑:后台管理员正确登陆后,对其ID和当前时间进行des加密存入COOKIE中,

                           新增账户时,对密码进行MD5加密存入数据库,并且在浏览器请求中拿到cookie解密取出管理员ID存放在createBy,和updateBy中,

代码如下:

cookie工具类
    /**
    * cookie中取得当前登录的adminID
    *
    * @param request
    * @return
    * @throws Exception
    */
   public static Long getAdminId(HttpServletRequest request) throws Exception {
//        int id =0;
       String id = "";
       Cookie[] cookies = request.getCookies();
       if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("carrots-zangai-admin")) {
String token = cookie.getValue();
                   log.info("cookie中的token进行解密中");
                   byte[] tk1 = DesUtil.decrypt(TypeUtil.hexStringToByte(token), "12345678");
                   String tk2 = new String(tk1);
                   String time = "";
                   log.info("遍历解密后的字符串,得到ID");
                   for (int j = 0; j < tk2.length(); j++) {
char c = tk2.charAt(j);
                       if (c == '=') {
//                            for (int k = j + 1; k < tk2.length(); k++) {
//                                time = time + tk2.charAt(k);
//                            }
                           log.info(" 截取后的ID值为:" + id);
                           return Long.parseLong(id);
                       }
id = id + c;
                   }
}
}
}
return Long.parseLong(id);
   }

/**
    * 将传入的ID和系统当前时间进行加密存入COOKIE
    *
    * @param request
    * @param response
    * @param id
    * @return
    * @throws Exception
    */
   public static void addCookie(HttpServletRequest request,
                                HttpServletResponse response,
                                Long id) throws Exception {

String source = id + "=" + System.currentTimeMillis();
       //ID和当前时间进行加密 生成token
       String token = TypeUtil.bytesToHexString(DesUtil.encrypt(source, "12345678"));
       //token放入cookie中。
       Cookie[] cookies = request.getCookies();
       if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("carrots-zangai-admin")) {
cookie.setValue(token);
                   cookie.setMaxAge(60 * 60 * 24);
                   response.addCookie(cookie);
               } else {
Cookie adminId = new Cookie("carrots-zangai-admin", token);
                   adminId.setMaxAge(60 * 60 * 24);
                   response.addCookie(adminId);
               }
}
} else {
Cookie adminId = new Cookie("carrots-zangai-admin", token);
           adminId.setMaxAge(60 * 60 * 24);
           response.addCookie(adminId);
       }

}

控制器

/**
* 登录接口
*
* @param request
* @param response
* @param model
* @param account
* @param password
* @return
* @throws Exception
*/
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(HttpServletRequest request,
                   HttpServletResponse response,
                   ModelMap model,
                   String account,
                   String password) throws Exception {
log.info("loginParam=account:" + account + ",pwd:" + password);
   if (account == null || account.equals("") || password == null || password.equals("")) {
log.info("名字或密码为空");
       model.addAttribute("code", -10);
   } else {

Map<String, Object> sql = DynamicUtil.getUserByAccountSql(account);
       log.info("sql" + sql);
       List<Long> ids = null;
       try {
ids = userService.getIdsByDynamicCondition(User.class, sql, 0, Integer.MAX_VALUE);
       } catch (Throwable t) {
t.printStackTrace();
           log.error("系统异常" + t.getMessage());
           model.addAttribute("code", -100000);
           return "/user/json/userDetailJson";
       }
if (ids != null && !ids.isEmpty() && ids.size() < 2) {
log.info("数据取得数据成功" + ids);
           try {
User user = userService.getObjectById(ids.get(0));
               if (user.getPassword().equals(MD5Util.stringToMD5(password))) {//密码相同,登录成功。
                   log.info("登录成功:id=" + user.getId());
                   //ID+当前系统时间存入cookie
                   CookieUtil.addCookie(request, response, ids.get(0));
                   model.addAttribute("code", 0);
                   model.addAttribute("user", user);
                   return "/user/json/userDetailJson";
               } else {
//密码错误
                   log.info("登录的密码错误:登录名:" + account);
                   model.addAttribute("code", -12);
               }
} catch (Throwable t) {
//系统异常
               t.printStackTrace();
               log.error("系统异常:" + t.getMessage());
               model.addAttribute("code", -100000);
           }
} else {
//用户不存在
           log.info("该用户不存在");
           model.addAttribute("code", -13);
       }
}
return "/user/json/userDetailJson";
}


/**
* 新增账户user
*
* @param request
* @param response
* @param model
* @param user
* @return
*/
@RequestMapping(value = "/a/u/user", method = RequestMethod.POST)
public String insertUser(HttpServletRequest request,
                        HttpServletResponse response,
                        ModelMap model,
                        User user) throws Exception {
log.info("insertUser-user:" + user);
   user.setId(null);
   //对密码用MD5加密后保存在user
   user.setPassword(MD5Util.stringToMD5(user.getPassword()));
   //cookie中的萝卜多后台adminid设置为创建人和更新人
   Long adminId = CookieUtil.getAdminId(request);
   user.setCreateBy(adminId);
   user.setCreateBy(adminId);

   if (user.getPassword() != null && user.getAccount() != null) {
Map<String, Object> sql = DynamicUtil.getUserByAccountSql(user.getAccount());
       log.info("sql" + sql);
       List<Long> ids = null;
       //查看数据库中是否存在同名的数据
       try {
ids = userService.getIdsByDynamicCondition(User.class, sql, 0, Integer.MAX_VALUE);
           log.info("================ids:" + ids);
       } catch (Throwable t) {
t.printStackTrace();
           log.error("系统异常:" + t.getMessage());
           model.addAttribute("code", -100000);
       }
//如果有同名的数据返回用户名已存在
       if (ids != null && !ids.isEmpty()) {

log.error("用户名已存在");
           model.addAttribute("code", -11);
       } else {
//没有则将用户插入数据库
           try {
Long id = userService.insert(user);
               log.info("新增用户成功!");
               model.addAttribute("code", 0);
           } catch (Throwable t) {
t.printStackTrace();
               log.error("新增账户失败," + t.getMessage());
               model.addAttribute("code", -100000);
               return "/user/json/userDetailJson";
           }
}
} else {
log.error("用户名或密码不能为空");
       model.addAttribute("code", -10);
   }
return "/user/json/userDetailJson";
}

2、听老大讲了支付的过程

  订单的状态:
1。待支付
    1.  支付成功
    2. 支付失败
    3. 已支付,待确认
  5.已取消
  6.已过期
   7.无效订单
   1.提交订单
          A 商品ID,数量
          B  商品ID,数量        
                 发货地址:

         支付方式:

       后端人员收到请求之后要创建订单:
         要把商品的所有属性(名称,价格)存到订单表里
   2.支付回调接
      收到支付回调接口之后,要去支付平台重新确认订单状态。
      如果收到重复订单,直接放弃
     3.用户查询支付状态接口   
     4.重新支付接口

明天计划的事:写后台接口

遇到的问题:加密解密的东西忘的差不多了(写cookie工具,和调试花了很长的时间)

收获:支付开发思路,重拾cookie的用法。

总结:好好学习


返回列表 返回列表
评论

    分享到