10 changed files with 452 additions and 1 deletions
@ -0,0 +1,110 @@ |
|||||
|
package com.bnyer.img.controller; |
||||
|
|
||||
|
import cn.hutool.http.server.HttpServerRequest; |
||||
|
import com.alibaba.fastjson.JSON; |
||||
|
import com.alibaba.fastjson.JSONObject; |
||||
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
||||
|
import com.bnyer.common.core.domain.R; |
||||
|
import com.bnyer.common.core.web.controller.BaseController; |
||||
|
import com.bnyer.common.core.web.domain.AjaxResult; |
||||
|
import com.bnyer.common.core.web.page.TableDataInfo; |
||||
|
import com.bnyer.common.redis.service.RedisService; |
||||
|
import com.bnyer.img.domain.Banner; |
||||
|
import com.bnyer.img.domain.BzData; |
||||
|
import com.bnyer.img.dto.BannerDto; |
||||
|
import com.bnyer.img.dto.BannerPageDto; |
||||
|
import com.bnyer.img.dto.BzDto; |
||||
|
import com.bnyer.img.dto.StatusDto; |
||||
|
import com.bnyer.img.service.BannerService; |
||||
|
import com.bnyer.img.service.BzDataService; |
||||
|
import com.github.pagehelper.PageHelper; |
||||
|
import com.xxl.job.core.biz.model.ReturnT; |
||||
|
import com.xxl.job.core.log.XxlJobLogger; |
||||
|
import io.swagger.annotations.Api; |
||||
|
import io.swagger.annotations.ApiOperation; |
||||
|
import io.swagger.annotations.ApiParam; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.http.HttpEntity; |
||||
|
import org.apache.http.HttpResponse; |
||||
|
import org.apache.http.client.methods.HttpGet; |
||||
|
import org.apache.http.impl.client.CloseableHttpClient; |
||||
|
import org.apache.http.impl.client.HttpClients; |
||||
|
import org.apache.http.util.EntityUtils; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.context.annotation.Lazy; |
||||
|
import org.springframework.validation.annotation.Validated; |
||||
|
import org.springframework.web.bind.annotation.*; |
||||
|
|
||||
|
import javax.servlet.http.HttpServletRequest; |
||||
|
import java.io.IOException; |
||||
|
import java.net.URLEncoder; |
||||
|
import java.time.LocalDate; |
||||
|
import java.util.List; |
||||
|
import java.util.Set; |
||||
|
|
||||
|
@Api(value = "【图文平台】八字运势接口",tags = "【图文平台】八字运势接口") |
||||
|
@RestController |
||||
|
@RequestMapping("/img/bz") |
||||
|
@Slf4j |
||||
|
public class BzDataController extends BaseController { |
||||
|
|
||||
|
@Autowired |
||||
|
private BzDataService bzDataService; |
||||
|
@Autowired |
||||
|
private RedisService redisService; |
||||
|
//@RequiresPermissions("system:config:list")
|
||||
|
@ApiOperation(value = "八字算命") |
||||
|
@PostMapping("/getYs") |
||||
|
public R<JSONObject> getYs(@RequestBody @ApiParam("八字运势") BzDto dto) { |
||||
|
JSONObject jsonObject = bzDataService.getYs(dto); |
||||
|
if (jsonObject!=null){ |
||||
|
return R.ok(jsonObject); |
||||
|
} |
||||
|
return R.fail(); |
||||
|
} |
||||
|
|
||||
|
@ApiOperation(value = "八字页面数据统计") |
||||
|
@PostMapping("/dataStatistics") |
||||
|
public R<JSONObject> dataStatistics(@RequestParam String source, HttpServletRequest request) { |
||||
|
String ip = getIpAddr(request); |
||||
|
bzDataService.dataStatistics(ip,source); |
||||
|
return R.ok(); |
||||
|
} |
||||
|
|
||||
|
private String getIpAddr(HttpServletRequest request) { |
||||
|
String ip = request.getHeader("x-forwarded-for"); |
||||
|
//System.out.println("x-forwarded-for ip: " + ip);
|
||||
|
if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) { |
||||
|
// 多次反向代理后会有多个ip值,第一个ip才是真实ip
|
||||
|
if (ip.indexOf(",") != -1) { |
||||
|
ip = ip.split(",")[0]; |
||||
|
} |
||||
|
} |
||||
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
||||
|
ip = request.getHeader("Proxy-Client-IP"); |
||||
|
//System.out.println("Proxy-Client-IP ip: " + ip);
|
||||
|
} |
||||
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
||||
|
ip = request.getHeader("WL-Proxy-Client-IP"); |
||||
|
//System.out.println("WL-Proxy-Client-IP ip: " + ip);
|
||||
|
} |
||||
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
||||
|
ip = request.getHeader("HTTP_CLIENT_IP"); |
||||
|
//System.out.println("HTTP_CLIENT_IP ip: " + ip);
|
||||
|
} |
||||
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
||||
|
ip = request.getHeader("HTTP_X_FORWARDED_FOR"); |
||||
|
//System.out.println("HTTP_X_FORWARDED_FOR ip: " + ip);
|
||||
|
} |
||||
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
||||
|
ip = request.getHeader("X-Real-IP"); |
||||
|
//System.out.println("X-Real-IP ip: " + ip);
|
||||
|
} |
||||
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
||||
|
ip = request.getRemoteAddr(); |
||||
|
//System.out.println("getRemoteAddr ip: " + ip);
|
||||
|
} |
||||
|
//System.out.println("获取客户端ip: " + ip);
|
||||
|
return ip; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,61 @@ |
|||||
|
package com.bnyer.img.domain; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.annotation.IdType; |
||||
|
import com.baomidou.mybatisplus.annotation.TableField; |
||||
|
import com.baomidou.mybatisplus.annotation.TableId; |
||||
|
import com.baomidou.mybatisplus.annotation.TableName; |
||||
|
import io.swagger.annotations.ApiModel; |
||||
|
import io.swagger.annotations.ApiModelProperty; |
||||
|
import lombok.*; |
||||
|
|
||||
|
@ApiModel(value="com-bnyer-img-domain-BzData") |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@ToString |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@TableName(value = "img_bz_data") |
||||
|
public class BzData { |
||||
|
/** |
||||
|
* 主键id |
||||
|
*/ |
||||
|
@TableId(value = "id", type = IdType.INPUT) |
||||
|
@ApiModelProperty(value="主键id") |
||||
|
private Long id; |
||||
|
|
||||
|
/** |
||||
|
* 日期 |
||||
|
*/ |
||||
|
@TableField(value = "date") |
||||
|
@ApiModelProperty(value="日期 年月日") |
||||
|
private String date; |
||||
|
|
||||
|
/** |
||||
|
* pv |
||||
|
*/ |
||||
|
@TableField(value = "pv") |
||||
|
@ApiModelProperty(value="pv") |
||||
|
private Long pv; |
||||
|
|
||||
|
/** |
||||
|
* uv |
||||
|
*/ |
||||
|
@TableField(value = "uv") |
||||
|
@ApiModelProperty(value="uv") |
||||
|
private Long uv; |
||||
|
|
||||
|
/** |
||||
|
* 平台(0->Hub;1->抖音;2->快手;3->微信) |
||||
|
*/ |
||||
|
@TableField(value = "source") |
||||
|
@ApiModelProperty(value="source") |
||||
|
private String source; |
||||
|
|
||||
|
/** |
||||
|
* 防止重复唯一标识 |
||||
|
*/ |
||||
|
@TableField(value = "mark") |
||||
|
@ApiModelProperty(value="mark") |
||||
|
private String mark; |
||||
|
private static final long serialVersionUID = 1L; |
||||
|
} |
||||
@ -0,0 +1,35 @@ |
|||||
|
package com.bnyer.img.dto; |
||||
|
|
||||
|
import io.swagger.annotations.ApiModel; |
||||
|
import io.swagger.annotations.ApiModelProperty; |
||||
|
import lombok.Getter; |
||||
|
import lombok.Setter; |
||||
|
|
||||
|
import javax.validation.constraints.NotBlank; |
||||
|
|
||||
|
/** |
||||
|
* @Author qyh |
||||
|
* @Date 2022/7/13 21:27 |
||||
|
* @Description |
||||
|
*/ |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@ApiModel("八字运势接收类") |
||||
|
public class BzDto { |
||||
|
@NotBlank(message = "姓不能为空!") |
||||
|
@ApiModelProperty(value="姓") |
||||
|
private String lastName; |
||||
|
|
||||
|
@NotBlank(message = "名不能为空!") |
||||
|
@ApiModelProperty(value="名") |
||||
|
private String firstName; |
||||
|
|
||||
|
@NotBlank(message = "性别不能为空!") |
||||
|
@ApiModelProperty(value="性别") |
||||
|
private String sex; |
||||
|
|
||||
|
@NotBlank(message = "出生年月不能为空!") |
||||
|
@ApiModelProperty(value="出生年月") |
||||
|
//19990209060808这样的
|
||||
|
private String birth; |
||||
|
} |
||||
@ -0,0 +1,15 @@ |
|||||
|
package com.bnyer.img.mapper; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
||||
|
import com.bnyer.img.domain.Banner; |
||||
|
import com.bnyer.img.domain.BzData; |
||||
|
import com.bnyer.img.vo.BannerVo; |
||||
|
import org.apache.ibatis.annotations.Mapper; |
||||
|
import org.apache.ibatis.annotations.Param; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
@Mapper |
||||
|
public interface BzDataMapper extends BaseMapper<BzData> { |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
package com.bnyer.img.service; |
||||
|
|
||||
|
import com.alibaba.fastjson.JSONObject; |
||||
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
||||
|
import com.bnyer.img.domain.Banner; |
||||
|
import com.bnyer.img.domain.BzData; |
||||
|
import com.bnyer.img.dto.BannerPageDto; |
||||
|
import com.bnyer.img.dto.BzDto; |
||||
|
import com.bnyer.img.vo.BannerVo; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
public interface BzDataService { |
||||
|
JSONObject getYs(BzDto dto); |
||||
|
|
||||
|
void dataStatistics(String ip,String source); |
||||
|
|
||||
|
int update(BzData bzData,UpdateWrapper<BzData> updateWrapper); |
||||
|
} |
||||
@ -0,0 +1,124 @@ |
|||||
|
package com.bnyer.img.service.impl; |
||||
|
|
||||
|
import com.alibaba.fastjson.JSON; |
||||
|
import com.alibaba.fastjson.JSONObject; |
||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
||||
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
||||
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
||||
|
import com.bnyer.common.core.domain.R; |
||||
|
import com.bnyer.common.core.utils.StringUtils; |
||||
|
import com.bnyer.common.redis.service.RedisService; |
||||
|
import com.bnyer.img.domain.Banner; |
||||
|
import com.bnyer.img.domain.BzData; |
||||
|
import com.bnyer.img.dto.BannerPageDto; |
||||
|
import com.bnyer.img.dto.BzDto; |
||||
|
import com.bnyer.img.mapper.BannerMapper; |
||||
|
import com.bnyer.img.mapper.BzDataMapper; |
||||
|
import com.bnyer.img.service.BannerService; |
||||
|
import com.bnyer.img.service.BzDataService; |
||||
|
import com.bnyer.img.vo.BannerVo; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.http.HttpEntity; |
||||
|
import org.apache.http.HttpResponse; |
||||
|
import org.apache.http.client.methods.HttpGet; |
||||
|
import org.apache.http.impl.client.CloseableHttpClient; |
||||
|
import org.apache.http.impl.client.HttpClients; |
||||
|
import org.apache.http.util.EntityUtils; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
import java.net.URLEncoder; |
||||
|
import java.time.LocalDate; |
||||
|
import java.util.Date; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@Service |
||||
|
@Slf4j |
||||
|
public class BzDataServiceImpl implements BzDataService { |
||||
|
|
||||
|
@Autowired |
||||
|
private RedisService redisService; |
||||
|
@Autowired |
||||
|
private BzDataMapper bzDataMapper; |
||||
|
|
||||
|
@Override |
||||
|
public JSONObject getYs(BzDto dto) { |
||||
|
//API产品路径
|
||||
|
String requestUrl = "https://xuanxue.market.alicloudapi.com/ai_china_knowledge/bazi/v1?"; |
||||
|
//阿里云APPCODE
|
||||
|
String appcode = "7a37e4f4f24d4737bf48143d371a215b"; |
||||
|
String lastName = dto.getLastName(); |
||||
|
String firstName = dto.getFirstName(); |
||||
|
String sex = dto.getSex(); |
||||
|
String birth = dto.getBirth(); |
||||
|
CloseableHttpClient httpClient = null; |
||||
|
try { |
||||
|
httpClient = HttpClients.createDefault(); |
||||
|
String str = "SECOND_NAME=" + URLEncoder.encode(firstName, "UTF-8") + "&GENDER=" + URLEncoder.encode(sex, "UTF-8") + "&BIRTH=" + URLEncoder.encode(birth, "UTF-8") + "&FIRST_NAME=" + URLEncoder.encode(lastName, "UTF-8"); |
||||
|
System.out.println(requestUrl + str); |
||||
|
// 创建一个HttpGet实例
|
||||
|
HttpGet httpGet = new HttpGet(requestUrl + str); |
||||
|
httpGet.addHeader("Authorization", "APPCODE " + appcode); |
||||
|
|
||||
|
// 发送GET请求
|
||||
|
HttpResponse execute = httpClient.execute(httpGet); |
||||
|
|
||||
|
// 获取状态码
|
||||
|
int statusCode = execute.getStatusLine().getStatusCode(); |
||||
|
System.out.println(statusCode); |
||||
|
|
||||
|
// 获取结果
|
||||
|
HttpEntity entity = execute.getEntity(); |
||||
|
String result = EntityUtils.toString(entity, "utf-8"); |
||||
|
//System.out.println(result);
|
||||
|
return JSON.parseObject(result); |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} finally { |
||||
|
if (httpClient != null) { |
||||
|
try { |
||||
|
httpClient.close(); |
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void dataStatistics(String ip, String source) { |
||||
|
String today = LocalDate.now() + ""; |
||||
|
String page="bzPage_" + source; |
||||
|
//保存今天的key
|
||||
|
redisService.redisTemplate.opsForZSet().add("bzPage_key" +today, today+page, 1.0); |
||||
|
//pv统计
|
||||
|
redisService.pvStatistics(today, page); |
||||
|
//uv统计
|
||||
|
redisService.uvStatistics(today, page, ip); |
||||
|
//查看是否已经有了这条数据
|
||||
|
if (!redisService.hasKey(today + source)) { |
||||
|
QueryWrapper<BzData> wrapper = new QueryWrapper<>(); |
||||
|
wrapper.eq("date", today); |
||||
|
wrapper.eq("source", source); |
||||
|
List<BzData> bzData = bzDataMapper.selectList(wrapper); |
||||
|
if (bzData.size() <= 0) { |
||||
|
BzData data = new BzData(); |
||||
|
data.setDate(today); |
||||
|
data.setPv(1L); |
||||
|
data.setUv(1L); |
||||
|
data.setSource(source); |
||||
|
data.setMark(today + source); |
||||
|
bzDataMapper.insert(data); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public int update(BzData bzData, UpdateWrapper<BzData> updateWrapper) { |
||||
|
return bzDataMapper.update(bzData,updateWrapper); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,60 @@ |
|||||
|
package com.bnyer.img.task; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
||||
|
import com.bnyer.common.redis.service.RedisService; |
||||
|
import com.bnyer.img.domain.BzData; |
||||
|
import com.bnyer.img.service.BzDataService; |
||||
|
import com.xxl.job.core.biz.model.ReturnT; |
||||
|
import com.xxl.job.core.handler.annotation.XxlJob; |
||||
|
import com.xxl.job.core.log.XxlJobLogger; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.scheduling.annotation.Scheduled; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
import java.time.LocalDate; |
||||
|
import java.util.Set; |
||||
|
|
||||
|
/** |
||||
|
* @Author qyh |
||||
|
* @Date 2022/7/14 16:43 |
||||
|
* @Description |
||||
|
*/ |
||||
|
@Component |
||||
|
@Slf4j |
||||
|
public class DateSyncTask { |
||||
|
@Autowired |
||||
|
private static RedisService redisService; |
||||
|
@Autowired |
||||
|
private static BzDataService bzDataService; |
||||
|
//@XxlJob("dateSyncTask")
|
||||
|
//@Scheduled(fixedDelay = 1000 * 60 * 60)
|
||||
|
public static ReturnT<String> dateSync() { |
||||
|
String day = LocalDate.now().plusDays(-1)+ ""; |
||||
|
String redisKey = "bzPage_key" + day; |
||||
|
System.out.println(redisService); |
||||
|
redisService.setCacheObject("4", 2); |
||||
|
Set range = redisService.redisTemplate.opsForZSet().range(redisKey, 0, -1); |
||||
|
for (Object key : redisService.redisTemplate.opsForZSet().range(redisKey, 0, -1)) { |
||||
|
Long pv = Long.parseLong(redisService.redisTemplate.opsForValue().get(key + "PV").toString()); |
||||
|
Long uv = Long.parseLong(redisService.redisTemplate.opsForHyperLogLog().size(key + "UV").toString()); |
||||
|
String[] s = key.toString().split("_"); |
||||
|
String source = s[1]; |
||||
|
redisService.deleteObject(key + "PV"); |
||||
|
redisService.deleteObject(key + "UV"); |
||||
|
UpdateWrapper<BzData> wrapper = new UpdateWrapper<>(); |
||||
|
wrapper.set("pv",pv); |
||||
|
wrapper.set("uv",uv); |
||||
|
wrapper.eq("date",day); |
||||
|
wrapper.eq("source",source); |
||||
|
bzDataService.update(new BzData(),wrapper); |
||||
|
// redisService.deleteObject(key + "PV");
|
||||
|
// redisService.deleteObject(key + "UV");
|
||||
|
} |
||||
|
redisService.deleteObject("bzPage_key" + day); |
||||
|
XxlJobLogger.log("{} 我执行了同步pv,uv任务", System.currentTimeMillis()); |
||||
|
return ReturnT.SUCCESS; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue