基于mall4j产品的二开项目后端
lee
2024-12-18 efdb99f8cecc4afb592afad79c761081d5d5cf22
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
/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许,不可做商业用途!
 *
 * 版权所有,侵权必究!
 */
package com.yami.shop.api.controller;
 
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;
import com.yami.shop.bean.app.param.SendSmsParam;
import com.yami.shop.bean.dto.ShopDetailDto;
import com.yami.shop.bean.enums.SendType;
import com.yami.shop.bean.model.ShopAuditing;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.param.ShopDetailParam;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.security.common.model.UpdatePasswordDto;
import com.yami.shop.security.common.model.UsernameAndPasswordDto;
import com.yami.shop.service.ShopAuditingService;
import com.yami.shop.service.ShopDetailService;
import com.yami.shop.service.SmsLogService;
import com.yami.shop.sys.common.model.ShopEmployee;
import com.yami.shop.sys.common.service.ShopEmployeeService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
 
import javax.validation.Valid;
import java.util.Objects;
 
 
/**
 * @author lgh on 2018/08/29.
 */
@RestController
@RequestMapping("/p/shop")
@Tag(name = "店铺相关接口")
@AllArgsConstructor
public class MyShopDetailController {
 
    private final ShopDetailService shopDetailService;
 
    private final PasswordEncoder passwordEncoder;
 
    private final ShopAuditingService shopAuditingService;
 
    private final MapperFacade mapperFacade;
 
    private final SmsLogService smsLogService;
 
    private final ShopEmployeeService shopEmployeeService;
 
    @PostMapping("/apply")
    @Operation(summary = "申请店铺")
    public ServerResponseEntity<Void> apply(@Valid @RequestBody ShopDetailParam shopDetailParam) {
        String userId = SecurityUtils.getUser().getUserId();
        shopDetailService.applyShop(userId, shopDetailParam);
        return ServerResponseEntity.success();
    }
 
    @PostMapping("/saveUsernameAndPassword")
    @Operation(summary = "保存店铺账号密码")
    public ServerResponseEntity<Void> saveUsernameAndPassword(@Valid @RequestBody UsernameAndPasswordDto usernameAndPasswordDto) {
        String userId = SecurityUtils.getUser().getUserId();
        ShopDetail shopDetail = shopDetailService.getShopDetailByUserId(userId);
        if (shopDetail == null) {
            // 请先进行开店申请
            throw new YamiShopBindException("yami.open.store");
        }
        if (StrUtil.isNotBlank(shopDetail.getMobile())) {
            // 店铺账号已设置不能修改
            throw new YamiShopBindException("yami.stop.cannot.modified");
        }
        ShopDetail dbUsernameUser = shopDetailService.getShopByMobile(usernameAndPasswordDto.getUsername());
        ShopEmployee employee = shopEmployeeService.getByUserName(usernameAndPasswordDto.getUsername());
        if (dbUsernameUser != null || Objects.nonNull(employee)) {
            // 该账号已经开通过店铺,无法重复开通
            throw new YamiShopBindException("yami.stop.cannot.open");
        }
        if (!smsLogService.checkValidCode(usernameAndPasswordDto.getUsername(), usernameAndPasswordDto.getCode(), SendType.VALID)) {
            // 验证码有误或已过期
            throw new YamiShopBindException("yami.user.code.error");
        }
        String password = passwordEncoder.encode(usernameAndPasswordDto.getPassword());
        shopDetail.setPassword(password);
        shopDetail.setMobile(usernameAndPasswordDto.getUsername());
        // 更新店铺账号密码
        shopEmployeeService.updateUserNameAndPassword(shopDetail);
        shopDetailService.removeShopDetailCacheByShopId(shopDetail.getShopId());
        return ServerResponseEntity.success();
    }
 
    @PostMapping("/updatePassword")
    @Operation(summary = "更新店铺密码")
    public ServerResponseEntity<Void> updatePassword(@Valid @RequestBody UpdatePasswordDto updatePasswordDto) {
        String userId = SecurityUtils.getUser().getUserId();
        ShopDetail shopDetail = shopDetailService.getShopDetailByUserId(userId);
        if (shopDetail == null) {
            // 请先进行开店申请
            throw new YamiShopBindException("yami.open.store");
        }
        if (!passwordEncoder.matches(updatePasswordDto.getPassword(), shopDetail.getPassword())) {
            // 原密码不正确
            throw new YamiShopBindException("yami.password.error");
        }
        //新密码
        String newPassword = passwordEncoder.encode(updatePasswordDto.getNewPassword());
        //更新密码
        shopEmployeeService.updatePasswordByUserName(shopDetail.getMobile(), newPassword);
        return ServerResponseEntity.success();
    }
 
 
    @GetMapping()
    @Operation(summary = "获取拥有的店铺信息")
    public ServerResponseEntity<ShopDetailDto> auditingDetail() {
        ShopDetail shopDetail = shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId());
        ShopDetailDto shopDetailDto = mapperFacade.map(shopDetail, ShopDetailDto.class);
        return ServerResponseEntity.success(shopDetailDto);
    }
 
    @GetMapping("/shopAudit")
    @Operation(summary = "获取店铺的审核信息")
    public ServerResponseEntity<ShopAuditing> getShopAuditing() {
        ShopAuditing shopAuditing = shopAuditingService.getOne(new LambdaQueryWrapper<ShopAuditing>()
                .eq(ShopAuditing::getUserId, SecurityUtils.getUser().getUserId()));
        return ServerResponseEntity.success(shopAuditing);
    }
 
    @PostMapping("/sendCode")
    @Operation(summary = "发送验证码")
    public ServerResponseEntity<Void> sendLoginCode(@Valid @RequestBody SendSmsParam sendSmsParam) {
        String userId = SecurityUtils.getUser().getUserId();
        if (Objects.isNull(sendSmsParam.getMobile())) {
            // 手机号不能为空
            throw new YamiShopBindException("yami.shop.phone.no.exist");
        }
        if (Objects.nonNull(sendSmsParam.getShopAccount()) && Objects.equals(sendSmsParam.getShopAccount(), 1)) {
            ShopDetail dbUsernameUser = shopDetailService.getShopByMobile(sendSmsParam.getMobile());
            if (dbUsernameUser != null) {
                // 该账号已经开通过店铺,无法重复开通
                throw new YamiShopBindException("yami.stop.cannot.open");
            }
        }
        smsLogService.sendSms(SendType.VALID, userId, sendSmsParam.getMobile(), Maps.newHashMap());
        return ServerResponseEntity.success();
    }
}