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);
|
}
|
|
}
|