“mitao”
2025-12-22 2d53554241065ae5b2bdf98b2f63363285c76c7c
feat:小程序端获取个人信息接口增加推广码生成
3个文件已添加
3个文件已修改
147 ■■■■■ 已修改文件
ruoyi-applet/src/main/java/com/ruoyi/web/controller/api/UserIndexController.java 30 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-applet/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java 1 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-system/src/main/java/com/ruoyi/system/contstant/RedisConstant.java 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-system/src/main/java/com/ruoyi/system/enums/UserTypeEnum.java 23 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-system/src/main/java/com/ruoyi/system/model/TAppUser.java 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-system/src/main/java/com/ruoyi/system/utils/wx/WxQrCodeUtil.java 75 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-applet/src/main/java/com/ruoyi/web/controller/api/UserIndexController.java
@@ -4,17 +4,25 @@
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.contstant.RedisConstant;
import com.ruoyi.system.dto.UpdatePhoneDto;
import com.ruoyi.system.dto.UpdateUserDto;
import com.ruoyi.system.enums.UserTypeEnum;
import com.ruoyi.system.model.TAppUser;
import com.ruoyi.system.service.TAppUserService;
import com.ruoyi.system.service.TCommissionTransactionFlowService;
import com.ruoyi.system.utils.wx.WxQrCodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.Objects;
/**
 * 个人中心相关接口
@@ -29,14 +37,34 @@
    private final TAppUserService tAppUserService;
    private final TokenService tokenService;
    private final TCommissionTransactionFlowService commissionTransactionFlowService;
    private final WxQrCodeUtil wxQrCodeUtil;
    private final RedisTemplate redisTemplate;
    @ApiOperation(value = "个人中心--用户信息",response = TAppUser.class)
    @GetMapping(value = "/getUserInfo")
    public R<TAppUser> getUserInfo() {
        Integer userId = tokenService.getLoginUserApplet().getUserId();
        TAppUser appUser = tAppUserService.getById(userId);
        if (!appUser.getUserType().equals(UserTypeEnum.VISITOR.getCode())) {
            Object cache = redisTemplate.opsForValue().get(RedisConstant.APP_USER_WX_IMG + appUser.getId());
            if (Objects.isNull(cache)) {
                if (StringUtils.isNotBlank(appUser.getCode())) {
                    // 获取推广码
                    String qrCodeBase64 = null;
                    try {
                        qrCodeBase64 = wxQrCodeUtil.getQrCodeBase64(appUser.getCode(), 2, RedisConstant.APP_USER_WX_IMG + appUser.getId());
                    } catch (Exception e) {
                        log.error("生成推广二维码失败,用户ID:{}", appUser.getId(), e);
                    }
                    appUser.setPromotionQrCode(qrCodeBase64);
        return R.ok();
                }
            } else {
                appUser.setPromotionQrCode(cache.toString());
            }
        }
        //todo 查询预估积分
        return R.ok(appUser);
    }
    @ApiOperation(value = "个人中心--修改手机号")
ruoyi-applet/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java
@@ -176,6 +176,7 @@
            }
            // 手机号相同但openId为空的情况
            sysAppUser.setOpenid(openid);
            sysAppUser.setPhone(appletUserDecodeData.getPhoneNumber());
            appUserService.updateById(sysAppUser);
        }
        LoginUserApplet loginUserApplet = new LoginUserApplet();
ruoyi-system/src/main/java/com/ruoyi/system/contstant/RedisConstant.java
New file
@@ -0,0 +1,8 @@
package com.ruoyi.system.contstant;
public interface RedisConstant {
    // 股东
    String ADMIN_WX_IMG = "ADMIN_WX_IMG";
    // 营销员/合伙人
    String APP_USER_WX_IMG = "APP_USER_WX_IMG";
}
ruoyi-system/src/main/java/com/ruoyi/system/enums/UserTypeEnum.java
New file
@@ -0,0 +1,23 @@
package com.ruoyi.system.enums;
import lombok.Getter;
import lombok.AllArgsConstructor;
@Getter
@AllArgsConstructor
public enum UserTypeEnum {
    VISITOR(1, "游客"),
    SALESMAN(2, "营销员"),
    PARTNER(3, "合伙人");
    private final Integer code;
    private final String desc;
    public static UserTypeEnum getEnumByCode(Integer code) {
        for (UserTypeEnum e : UserTypeEnum.values()) {
            if (e.code.equals(code)) {
                return e;
            }
        }
        return null;
    }
}
ruoyi-system/src/main/java/com/ruoyi/system/model/TAppUser.java
@@ -71,7 +71,7 @@
    @ApiModelProperty(value = "微信openid")
    @TableField("openid")
    private String openid;
// 1:游客;2:营销员;3:合伙人;
    @ApiModelProperty(value = "角色(1=游客,2=营销员,3=合伙人)")
    @TableField("user_type")
    private Integer userType;
@@ -80,6 +80,10 @@
    @TableField("create_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;
    @ApiModelProperty(value = "历史累计积分")
    @TableField("total_accumulative_integral")
    private Integer totalAccumulativeIntegral;
    @ApiModelProperty(value = "累计积分")
    @TableField("accumulative_integral")
@@ -113,4 +117,8 @@
    @TableField("referrer_sys_user_id")
    private Integer referrerSysUserId;
    @ApiModelProperty(value = "当月预估积分")
    @TableField(exist = false)
    private Integer estimatedMonthlyIntegral;
}
ruoyi-system/src/main/java/com/ruoyi/system/utils/wx/WxQrCodeUtil.java
New file
@@ -0,0 +1,75 @@
package com.ruoyi.system.utils.wx;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.icbc.api.internal.util.internal.util.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.text.MessageFormat;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Component
public class WxQrCodeUtil {
    @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;
    private 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;
    }
    /**
     *
     * @param code 推荐码
     * @param type 推荐人类型 1:股东 2:营销员/合伙人
     * @param cacheKey 缓存key
     * @return
     */
    public String getQrCodeBase64(String code, Integer type, String cacheKey){
        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", "code=" + code + "&type=" + type); // 二维码尺寸
        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(cacheKey, img);
        return img;
    }
}