hjl
2024-06-18 aaa3384609da2dfb7d6788a2a2b3d92a2bff0813
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package com.ruoyi.worker.controller;
 
 
import com.ruoyi.admin.api.entity.Agreement;
import com.ruoyi.admin.api.feignClient.AdminClient;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.constant.RedisConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.auth.AuthUtil;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUserInfo;
import com.ruoyi.worker.entity.MasterWorker;
import com.ruoyi.worker.entity.WorkerProcess;
import com.ruoyi.worker.request.LoginPasswordRequest;
import com.ruoyi.worker.request.LoginPhoneRequest;
import com.ruoyi.worker.service.MasterWorkerService;
import com.ruoyi.worker.service.WorkerProcessService;
import com.ruoyi.worker.vo.HomePageInfoVO;
import com.ruoyi.worker.vo.OrderCountVO;
import com.ruoyi.worker.vo.OrderListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;
 
/**
 * <p>
 * 师傅信息表 前端控制器
 * </p>
 *
 * @author hjl
 * @since 2024-06-03
 */
@RestController
@RequestMapping("/worker")
@Api(value = "师傅端", tags = "师傅端")
public class MasterWorkerController {
 
    @Resource
    private MasterWorkerService masterWorkerService;
    @Resource
    private TokenService tokenService;
    @Resource
    private RedisService redisService;
    @Resource
    private WorkerProcessService workerProcessService;
    @Resource
    private AdminClient adminClient;
 
    /**
     * 师傅端-密码登录
     *
     * @param loginPasswordRequest 手机号及密码信息
     */
    @ApiOperation(value = "密码登录", tags = {"师傅端-登录"})
    @PostMapping(value = "/passwordLogin")
    public R<Object> passwordLogin(@RequestBody @Validated LoginPasswordRequest loginPasswordRequest) {
        String phone = loginPasswordRequest.getPhone();
        String password = loginPasswordRequest.getPassword();
        MasterWorker worker = masterWorkerService.lambdaQuery().eq(MasterWorker::getPhone, phone).eq(MasterWorker::getIsDelete, 0).one();
        if (null == worker) {
            return R.unregistered("当前手机号未注册!");
        }
        if (!Constants.ONE.equals(worker.getIsEnable())) {
            return R.notEnabled("当前手机号已禁用!");
        }
        // MD5加密登录密码(新)
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        // 校验用户输入密码与加密后的密码是否匹配
        boolean matches = passwordEncoder.matches(password, worker.getPassword());
        if (!matches) {
            return R.passwordError("登录密码错误!");
        }
        return R.ok(generateLoginToken(worker));
    }
 
    /**
     * 生成登录信息及token加密
     *
     * @param worker 登录信息
     * @return token
     */
    private HashMap<String, Object> generateLoginToken(MasterWorker worker) {
        // 校验通过,生成token及过期时间
        LoginUserInfo loginUserInfo = new LoginUserInfo();
        loginUserInfo.setName(worker.getRealName());
        loginUserInfo.setUserid(worker.getId());
        loginUserInfo.setPhone(worker.getPhone());
        loginUserInfo.setLoginTime(System.currentTimeMillis());
        HashMap<String, Object> map = new HashMap<>(8);
        map.put("token", tokenService.createTokenByWorker(loginUserInfo));
        return map;
    }
 
    /**
     * 师傅端-手机验证码登录
     *
     * @param loginPhoneRequest 手机号及密码信息
     */
    @ApiOperation(value = "手机验证码登录", tags = {"师傅端-登录"})
    @PostMapping(value = "/phoneCodeLogin")
    public R<Object> phoneCodeLogin(@RequestBody @Validated LoginPhoneRequest loginPhoneRequest) {
        String phone = loginPhoneRequest.getPhone();
        MasterWorker worker = masterWorkerService.lambdaQuery().eq(MasterWorker::getPhone, phone)
                .eq(MasterWorker::getIsDelete, 0).one();
        if (null != worker) {
            if (!Constants.ONE.equals(worker.getIsEnable())) {
                return R.notEnabled("登录失败,当前账号未启用!");
            }
            String phoneCode = loginPhoneRequest.getPhoneCode();
            // 获取缓存验证码
            Object phoneCodeRedis = redisService.getCacheObject(RedisConstants.USER_LOGIN_PHONE_CODE + phone);
            if (null == phoneCodeRedis) {
                return R.errorCode("登录失败,验证码已过期!");
            } else {
                // redis 验证码的value 为 code:时间戳
                String rCodeAndTime = String.valueOf(phoneCodeRedis);
                String rCode = rCodeAndTime.split(":")[0];
                if (!rCode.equalsIgnoreCase(phoneCode)) {
                    return R.errorCode("登录失败,验证码无效!");
                }
            }
        } else {
            return R.unregistered("当前手机号未注册!");
        }
        // 校验通过,生成token及过期时间
        return R.ok(generateLoginToken(worker));
    }
 
    /**
     * 师傅端-提交入驻申请
     *
     * @param workerProcess 入驻申请相关信息
     */
    @ApiOperation(value = "入驻申请", tags = {"师傅端-登录"})
    @PostMapping(value = "/settledApply")
    public R<String> settledApply(@RequestBody WorkerProcess workerProcess) {
        String phone = workerProcess.getPhone();
        String phoneCode = workerProcess.getPhoneCode();
        Object redisCodeObj = redisService.getCacheObject(RedisConstants.WORKER_SETTLE_KEY + phone);
        if (null == redisCodeObj) {
            return R.errorCode("验证码错误或已过期!");
        }
        String redisCodeStr = String.valueOf(redisCodeObj).split(":")[0];
        if (!String.valueOf(redisCodeStr).equalsIgnoreCase(phoneCode)) {
            return R.errorCode("验证码错误或已过期!");
        }
        // 校验手机号是否已注册
        MasterWorker masterWorker = masterWorkerService.lambdaQuery().eq(MasterWorker::getPhone, phone)
                .eq(MasterWorker::getIsDelete, 0).one();
        if (null != masterWorker) {
            return R.registered("当前手机号已注册!");
        }
        // 校验手机号是否已提交申请
        WorkerProcess dbProcess = workerProcessService.lambdaQuery().eq(WorkerProcess::getPhone, phone)
                .eq(WorkerProcess::getIsDelete, 0).one();
        if (null != dbProcess) {
            return R.repeatedSubmission("当前手机号已提交申请,请勿重复提交!");
        }
        // 默认状态待审核
        workerProcess.setState(Constants.ZERO);
        // 获取用户性别
        String idNumber = workerProcess.getIdNumber();
        // 判断身份证号码长度是否合法
        if (idNumber.length() == Constants.EIGHTEEN) {
            // 获取身份证号码倒数第二位的数字
            int genderNum = Integer.parseInt(idNumber.substring(16, 17));
            // 判断性别
            if (genderNum % Constants.TWO == 0) {
                workerProcess.setSex("女");
            } else {
                workerProcess.setSex("男");
            }
        } else {
            return R.idNumberIllegal("身份证号码不合法!");
        }
        return workerProcessService.save(workerProcess) ? R.ok("申请成功!") : R.fail("申请失败!");
    }
 
