UserController.java 5.56 KB
package vion.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.linpeilie.Converter;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.lang.Opt;
import org.dromara.hutool.core.net.url.UrlEncoder;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.tree.MapTree;
import org.springframework.web.bind.annotation.*;
import vion.dto.DingDTO;
import vion.dto.UserDTO;
import vion.model.RUserRole;
import vion.model.Role;
import vion.model.User;
import vion.service.IRUserRoleService;
import vion.service.IRoleService;
import vion.service.IUserService;
import vion.third.DingMod;
import vion.third.WechatMod;
import vion.vo.RoleVO;
import vion.vo.UserVO;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
@Slf4j
public class UserController {

    private final IUserService userService;
    private final IRUserRoleService userRoleService;
    private final IRoleService roleService;
    private final Converter converter;
    private final DingMod dingMod;
    private final WechatMod wechatMod;

    @GetMapping("/users")
    @SaCheckPermission(value = "user:list", orRole = "admin")
    public Page<UserVO> getUserList(UserDTO dto) {
        return userService.getUserList(dto);
    }

    @GetMapping("/user/onlyName")
    @SaCheckPermission(value = "user:listName", orRole = "admin")
    public List<UserVO> getNameList(UserDTO dto) {
        return converter.convert(userService.lambdaQuery(converter.convert(dto, new User()))
                .select(User::getId, User::getUserid, User::getUsername, User::getPreWorkOrder, User::getEmployeeStatus)
                .ne(User::getStatus, 5)
                .list(), UserVO.class);
    }

    @GetMapping("/user")
    @SaCheckPermission(value = "user:query", orRole = "admin")
    public User getUserById(@RequestParam(name = "id") Integer id) {
        return userService.getById(id);
    }

    @PostMapping("/user")
    @SaCheckPermission(value = "user:edit", orRole = "admin")
    public String update(@RequestBody List<User> data) {
        return Opt.ofEmptyAble(data).map(userService::updateBatchById).orElse(false) ? "更新成功" : "更新失败";
    }

    @GetMapping("/user/{token}")
    @SaCheckPermission(value = "user:token:query", orRole = "admin")
    public UserVO getUserByToken(@PathVariable String token) {
        return (UserVO) StpUtil.getTokenSessionByToken(token).get("curLoginUser");
    }

    @GetMapping("/orgTree")
    @SaCheckPermission(value = "user:orgTree", orRole = "admin")
    public List<MapTree<String>> getOrgTree(String deptId) {
        return userService.getOrgTree(deptId);
    }

    @GetMapping("/user/role/{id}")
    @SaCheckPermission(value = "user:role", orRole = "admin")
    public List<RoleVO> listRoleById(@PathVariable Long id) {
        List<RUserRole> userRoleList = userRoleService.lambdaQuery().eq(RUserRole::getUserId, id).list();
        List<Role> roleList = Opt.ofEmptyAble(userRoleList)
                .map(l -> l.stream().map(RUserRole::getRoleId).collect(Collectors.toList()))
                .filter(CollUtil::isNotEmpty)
                .map(roleIdList -> roleService.lambdaQuery().in(Role::getId, roleIdList).list())
                .orElse(ListUtil.empty());
        return converter.convert(roleList, RoleVO.class);
    }

    @GetMapping("/user/manual")
    @SaCheckPermission(value = "user:manual", orRole = "admin")
    public String manualUser() {
        dingMod.getDingUserList();
        return "成功";
    }

    @GetMapping("/ding/callback/{target}")
    public Object dingCallback(@PathVariable String target, DingDTO dto, HttpServletResponse res) {
        return dingMod.dingCallback(target, dto, res);
    }

    @GetMapping("/ding/getPlatformToken")
    public Object getPlatformToken(String authCode) {
        return dingMod.getPlatformToken(authCode);
    }

    @GetMapping("/logout")
    public String logout() {
        StpUtil.logout();
        return "注销成功";
    }

    /**
     * 用于微信公众号自定义菜单里填写的回调地址
     *
     * @param code   授权码
     * @param active 前端标识,根据此标识跳转到不同的页面
     * @param res    response
     * @return java.lang.Object
     */
    @GetMapping("/wechatCallback")
    public Object wechatCallback(String code, Integer active, HttpServletResponse res) throws IOException {
        Object obj = wechatMod.getOpenid(code);
        if (obj instanceof Map) {
            Map<String, String> map = (Map<String, String>) obj;
            res.sendRedirect(StrUtil.format("https://yunwei.vionyun.com/wap?openid={}&nickname={}&active={}", map.get("openid"), UrlEncoder.encodeAll(map.get("nickname")), active));
        } else {
            return obj;
        }
        return "success";
    }

    @GetMapping("/getQRCode")
    public String getCallbackQRCode(Long id, Integer flag) {
        Assert.notNull(id, "id 不能为空");
        Assert.notNull(flag, "flag 不能为空");
        return wechatMod.genCallbackQRCode(id, flag);
    }

    @GetMapping("/getFollowingOpenid")
    public Object getOpenid(Long id, Integer flag, String code) {
        return wechatMod.getFollowingOpenid(id, flag, code);
    }

}