wlzboy
2025-09-27 c8a3df8ef73d08de60ca49e09a343e87bcc66a91
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
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<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;
 
    /**
     * 生成微信授权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<String, String> 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<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("获取微信用户信息失败");
        }
    }
 
 
    /**
     * 获取评价统计数据
     */
    @PreAuthorize("@ss.hasPermi('evaluation:statistics')")
    @GetMapping("/evaluation/statistics")
    public AjaxResult getStatistics(CustomerEvaluation customerEvaluation) {
        try {
            // 获取统计数据
            Map<String, Object> 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;
    }
}