|
@@ -1,6 +1,8 @@
|
|
package com.gis.web.controller;
|
|
package com.gis.web.controller;
|
|
|
|
|
|
-import com.gis.common.util.Result;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.gis.common.constant.TypeCode;
|
|
|
|
+import com.gis.common.util.*;
|
|
import com.gis.domain.entity.GoodsEntity;
|
|
import com.gis.domain.entity.GoodsEntity;
|
|
import com.gis.domain.entity.WxCommentEntity;
|
|
import com.gis.domain.entity.WxCommentEntity;
|
|
import com.gis.domain.request.PageRequest;
|
|
import com.gis.domain.request.PageRequest;
|
|
@@ -13,15 +15,16 @@ import com.github.pagehelper.PageInfo;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import lombok.extern.log4j.Log4j2;
|
|
import lombok.extern.log4j.Log4j2;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.junit.Test;
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.validation.Valid;
|
|
import javax.validation.Valid;
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.Date;
|
|
|
|
-import java.util.List;
|
|
|
|
-import java.util.Map;
|
|
|
|
|
|
+import java.util.*;
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -42,6 +45,9 @@ public class WxCommentController extends BaseController {
|
|
@Autowired
|
|
@Autowired
|
|
private CommonMapper commonMapper;
|
|
private CommonMapper commonMapper;
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private RedisTemplate<String, String> redisTemplate;
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
@ApiOperation(value = "列表")
|
|
@ApiOperation(value = "列表")
|
|
@@ -65,26 +71,43 @@ public class WxCommentController extends BaseController {
|
|
|
|
|
|
@ApiOperation(value = "新增")
|
|
@ApiOperation(value = "新增")
|
|
@PostMapping(value = "save")
|
|
@PostMapping(value = "save")
|
|
- public Result save(@Valid @RequestBody WxCommentRequest param) {
|
|
|
|
|
|
+ public Result save(@Valid @RequestBody WxCommentRequest param) throws Exception {
|
|
|
|
|
|
WxCommentEntity entity = new WxCommentEntity();
|
|
WxCommentEntity entity = new WxCommentEntity();
|
|
- BeanUtils.copyProperties(param, entity);
|
|
|
|
- entity.setWxId(getWxOpenId());
|
|
|
|
- wxCommentService.save(entity);
|
|
|
|
|
|
+ String comment = entity.getComment();
|
|
|
|
+ String s = wxMsgCheck(comment);
|
|
|
|
|
|
|
|
+ JSONObject reJson = JSONObject.parseObject(s);
|
|
|
|
+ Integer errcode = reJson.getInteger("errcode");
|
|
|
|
|
|
|
|
|
|
- GoodsEntity goods = goodsService.findById(param.getGoodsId());
|
|
|
|
- if (goods == null) {
|
|
|
|
- log.error("对象不存在: {}", param.getGoodsId());
|
|
|
|
- return Result.failure("对象不存在");
|
|
|
|
- }
|
|
|
|
|
|
+ log.info("result: {}", s);
|
|
|
|
+ if (errcode == 0) {
|
|
|
|
|
|
- goods.setCountComment(goods.getCountComment() + 1);
|
|
|
|
|
|
+ BeanUtils.copyProperties(param, entity);
|
|
|
|
+ entity.setWxId(getWxOpenId());
|
|
|
|
+ wxCommentService.save(entity);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ GoodsEntity goods = goodsService.findById(param.getGoodsId());
|
|
|
|
+ if (goods == null) {
|
|
|
|
+ log.error("对象不存在: {}", param.getGoodsId());
|
|
|
|
+ return Result.failure("对象不存在");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ goods.setCountComment(goods.getCountComment() + 1);
|
|
|
|
+
|
|
|
|
+ goodsService.update(goods);
|
|
|
|
+
|
|
|
|
+ return Result.success();
|
|
|
|
+ } else {
|
|
|
|
+ String errmsg = reJson.getString("errmsg");
|
|
|
|
+ log.error("留言内容违法: {}",errmsg);
|
|
|
|
+ return Result.failure(errmsg);
|
|
|
|
+ }
|
|
|
|
|
|
- goodsService.update(goods);
|
|
|
|
|
|
|
|
- return Result.success();
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -108,8 +131,52 @@ public class WxCommentController extends BaseController {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 微信api
|
|
|
|
+ * 检查一段文本是否含有违法违规内容
|
|
|
|
+ * @param msg
|
|
|
|
+ * @return
|
|
|
|
+ * @throws Exception
|
|
|
|
+ */
|
|
|
|
+ @Test
|
|
|
|
+ public String wxMsgCheck(String msg) throws Exception {
|
|
|
|
+// String AccessToken = "35_fgwbTUFcmsorNtFCEF0bi1qimwmLizUmo_P8wLki-Tc9Lhvdy9cLyUuVD7tI5mvCGrA4CkMHXL89mxZ5l1BZIO4H5Pjop-ugvPa0OZ1TKFbVqUjLK4xWycqIT6AqGaS1guv54Sq1gr36U_4NCHHgABAKPP";
|
|
|
|
+ String url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=" + getAccessToken();
|
|
|
|
+// String url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=" + AccessToken;
|
|
|
|
+
|
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
|
+ jsonObject.put("content", msg);
|
|
|
|
+
|
|
|
|
+ return HttpClientUtil.doPostJson(url, jsonObject.toJSONString());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取微信access_token
|
|
|
|
+ * 有效期7200s, 两小时
|
|
|
|
+ *
|
|
|
|
+ * GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
|
|
|
|
+ */
|
|
|
|
+ private String getAccessToken(){
|
|
|
|
|
|
|
|
+ // 校验请求token是否跟redis token一致
|
|
|
|
+ String accessToken = redisTemplate.opsForValue().get(TypeCode.REDIS_WX_ACCESS_TOKEN_KEY);
|
|
|
|
+ if (StringUtils.isBlank(accessToken)) {
|
|
|
|
+ accessToken = WxUtil.getAccessToken(APP_ID, APP_SECRET);
|
|
|
|
|
|
|
|
|
|
|
|
+ // 更新accessToken
|
|
|
|
+ // 更新accessToken, 有效期7200s, 旧token无效, 做单用户登录
|
|
|
|
+ redisTemplate.opsForValue().set(TypeCode.REDIS_WX_ACCESS_TOKEN_KEY, accessToken, Long.parseLong("2"), TimeUnit.HOURS);
|
|
|
|
+ }
|
|
|
|
+ return accessToken;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @ApiOperation("AccessToken")
|
|
|
|
+ @GetMapping("get")
|
|
|
|
+ public Result get(){
|
|
|
|
+ return Result.success(getAccessToken());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
}
|
|
}
|