liujie
2023-05-26 786466f6accfe836160342c56cdf3415e09bcb38
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package com.stylefeng.guns.modular.api;
 
import cn.hutool.core.bean.BeanUtil;
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.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.TCompanyMapper;
import com.stylefeng.guns.modular.system.dao.TDriverMapper;
import com.stylefeng.guns.modular.system.dao.UserMapper;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.service.ITCompanyService;
import com.stylefeng.guns.modular.system.service.TCarriersService;
import com.stylefeng.guns.modular.system.utils.EmailUtil;
import com.stylefeng.guns.modular.system.utils.RedisUtil;
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;
import java.util.Random;
 
/**
 * 接口控制器提供
 *
 * @author stylefeng
 * @Date 2018/7/20 23:39
 */
@RestController
@Api(tags = "登录")
@RequestMapping("/gunsApi")
public class ApiController extends BaseController {
 
    @Resource
    private TDriverMapper driverMapper;
 
    @Resource
    private ITCompanyService companyService;
 
    @Resource
    private TCarriersService tCarriersService;
 
    @Autowired
    private RedisUtil redisUtil;
    /**
     * 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<TDriver> drivers = driverMapper.selectList(new EntityWrapper<TDriver>().eq("account",username));
        if (drivers.size()==0) {
            return new ErrorTip(500, "The account you entered is not registered!");
        }
        String encrypt = MD5Util.encrypt(password);
        if (!encrypt.equals(drivers.get(0).getPassword())) {
            return new ErrorTip(500, "The password you entered is incorrect!");
        } else {
            TDriver driver = drivers.get(0);
            TDriverLogin tDriverLogin = new TDriverLogin();
            Integer isCarriers = driver.getIsCarriers();
            if(isCarriers==1){
                Integer companyId = driver.getCompanyId();
                TCompany company = companyService.selectById(companyId);
                tDriverLogin.setHeadImg(company.getHeadImg());
            }else {
                Integer carriersId = driver.getCarriersId();
                TCarriers tCarriers = tCarriersService.selectById(carriersId);
                tDriverLogin.setHeadImg(tCarriers.getCompanyLogo());
            }
            BeanUtil.copyProperties(driver,tDriverLogin);
            tDriverLogin.setToken(JwtTokenUtil.generateToken(String.valueOf(driver.getId())));
            HashMap<String, Object> result = new HashMap<>();
            result.put("driver", driver);
            return new SuccessTip(tDriverLogin);
        }
    }
 
 
 
 
    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"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "String"),
    })
    public Object forget(@RequestParam("username") String username,
                         @RequestParam("password") String password,
                         @RequestParam("code") String code) {
        List<TDriver> user = driverMapper.selectList(new EntityWrapper<TDriver>().eq("account",username));
        String value = redisUtil.getValue(username);
        if(!code.equals(value)){
            return new ErrorTip(5001, "Verification code error!");
        }
        if (user.size()==0){
            return new ErrorTip(500, "The account you entered is not registered!");
        }
        user.get(0).setPassword(MD5Util.encrypt(password));
        driverMapper.updateById(user.get(0));
        return new SuccessTip();
    }
    @PostMapping("/sendCode")
    @ApiOperation(value = "发送验证码", notes = "发送验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "email", value = "用户邮箱", required = true, dataType = "String"),
    })
    public Object sendCode(@RequestParam("email") String email) {
        String randomNumber = getRandomString(6);
        redisUtil.setStrValue(email,randomNumber,300);
        try {
            EmailUtil.sendMailGMail(email, randomNumber);
            return new com.stylefeng.guns.core.base.tips.SuccessTip();
        }catch (Exception e){
            e.printStackTrace();
            return new ErrorTip(500,"ERROR");
        }
    }
 
    private  String getRandomString(int length) {
        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
 
        for(int i = 0; i < length; ++i) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
 
        return sb.toString();
    }
}