Search in sources :

Example 1 with E3Result

use of cn.e3mall.common.pojo.E3Result in project e3mall by colg-cloud.

the class TbUserController method login.

/**
 * 登录
 *
 * 登录成功后,把token写入cookie
 *
 * @param username
 * @param password
 * @return
 */
@PostMapping("/login")
public E3Result login(String username, String password, HttpServletRequest request, HttpServletResponse response) {
    E3Result e3Result = tbUserService.userLogin(username, password);
    // 判断是否登录成功
    if (e3Result.isSuccess()) {
        String token = e3Result.getData().toString();
        // 如果登录成功需要把token写入cookie
        CookieUtils.setCookie(request, response, TOKEN_KEY, token);
    }
    return e3Result;
}
Also used : E3Result(cn.e3mall.common.pojo.E3Result) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 2 with E3Result

use of cn.e3mall.common.pojo.E3Result in project e3mall by colg-cloud.

the class LoginInterceptor method preHandle.

/**
 * 拦截处理程序的执行。在HandlerMapping之后调用确定了合适的处理程序对象,但在HandlerAdapter调用处理程序之前调用。
 */
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    /*
		 * return true: 放行, false: 拦截
		 * 1. 从cookie中取token
		 * 		没有token:	未登录状态, 直接放行
		 * 		有token:	
		 * 				1). 调用sso系统的服务,根据token取用户信息
		 * 					没有用户信息:	登录过期, 直接放行
		 * 					有用户信息:		登录状态, 把用户信息放到request中, 只需要在controller中判断request中是否包含user信息.放行
		 */
    // 从cookie中取token
    String token = CookieUtils.getCookieValue(request, TOKEN_KEY);
    // 如果没有token,直接放行
    if (StringUtils.isBlank(token)) {
        return true;
    }
    // 取得token, 调用sso系统服务,根据token取用户信息
    E3Result e3Result = tokenService.getUserByToken(token);
    if (!e3Result.isSuccess()) {
        // 没有用户信息
        return true;
    }
    // 取得用户信息
    TbUser tbUser = (TbUser) e3Result.getData();
    request.setAttribute("user", tbUser);
    return true;
}
Also used : E3Result(cn.e3mall.common.pojo.E3Result) TbUser(cn.e3mall.manager.pojo.TbUser)

Example 3 with E3Result

use of cn.e3mall.common.pojo.E3Result in project e3mall by colg-cloud.

the class TokenController method getUserByToken2.

/**
 * 根据token获取user信息
 *
 * 由于使用了fastjson框架解析,直接返回String,会自动拼上双引号,js认不出此对象,所以 直接用response回显回去,绕过fastjson
 *
 * "test123({\"data\":{\"created\":1521914815000,\"id\":7,\"phone\":\"12344444443\",\"updated\":1521914815000,\"username\":\"Jack\"},\"msg\":\"OK\",\"status\":200,\"success\":true});"
 *
 * 此格式不行
 *
 * @param token
 * @param callback
 *            jsonp 跨域请求
 * @return
 * @throws IOException
 */
@GetMapping(value = "/user2/{token}")
public void getUserByToken2(@PathVariable String token, String callback, HttpServletResponse response) {
    E3Result e3Result = tokenService.getUserByToken(token);
    PrintWriter out = null;
    try {
        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        out = response.getWriter();
        // 响应结果之前,判断是否为jsonp请求, jsonp会自动带 callback
        if (StringUtils.isNotBlank(callback)) {
            // 把结果封装成一个js语句响应
            String jsonString = callback + "(" + JSON.toJSONString(e3Result) + ");";
            out.write(jsonString);
        } else {
            out.write(JSON.toJSONString(e3Result));
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(out);
    }
}
Also used : E3Result(cn.e3mall.common.pojo.E3Result) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 4 with E3Result

use of cn.e3mall.common.pojo.E3Result in project e3mall by colg-cloud.

the class TokenController method getUserByToken.

/**
 * 根据token获取user信息
 *
 * 使用 fastjson JSONPObject对象处理
 *
 * @param token
 * @param callback
 *            跨域请求
 * @return
 */
@GetMapping(value = "/user/{token}")
public Object getUserByToken(@PathVariable String token, String callback) {
    E3Result e3Result = tokenService.getUserByToken(token);
    // 响应结果之前,判断是否为jsonp请求, jsonp会自动带 callback
    if (StringUtils.isNotBlank(callback)) {
        // 创建jsonp对象
        JSONPObject jsonpObject = new JSONPObject(callback);
        jsonpObject.addParameter(e3Result);
        return jsonpObject;
    }
    return e3Result;
}
Also used : E3Result(cn.e3mall.common.pojo.E3Result) JSONPObject(com.alibaba.fastjson.JSONPObject) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 5 with E3Result

use of cn.e3mall.common.pojo.E3Result in project e3mall by colg-cloud.

the class OrderController method createOrderInfo.

/**
 * 提交订单
 *
 * @param tbUser
 *            request里取得tbUser
 * @param payment
 *            传值给request
 * @param orderInfo
 *            订单信息
 * @return
 */
@PostMapping("/create")
public String createOrderInfo(@RequestAttribute(value = "user") TbUser tbUser, Model model, OrderInfo orderInfo) {
    // 生成订单
    E3Result e3Result = orderService.createOrder(orderInfo, tbUser);
    model.addAttribute("orderId", e3Result.getData());
    model.addAttribute("payment", orderInfo.getPayment());
    // 清空当前用户的购物车
    cartService.deleteAllCart(tbUser.getId());
    // 返回结算成功页面
    return "success";
}
Also used : E3Result(cn.e3mall.common.pojo.E3Result) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

E3Result (cn.e3mall.common.pojo.E3Result)6 TbUser (cn.e3mall.manager.pojo.TbUser)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 TbItem (cn.e3mall.manager.pojo.TbItem)1 JSONPObject (com.alibaba.fastjson.JSONPObject)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1