Authored by fangyeqing

ADD:add register

... ... @@ -69,6 +69,12 @@
<artifactId>fastjson</artifactId>
<version>1.2.9</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
<build>
... ...
... ... @@ -42,6 +42,7 @@ public class LogAspect {
}else{
sb.append(",");
}
if(obj!=null)
sb.append(obj.toString());
}
}
... ...
... ... @@ -41,7 +41,7 @@ public class CurrentAdminMethodArgumentResolver implements HandlerMethodArgument
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
//取出鉴权时存入的登录用户Id
String currentAdminId = ((String) webRequest.getAttribute(Constants.CURRENT_USER_ID, RequestAttributes.SCOPE_REQUEST)).
replace(UpSoftAccountController.UPSOFT_TOKEN_PREFIX, "").replace(AdminAccountController.USPIH_TOKEN_PREFIX, "");
replace(UpSoftAccountController.UPSOFT_TOKEN_PREFIX, "").replace(AdminAccountController.USPIH_TOKEN_PREFIX, "").replace(Constants.ADMIN_TOKEN_PREFIX,"");
if (currentAdminId != null) {
//从数据库中查询并返回
XklAdminEntity admin = adminRepository.findByIdAndStatus(Long.parseLong(currentAdminId), Constants.STATUS_OK);
... ...
... ... @@ -3,6 +3,7 @@ package com.xkl.config;
import com.xkl.domain.XklAmpReportCategoryEntity;
import com.xkl.domain.XklAmpReportMetaItemsEntity;
import com.xkl.domain.XklAmpReportMetaScoreStandardEntity;
import com.xkl.model.CityModel;
import java.util.HashMap;
import java.util.Map;
... ... @@ -42,6 +43,19 @@ public interface Constants {
* 分年龄段平均得分
*/
Map<String,Double> aveScoreMap = new HashMap<>();
/**
* 身份证前六位-省市区
*/
Map<String,CityModel> cityMap = new HashMap<>();
int ID_TYPE_COUNTRY = 2;
int ID_TYPE_PROVINCE = 3;
int ID_TYPE_CITY = 4;
int ID_TYPE_COUNTY = 5;
/**
* admin-token前缀
*/
String ADMIN_TOKEN_PREFIX = "ADMINTOKENPREFIX";
public static final int MALE = 0;
public static final int FEMALE = 1;
... ...
package com.xkl.config;
import com.xkl.domain.XklAmpReportAverageScoreEntity;
import com.xkl.domain.XklAmpReportCategoryEntity;
import com.xkl.domain.XklAmpReportMetaItemsEntity;
import com.xkl.domain.XklAmpReportMetaScoreStandardEntity;
import com.xkl.repository.XklAmpReportAveScoreRespository;
import com.xkl.repository.XklAmpReportCategoryRespository;
import com.xkl.repository.XklAmpReportMetaItemsRespository;
import com.xkl.repository.XklAmpReportMetaScoreStandardRespository;
import com.xkl.domain.*;
import com.xkl.model.CityModel;
import com.xkl.repository.*;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Iterator;
import java.util.*;
/**
... ... @@ -31,6 +26,8 @@ public class ScheduledTask implements Constants{
private XklAmpReportCategoryRespository reportCategoryRespository;
@Autowired
private XklAmpReportAveScoreRespository reportAveScoreRespository;
@Autowired
private XklCityRespository xklCityRespository;
@Scheduled(initialDelay = 0,fixedRate = 24*60*60*1000)
public void getTableMap(){
... ... @@ -72,4 +69,68 @@ public class ScheduledTask implements Constants{
aveScoreMap.put(ageAndType,reportCategory.getAverageScore());
}
}
@Scheduled(initialDelay = 0,fixedRate = 24*60*60*1000)
public void getCityMap(){
log.info("Load City Table");
Map<Long,XklCityEntity> idCityMap = new HashMap<>();
Iterator<XklCityEntity> iterator = xklCityRespository.findAll().iterator();
while(iterator.hasNext()){
XklCityEntity city = iterator.next();
idCityMap.put(city.getId(),city);
}
for (Map.Entry<Long, XklCityEntity> entry : idCityMap.entrySet()) {
long id = entry.getKey();
XklCityEntity xklCity = entry.getValue();
long parentId = xklCity.getParentId();
List<Long> idList = new ArrayList<>();
idList.add(id);
idList.add(parentId);
getIdList(parentId,idCityMap,idList);
CityModel cityModel = new CityModel();
if(idList.size()== Constants.ID_TYPE_COUNTRY){//国家级:中国000000
cityModel.setType(Constants.ID_TYPE_COUNTRY);
cityModel.setCountry(idList.get(0));
}else if(idList.size() == Constants.ID_TYPE_PROVINCE){//省级:北京110000,河北130000
cityModel.setType(Constants.ID_TYPE_PROVINCE);
cityModel.setCountry(idList.get(1));
cityModel.setProvince(idList.get(0));
}else if(idList.size() == Constants.ID_TYPE_CITY){//市级:东城区110101,石家庄市130100
cityModel.setType(Constants.ID_TYPE_CITY);
cityModel.setCountry(idList.get(2));
cityModel.setProvince(idList.get(1));
cityModel.setCity(idList.get(0));
}else if(idList.size() == Constants.ID_TYPE_COUNTY){//区级:石家庄市长安区130102
cityModel.setType(Constants.ID_TYPE_COUNTY);
cityModel.setCountry(idList.get(3));
cityModel.setProvince(idList.get(2));
cityModel.setCity(idList.get(1));
cityModel.setCounty(idList.get(0));
}else{
cityModel.setType(Constants.ID_TYPE_COUNTRY);
cityModel.setCountry(idList.get(0));
}
cityMap.put(xklCity.getIdCardCode(),cityModel);
}
log.info("load city table success");
}
/**
* 递归找parent,直到中国
* @param parentId
* @param idCityMap
* @param idList
*/
public void getIdList(long parentId,Map<Long,XklCityEntity> idCityMap,List<Long> idList){
if(parentId == 0) return;
XklCityEntity city = idCityMap.get(parentId);
if(city!=null){
long grandParentId = city.getParentId();
idList.add(grandParentId);
getIdList(grandParentId,idCityMap,idList);
}else{
return;
}
}
}
... ...
... ... @@ -3,14 +3,13 @@ package com.xkl.controller;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xkl.authorization.annotation.Authorization;
import com.xkl.authorization.annotation.CurrentUser;
import com.xkl.authorization.annotation.LogAnnotation;
import com.xkl.authorization.annotation.Sign;
import com.xkl.authorization.annotation.*;
import com.xkl.authorization.manager.ITokenManager;
import com.xkl.authorization.model.TokenModel;
import com.xkl.config.Constants;
import com.xkl.config.ResultStatus;
import com.xkl.domain.User;
import com.xkl.domain.XklAdminEntity;
import com.xkl.model.ResultModel;
import com.xkl.security.AntiXSS;
import com.xkl.service.ILoginService;
... ... @@ -76,4 +75,43 @@ public class TokenController {
return new ResponseEntity<>(ResultModel.ok(ResultStatus.USER_LOGOUT), HttpStatus.OK);
}
@LogAnnotation
@AntiXSS
@Sign
@RequestMapping(value = "/admin", method = RequestMethod.POST)
@ApiOperation(value = "管理员登录接口")
public ResponseEntity<ResultModel> adminLogin(HttpServletRequest request, @RequestParam String username, @RequestParam String password,
@RequestParam String sign, @RequestParam long t, @RequestParam int type) {
if (!(boolean) request.getAttribute("signAspect"))
return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK);
Assert.notNull(username, "username can not be empty");
Assert.notNull(password, "password can not be empty");
XklAdminEntity admin = loginService.checkAdmin(username, password);
if (admin == null) {//用户,密码错误
return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.OK);
}
//生成一个token,保存用户登录状态
TokenModel model = tokenManager.createToken(Constants.ADMIN_TOKEN_PREFIX + String.valueOf(admin.getId()));
return new ResponseEntity<>(ResultModel.ok(model), HttpStatus.OK);
}
@LogAnnotation
@AntiXSS
@Authorization
@Sign
@RequestMapping(value = "/admin",method = RequestMethod.DELETE)
@ApiOperation(value = "Admin logout")
@ApiImplicitParams({
@ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"),
})
public ResponseEntity<ResultModel> logout(HttpServletRequest request, @CurrentAdmin XklAdminEntity admin,
@RequestParam String sign, @RequestParam long t, @RequestParam int type) {
if (!(boolean) request.getAttribute("signAspect"))
return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK);
tokenManager.deleteToken(Constants.ADMIN_TOKEN_PREFIX + admin.getId());
return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK);
}
}
... ...
... ... @@ -6,15 +6,19 @@ import com.xkl.config.Constants;
import com.xkl.config.ResultStatus;
import com.xkl.domain.User;
import com.xkl.domain.XklAdminEntity;
import com.xkl.domain.XklCompanyEntity;
import com.xkl.domain.XklMemberEntity;
import com.xkl.model.CityModel;
import com.xkl.model.ResultModel;
import com.xkl.repository.UserRepository;
import com.xkl.repository.XklCompanyRespository;
import com.xkl.repository.XklMemberRespository;
import com.xkl.security.AntiXSS;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xkl.security.SecurityTool;
import com.xkl.tools.UtilTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
... ... @@ -25,7 +29,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Result;
import java.sql.Date;
/**
* Created by win7 on 2016/10/19.
... ... @@ -39,40 +43,78 @@ public class UserInfoController {
private ITokenManager tokenManager;
@Autowired
private XklMemberRespository xklMemberRespository;
@Autowired
private XklCompanyRespository xklCompanyRespository;
@LogAnnotation
@AntiXSS
@Authorization
@Sign
@RequestMapping(method = RequestMethod.POST)
@ApiOperation(value = "用户注册接口")
public ResponseEntity<ResultModel> register(HttpServletRequest request,@RequestParam String username, @RequestParam String password,
@RequestParam String sign,@RequestParam long t,@RequestParam int type) {
public ResponseEntity<ResultModel> register(HttpServletRequest request, @CurrentAdmin XklAdminEntity admin, @RequestParam String username, @RequestParam String password,
@RequestParam String name, @RequestParam String phone,@RequestParam boolean sex,@RequestParam String birthDate,
@RequestParam(required=false) String idcard,
@RequestParam String sign, @RequestParam long t, @RequestParam int type) {
if(!(boolean)request.getAttribute("signAspect"))
return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK);
Assert.notNull(username, "username can not be empty");
Assert.notNull(password, "password can not be empty");
Assert.notNull(name, "name can not be empty");
Assert.notNull(sex, "sex can not be empty");
Assert.notNull(birthDate, "birthDate can not be empty");
Assert.notNull(phone, "phone can not be empty");
User user = userRepository.findByLoginAccountAndStatus(username, Constants.STATUS_OK2);
if (user != null ) { //用户已注册
return new ResponseEntity<>(ResultModel.error(ResultStatus.USER_IS_EXIT), HttpStatus.OK);
}else{
/**
* member表
*/
XklMemberEntity member = new XklMemberEntity();
member.setName(name);
member.setSex(sex);
member.setBirthDate(birthDate);
member.setPhone(phone);
member.setRegisterBy(admin.getId());
member.setCompanyId(admin.getCoid());
member.setRegisterTime(UtilTools.getNow());
member.setStatus(1);
//member表自动判断归属地
if(idcard!=null&&idcard!=""){//身份证存在
member.setIdcard(idcard);
CityModel cityModel = Constants.cityMap.get(idcard.substring(0,6));
if(cityModel!=null){
member.setCountry(cityModel.getCountry());
member.setProvince(cityModel.getProvince());
member.setCity(cityModel.getCity());
}
}else{
long coid = admin.getCoid();
XklCompanyEntity xklCompanyEntity = xklCompanyRespository.findOne(coid);
if(xklCompanyEntity!=null){
member.setCountry(xklCompanyEntity.getCountryId());
member.setProvince(xklCompanyEntity.getProvinceId());
member.setCity(xklCompanyEntity.getCityId());
}
}
XklMemberEntity memberEntity = xklMemberRespository.save(member);
if(memberEntity!=null){
String salt= SecurityTool.genSalt();
String pass=SecurityTool.getPassword(username,password,salt);
user = new User();
/**
* TODO:
* 1. sign检测注解@Sign先注释掉 ,便于测试
* 2. 暂时把所有注册的用户的member表member_id都设置为1
*/
user.setMemberId(1);
user.setMemberId(memberEntity.getId());
user.setLoginAccount(username);
user.setLoginPwd(pass);
user.setSalt(salt);
user.setStatus(true);
userRepository.save(user);
}
}
return new ResponseEntity<>(ResultModel.ok(ResultStatus.USER_REGISTER), HttpStatus.OK);
}
... ...
... ... @@ -32,54 +32,6 @@ public class User {
private boolean status;
@Column(name = "member_id")
private long memberId;
private int memberId;
public String getLoginAccount() {
return loginAccount;
}
public void setLoginAccount(String loginAccount) {
this.loginAccount = loginAccount;
}
public String getLoginPwd() {
return loginPwd;
}
public void setLoginPwd(String loginPwd) {
this.loginPwd = loginPwd;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getSalt() {
return salt;
}
public void setSalt(String salt) {
this.salt = salt;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public int getMemberId() {
return memberId;
}
public void setMemberId(int memberId) {
this.memberId = memberId;
}
}
... ...
package com.xkl.domain;
import lombok.Data;
import javax.persistence.*;
/**
* Created by win7 on 2017/2/27.
*/
@Data
@Entity
@Table(name = "xkl_city")
public class XklCityEntity {
@Id
@GeneratedValue
@Column(name = "id")
private long id;
@Basic
@Column(name = "city_name")
private String cityName;
@Basic
@Column(name = "parent_id")
private long parentId;
@Basic
@Column(name = "state")
private Byte state;
@Basic
@Column(name = "remark")
private String remark;
@Basic
@Column(name = "city_sort")
private Byte citySort;
@Basic
@Column(name = "city_area")
private String cityArea;
@Basic
@Column(name = "id_card_code")
private String idCardCode;
@Basic
@Column(name = "first_pinyin")
private String firstPinyin;
@Basic
@Column(name = "full_pinyin")
private String fullPinyin;
@Basic
@Column(name = "simple_pinyin")
private String simplePinyin;
}
... ...
package com.xkl.domain;
import lombok.Data;
import javax.persistence.*;
/**
* Created by win7 on 2017/2/27.
*/
@Data
@Entity
@Table(name = "xkl_company")
public class XklCompanyEntity {
@Id
@GeneratedValue
@Column(name = "id")
private long id;
@Basic
@Column(name = "name")
private String name;
@Basic
@Column(name = "parent_id")
private long parentId;
@Basic
@Column(name = "province_id")
private long provinceId;
@Basic
@Column(name = "city_id")
private long cityId;
@Basic
@Column(name = "country_id")
private long countryId;
@Basic
@Column(name = "company_code")
private long companyCode;
@Basic
@Column(name = "company_type")
private Byte companyType;
@Basic
@Column(name = "level")
private Byte level;
@Basic
@Column(name = "show_level")
private Byte showLevel;
@Basic
@Column(name = "status")
private Byte status;
}
... ...
... ... @@ -23,10 +23,10 @@ public class XklMemberEntity {
private String name;
@Basic
@Column(name = "sex")
private Byte sex;
private boolean sex;
@Basic
@Column(name = "birth_date")
private Date birthDate;
private String birthDate;
@Basic
@Column(name = "idcard")
private String idcard;
... ... @@ -53,5 +53,5 @@ public class XklMemberEntity {
private long registerBy;
@Basic
@Column(name = "status")
private Byte status;
private int status;
}
... ...
package com.xkl.model;
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* Created by win7 on 2017/2/27.
*/
@Data
public class CityModel {
long country = 0;//国家
long province = 0;//省(直辖市)
long city = 0;//市(直辖市的区)
long county = 0;//县
int type = 0;
}
... ...
package com.xkl.repository;
import com.xkl.domain.XklCityEntity;
import com.xkl.domain.XklInterKeyEntity;
import org.springframework.data.repository.CrudRepository;
/**
* Created by win7 on 2016/11/20.
*/
public interface XklCityRespository extends CrudRepository<XklCityEntity, Long> {
}
... ...
package com.xkl.repository;
import com.xkl.domain.XklCityEntity;
import com.xkl.domain.XklCompanyEntity;
import org.springframework.data.repository.CrudRepository;
/**
* Created by win7 on 2016/11/20.
*/
public interface XklCompanyRespository extends CrudRepository<XklCompanyEntity, Long> {
}
... ...
package com.xkl.service;
import com.xkl.domain.User;
import com.xkl.domain.XklAdminEntity;
/**
* Created by win7 on 2016/12/18.
... ... @@ -8,5 +9,5 @@ import com.xkl.domain.User;
public interface ILoginService {
public User check(String username, String password);
public User check(String username);
public XklAdminEntity checkAdmin(String username, String password);
}
... ...
... ... @@ -2,6 +2,8 @@ package com.xkl.service;
import com.xkl.config.Constants;
import com.xkl.domain.User;
import com.xkl.domain.XklAdminEntity;
import com.xkl.repository.AdminRepository;
import com.xkl.repository.UserRepository;
import com.xkl.security.SecurityTool;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -14,6 +16,8 @@ import org.springframework.stereotype.Service;
public class LoginServiceImpl implements ILoginService{
@Autowired
private UserRepository userRepository;
@Autowired
private AdminRepository adminRepository;
@Override
public User check(String username, String password) {
User user = userRepository.findByLoginAccountAndStatus(username, Constants.STATUS_OK2);
... ... @@ -35,4 +39,19 @@ public class LoginServiceImpl implements ILoginService{
User user = userRepository.findByLoginAccountAndStatus(username.trim(), Constants.STATUS_OK2);
return user;
}
@Override
public XklAdminEntity checkAdmin(String username, String password) {
XklAdminEntity admin = adminRepository.findByAccountAndStatus(username, Constants.STATUS_OK);
if(admin == null){
return null;
}else{
String salt = admin.getSalt();
String pass_in_db = admin.getPwd();
String pass = SecurityTool.getPassword(username, password, salt);
if (!pass.equals(pass_in_db))//密码错误
return null;
}
return admin;
}
}
... ...
... ... @@ -79,6 +79,13 @@ public class UtilTools {
return a;
}
public static String getNow(){
Calendar c = Calendar.getInstance(); //获取东八区时间
SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String curDate = s.format(c.getTime());
return curDate;
}
public static long _long(String value) {
if (value == null || "null".equals(value)) {
return 0;
... ... @@ -130,6 +137,7 @@ public class UtilTools {
public static void main(String[] args) {
System.out.println(getLongTime("2016-10-11 22:22:22"));
System.out.println(getNow());
}
... ...