Commit 729e0e2e authored by Quxl's avatar Quxl

增加会员登陆

parent e32f2d82
package com.egolm.film.login;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.egolm.common.bean.Rjx;
import com.egolm.film.config.XException;
import com.egolm.film.login.service.MemberTokenService;
import com.egolm.film.model.LoginToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@Api
@Controller
@RequestMapping("member")
public class MemberLoginController {
@Autowired
private MemberTokenService tokenService;
@ResponseBody
@PostMapping("login")
@ApiOperation("fc_user登陆")
public Object login(String username, String password) {
if(tokenService.isLogin()) {
throw new XException("用户已登录", 200);
} else {
LoginToken token = tokenService.doLogin(username, password);
if(token != null) {
return Rjx.jsonOk().setData(token);
} else {
throw new XException("用户名或密码错误");
}
}
}
@ResponseBody
@PostMapping("logout")
@ApiOperation("fc_user退出")
public Object logout() {
tokenService.doLogout();
return Rjx.jsonOk();
}
}
package com.egolm.film.login.service;
public interface MemberTokenService extends TokenService {
final String TOKEN_NAME = "FILM_MEMBER_LOGIN_TOKEN_NAME";
}
package com.egolm.film.login.service.impl;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.egolm.common.StringUtil;
import com.egolm.common.jdbc.JdbcTemplate;
import com.egolm.film.config.WebMvcConfig;
import com.egolm.film.config.XException;
import com.egolm.film.login.service.MemberTokenService;
import com.egolm.film.model.LoginToken;
@Service
public class MemberTokenServiceImpl implements MemberTokenService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public LoginToken getToken() {
HttpSession session = WebMvcConfig.getSession();
LoginToken token = (LoginToken)session.getAttribute(TOKEN_NAME);
return token;
}
@Override
public LoginToken doLogin(String username, String password) {
HttpSession session = WebMvcConfig.getSession();
String md5String = StringUtil.toMD5HexString(password);
String loginSql = "select * from fc_member where username = ? and password = ?";
List<Map<String, Object>> list = this.jdbcTemplate.queryForList(loginSql, username, md5String);
if(list != null) {
if(list.size() == 1) {
Map<String,Object> map = list.get(0);
Integer state = (Integer)map.get("state");
if(state == 1) {
Integer adminid = (Integer)map.get("adminid");
LoginToken token = new LoginToken(adminid);
session.setAttribute(TOKEN_NAME, token);
return token;
} else if(state == 2) {
throw new XException("用户已禁用");
} else {
throw new XException("用户状态未知");
}
} else if(list.size() == 0) {
throw new XException("用户名或密码错误");
} else {
throw new XException("用户登陆信息重复,请联系管理员");
}
} else {
throw new XException("用户名或密码错误");
}
}
@Override
public void doLogout() {
HttpSession session = WebMvcConfig.getSession();
session.removeAttribute(TOKEN_NAME);
session.invalidate();
}
@Override
public boolean isLogin() {
return this.getToken() != null;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment