package com.ruoyi.other.controller;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.ruoyi.common.core.domain.R;
|
import com.ruoyi.common.core.web.page.PageInfo;
|
import com.ruoyi.common.log.annotation.Log;
|
import com.ruoyi.common.log.enums.BusinessType;
|
import com.ruoyi.common.security.service.TokenService;
|
import com.ruoyi.other.api.domain.IntegralPay;
|
import com.ruoyi.other.api.domain.TIntegralRule;
|
import com.ruoyi.other.query.IntegralListQuery;
|
import com.ruoyi.other.service.TIntegralPayService;
|
import com.ruoyi.other.service.TIntegralRecordService;
|
import com.ruoyi.other.service.TIntegralRuleService;
|
import com.ruoyi.other.util.MyQrCodeUtil;
|
import com.ruoyi.other.util.ObsUploadUtil;
|
import com.ruoyi.other.util.QRCodeUtil;
|
import com.ruoyi.other.util.UUIDUtil;
|
import com.ruoyi.other.util.payment.wx.WechatPayService;
|
import com.ruoyi.other.vo.IntegralListVO;
|
import com.ruoyi.other.vo.IntegralVO;
|
import com.ruoyi.system.api.domain.SysUser;
|
import com.ruoyi.system.api.feignClient.SysUserClient;
|
import io.swagger.annotations.ApiOperation;
|
import org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.core.io.ByteArrayResource;
|
import org.springframework.mock.web.MockMultipartFile;
|
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.annotation.Resource;
|
import javax.imageio.ImageIO;
|
import java.awt.image.BufferedImage;
|
import java.io.IOException;
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.text.SimpleDateFormat;
|
import java.time.LocalDateTime;
|
import java.util.Date;
|
import java.util.Map;
|
|
/**
|
* <p>
|
* 前端控制器
|
* </p>
|
*
|
* @author 无关风月
|
* @since 2024-08-06
|
*/
|
@RestController
|
@RequestMapping("/integral")
|
public class TIntegralController {
|
|
@Autowired
|
private TIntegralRuleService integralRuleService;
|
@Autowired
|
private TIntegralRecordService integralRecordService;
|
@Autowired
|
private TIntegralPayService integralPayService;
|
|
@Autowired
|
private TokenService tokenService;
|
@Resource
|
private SysUserClient sysUserClient;
|
@Resource
|
private WechatPayService wechatPayService;
|
@PostMapping("/integralPageList")
|
@ApiOperation(tags = {"2.0-积分钱包"},value = "积分钱包")
|
public R<IntegralVO> integralPageList(@RequestBody IntegralListQuery query) {
|
Long userid = tokenService.getLoginUser().getUserid();
|
R<SysUser> sysUser = sysUserClient.getSysUser(userid);
|
query.setSiteId(sysUser.getData().getSiteId());
|
Integer integral = sysUser.getData().getIntegral();
|
IntegralVO integralVO = new IntegralVO();
|
PageInfo<IntegralListVO> integralListVOPageInfo = integralRecordService.integralPageList(query);
|
integralVO.setIntegralList(integralListVOPageInfo);
|
integralVO.setIntegral(integral);
|
return R.ok(integralVO);
|
}
|
@PostMapping("/queryPayStatus")
|
@ApiOperation(tags = {"2.0-积分钱包"},value = "查询支付结果 true成功 false失败")
|
public R<Boolean> integralPageList(@RequestParam("code") String code) throws Exception {
|
Map<String, String> resMap = wechatPayService.queryOrder(code);
|
if("SUCCESS".equals(resMap.get("return_code"))){
|
return R.ok(true);
|
}else{
|
return R.ok(false);
|
}
|
}
|
|
@PostMapping("/nativePay")
|
@ApiOperation(tags = {"2.0-积分钱包"},value = "获取支付二维码")
|
public R nativePay(@RequestParam("amount") String amount) throws Exception {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
|
String code = sdf.format(new Date()) + UUIDUtil.getNumberRandom(5);
|
Map<String, String> res = wechatPayService.unifiedOrder(code, amount, "积分充值", "/other/wx/integralCallback");
|
Long userid = tokenService.getLoginUser().getUserid();
|
R<SysUser> sysUser = sysUserClient.getSysUser(userid);
|
SysUser data = sysUser.getData();
|
// 生成待支付记录
|
IntegralPay integralPay = new IntegralPay();
|
integralPay.setUserId(data.getUserId());
|
TIntegralRule one = integralRuleService.lambdaQuery().eq(TIntegralRule::getSiteId, 0).one();
|
if (one==null){
|
return R.fail("获取支付失败,平台未设置充值积分比例");
|
}
|
String chargeCredit = one.getChargeCredit();
|
JSONObject jsonObject = JSONObject.parseObject(chargeCredit);
|
Integer num1 = jsonObject.getInteger("num1");
|
BigDecimal bigDecimal = new BigDecimal(amount);
|
// 向上取整 比如5.1 当作6 5.9也当作6
|
integralPay.setIntegralCount(bigDecimal.divide(new BigDecimal(num1), RoundingMode.UP).intValue());
|
int i = bigDecimal.multiply(new BigDecimal(num1)).intValue();
|
integralPay.setIntegralCount(i);
|
integralPay.setAmount(new BigDecimal(amount));
|
integralPay.setCode(code);
|
integralPay.setPayStatus(1);
|
integralPay.setDelFlag(0);
|
integralPay.setCreateTime(LocalDateTime.now());
|
integralPayService.save(integralPay);
|
String codeUrl = res.get("code_url");
|
MyQrCodeUtil.createCodeToFile(codeUrl);
|
BufferedImage blueImage = QRCodeUtil.createImage(codeUrl);
|
MultipartFile blueFile = convert(blueImage, new Date().getTime() + UUIDUtil.getRandomCode(3) + ".PNG");
|
String s = ObsUploadUtil.obsUpload(blueFile);
|
System.err.println(s);
|
return R.ok(s);
|
}
|
public static MultipartFile convert(BufferedImage bufferedImage, String fileName) throws IOException {
|
// 将 BufferedImage 转换为字节数组
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ImageIO.write(bufferedImage, "png", baos);
|
byte[] bytes = baos.toByteArray();
|
|
// 创建 ByteArrayResource
|
ByteArrayResource resource = new ByteArrayResource(bytes);
|
|
// 创建 MockMultipartFile
|
MockMultipartFile multipartFile = new MockMultipartFile(
|
"file",
|
fileName,
|
"image/png",
|
resource.getInputStream()
|
);
|
|
return multipartFile;
|
}
|
@GetMapping("/getSet")
|
@ApiOperation(tags = {"2.0-积分管理"},value = "获取积分设置")
|
public R<TIntegralRule> getSet() {
|
Long userid = tokenService.getLoginUser().getUserid();
|
SysUser data = sysUserClient.getSysUser(userid).getData();
|
|
if (data!=null&&data.getSiteId()!=null){
|
if (data.getRoleType()==1){
|
// 平台
|
TIntegralRule res = integralRuleService.lambdaQuery().eq(TIntegralRule::getSiteId, 0).one();
|
return R.ok(res);
|
}else{
|
TIntegralRule res = integralRuleService.lambdaQuery().eq(TIntegralRule::getSiteId, data.getSiteId()).one();
|
return R.ok(res);
|
}
|
}else {
|
return R.ok(new TIntegralRule());
|
}
|
}
|
|
@PostMapping("/saveSet")
|
@ApiOperation(tags = {"2.0-积分管理"},value = "保存积分设置")
|
@Log(title = "【积分管理】保存积分设置", businessType = BusinessType.INSERT)
|
public R saveSet(@RequestBody TIntegralRule dto) {
|
Long userid = tokenService.getLoginUser().getUserid();
|
SysUser data = sysUserClient.getSysUser(userid).getData();
|
TIntegralRule one;
|
if (data.getRoleType()==1){
|
one = integralRuleService.lambdaQuery().eq(TIntegralRule::getSiteId, 0).one();
|
}else{
|
one = integralRuleService.lambdaQuery().eq(TIntegralRule::getSiteId, data.getSiteId()).one();
|
}
|
if (one!=null){
|
dto.setId(one.getId());
|
integralRuleService.saveOrUpdate(dto);
|
}else{
|
integralRuleService.saveOrUpdate(dto);
|
}
|
return R.ok();
|
}
|
|
}
|