package com.ruoyi.web.controller.evaluation; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.ruoyi.common.utils.WechatUtils; import com.ruoyi.common.utils.StringUtils; 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; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ruoyi.common.annotation.Log; import com.ruoyi.common.annotation.Anonymous; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.system.domain.CustomerEvaluation; import com.ruoyi.system.service.ICustomerEvaluationService; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.common.core.page.TableDataInfo; /** * 客户评价Controller * * @author ruoyi * @date 2025-01-27 */ @RestController @RequestMapping("/evaluation") public class EvaluationController extends BaseController { @Autowired private ICustomerEvaluationService customerEvaluationService; @Autowired private IEvaluationDimensionService evaluationDimensionService; /** * 查询客户评价列表 */ @PreAuthorize("@ss.hasPermi('evaluation:list')") @GetMapping("/list") public TableDataInfo list(CustomerEvaluation customerEvaluation) { startPage(); List list = customerEvaluationService.selectCustomerEvaluationList(customerEvaluation); return getDataTable(list); } /** * 导出客户评价列表 */ @PreAuthorize("@ss.hasPermi('evaluation:export')") @Log(title = "客户评价", businessType = BusinessType.EXPORT) @PostMapping("/export") public void export(HttpServletResponse response, CustomerEvaluation customerEvaluation) { List list = customerEvaluationService.selectCustomerEvaluationList(customerEvaluation); ExcelUtil util = new ExcelUtil(CustomerEvaluation.class); util.exportExcel(response, list, "客户评价数据"); } /** * 获取客户评价详细信息 */ @PreAuthorize("@ss.hasPermi('evaluation:query')") @GetMapping(value = "/{evaluationId}") public AjaxResult getInfo(@PathVariable("evaluationId") Long evaluationId) { return success(customerEvaluationService.selectCustomerEvaluationByEvaluationId(evaluationId)); } /** * 新增客户评价 */ @PreAuthorize("@ss.hasPermi('evaluation:add')") @Log(title = "客户评价", businessType = BusinessType.INSERT) @PostMapping public AjaxResult add(@RequestBody CustomerEvaluation customerEvaluation) { return toAjax(customerEvaluationService.insertCustomerEvaluation(customerEvaluation)); } /** * 修改客户评价 */ @PreAuthorize("@ss.hasPermi('evaluation:edit')") @Log(title = "客户评价", businessType = BusinessType.UPDATE) @PutMapping public AjaxResult edit(@RequestBody CustomerEvaluation customerEvaluation) { return toAjax(customerEvaluationService.updateCustomerEvaluation(customerEvaluation)); } /** * 删除客户评价 */ @PreAuthorize("@ss.hasPermi('evaluation:remove')") @Log(title = "客户评价", businessType = BusinessType.DELETE) @DeleteMapping("/{evaluationIds}") public AjaxResult remove(@PathVariable Long[] evaluationIds) { return toAjax(customerEvaluationService.deleteCustomerEvaluationByEvaluationIds(evaluationIds)); } /** * 获取评价维度配置 */ @Anonymous @GetMapping("/dimensions") public AjaxResult getDimensions() { List dimensions = evaluationDimensionService.selectEnabledEvaluationDimensionList(); return success(dimensions); } /** * 提交客户评价(公开接口) */ @Anonymous @PostMapping("/submit") public AjaxResult submitEvaluation(@RequestBody CustomerEvaluation customerEvaluation, HttpServletRequest request) { try { // 设置IP地址和用户代理 customerEvaluation.setIpAddress(getClientIP(request)); customerEvaluation.setUserAgent(request.getHeader("User-Agent")); // 提交评价 int result = customerEvaluationService.submitCustomerEvaluation(customerEvaluation); if (result > 0) { // 根据评分返回不同的提示信息 if (customerEvaluation.getTotalScore() != null && customerEvaluation.getTotalScore().doubleValue() < 3.0) { return success("提交成功,我们将竭力改进"); } else { return success("提交成功,感谢您的参与"); } } else { return error("提交失败,请重试"); } } catch (Exception e) { logger.error("提交评价失败", e); return error("提交失败,请重试"); } } @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(wechatAppId, redirectUri, "snsapi_userinfo", state); if (authUrl == null) { return error("生成微信授权URL失败"); } logger.info("生成微信授权URL成功: {}", authUrl); Map result = new HashMap<>(); result.put("authUrl", authUrl); result.put("originalRedirectUri", redirectUri); result.put("appId", wechatAppId); return success(result); } catch (Exception e) { logger.error("生成微信授权URL失败", e); return error("生成微信授权URL失败: " + e.getMessage()); } } /** * 获取微信用户信息 */ @Anonymous @GetMapping("/wechat/userinfo") public AjaxResult getWechatUserInfo(String code, HttpServletRequest request) { try { if (code.isEmpty()) { return error("授权码不能为空"); } // 获取网页授权Access Token JSONObject tokenInfo = WechatUtils.getWebAccessToken(wechatAppId, wechatAppSecret, code); if (tokenInfo == null) { return error("获取微信授权信息失败"); } String accessToken = tokenInfo.getString("access_token"); String openid = tokenInfo.getString("openid"); // 获取用户信息 JSONObject userInfo = WechatUtils.getWebUserInfo(accessToken, openid); if (userInfo == null) { return error("获取微信用户信息失败"); } // 处理用户信息,确保字段名称一致 Map 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("获取微信用户信息失败"); } } /** * 获取评价统计数据 */ @PreAuthorize("@ss.hasPermi('evaluation:statistics')") @GetMapping("/evaluation/statistics") public AjaxResult getStatistics(CustomerEvaluation customerEvaluation) { try { // 获取统计数据 Map statistics = customerEvaluationService.getEvaluationStatistics(customerEvaluation); return success(statistics); } catch (Exception e) { logger.error("获取评价统计数据失败", e); return error("获取统计数据失败"); } } /** * 获取客户端IP地址 */ private String getClientIP(HttpServletRequest request) { String ip = request.getHeader("X-Forwarded-For"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_CLIENT_IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_X_FORWARDED_FOR"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return ip; } }