|
|
@@ -0,0 +1,277 @@
|
|
|
+package com.cyksj.web.controller.cps;
|
|
|
+
|
|
|
+import cn.hutool.core.codec.Base64;
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.cyksj.common.annotation.NoSubmit;
|
|
|
+import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
+import com.cyksj.common.snowflake.Sequence;
|
|
|
+import com.cyksj.common.util.Codec;
|
|
|
+import com.cyksj.common.util.Jsons;
|
|
|
+import com.cyksj.config.wxlogin.WxOpenPlatYHLXLoginConfig;
|
|
|
+import com.cyksj.dto.Result;
|
|
|
+import com.cyksj.enums.GatewayResponse;
|
|
|
+import com.cyksj.mapper.GoodsDonMapper;
|
|
|
+import com.cyksj.mapper.UserMapper;
|
|
|
+import com.cyksj.mapper.manage.distribute.UserDistributeMapper;
|
|
|
+import com.cyksj.model.dto.AuthRedirect;
|
|
|
+import com.cyksj.model.dto.AuthToken;
|
|
|
+import com.cyksj.model.dto.GzhOAuth2UserInfo;
|
|
|
+import com.cyksj.model.entity.*;
|
|
|
+import com.cyksj.model.request.LoginReq;
|
|
|
+import com.cyksj.model.views.*;
|
|
|
+import com.cyksj.redis.RedisService;
|
|
|
+import com.cyksj.service.authorization.AuthorizationService;
|
|
|
+import com.cyksj.service.cps.CpsManageService;
|
|
|
+import com.cyksj.service.wechat.WeChatService;
|
|
|
+import com.cyksj.web.util.StpCpsManageUser;
|
|
|
+import com.cyksj.web.util.StpUserUtil;
|
|
|
+import com.ejlchina.searcher.BeanSearcher;
|
|
|
+import com.ejlchina.searcher.SearchResult;
|
|
|
+import com.ejlchina.searcher.util.MapBuilder;
|
|
|
+import com.ejlchina.searcher.util.MapUtils;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import javax.servlet.http.Cookie;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Optional;
|
|
|
+
|
|
|
+/*
|
|
|
+ *项目名: netflix
|
|
|
+ *文件名: CpsBackLoginController
|
|
|
+ *创建者: JavaZou
|
|
|
+ *创建时间:2023/5/12 11:00
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping("/cps/manage")
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class CpsBackLoginController {
|
|
|
+ private final CpsManageService cpsManageService;
|
|
|
+
|
|
|
+ private final BeanSearcher beanSearcher;
|
|
|
+
|
|
|
+ private final HttpServletRequest request;
|
|
|
+
|
|
|
+ private final GoodsDonMapper goodsDonMapper;
|
|
|
+
|
|
|
+ private final RedisService redisService;
|
|
|
+
|
|
|
+ private final static Sequence SEQUENCE = new Sequence(0);
|
|
|
+
|
|
|
+ private final WxOpenPlatYHLXLoginConfig wxOpenPlatYHLXLoginConfig;
|
|
|
+
|
|
|
+ private final AuthorizationService authorizationService;
|
|
|
+
|
|
|
+ private final WeChatService weChatService;
|
|
|
+
|
|
|
+ private final UserMapper userMapper;
|
|
|
+
|
|
|
+ private final UserDistributeMapper userDistributeMapper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取登录验证码
|
|
|
+ */
|
|
|
+ @GetMapping("/get/LoginCode")
|
|
|
+ @NoSubmit
|
|
|
+ public Result getLoginCode(@RequestParam(defaultValue = "1") Integer type, String login) {
|
|
|
+ cpsManageService.getLoginCode(type, login);
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 手机号、邮箱登录
|
|
|
+ */
|
|
|
+ @PostMapping("/login")
|
|
|
+ public Result login(@RequestBody LoginReq loginReq) {
|
|
|
+ User user = cpsManageService.login(loginReq.getType(), loginReq.getLogin(), loginReq.getCode());
|
|
|
+ StpCpsManageUser.login(user.getId());
|
|
|
+ String cpsToken = StpCpsManageUser.getTokenValue();
|
|
|
+ JSONObject re = new JSONObject();
|
|
|
+ re.putOpt("satoken-cpsUser", cpsToken);
|
|
|
+ re.putOpt("img", user.getHeadimgurl());
|
|
|
+ re.putOpt("nickname", user.getNickname());
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult(re.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * cps后台微信授权登录
|
|
|
+ */
|
|
|
+ @GetMapping("/wx/authLogin")
|
|
|
+ public Result<String> wxAuthorize() throws Exception {
|
|
|
+ String wxAuthUrl = redisService.getStr(RedisService.key.WX_LOGIN_AUTH_URI.getNameFormat(wxOpenPlatYHLXLoginConfig.getAppId()));
|
|
|
+ Long mappingId = SEQUENCE.nextId();
|
|
|
+ redisService.set(mappingId.toString(), wxOpenPlatYHLXLoginConfig.getCpsLoginUrl(), 60 * 60 * 24L);
|
|
|
+ AuthRedirect redirect = new AuthRedirect().setMappingId(mappingId);
|
|
|
+ String state = Codec.DoBase64.custom().setData(Jsons.toJson(redirect)).encodeAsText();
|
|
|
+ if (StrUtil.isEmpty(wxAuthUrl)) {
|
|
|
+ wxAuthUrl = authorizationService.createWxAuthLoginUrl(state, true);
|
|
|
+ }
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult(wxAuthUrl);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 网页微信授权后跳转
|
|
|
+ */
|
|
|
+ @GetMapping("/wx/authLogin/baseRedirect")
|
|
|
+ public void wxAuthLoginRedirect(String code, String state, HttpServletResponse response) throws Exception {
|
|
|
+
|
|
|
+ AuthToken token = weChatService.getAuthToken(code, wxOpenPlatYHLXLoginConfig.getAppId());
|
|
|
+ GzhOAuth2UserInfo userinfo = weChatService.getUserInfo(token.getAccessToken(), token.getOpenid());
|
|
|
+ String unionId = userinfo.getUnionid();
|
|
|
+ String openId = userinfo.getOpenid();
|
|
|
+ User user = Optional.ofNullable(
|
|
|
+ userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUnionid, unionId)
|
|
|
+ .orderByAsc(User::getId)
|
|
|
+ .last("limit 1")))
|
|
|
+ .orElseGet(() -> userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getOpenId, openId)
|
|
|
+ .orderByAsc(User::getId)
|
|
|
+ .last("limit 1")));
|
|
|
+ if (user == null) throw BusinessRuntimeException.getInstance("用户信息不存在");
|
|
|
+ UserDistribute userDistribute = userDistributeMapper.selectOne(Wrappers.lambdaQuery(UserDistribute.class).eq(UserDistribute::getUserId, user.getId()).select(UserDistribute::getId).last("limit 1"));
|
|
|
+ if (userDistribute == null) throw BusinessRuntimeException.getInstance("只支持渠道分销用户登录");
|
|
|
+
|
|
|
+ //写回登陆信息
|
|
|
+ StpCpsManageUser.login(user.getId());
|
|
|
+ response.addHeader("satoken-cpsUser", StpCpsManageUser.getTokenValue());
|
|
|
+
|
|
|
+ Cookie cookie = new Cookie("satoken-cpsUser", StpCpsManageUser.getTokenValue());
|
|
|
+ cookie.setMaxAge(31536000);
|
|
|
+ cookie.setPath("/");
|
|
|
+ response.addCookie(cookie);
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.putOpt("img", user.getHeadimgurl());
|
|
|
+ jsonObject.putOpt("nickname", user.getNickname());
|
|
|
+
|
|
|
+ Cookie userInfo = new Cookie("info", Base64.encode(jsonObject.toString()));
|
|
|
+ cookie.setMaxAge(31536000);
|
|
|
+ cookie.setPath("/");
|
|
|
+ response.addCookie(userInfo);
|
|
|
+ // 转跳参数
|
|
|
+ String json = Codec.DoBase64.custom().setData(state).decodeAsText();
|
|
|
+ AuthRedirect re = Jsons.parseObject(json, AuthRedirect.class);
|
|
|
+ String url = redisService.getStr(re.getMappingId().toString());
|
|
|
+ response.sendRedirect(url.contains("?") ? url + "&t=" + StpUserUtil.getTokenValue() : url + "?t=" + StpUserUtil.getTokenValue());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 个人分销平台数据详情
|
|
|
+ */
|
|
|
+ @GetMapping("/get/distribute/detail")
|
|
|
+ public Result<CpsUserDistributeDetailView> getDistributeDetail() {
|
|
|
+ long cpsUserId = StpCpsManageUser.getLoginIdAsLong();
|
|
|
+ User user = beanSearcher.searchFirst(User.class, MapUtils.builder().field(User::getId, cpsUserId).onlySelect(User::getPoints).build());
|
|
|
+ BigDecimal thresholdPoints = BigDecimal.valueOf(1000);
|
|
|
+ BigDecimal wApplyMoney = user.getPoints().divide(thresholdPoints, 2, RoundingMode.DOWN);
|
|
|
+ //已提现
|
|
|
+ Number gotApplyMoney = beanSearcher.searchSum(DistributeMoneyApply.class, MapUtils.builder()
|
|
|
+ .field(DistributeMoneyApply::getUserId, cpsUserId)
|
|
|
+ .field(DistributeMoneyApply::getVerifyStatus, DistributeMoneyApply.Status.success.name())
|
|
|
+ .build(), "money");
|
|
|
+ Number reMoneyPoints = beanSearcher.searchSum(DistributeWaitingSendPoints.class, MapUtils.builder().field(DistributeWaitingSendPoints::getSharedId, cpsUserId).field(DistributeWaitingSendPoints::getSendStatus, DistributeWaitingSendPoints.Status.success.name()).build(), "points");
|
|
|
+ Number wMoneyPoints = beanSearcher.searchSum(DistributeWaitingSendPoints.class, MapUtils.builder().field(DistributeWaitingSendPoints::getSharedId, cpsUserId).field(DistributeWaitingSendPoints::getSendStatus, DistributeWaitingSendPoints.Status.waiting.name()).build(), "points");
|
|
|
+ List<UserDistributeRateView> rateViews = beanSearcher.searchAll(UserDistributeRateView.class, MapUtils.builder()
|
|
|
+ .put("distribute_id", String.format("and distribute_id = (select id from user_distribute where user_id = %s and deleted is true limit 1)", cpsUserId))
|
|
|
+ .orderBy(UserDistributeRateView::getRate).desc()
|
|
|
+ .build());
|
|
|
+ CpsUserDistributeDetailView detailView = new CpsUserDistributeDetailView();
|
|
|
+ detailView.setRateViews(rateViews);
|
|
|
+ detailView.setWMoney(BigDecimal.valueOf(wMoneyPoints.doubleValue()).divide(thresholdPoints, 2, RoundingMode.DOWN));
|
|
|
+ detailView.setReMoney(BigDecimal.valueOf(reMoneyPoints.doubleValue()).divide(thresholdPoints, 2, RoundingMode.DOWN));
|
|
|
+
|
|
|
+ detailView.setWApplyMoney(wApplyMoney);
|
|
|
+ detailView.setGotApplyMoney(BigDecimal.valueOf(gotApplyMoney.doubleValue()).divide(BigDecimal.valueOf(100)));
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult(detailView);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 待入账数据统计
|
|
|
+ */
|
|
|
+ @GetMapping("/get/wMoney/statisticsData")
|
|
|
+ public Result<BrokenLineCpsUserWMoneyView> getWMoneyStatisticsDataView(Long firstDate, Long lastDate) {
|
|
|
+ long cpsUserId = StpCpsManageUser.getLoginIdAsLong();
|
|
|
+ DateTime first = null;
|
|
|
+ DateTime end = null;
|
|
|
+ if (firstDate != null) {
|
|
|
+ first = DateUtil.date(firstDate);
|
|
|
+ }
|
|
|
+ if (lastDate != null) {
|
|
|
+ end = DateUtil.date(lastDate);
|
|
|
+ }
|
|
|
+ BrokenLineCpsUserWMoneyView brokenLineCpsUserWMoneyView = Optional.ofNullable(cpsManageService.getWMoneyStatisticsDate(cpsUserId, first, end)).orElse(new BrokenLineCpsUserWMoneyView());
|
|
|
+ List<CpsUserWMoneyStatisticsDataView> dataView = cpsManageService.getWMoneyStatisticsDataView(cpsUserId, first, end);
|
|
|
+ brokenLineCpsUserWMoneyView.setDataView(dataView);
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult(brokenLineCpsUserWMoneyView);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分销订单收入统计
|
|
|
+ */
|
|
|
+ @GetMapping("/get/distribute/orders/statistics")
|
|
|
+ public Result<SearchResult<UserDistributeOrdersStatisticsView>> getUserDistributeOrdersStatisticsView(Long firstDate, Long lastDate) {
|
|
|
+ long cpsUserId = StpCpsManageUser.getLoginIdAsLong();
|
|
|
+ String first = null;
|
|
|
+ String end = null;
|
|
|
+ String timeSql = StrUtil.EMPTY;
|
|
|
+ if (firstDate != null) {
|
|
|
+ first = DateUtil.format(DateTime.of(firstDate), "yyyy-MM-dd HH:mm:ss");
|
|
|
+ timeSql += String.format(" and o.created_time >= '%s'", first);
|
|
|
+ }
|
|
|
+ if (lastDate != null) {
|
|
|
+ end = DateUtil.format(DateTime.of(lastDate), "yyyy-MM-dd HH:mm:ss");
|
|
|
+ timeSql += String.format(" and o.created_time < '%s'", end);
|
|
|
+ }
|
|
|
+ MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
|
|
|
+ builder.put("sharedId", String.format("shared_id = %s", cpsUserId));
|
|
|
+ if (StrUtil.isNotEmpty(timeSql)) {
|
|
|
+ builder.put("time", timeSql);
|
|
|
+ }
|
|
|
+ SearchResult<UserDistributeOrdersStatisticsView> search = beanSearcher.search(UserDistributeOrdersStatisticsView.class, builder.build());
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult(search);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分销订单收入详细
|
|
|
+ */
|
|
|
+ @GetMapping("/get/distribute/orders/detail")
|
|
|
+ public Result<SearchResult<UserDistributeOrdersDetailView>> getUserDistributeOrdersDetailView(Long firstDate, Long lastDate) {
|
|
|
+ long cpsUserId = StpCpsManageUser.getLoginIdAsLong();
|
|
|
+ String first = null;
|
|
|
+ String end = null;
|
|
|
+ String timeSql = StrUtil.EMPTY;
|
|
|
+ if (firstDate != null) {
|
|
|
+ first = DateUtil.format(DateTime.of(firstDate), "yyyy-MM-dd HH:mm:ss");
|
|
|
+ timeSql += String.format(" and o.created_time >= '%s'", first);
|
|
|
+ }
|
|
|
+ if (lastDate != null) {
|
|
|
+ end = DateUtil.format(DateTime.of(lastDate), "yyyy-MM-dd HH:mm:ss");
|
|
|
+ timeSql += String.format(" and o.created_time < '%s'", end);
|
|
|
+ }
|
|
|
+ MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
|
|
|
+ builder.put("sharedId", String.format("shared_id = %s", cpsUserId));
|
|
|
+ if (StrUtil.isNotEmpty(timeSql)) {
|
|
|
+ builder.put("time", timeSql);
|
|
|
+ }
|
|
|
+ SearchResult<UserDistributeOrdersDetailView> search = beanSearcher.search(UserDistributeOrdersDetailView.class, builder.build());
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult(search);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分销商品列表
|
|
|
+ */
|
|
|
+ @GetMapping("/get/goods")
|
|
|
+ public Result<List<GoodsDon>> getGoodsList() {
|
|
|
+ long cpsUserId = StpCpsManageUser.getLoginIdAsLong();
|
|
|
+ List<GoodsDon> goodsDons = goodsDonMapper.getDistributeGoodsDetail(cpsUserId);
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult(goodsDons);
|
|
|
+ }
|
|
|
+}
|