陶杰
2024-08-22 ee9032d9baf5f33e376d2d2699136e0a7b26bec7
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
package com.mzl.flower.web.current;
 
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.mzl.flower.base.BaseController;
import com.mzl.flower.base.R;
import com.mzl.flower.base.ReturnDataDTO;
import com.mzl.flower.base.cache.StringCacheClient;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.constant.Constants;
import com.mzl.flower.dto.request.BindWechatDTO;
import com.mzl.flower.dto.request.system.ChangePasswordDTO;
import com.mzl.flower.dto.request.system.UserInfoDTO;
import com.mzl.flower.dto.response.current.CurrentUserDTO;
import com.mzl.flower.dto.response.customer.CustomerDTO;
import com.mzl.flower.dto.response.partner.PartnerDTO;
import com.mzl.flower.dto.response.supplier.SupplierDTO;
import com.mzl.flower.dto.response.system.MenuTreeDTO;
import com.mzl.flower.entity.system.User;
import com.mzl.flower.service.customer.CustomerService;
import com.mzl.flower.service.partner.PartnerService;
import com.mzl.flower.service.supplier.SupplierService;
import com.mzl.flower.service.system.UserService;
import com.mzl.flower.service.system.WeChatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import java.util.List;
import java.util.Map;
 
@RestController
@RequestMapping("/api/current")
@Api(tags = "系统-当前用户")
@Validated
public class CurrentUserController extends BaseController {
    private final UserService userService;
 
    private final SupplierService supplierService;
    private final PartnerService partnerService;
 
    private final CustomerService customerService;
 
 
    private final StringCacheClient stringCacheClient;
 
    private final WeChatService weChatService;
 
 
    public static final String SMS_CODE_KEY = "SMS-CODE-KEY";
 
    public static final String SEPARATOR = ":";
 
    public CurrentUserController(UserService userService,
                                 SupplierService supplierService,
                                 PartnerService partnerService,
                                 CustomerService customerService, StringCacheClient stringCacheClient, WeChatService weChatService) {
        this.userService = userService;
        this.supplierService = supplierService;
        this.partnerService = partnerService;
        this.customerService = customerService;
        this.stringCacheClient = stringCacheClient;
        this.weChatService = weChatService;
    }
 
    @GetMapping("/user")
    @ApiOperation(value = "获取账号信息")
    public ResponseEntity<ReturnDataDTO<CurrentUserDTO>> getCurrentUser() {
        return returnData(R.SUCCESS.getCode(), userService.getCurrentUser());
    }
 
    @GetMapping("/supplier")
    @ApiOperation(value = "获取当前供应商信息")
    public ResponseEntity<ReturnDataDTO<SupplierDTO>> getCurrentSupplier() {
        return returnData(R.SUCCESS.getCode(), supplierService.getCurrentSupplier());
    }
 
    @GetMapping("/partner")
    @ApiOperation(value = "获取当前合伙人信息")
    public ResponseEntity<ReturnDataDTO<PartnerDTO>> getCurrentPartner() {
        return returnData(R.SUCCESS.getCode(), partnerService.getCurrentPartner());
    }
 
    @GetMapping("/customer")
    @ApiOperation(value = "获取当前商户信息")
    public ResponseEntity<ReturnDataDTO<CustomerDTO>> getCurrentCustomer() {
        return returnData(R.SUCCESS.getCode(), customerService.getCurrentCustomer());
    }
 
    @GetMapping("/customer/partner")
    @ApiOperation(value = "获取当前商户信息绑定的合伙人信息")
    public ResponseEntity<ReturnDataDTO<PartnerDTO>> getCurrentBindPartner() {
        return returnData(R.SUCCESS.getCode(), customerService.getCurrentBindPartner());
    }
 
    @GetMapping("/user/menu")
    @ApiOperation(value = "获取账号信息菜单")
    public ResponseEntity<ReturnDataDTO<List<MenuTreeDTO>>> getCurrentUserMenu() {
        return returnData(R.SUCCESS.getCode(), userService.getCurrentUserMenuTrue());
    }
 
    @PostMapping("/user/update")
    @ApiOperation(value = "更新账号信息")
    public ResponseEntity<ReturnDataDTO<?>> updateUserInfo(@RequestBody UserInfoDTO dto) {
        userService.updateUserInfo(dto);
        return returnData(R.SUCCESS.getCode(), null);
    }
 
    @PostMapping("/user/password/change")
    @ApiOperation(value = "修改密码")
    public ResponseEntity<ReturnDataDTO<?>> changePassword(@RequestBody ChangePasswordDTO dto) {
        userService.changePassword(dto);
        return returnData(R.SUCCESS.getCode(), null);
    }
 
 
    @PostMapping("/bind/wechat")
    @ApiOperation(value = "账号绑定结算微信账号", notes = "账号绑定结算微信账号")
    public ResponseEntity<ReturnDataDTO> BindWechat(@Validated @RequestBody BindWechatDTO dto) {
        String usetType = SecurityUtils.getUserType();
        //从缓存中获取验证码
        String smsCacheCode = stringCacheClient.get(SMS_CODE_KEY + SEPARATOR + dto.getUserType() + SEPARATOR + dto.getTel());
        if (!StringUtils.equals(dto.getSmsCode(), smsCacheCode)) {
            throw new ValidationException("手机验证码不正确");
        }
        Map<String, Object> session= null;
        try {
            session = weChatService.getWechatOpenId(dto.getWxCode(), usetType);
        } catch (Exception e) {
            throw new ValidationException("获取微信信息失败,请联系管理员");
        }
        String openId = (String) session.get("openid");
        String sessionKey = (String) session.get("session_key");
        String unionId = (String) session.get("unionid");
        if(StringUtils.isBlank(openId)){
            throw new ValidationException("获取微信openId信息失败,请联系管理员");
        }
        User user = userService.getUserById(SecurityUtils.getUserId());
        if(user == null || !usetType.equals(user.getType())){
            throw new ValidationException("用户不存在");
        }
        if(!user.getTel().equals(dto.getTel())){
            throw new ValidationException("手机号与账号手机号不一致");
        }
        userService.bindWechat(user.getId(),openId,unionId,sessionKey,dto);
        //删除缓存中的验证码
        stringCacheClient.delete(SMS_CODE_KEY + SEPARATOR + dto.getUserType() + SEPARATOR + dto.getTel());
        return returnData(R.SUCCESS.getCode(),null);
    }
 
}