wlzboy
2025-09-21 7d81ce01560d384f15212edc40ebeaa9924913f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package com.ruoyi.web.controller.evaluation;
 
import java.util.List;
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<CustomerEvaluation> 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<CustomerEvaluation> list = customerEvaluationService.selectCustomerEvaluationList(customerEvaluation);
        ExcelUtil<CustomerEvaluation> util = new ExcelUtil<CustomerEvaluation>(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<EvaluationDimension> 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;
 
    /**
     * 获取微信用户信息
     */
    @Anonymous
    @GetMapping("/wechat/userinfo")
    public AjaxResult getWechatUserInfo(String code, HttpServletRequest request) {
        try {
            
            if (StringUtils.isEmpty(code)) {
                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("获取微信用户信息失败");
            }
 
            return success(userInfo);
        } 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;
    }
}