package com.stylefeng.guns.modular.api;
|
|
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
import com.stylefeng.guns.core.base.controller.BaseController;
|
import com.stylefeng.guns.core.base.tips.ErrorTip;
|
import com.stylefeng.guns.core.common.constant.factory.ConstantFactory;
|
import com.stylefeng.guns.core.shiro.ShiroUser;
|
import com.stylefeng.guns.core.util.Convert;
|
import com.stylefeng.guns.core.util.JwtTokenUtil;
|
import com.stylefeng.guns.core.util.MD5Util;
|
import com.stylefeng.guns.modular.system.dao.TCompanyMapper;
|
import com.stylefeng.guns.modular.system.dao.UserMapper;
|
import com.stylefeng.guns.modular.system.model.TCompany;
|
import com.stylefeng.guns.modular.system.model.User;
|
import com.stylefeng.guns.modular.system.model.UserInfo;
|
import com.stylefeng.guns.modular.system.utils.tips.SuccessTip;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiImplicitParam;
|
import io.swagger.annotations.ApiImplicitParams;
|
import io.swagger.annotations.ApiOperation;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.*;
|
|
import javax.annotation.Resource;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
|
/**
|
* 接口控制器提供
|
*
|
* @author stylefeng
|
* @Date 2018/7/20 23:39
|
*/
|
@RestController
|
@Api(tags = "登录")
|
@RequestMapping("/gunsApi")
|
public class ApiController extends BaseController {
|
|
@Autowired
|
private UserMapper userMapper;
|
|
@Resource
|
private TCompanyMapper companyMapper;
|
/**
|
* api登录接口,通过账号密码获取token
|
*/
|
|
@PostMapping("/companyLogin")
|
@ApiOperation(value = "卡车公司登录", notes = "卡车公司登录")
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "username", value = "用户账号", required = true, dataType = "String"),
|
@ApiImplicitParam(name = "password", value = "用户密码", required = true, dataType = "String")
|
})
|
public Object companyLogin(@RequestParam("username") String username,
|
@RequestParam("password") String password) {
|
|
|
//获取数据库中的账号密码,准备比对
|
List<TCompany> user = companyMapper.selectList(new EntityWrapper<TCompany>().eq("account",username));
|
if (user.size()==0) {
|
return new ErrorTip(500, "账号密码错误!");
|
}
|
UserInfo userInfo = new UserInfo();
|
BeanUtils.copyProperties(user, userInfo);
|
// String credentials = user.getPassword();
|
// String salt = user.getSalt();
|
// ByteSource credentialsSalt = new Md5Hash(salt);
|
// SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
|
// new ShiroUser(), credentials, credentialsSalt, "");
|
|
//校验用户账号密码
|
// HashedCredentialsMatcher md5CredentialsMatcher = new HashedCredentialsMatcher();
|
// md5CredentialsMatcher.setHashAlgorithmName(ShiroKit.hashAlgorithmName);
|
// md5CredentialsMatcher.setHashIterations(ShiroKit.hashIterations);
|
// boolean passwordTrueFlag = md5CredentialsMatcher.doCredentialsMatch(
|
// usernamePasswordToken, simpleAuthenticationInfo);
|
String encrypt = MD5Util.encrypt(password);
|
if (!encrypt.equals(user.get(0).getPassword())) {
|
return new ErrorTip(500, "账号密码错误!");
|
} else {
|
TCompany company = user.get(0);
|
HashMap<String, Object> result = new HashMap<>();
|
result.put("token", JwtTokenUtil.generateToken(String.valueOf(company.getId())));
|
result.put("company", company);
|
super.getSession().setAttribute("companyId",String.valueOf(company.getId()));
|
return result;
|
}
|
}
|
|
|
|
|
private ShiroUser shiroUser(User user) {
|
ShiroUser shiroUser = new ShiroUser();
|
|
shiroUser.setId(user.getId());
|
shiroUser.setAccount(user.getAccount());
|
shiroUser.setDeptId(user.getDeptid());
|
shiroUser.setDeptName(ConstantFactory.me().getDeptName(user.getDeptid()));
|
shiroUser.setName(user.getName());
|
|
Integer[] roleArray = Convert.toIntArray(user.getRoleid());
|
List<Integer> roleList = new ArrayList<Integer>();
|
List<String> roleNameList = new ArrayList<String>();
|
for (int roleId : roleArray) {
|
roleList.add(roleId);
|
roleNameList.add(ConstantFactory.me().getSingleRoleName(roleId));
|
}
|
shiroUser.setRoleList(roleList);
|
shiroUser.setRoleNames(roleNameList);
|
|
return shiroUser;
|
}
|
|
/**
|
* 测试接口是否走鉴权
|
*/
|
@RequestMapping(value = "/test", method = RequestMethod.POST)
|
public Object test() {
|
return SUCCESS_TIP;
|
}
|
|
|
@PostMapping("/forget")
|
@ApiOperation(value = "忘记密码", notes = "忘记密码")
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "username", value = "用户账号", required = true, dataType = "String"),
|
@ApiImplicitParam(name = "password", value = "用户密码", required = true, dataType = "String"),
|
})
|
public Object forget(@RequestParam("username") String username,
|
@RequestParam("password") String password) {
|
List<TCompany> user = companyMapper.selectList(new EntityWrapper<TCompany>().eq("account",username));
|
if (user.size()==0){
|
return new ErrorTip(500, "账号不存在!");
|
}
|
user.get(0).setPassword(MD5Util.encrypt(password));
|
companyMapper.updateById(user.get(0));
|
return new SuccessTip();
|
}
|
|
|
}
|