liujie
2025-12-25 ac337f75ec0277e9ddfa2dfd5ff84e0277d378b3
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java
@@ -1,6 +1,10 @@
package com.ruoyi.web.controller.system;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
@@ -23,13 +27,15 @@
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.text.MessageFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
@@ -181,10 +187,57 @@
        }
        user.setCreateBy(getUsername());
        user.setPassword(SecurityUtils.encryptPassword("123456"));
        // 随机10数数字
        user.setCode(RandomUtil.randomNumbers(10));
        userService.insertUser(user);
        if(user.getCompanyId()!=null){
            String accessToken = getAccessToken();
            String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;
            Map<String, Object> param = new HashMap<>();
            param.put("page", "pages/home/home");
            param.put("width", 430); //二维码尺寸
            param.put("scene", "userId=" + user.getUserId()); //二维码尺寸
            param.put("env_version", "trial"); //二维码尺寸
            HttpRequest post = HttpUtil.createPost(url);
            post.body(JSON.toJSONString(param));
            byte[] result = post.execute().bodyBytes();
            // base64
            String img = Base64.getEncoder().encodeToString(result);
            // 后台用户的小程序码
            redisTemplate.opsForValue().set("ADMIN_WX_IMG" + user.getUserId(), img);
        }
        return R.ok();
    }
    @Value("${wx.url}")
    private String ACCESS_TOKEN_URL;
    @Value("${wx.conf.secretId}")
    private String memberAppSecret;
    @Value("${wx.conf.appId}")
    private String wxAppId;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private RedisTemplate redisTemplate;
    public  String getAccessToken() {
        Object o = redisTemplate.opsForValue().get("accessToken");
        if(o!=null){
            return o.toString();
        }
        String requestUrl = MessageFormat.format(ACCESS_TOKEN_URL,wxAppId,memberAppSecret);
        String respBody = restTemplate.getForEntity(requestUrl, String.class).getBody();
        JSONObject jsonObject = JSONObject.parseObject(respBody);
        String string = jsonObject.getString("access_token");
        redisTemplate.opsForValue().set("accessToken", string,5000,TimeUnit.SECONDS);
        return string;
    }
    /**
     * 账号继承
     */