MallAuthApi.java 2.82 KB
package com.diligrp.cashier.mall.api;

import com.diligrp.cashier.mall.MallConstants;
import com.diligrp.cashier.mall.context.MallInitializeContext;
import com.diligrp.cashier.mall.domain.rtmall.RtMarkMessage;
import com.diligrp.cashier.mall.domain.rtmall.co.AuthLoginCO;
import com.diligrp.cashier.mall.domain.rtmall.co.UserInfoCO;
import com.diligrp.cashier.mall.domain.rtmall.vo.UserInfoVO;
import com.diligrp.cashier.mall.exception.RtMartMallException;
import com.diligrp.cashier.mall.sign.RtMallSign;
import com.diligrp.cashier.mall.type.RtMarkErrorCode;
import com.diligrp.cashier.mall.util.RtMallValidateUtils;
import com.diligrp.cashier.shared.annotation.ParamLogPrint;
import com.diligrp.cashier.shared.annotation.Sign;
import com.diligrp.cashier.shared.domain.Message;
import com.diligrp.cashier.shared.util.JsonUtils;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @ClassName MallAuthApi.java
 * @author dengwei
 * @version 1.0.0
 * @Description MallAuthApi
 */
@RestController
@RequestMapping(path = {"/mall", "/rt/mall"})
@Validated
public class MallAuthApi {
    private static final Logger log = LoggerFactory.getLogger(MallAuthApi.class);
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 获取授权连接
     */
    @PostMapping("/auth")
    @ParamLogPrint(outPrint = true)
    public Message<String> authLogin(@RequestBody @Valid AuthLoginCO authLogin) {
        return Message.success(MallInitializeContext.getBySource(authLogin.getSource()).authLogin(authLogin));
    }

    /**
     * 获取用户信息
     */
    @PostMapping("/user/info")
    @ParamLogPrint(outPrint = true)
    @Sign(sign = RtMallSign.class)
    public RtMarkMessage<UserInfoVO> userInfo(@RequestBody @Valid Object req) {
        UserInfoCO userInfo = JsonUtils.convertValue(req, UserInfoCO.class);
        RtMallValidateUtils.valid(userInfo);

        // token认证
        Object cache = redisTemplate.opsForValue().get(MallConstants.MALL_TOKEN + userInfo.getToken());
        if (Objects.isNull(cache)) {
            throw new RtMartMallException(RtMarkErrorCode.E5001);
        }

        AuthLoginCO authLogin = JsonUtils.fromJsonString(Objects.requireNonNull(cache).toString(), AuthLoginCO.class);
        UserInfoVO userInfoVO = MallInitializeContext.getBySource(authLogin.getSource()).userInfo(authLogin);
        return RtMarkMessage.success(userInfoVO);
    }
}