    /**
     * 师傅端-验证手机号
     * 找回密码和修改密码都需要验证手机号
     *
     * @param phone 验证的手机号
     */
    @ApiOperation(value = "找回密码/修改密码验证手机号", tags = {"师傅端-登录"})
    @GetMapping(value = "/verifyPhone")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "验证手机号", name = "phone", dataType = "String", required = true),
            @ApiImplicitParam(value = "手机验证码", name = "phoneCode", dataType = "String", required = true)
    })
    public R<String> verifyPhone(@RequestParam String phone, @RequestParam String phoneCode) {
        // 验证码是否一致
        Object redisCode = redisService.getCacheObject(RedisConstants.WORKER_APPLY_KEY + phone);
        if (null == redisCode) {
            return R.errorCode("验证码错误或已过期!");
        }
        String redisCodeStr = String.valueOf(redisCode).split(",")[0];
        if (!String.valueOf(redisCodeStr).equalsIgnoreCase(phoneCode)) {
            return R.errorCode("验证码错误或已过期!");
        }
        // 手机号是否注册
        MasterWorker worker = masterWorkerService.lambdaQuery().eq(MasterWorker::getPhone, phone)
                .eq(MasterWorker::getIsDelete, 0).one();
        if (null == worker) {
            return R.registered("该手机号未提交入驻申请!");
        }
        return R.ok("验证成功!");
    }
 
    /**
     * 师傅端-修改登录密码
     * 找回密码和修改密码验证手机号通过后,执行修改登录密码并删除登录信息
     *
     * @param password 新密码
     */
    @ApiOperation(value = "修改登录密码", tags = {"师傅端-登录"})
    @GetMapping(value = "/updatePassword")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "手机号", name = "phone", dataType = "String", required = true),
            @ApiImplicitParam(value = "登录密码", name = "password", dataType = "String", required = true)
    })
    public R<Boolean> updatePassword(@RequestParam String phone, @RequestParam String password, HttpServletRequest request) {
        // 密码长度至少为8位,且不能全是英文字母或数字
        String regex = "^(?=.*[0-9])(?=.*[a-zA-Z])(?!.*[a-zA-Z]{8,})(?!.*\\d{8,}).{8,}$";
        if (!Pattern.matches(regex, password)) {
            return R.passwordIllegality("密码至少8个字符,不能全是字母或数字");
        }
        // 手机号是否已被后台软删除
        MasterWorker worker = masterWorkerService.lambdaQuery().eq(MasterWorker::getPhone, phone)
                .eq(MasterWorker::getIsDelete, 0).one();
        if (null == worker) {
            return R.registered("该手机号未提交入驻申请!");
        }
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String md5Password = passwordEncoder.encode(password);
        worker.setPassword(md5Password);
        // 修改成功后,删除token
        masterWorkerService.updateById(worker);
        String token = SecurityUtils.getToken(request);
        if (null != token) {
            // 删除用户缓存记录
            AuthUtil.logoutByTokenWorker(token);
        }
        return R.ok();
    }
 
    /**
     * 师傅端-首页个人信息及订单待上门信息
     */
    @ApiOperation(value = "首页个人信息", tags = {"师傅端-首页"})
    @GetMapping(value = "/homePage")
    public R<HomePageInfoVO> homePage() {
        LoginUserInfo loginUser = tokenService.getLoginUserByWorker();
        if (null == loginUser) {
            return R.loginExpire("登录已失效!");
        }
        // 师傅信息
        MasterWorker masterWorker = masterWorkerService.lambdaQuery().eq(MasterWorker::getId, loginUser.getUserid())
                .eq(MasterWorker::getIsDelete, 0).one();
        if (null == masterWorker) {
            return R.registered("当前账号未注册!");
        }
        // 订单统计
        OrderCountVO orderCount = masterWorkerService.orderCount(masterWorker.getId());
        // 操作指导详情
        Agreement agreement = adminClient.dataInfo(Constants.TWO).getData();
        return R.ok(new HomePageInfoVO(masterWorker, orderCount, agreement.getContent()));
    }
 
    /**
     * 师傅端-订单代办列表
     */
    @ApiOperation(value = "订单代办列表", tags = {"师傅端-首页"})
    @GetMapping(value = "/orderNotHandle")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "师傅所在经度", name = "longitude", dataType = "String", required = true),
            @ApiImplicitParam(value = "师傅所在纬度", name = "latitude", dataType = "String", required = true)
    })
    public R<List<OrderListVO>> orderNotHandle(@RequestParam String longitude, @RequestParam String latitude) {
        LoginUserInfo loginWorker = tokenService.getLoginUserByWorker();
        if (null == loginWorker) {
            return R.loginExpire("登录已失效!");
        }
        return R.ok(masterWorkerService.orderNotHandle(loginWorker.getUserid(), longitude, latitude));
    }
 
    /**
     * 师傅端-登录用户详情
     */
    @ApiOperation(value = "登录用户详情", tags = {"师傅端-个人中心"})
    @GetMapping(value = "/workerInfo")
    public R<MasterWorker> workerInfo() {
        LoginUserInfo loginWorker = tokenService.getLoginUserByWorker();
        if (null == loginWorker) {
            return R.loginExpire("登录已失效!");
        }
        return R.ok(masterWorkerService.getById(loginWorker.getUserid()));
    }
 
    /**
     * 师傅端-登录用户详情
     */
    @ApiOperation(value = "退出登录", tags = {"师傅端-个人中心"})
    @GetMapping(value = "/logout")
    public R<String> logout(HttpServletRequest request) {
        LoginUserInfo loginWorker = tokenService.getLoginUserByWorker();
        if (null == loginWorker) {
            return R.loginExpire("登录已失效!");
        }
        String token = SecurityUtils.getToken(request);
        if (null != token) {
            // 删除用户缓存记录
            AuthUtil.logoutByTokenWorker(token);
        }
        return R.ok("退出登录成功!");
    }
 
    /**
     * 师傅端-修改姓名
     */
    @ApiOperation(value = "修改姓名", tags = {"师傅端-个人中心"})
    @GetMapping(value = "/updateName")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "姓名", name = "name", dataType = "String", required = true)
    })
    public R<String> updateName(@RequestParam String name) {
        LoginUserInfo loginWorker = tokenService.getLoginUserByWorker();
        if (null == loginWorker) {
            return R.loginExpire("登录已失效!");
        }
        boolean update = masterWorkerService.lambdaUpdate().set(MasterWorker::getRealName, name).eq(MasterWorker::getId, loginWorker.getUserid())
                .eq(MasterWorker::getIsDelete, 0).update();
        return update ? R.ok("修改成功!") : R.fail("修改失败!");
    }
 
    /**
     * 师傅端-修改性别
     */
    @ApiOperation(value = "修改性别", tags = {"师傅端-个人中心"})
    @GetMapping(value = "/updateSex")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "性别", name = "sex", dataType = "String", required = true)
    })
    public R<String> updateSex(@RequestParam String sex) {
        LoginUserInfo loginWorker = tokenService.getLoginUserByWorker();
        if (null == loginWorker) {
            return R.loginExpire("登录已失效!");
        }
        boolean update = masterWorkerService.lambdaUpdate().set(MasterWorker::getSex, sex).eq(MasterWorker::getId, loginWorker.getUserid())
                .eq(MasterWorker::getIsDelete, 0).update();
        return update ? R.ok("修改成功!") : R.fail("修改失败!");
    }
 
    /**
     * 师傅端-修改生日
     */
    @ApiOperation(value = "修改生日", tags = {"师傅端-个人中心"})
    @GetMapping(value = "/updateBirthday")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "生日", name = "birthday", dataType = "Date", required = true)
    })
    public R<String> updateBirthday(@RequestParam Date birthday) {
        LoginUserInfo loginWorker = tokenService.getLoginUserByWorker();
        if (null == loginWorker) {
            return R.loginExpire("登录已失效!");
        }
        boolean update = masterWorkerService.lambdaUpdate().set(MasterWorker::getBirthday, birthday).eq(MasterWorker::getId, loginWorker.getUserid())
                .eq(MasterWorker::getIsDelete, 0).update();
        return update ? R.ok("修改成功!") : R.fail("修改失败!");
    }
 
    /**
     * 师傅端-修改手机号
     */
    @ApiOperation(value = "修改手机号", tags = {"师傅端-个人中心"})
    @GetMapping(value = "/updatePhone")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "手机号", name = "phone", dataType = "String", required = true),
            @ApiImplicitParam(value = "手机验证码", name = "code", dataType = "String", required = true)
    })
    public R<String> updatePhone(@RequestParam String phone, @RequestParam String code, HttpServletRequest request) {
        // 验证码是否一致
        Object redisCode = redisService.getCacheObject(RedisConstants.WORKER_APPLY_KEY + phone);
        if (null == redisCode) {
            return R.errorCode("验证码错误或已过期!");
        }
        String redisCodeStr = String.valueOf(redisCode).split(",")[0];
        if (!String.valueOf(redisCodeStr).equalsIgnoreCase(code)) {
            return R.errorCode("验证码错误或已过期!");
        }
        LoginUserInfo loginWorker = tokenService.getLoginUserByWorker();
        if (null == loginWorker) {
            return R.loginExpire("登录已失效!");
        }
        boolean update = masterWorkerService.lambdaUpdate().set(MasterWorker::getPhone, phone).eq(MasterWorker::getId, loginWorker.getUserid())
                .eq(MasterWorker::getIsDelete, 0).update();
        String token = SecurityUtils.getToken(request);
        if (null != token) {
            // 删除用户缓存记录
            AuthUtil.logoutByTokenWorker(token);
        }
        return update ? R.ok("修改成功!") : R.fail("修改失败!");
    }
 
}