wlzboy
2025-11-07 2aebbc9601ab439707f69b08e467808df9f7549c
ruoyi-admin/src/main/java/com/ruoyi/web/controller/evaluation/EvaluationController.java
@@ -1,5 +1,6 @@
package com.ruoyi.web.controller.evaluation;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
@@ -7,13 +8,13 @@
import com.ruoyi.common.utils.WechatUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.config.WechatMpConfig;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.system.domain.EvaluationDetail;
import com.ruoyi.system.service.IEvaluationDimensionService;
import com.ruoyi.system.domain.EvaluationDimension;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
@@ -46,6 +47,9 @@
    @Autowired
    private IEvaluationDimensionService evaluationDimensionService;
    @Autowired
    private WechatMpConfig wechatMpConfig;
    /**
     * 查询客户评价列表
@@ -149,11 +153,42 @@
        }
    }
    @Value("${wechat.appId}")
    private String wechatAppId;
    @Value("${wechat.appSecret}")
    private String wechatAppSecret;
    /**
     * 生成微信授权URL
     */
    @Anonymous
    @GetMapping("/wechat/authurl")
    public AjaxResult getWechatAuthUrl(String redirectUri, String state) {
        try {
            if (StringUtils.isEmpty(redirectUri)) {
                return error("回调地址不能为空");
            }
            logger.info("生成微信授权URL - 原始redirectUri: {}", redirectUri);
            // 生成微信授权URL,使用snsapi_userinfo获取用户信息
            String authUrl = WechatUtils.generateAuthUrl(
                wechatMpConfig.getAppId(),
                redirectUri,
                "snsapi_userinfo",
                state
            );
            if (authUrl == null) {
                return error("生成微信授权URL失败");
            }
            logger.info("生成微信授权URL成功: {}", authUrl);
            Map<String, String> result = new HashMap<>();
            result.put("authUrl", authUrl);
            result.put("originalRedirectUri", redirectUri);
            result.put("appId", wechatMpConfig.getAppId());
            return success(result);
        } catch (Exception e) {
            logger.error("生成微信授权URL失败", e);
            return error("生成微信授权URL失败: " + e.getMessage());
        }
    }
    /**
     * 获取微信用户信息
@@ -163,12 +198,16 @@
    public AjaxResult getWechatUserInfo(String code, HttpServletRequest request) {
        try {
            
            if (StringUtils.isEmpty(code)) {
            if (code.isEmpty()) {
                return error("授权码不能为空");
            }
            // 获取网页授权Access Token
            JSONObject tokenInfo = WechatUtils.getWebAccessToken(wechatAppId, wechatAppSecret, code);
            JSONObject tokenInfo = WechatUtils.getWebAccessToken(
                wechatMpConfig.getAppId(),
                wechatMpConfig.getAppSecret(),
                code
            );
            if (tokenInfo == null) {
                return error("获取微信授权信息失败");
            }
@@ -182,7 +221,22 @@
                return error("获取微信用户信息失败");
            }
            return success(userInfo);
            // 处理用户信息,确保字段名称一致
            Map<String, Object> result = new HashMap<>();
            result.put("openid", userInfo.getString("openid"));
            result.put("nickname", userInfo.getString("nickname"));
            result.put("headimgurl", userInfo.getString("headimgurl"));
            result.put("sex", userInfo.getInteger("sex"));
            result.put("province", userInfo.getString("province"));
            result.put("city", userInfo.getString("city"));
            result.put("country", userInfo.getString("country"));
            result.put("unionid", userInfo.getString("unionid"));
            // 注意:微信网页授权无法直接获取手机号,需要通过其他方式
            // 如需获取手机号,需要使用微信小程序的getPhoneNumber接口或微信开放平台的手机号快速验证组件
            result.put("phone", "");
            result.put("phoneNote", "微信网页授权无法直接获取手机号,请手动输入");
            return success(result);
        } catch (Exception e) {
            logger.error("获取微信用户信息失败", e);
            return error("获取微信用户信息失败");