Эх сурвалжийг харах

将web登录接口迁移到geteway中

qrs 4 сар өмнө
parent
commit
df9decf912

+ 54 - 0
alien-gateway/src/main/java/shop/alien/gateway/controller/SystemController.java

@@ -0,0 +1,54 @@
+package shop.alien.gateway.controller;
+
+import io.swagger.annotations.*;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.bind.annotation.*;
+import shop.alien.entity.result.R;
+import shop.alien.entity.store.UserLoginInfo;
+import shop.alien.entity.store.dto.SystemLoginDto;
+import shop.alien.entity.store.vo.SystemLoginVo;
+import shop.alien.gateway.service.SystemService;
+import shop.alien.util.common.TokenInfo;
+import springfox.documentation.annotations.ApiIgnore;
+
+/**
+ * 商家会员记录 前端控制器
+ *
+ * @author ssk
+ * @since 2025-02-20
+ */
+@Slf4j
+@Api(tags = {"数据中台基础服务接口"})
+@ApiSort(1)
+@CrossOrigin
+@RestController
+@RequestMapping("/sys")
+@RequiredArgsConstructor
+public class SystemController {
+
+    private final SystemService systemService;
+
+    @ApiOperation("web中台登录")
+    @ApiOperationSupport(order = 1)
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "userName", value = "用户名", dataType = "String", paramType = "query"),
+            @ApiImplicitParam(name = "password", value = "密码", dataType = "String", paramType = "query"),
+    })
+    @PostMapping(value = "/login")
+    public R<SystemLoginVo> getStoreVipLogPage(@RequestBody SystemLoginDto systemLogin) {
+        return R.data(systemService.login(systemLogin.getUsername(), systemLogin.getPassword()));
+    }
+
+    @ApiOperation("web中台退出登录")
+    @ApiOperationSupport(order = 1)
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "userName", value = "用户名", dataType = "String", paramType = "query"),
+            @ApiImplicitParam(name = "password", value = "密码", dataType = "String", paramType = "query"),
+    })
+    @PostMapping(value = "/logout")
+    public R<SystemLoginVo> logout(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo) {
+        return R.success("退出成功");
+    }
+
+}

+ 12 - 0
alien-gateway/src/main/java/shop/alien/gateway/mapper/LifeSysMapper.java

@@ -0,0 +1,12 @@
+package shop.alien.gateway.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Mapper;
+import shop.alien.entity.store.LifeSys;
+
+/**
+ * 系统用户
+ */
+@Mapper
+public interface LifeSysMapper extends BaseMapper<LifeSys> {
+}

+ 18 - 0
alien-gateway/src/main/java/shop/alien/gateway/service/SystemService.java

@@ -0,0 +1,18 @@
+package shop.alien.gateway.service;
+
+import shop.alien.entity.store.UserLoginInfo;
+import shop.alien.entity.store.vo.SystemLoginVo;
+
+/**
+ * 数据中台基本服务接口
+ *
+ * @author YinDP
+ * @since 2025-02-27
+ */
+public interface SystemService {
+
+    public SystemLoginVo login(String username, String password);
+
+    public SystemLoginVo logout(UserLoginInfo userLoginInfo);
+
+}

+ 121 - 0
alien-gateway/src/main/java/shop/alien/gateway/service/impl/SystemServiceImpl.java

@@ -0,0 +1,121 @@
+package shop.alien.gateway.service.impl;
+
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import lombok.RequiredArgsConstructor;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+import shop.alien.entity.store.LifeSys;
+import shop.alien.entity.store.UserLoginInfo;
+import shop.alien.entity.store.vo.SystemLoginVo;
+import shop.alien.gateway.config.BaseRedisService;
+import shop.alien.gateway.mapper.LifeSysMapper;
+import shop.alien.gateway.service.SystemService;
+import shop.alien.util.common.JwtUtil;
+
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * <p>
+ * 商家会员记录 服务实现类
+ * </p>
+ *
+ * @author ssk
+ * @since 2025-02-20
+ */
+@Service
+@RequiredArgsConstructor
+public class SystemServiceImpl implements SystemService {
+
+    private final LifeSysMapper lifeSysMapper;
+
+    private final BaseRedisService baseRedisService;
+
+    @Value("${jwt.expiration-time}")
+    private String effectiveTime;
+
+    @Override
+    public SystemLoginVo login(String username, String password) {
+        int effectiveTimeInt = Integer.parseInt(effectiveTime.substring(0, effectiveTime.length() - 1));
+        String effectiveTimeUnit = effectiveTime.substring(effectiveTime.length() - 1);
+        long effectiveTimeIntLong = 0L;
+        switch (effectiveTimeUnit) {
+            case "s": {
+                effectiveTimeIntLong = effectiveTimeInt * 1000L;
+                break;
+            }
+            case "m": {
+                effectiveTimeIntLong = effectiveTimeInt * 60L * 1000L;
+                break;
+            }
+            case "h": {
+                effectiveTimeIntLong = effectiveTimeInt * 60L * 60L * 1000L;
+                break;
+            }
+            case "d": {
+                effectiveTimeIntLong = effectiveTimeInt * 24L * 60L * 60L * 1000L;
+                break;
+            }
+        }
+        SystemLoginVo result = new SystemLoginVo();
+        //给密码加密MD5,查询用户是否存在
+        LifeSys lifeSys = lifeSysMapper.selectOne(
+                new LambdaQueryWrapper<LifeSys>()
+                        .eq(LifeSys::getUserName, username)
+        );
+        if (lifeSys != null && encryptToMD5(lifeSys.getUserPassword()).equals(password)) {
+            Map<String, String> tokenMap = new HashMap<>();
+            tokenMap.put("phone", "123456");
+            tokenMap.put("userName", lifeSys.getUserName());
+            tokenMap.put("userId", lifeSys.getId());
+            tokenMap.put("userType", "web");
+            //存入token
+            result.setToken(JwtUtil.createJWT("web_" + lifeSys.getId(), lifeSys.getUserName(), JSONObject.toJSONString(tokenMap), effectiveTimeIntLong));
+            baseRedisService.setString("web_" + lifeSys.getUserName(), result.getToken());
+            //登录结果
+            result.setResult(true);
+            //登录结果
+            result.setMessage("登录成功!!");
+        } else {
+            result.setResult(false);
+            result.setMessage("用户名或者密码错误,请确认!!");
+        }
+        return result;
+    }
+
+    @Override
+    public SystemLoginVo logout(UserLoginInfo userLoginInfo) {
+        //JWT移除token
+        return null;
+    }
+
+    public static String encryptToMD5(String input) {
+        try {
+            // 获取 MD5 算法的 MessageDigest 实例
+            MessageDigest md = MessageDigest.getInstance("MD5");
+            // 将输入的字符串转换为字节数组,并进行 MD5 计算
+            byte[] messageDigest = md.digest(input.getBytes());
+
+            // 创建一个 StringBuilder 用于存储十六进制字符串
+            StringBuilder hexString = new StringBuilder();
+            for (byte b : messageDigest) {
+                // 将字节转换为十六进制字符串
+                String hex = Integer.toHexString(0xFF & b);
+                if (hex.length() == 1) {
+                    // 如果十六进制字符串长度为 1,则在前面补 0
+                    hexString.append('0');
+                }
+                hexString.append(hex);
+            }
+            // 返回最终的 MD5 加密后的十六进制字符串
+            return hexString.toString();
+        } catch (NoSuchAlgorithmException e) {
+            // 若指定的算法(MD5)不可用,抛出运行时异常
+            throw new RuntimeException(e);
+        }
+    }
+
+}