无关风月
4 天以前 7fd053651ac11db87fe4f6c57e65eed3b9a59452
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
package com.ruoyi.other.controller;
 
import com.alibaba.fastjson.JSON;
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.pay.CreateLinkStringByGet1;
import com.ruoyi.other.util.pay.HttpRequester;
import com.ruoyi.other.util.pay.HttpRespons;
import com.ruoyi.other.util.pay.Md5_Sign;
import com.ruoyi.other.util.payment.wx.WechatPayService;
import com.ruoyi.other.vo.IntegralListVO;
import com.ruoyi.other.vo.IntegralVO;
import com.ruoyi.other.vo.PayDto;
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.HashMap;
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);
    }
 
    @ApiOperation(tags = {"2.0-积分钱包"},value = "查询支付结果 true成功 false失败")
    @PostMapping(value = "/queryPayStatus")
    public R buy(@RequestParam Integer id)  {
        IntegralPay integralPay = integralPayService.getById(id);
        if (integralPay.getPayStatus()==2){
            return R.ok(true);
        }else {
            return R.ok(false);
        }
    }
    @PostMapping("/nativePay")
    @ApiOperation(tags = {"2.0-积分钱包"},value = "获取支付二维码")
    public R<PayDto> 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);
        int money = new BigDecimal(amount).multiply(BigDecimal.valueOf(100)).intValue();
        String key = "ad273ceb5e1b49e68d5c565d28d1d305";/** md5密钥商户后台-商户中心-商户设置-密钥管理获取 必填!*/
        Map<String, String> map = new HashMap<String, String>();
        map.put("p0_Version", "2.5");/** 版本号 */
        map.put("p1_MerchantNo", "888122400007793");/** 商户编号 */
        map.put("p2_OrderNo", code); /**商户订单号*/
        map.put("p3_Amount", String.valueOf(money));/**订单金额*/
        map.put("p4_Cur", "1"); /**交易币种 */
        map.put("p5_ProductName", "充值"); /** 商品名称 */
        map.put("p6_ProductDesc", "积分充值"); /** 商品名称 */
//        map.put("p7_Mp", ""); /** 如果商户请求时传递了该参数,则返回给商户时会原值传 回。 */
        map.put("p9_NotifyUrl", "http://221.182.45.100:8084/other/wx/integralCallback"); /** 服务器异步通知地址 */
        map.put("q1_FrpCode", "WEIXIN_NATIVE"); /** 微信扫码(主扫)【注:此为用户主扫,商户被扫】*/
        map.put("q4_IsShowPic", "1"); /** 是否展示二 维码图片 1表示输出*/
        map.put("q7_AppId", "wx1a4a7760be53a835"); /** 交易类型*/
        map.put("qa_TradeMerchantNo", "777165000859101"); /** 777开头的报备商户号   必填!*/
        map.put("qi_FqSellerPercen", "0"); /** 卖家承担收 费比例 目前仅支持传入 0 ,即用户承 担手续费!*/
        String Strmap = CreateLinkStringByGet1.createLinkStringByGet(map);
        // 签名
        String sign = "";
        sign = Md5_Sign.SignByMD5(Strmap, key);
        map.put("hmac", sign);/** 签名数据 */
        System.out.println("发送:" + JSON.toJSONString(map).toString());
 
        // post请求参数内容
        HttpRequester hr = new HttpRequester();
        HttpRespons HP = hr.sendPost("https://trade.joinpay.com/tradeRt/uniPay", map);
        System.out.println("接收返回参数:" + HP.getContent());
        JSONObject resPay = JSONObject.parseObject(HP.getContent());
        String rcResult = resPay.getString("rd_Pic");
        Map<String, Object> map2 = new HashMap<>();
        map2.put("rcResult", rcResult);
        PayDto payDto = new PayDto();
        payDto.setId(integralPay.getId());
        payDto.setQrCode(rcResult);
        return R.ok(payDto);
    }
 
    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();
    }
 
}