Commit e3ed14ae63457b6fac71f36ed5b66735784880da
1 parent
3f14b70dbf
Exists in
master
and in
1 other branch
支付相关接口
Showing 26 changed files with 1242 additions and 9 deletions
- talkonlineweb/pom.xml
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/TalkonlinewebApplication.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/aop/ResubmitDataAspect.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/OrderController.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsGoods.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsOrder.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsPatient.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/enums/PayStatus.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/mapper/LymsGoodsMapper.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/mapper/LymsPatientMapper.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/service/LymsGoodsService.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/service/LymsOrderService.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/service/impl/LymsGoodsServiceImpl.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/service/impl/LymsOrderServiceImpl.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/Constant.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/DateUtil.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/HttpUtil.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/IdWorker.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/JsonUtil.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/NumberUtil.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/PayDigestUtil.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/StringUtil.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/WeiXinUtil.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/XmlUtil.java
- talkonlineweb/src/main/resources/application.yml
- talkonlineweb/src/main/resources/logback-spring.xml
talkonlineweb/pom.xml
View file @
e3ed14a
| ... | ... | @@ -63,8 +63,16 @@ |
| 63 | 63 | <artifactId>druid-spring-boot-starter</artifactId> |
| 64 | 64 | <version>1.1.22</version> |
| 65 | 65 | </dependency> |
| 66 | - | |
| 67 | - | |
| 66 | + <dependency> | |
| 67 | + <groupId>commons-httpclient</groupId> | |
| 68 | + <artifactId>commons-httpclient</artifactId> | |
| 69 | + <version>3.1</version> | |
| 70 | + </dependency> | |
| 71 | + <dependency> | |
| 72 | + <groupId>org.apache.httpcomponents</groupId> | |
| 73 | + <artifactId>httpclient</artifactId> | |
| 74 | + <version>4.5.2</version> | |
| 75 | + </dependency> | |
| 68 | 76 | <dependency> |
| 69 | 77 | <groupId>org.springframework.boot</groupId> |
| 70 | 78 | <artifactId>spring-boot-devtools</artifactId> |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/TalkonlinewebApplication.java
View file @
e3ed14a
| ... | ... | @@ -4,8 +4,10 @@ |
| 4 | 4 | import org.springframework.boot.SpringApplication; |
| 5 | 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 6 | 6 | import org.springframework.scheduling.annotation.EnableScheduling; |
| 7 | +import org.springframework.transaction.annotation.EnableTransactionManagement; | |
| 7 | 8 | |
| 8 | 9 | @SpringBootApplication |
| 10 | +@EnableTransactionManagement | |
| 9 | 11 | @MapperScan("com.lyms.talkonlineweb.mapper") |
| 10 | 12 | @EnableScheduling |
| 11 | 13 | public class TalkonlinewebApplication { |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/aop/ResubmitDataAspect.java
View file @
e3ed14a
talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/OrderController.java
View file @
e3ed14a
| 1 | +package com.lyms.talkonlineweb.controller; | |
| 2 | + | |
| 3 | +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | |
| 4 | +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | |
| 5 | +import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; | |
| 6 | +import com.lyms.talkonlineweb.annotation.Resubmit; | |
| 7 | +import com.lyms.talkonlineweb.domain.LymsGoods; | |
| 8 | +import com.lyms.talkonlineweb.domain.LymsOrder; | |
| 9 | +import com.lyms.talkonlineweb.domain.LymsPatient; | |
| 10 | +import com.lyms.talkonlineweb.enums.PayStatus; | |
| 11 | +import com.lyms.talkonlineweb.result.BaseResponse; | |
| 12 | +import com.lyms.talkonlineweb.service.*; | |
| 13 | +import com.lyms.talkonlineweb.util.*; | |
| 14 | +import lombok.extern.log4j.Log4j2; | |
| 15 | +import org.apache.catalina.util.IOTools; | |
| 16 | +import org.apache.commons.io.IOUtils; | |
| 17 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 18 | +import org.springframework.beans.factory.annotation.Value; | |
| 19 | +import org.springframework.web.bind.annotation.PostMapping; | |
| 20 | +import org.springframework.web.bind.annotation.RequestBody; | |
| 21 | +import org.springframework.web.bind.annotation.RequestMapping; | |
| 22 | +import org.springframework.web.bind.annotation.RestController; | |
| 23 | + | |
| 24 | +import javax.servlet.http.HttpServletRequest; | |
| 25 | +import javax.servlet.http.HttpServletResponse; | |
| 26 | +import java.io.IOException; | |
| 27 | +import java.io.PrintWriter; | |
| 28 | +import java.util.*; | |
| 29 | + | |
| 30 | +/** | |
| 31 | + * @ProjectName: talkonline | |
| 32 | + * @Package: com.lyms.talkonlineweb.controller | |
| 33 | + * @ClassName: OrderController | |
| 34 | + * @Author: lqy | |
| 35 | + * @Description: 支付订单类 | |
| 36 | + * @Date: 2021-09-11 09:57 | |
| 37 | + * @Version: | |
| 38 | + */ | |
| 39 | +@RestController | |
| 40 | +@RequestMapping("order") | |
| 41 | +@Log4j2 | |
| 42 | +public class OrderController { | |
| 43 | + | |
| 44 | + @Autowired | |
| 45 | + private LymsGoodsService lymsGoodsService;//问诊卡信息 | |
| 46 | + | |
| 47 | + @Autowired | |
| 48 | + private LymsPatientService lymsPatientService;//患者 | |
| 49 | + | |
| 50 | + @Autowired | |
| 51 | + private LymsTcardService lymsTcardService;//问诊卡信息 | |
| 52 | + | |
| 53 | + @Autowired | |
| 54 | + private LymsLogsService lymsLogsService;//日志记录 | |
| 55 | + @Autowired | |
| 56 | + private LymsOrderService lymsOrderService; | |
| 57 | + | |
| 58 | + /** | |
| 59 | + * 微信回调通知接口 | |
| 60 | + */ | |
| 61 | + @Value("${notify.url}") | |
| 62 | + private String notifyUrl; | |
| 63 | + | |
| 64 | + | |
| 65 | + /** | |
| 66 | + * 创建支付订单,并把订单提交到微信平台 | |
| 67 | + * 传入患者id和购买数量 | |
| 68 | + * https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_1 微信统一下单api文档地址 | |
| 69 | + * @param lymsOrder | |
| 70 | + * @param request | |
| 71 | + * @param response | |
| 72 | + */ | |
| 73 | + @PostMapping("createPayOrder") | |
| 74 | + @Resubmit | |
| 75 | + public BaseResponse createPayOrder(@RequestBody LymsOrder lymsOrder, | |
| 76 | + HttpServletRequest request, HttpServletResponse response) { | |
| 77 | + | |
| 78 | + log.info("create pay order request info : {}",lymsOrder.toString()); | |
| 79 | + | |
| 80 | + BaseResponse baseResponse = new BaseResponse(); | |
| 81 | + //参数校验 | |
| 82 | + if (lymsOrder.getPid() == null || lymsOrder.getCnt() == null) | |
| 83 | + { | |
| 84 | + baseResponse.setErrorcode(1); | |
| 85 | + baseResponse.setErrormsg("创建订单参数错误"); | |
| 86 | + return baseResponse; | |
| 87 | + } | |
| 88 | + | |
| 89 | + LymsPatient patient = lymsPatientService.getById(lymsOrder.getPid()); | |
| 90 | + if (patient == null) | |
| 91 | + { | |
| 92 | + baseResponse.setErrorcode(1); | |
| 93 | + baseResponse.setErrormsg("用户不存在"); | |
| 94 | + return baseResponse; | |
| 95 | + } | |
| 96 | + | |
| 97 | + | |
| 98 | + LambdaQueryWrapper<LymsGoods> wrapper = new QueryWrapper().lambda(); | |
| 99 | + wrapper.eq(LymsGoods::getType, "card"); | |
| 100 | + LymsGoods goods = lymsGoodsService.getOne(wrapper); | |
| 101 | + if (goods == null || goods.getPrice() <= 0) | |
| 102 | + { | |
| 103 | + baseResponse.setErrorcode(1); | |
| 104 | + baseResponse.setErrormsg("商品价格配置不存在"); | |
| 105 | + return baseResponse; | |
| 106 | + } | |
| 107 | + if (goods.getPrice() != lymsOrder.getPrice() && lymsOrder.getPrice() > 0) | |
| 108 | + { | |
| 109 | + baseResponse.setErrorcode(1); | |
| 110 | + baseResponse.setErrormsg("支付价格不一致!"); | |
| 111 | + return baseResponse; | |
| 112 | + } | |
| 113 | + try | |
| 114 | + { | |
| 115 | + //生成系统订单号 | |
| 116 | + String orderNo = StringUtil.getUniqueNo(); | |
| 117 | + log.info("create order pid {} ,orderNo {}",lymsOrder.getPid(),orderNo); | |
| 118 | + lymsOrder.setCreatedtime(new Date()); | |
| 119 | + lymsOrder.setOrderno(orderNo); | |
| 120 | + lymsOrder.setStatus(PayStatus.CREATED.getCode()); | |
| 121 | + lymsOrder.setOpenid(patient.getOpenid()); | |
| 122 | + lymsOrderService.save(lymsOrder); | |
| 123 | + //创建微信订单 | |
| 124 | + Map<String,String> data = createWxOrder(lymsOrder); | |
| 125 | + if (data != null) | |
| 126 | + { | |
| 127 | + | |
| 128 | + LymsOrder updateOrder = new LymsOrder(); | |
| 129 | + updateOrder.setId(lymsOrder.getId()); | |
| 130 | + updateOrder.setPayorderid(data.get("payId")); | |
| 131 | + //根据订单号更新支付订单号 | |
| 132 | + lymsOrderService.updateById(updateOrder); | |
| 133 | + data.put("orderNo",orderNo); | |
| 134 | + baseResponse.setObject(data); | |
| 135 | + return baseResponse; | |
| 136 | + } | |
| 137 | + }catch (Exception e) | |
| 138 | + { | |
| 139 | + log.error("create order error.",e); | |
| 140 | + } | |
| 141 | + baseResponse.setErrorcode(1); | |
| 142 | + baseResponse.setErrormsg("创建订单失败"); | |
| 143 | + return baseResponse; | |
| 144 | + } | |
| 145 | + | |
| 146 | + /** | |
| 147 | + * 创建微信订单 | |
| 148 | + */ | |
| 149 | + private Map<String,String> createWxOrder(LymsOrder lymsOrder) | |
| 150 | + { | |
| 151 | + try { | |
| 152 | + String xml = buildRequestXml(lymsOrder); | |
| 153 | + log.info("create order pid : {},buildRequestMap xml : {}",lymsOrder.getPid(),xml); | |
| 154 | + | |
| 155 | + //调用微信统一下单接口 | |
| 156 | + String result = HttpUtil.postData(Constant.PAY_URL, xml); | |
| 157 | + log.info("wx create order result : {}" ,result); | |
| 158 | + | |
| 159 | + return parseWxResult(result); | |
| 160 | + } catch (Exception e) { | |
| 161 | + log.error("createWxOrder error.",e); | |
| 162 | + } | |
| 163 | + return null; | |
| 164 | + } | |
| 165 | + | |
| 166 | + | |
| 167 | + /** | |
| 168 | + * 解析微信创建订单的结果 | |
| 169 | + * @param resultXml | |
| 170 | + * @throws Exception | |
| 171 | + */ | |
| 172 | + private Map<String,String> parseWxResult(String resultXml) throws Exception{ | |
| 173 | + try | |
| 174 | + { | |
| 175 | + Map<String,String> data = new HashMap(5); | |
| 176 | + Map<String,String> retMap = XmlUtil.xmlToMap(resultXml); | |
| 177 | + String success = "SUCCESS"; | |
| 178 | + if (retMap != null && retMap.size() > 0 && success.equals(retMap.get("return_code")) && success.equals(retMap.get("result_code"))) | |
| 179 | + { | |
| 180 | + data.put("payId",retMap.get("prepay_id")); | |
| 181 | + return data; | |
| 182 | + } | |
| 183 | + } | |
| 184 | + catch (Exception e) | |
| 185 | + { | |
| 186 | + log.error("parse wx result error.",e); | |
| 187 | + throw e; | |
| 188 | + } | |
| 189 | + return null; | |
| 190 | + } | |
| 191 | + | |
| 192 | + | |
| 193 | + /** | |
| 194 | + * 构建微信订单参数 | |
| 195 | + * @param lymsOrder | |
| 196 | + * @return | |
| 197 | + * @throws Exception | |
| 198 | + */ | |
| 199 | + public String buildRequestXml(LymsOrder lymsOrder) throws Exception { | |
| 200 | + Map<String,String> paramMap = new TreeMap((key1,key2) -> { | |
| 201 | + return key1.toString().compareTo(key2.toString()); | |
| 202 | + } | |
| 203 | + ); | |
| 204 | + //小程序ID | |
| 205 | + paramMap.put("appid", Constant.PAT_APP_ID); | |
| 206 | + //商户号 | |
| 207 | + paramMap.put("mch_id", Constant.MCHID); | |
| 208 | + //随机字符串 | |
| 209 | + paramMap.put("nonce_str", NumberUtil.getRandomString(16)); | |
| 210 | + //签名类型 | |
| 211 | + paramMap.put("sign_type", "MD5"); | |
| 212 | + //商户订单号 | |
| 213 | + paramMap.put("out_trade_no", lymsOrder.getOrderno()); | |
| 214 | + //标价金额 | |
| 215 | + paramMap.put("total_fee", String.valueOf(lymsOrder.getCnt() * lymsOrder.getPrice())); | |
| 216 | + //终端IP | |
| 217 | + paramMap.put("spbill_create_ip", "119.90.57.26"); | |
| 218 | + //通知地址 | |
| 219 | + paramMap.put("notify_url", notifyUrl); | |
| 220 | + //交易类型 | |
| 221 | + paramMap.put("trade_type", "JSAPI"); | |
| 222 | + //用户标识 | |
| 223 | + paramMap.put("openid", "o4hf60FZzQ4PE-SXYk-jNaouGG-0"); | |
| 224 | + //商品描述 | |
| 225 | + paramMap.put("body", "问诊支付"); | |
| 226 | + | |
| 227 | + String reqSign = PayDigestUtil.getSign(paramMap, Constant.REQ_KEY); | |
| 228 | + // 签名 | |
| 229 | + paramMap.put("sign", reqSign); | |
| 230 | + return XmlUtil.mapToXml(paramMap); | |
| 231 | + } | |
| 232 | + | |
| 233 | + | |
| 234 | + /** | |
| 235 | + * 微信支付成功后回调接口 | |
| 236 | + * https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_7&index=8 | |
| 237 | + * @param request | |
| 238 | + * @param response | |
| 239 | + * @return | |
| 240 | + */ | |
| 241 | + @PostMapping("payNotify") | |
| 242 | + public void payNotify(HttpServletRequest request, HttpServletResponse response) { | |
| 243 | + | |
| 244 | + Map<String,String> result = new HashMap<>(2); | |
| 245 | + result.put("return_code","<![CDATA[FAIL]]>"); | |
| 246 | + result.put("return_msg","参数错误"); | |
| 247 | + try { | |
| 248 | + Map<String, String> paramMap = getPayNotifyParamMap(request); | |
| 249 | + | |
| 250 | + //验证支付通知的参数合法性 | |
| 251 | + if (!verifyPayNotify(paramMap)) { | |
| 252 | + //微信支付订单号 | |
| 253 | + String payOrderId = paramMap.get("transaction_id"); | |
| 254 | + //商户订单号 | |
| 255 | + String mchOrderNo = paramMap.get("out_trade_no"); | |
| 256 | + lymsOrderService.handleOrder(payOrderId,mchOrderNo); | |
| 257 | + result.put("return_code","<![CDATA[SUCCESS]]>"); | |
| 258 | + result.put("return_msg","<![CDATA[OK]]>"); | |
| 259 | + } | |
| 260 | + outResult(response, XmlUtil.mapToXml(result)); | |
| 261 | + }catch (Exception e) | |
| 262 | + { | |
| 263 | + log.error("pay notify error.",e); | |
| 264 | + try | |
| 265 | + { | |
| 266 | + outResult(response, XmlUtil.mapToXml(result)); | |
| 267 | + }catch (Exception e1) { | |
| 268 | + | |
| 269 | + } | |
| 270 | + } | |
| 271 | + } | |
| 272 | + | |
| 273 | + | |
| 274 | + /** | |
| 275 | + * 回调接口响应微信 | |
| 276 | + * @param response | |
| 277 | + * @param content | |
| 278 | + */ | |
| 279 | + private void outResult(HttpServletResponse response, String content) { | |
| 280 | + response.setContentType("text/html"); | |
| 281 | + PrintWriter pw; | |
| 282 | + try { | |
| 283 | + pw = response.getWriter(); | |
| 284 | + pw.print(content); | |
| 285 | + log.info("response pay complete."); | |
| 286 | + } catch (IOException e) { | |
| 287 | + log.error("response pay write exception.", e); | |
| 288 | + } | |
| 289 | + } | |
| 290 | + | |
| 291 | + | |
| 292 | + /** | |
| 293 | + * 解析支付通知参数为map | |
| 294 | + * @param request | |
| 295 | + * @return | |
| 296 | + * @throws Exception | |
| 297 | + */ | |
| 298 | + public Map<String, String> getPayNotifyParamMap(HttpServletRequest request) throws Exception{ | |
| 299 | + try | |
| 300 | + { | |
| 301 | + String resultXml = IOUtils.toString(request.getInputStream(),"utf-8"); | |
| 302 | + log.info("pay notify info : {}",resultXml); | |
| 303 | + if (StringUtil.isNotEmpty(resultXml)) | |
| 304 | + { | |
| 305 | + Map<String, String> retMap = XmlUtil.xmlToMap(resultXml); | |
| 306 | + String success = "SUCCESS"; | |
| 307 | + if (retMap != null && retMap.size() > 0 && success.equals(retMap.get("return_code")) && success.equals(retMap.get("result_code"))) | |
| 308 | + { | |
| 309 | + return retMap; | |
| 310 | + } | |
| 311 | + } | |
| 312 | + }catch (Exception e) | |
| 313 | + { | |
| 314 | + log.error("get pay notify error.",e); | |
| 315 | + throw e; | |
| 316 | + } | |
| 317 | + return null; | |
| 318 | + } | |
| 319 | + | |
| 320 | + /** | |
| 321 | + * 微信回调参数验证 | |
| 322 | + * @param map | |
| 323 | + * @return | |
| 324 | + */ | |
| 325 | + public boolean verifyPayNotify(Map<String, String> map) { | |
| 326 | + //商户id | |
| 327 | + String mchId = map.get("mch_id"); | |
| 328 | + //微信支付订单号 | |
| 329 | + String payOrderId = map.get("transaction_id"); | |
| 330 | + //商户订单号 | |
| 331 | + String mchOrderNo = map.get("out_trade_no"); | |
| 332 | + //现金支付金额 | |
| 333 | + String amount = map.get("cash_fee"); | |
| 334 | + //签名 | |
| 335 | + String sign = map.get("sign"); | |
| 336 | + | |
| 337 | + if (StringUtil.isEmpty(mchId)) { | |
| 338 | + log.warn("Params error. mchId : {}", mchId); | |
| 339 | + return false; | |
| 340 | + } | |
| 341 | + if (StringUtil.isEmpty(payOrderId)) { | |
| 342 | + log.warn("Params error. payOrderId : {}", payOrderId); | |
| 343 | + return false; | |
| 344 | + } | |
| 345 | + if (StringUtil.isEmpty(amount)) { | |
| 346 | + log.warn("Params error. amount : {}", amount); | |
| 347 | + return false; | |
| 348 | + } | |
| 349 | + if (StringUtil.isEmpty(sign)) { | |
| 350 | + log.warn("Params error. sign : {}", sign); | |
| 351 | + return false; | |
| 352 | + } | |
| 353 | + | |
| 354 | + // 验证签名 | |
| 355 | + if (!verifySign(map)) { | |
| 356 | + log.warn("verify params sign failed. payOrderId={}", payOrderId); | |
| 357 | + return false; | |
| 358 | + } | |
| 359 | + | |
| 360 | + // 根据payOrderId查询业务订单,验证订单是否存在 | |
| 361 | + LambdaQueryWrapper<LymsOrder> wrapper = new QueryWrapper().lambda(); | |
| 362 | + wrapper.eq(LymsOrder::getOrderno, mchOrderNo); | |
| 363 | + List<LymsOrder> lymsOrders = lymsOrderService.list(wrapper); | |
| 364 | + if (CollectionUtils.isNotEmpty(lymsOrders)) { | |
| 365 | + log.warn("local orderno not exists,payOrderId : {},orderno : {}", payOrderId, mchOrderNo); | |
| 366 | + return false; | |
| 367 | + } | |
| 368 | + // 核对金额 | |
| 369 | + if (lymsOrders.get(0).getPrice() != Integer.parseInt(amount)) { | |
| 370 | + log.warn("Inconsistent payment amount ,dbPayPrice : {},payPrice : {}", lymsOrders.get(0).getPrice(), amount); | |
| 371 | + return false; | |
| 372 | + } | |
| 373 | + return true; | |
| 374 | + } | |
| 375 | + | |
| 376 | + | |
| 377 | + /** | |
| 378 | + * 回调参数的签名验证 | |
| 379 | + * @param map | |
| 380 | + * @return | |
| 381 | + */ | |
| 382 | + public boolean verifySign(Map<String, String> map) { | |
| 383 | + String mchId = map.get("mch_id"); | |
| 384 | + //检查商户id | |
| 385 | + if (!Constant.MCHID.equals(mchId) ) { | |
| 386 | + return false; | |
| 387 | + } | |
| 388 | + String localSign = PayDigestUtil.getSign(map, Constant.REQ_KEY, "sign"); | |
| 389 | + String sign = map.get("sign"); | |
| 390 | + return localSign.equalsIgnoreCase(sign); | |
| 391 | + } | |
| 392 | + | |
| 393 | + | |
| 394 | +} |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsGoods.java
View file @
e3ed14a
| 1 | +package com.lyms.talkonlineweb.domain; | |
| 2 | + | |
| 3 | +import com.baomidou.mybatisplus.annotation.IdType; | |
| 4 | +import com.baomidou.mybatisplus.annotation.TableField; | |
| 5 | +import com.baomidou.mybatisplus.annotation.TableId; | |
| 6 | +import com.baomidou.mybatisplus.annotation.TableName; | |
| 7 | +import lombok.Data; | |
| 8 | +import lombok.ToString; | |
| 9 | + | |
| 10 | +import java.io.Serializable; | |
| 11 | +import java.util.Date; | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * @ProjectName: talkonline | |
| 15 | + * @Package: com.lyms.talkonlineweb.domain | |
| 16 | + * @ClassName: LymsAttention | |
| 17 | + * @Author: lqy | |
| 18 | + * @Description: 商品表 | |
| 19 | + * @Date: 2021-09-10 16:08 | |
| 20 | + * @Version: | |
| 21 | + */ | |
| 22 | +@TableName(value ="lyms_good") | |
| 23 | +@Data | |
| 24 | +@ToString | |
| 25 | +public class LymsGoods extends BaseModel implements Serializable { | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * | |
| 29 | + */ | |
| 30 | + @TableId(value = "id", type = IdType.AUTO) | |
| 31 | + private Integer id; | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * 价格 | |
| 35 | + */ | |
| 36 | + @TableField(value = "price") | |
| 37 | + private int price; | |
| 38 | + | |
| 39 | + /** | |
| 40 | + * 名称 | |
| 41 | + */ | |
| 42 | + @TableField(value = "name") | |
| 43 | + private String name; | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * 类型 | |
| 47 | + */ | |
| 48 | + @TableField(value = "type") | |
| 49 | + private String type; | |
| 50 | + | |
| 51 | + | |
| 52 | + /** | |
| 53 | + * 创建时间 | |
| 54 | + */ | |
| 55 | + @TableField(value = "createdtime") | |
| 56 | + private Date createdtime; | |
| 57 | + | |
| 58 | + | |
| 59 | +} |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsOrder.java
View file @
e3ed14a
| ... | ... | @@ -53,12 +53,31 @@ |
| 53 | 53 | * 价格 |
| 54 | 54 | */ |
| 55 | 55 | @TableField(value = "price") |
| 56 | - private Integer price; | |
| 56 | + private int price; | |
| 57 | 57 | /** |
| 58 | 58 | * 购买数量 |
| 59 | 59 | */ |
| 60 | 60 | @TableField(value = "cnt") |
| 61 | 61 | private Integer cnt; |
| 62 | + | |
| 63 | + /** | |
| 64 | + * 订单状态 订单生成(0),支付成功(1),处理完成(2),处理失败(-1) | |
| 65 | + */ | |
| 66 | + @TableField(value = "status") | |
| 67 | + private Integer status; | |
| 68 | + | |
| 69 | + /** | |
| 70 | + * 支付订单号 | |
| 71 | + */ | |
| 72 | + @TableField(value = "payorderid") | |
| 73 | + private String payorderid; | |
| 74 | + | |
| 75 | + /** | |
| 76 | + * 用户微信openid | |
| 77 | + */ | |
| 78 | + @TableField(value = "openid") | |
| 79 | + private String openid; | |
| 80 | + | |
| 62 | 81 | |
| 63 | 82 | |
| 64 | 83 | /** |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsPatient.java
View file @
e3ed14a
talkonlineweb/src/main/java/com/lyms/talkonlineweb/enums/PayStatus.java
View file @
e3ed14a
| 1 | +package com.lyms.talkonlineweb.enums; | |
| 2 | + | |
| 3 | +import lombok.Getter; | |
| 4 | + | |
| 5 | +/** | |
| 6 | + * @ProjectName: talkonline | |
| 7 | + * @Package: com.lyms.talkonlineweb.enums | |
| 8 | + * @ClassName: PayStatus | |
| 9 | + * @Author: lqy | |
| 10 | + * @Description: 订单支付状态 | |
| 11 | + * @Date: 2021-09-11 10:23 | |
| 12 | + * @Version: | |
| 13 | + */ | |
| 14 | +@Getter | |
| 15 | +public enum PayStatus { | |
| 16 | +//订单生成(0),支付成功(1),处理完成(2),处理失败(-1) | |
| 17 | + CREATED(0, "订单生成"), | |
| 18 | + SUCCESS(1, "支付成功"), | |
| 19 | + COMPLITE(2, "处理完成"), | |
| 20 | + FAIL(-1, "处理失败"); | |
| 21 | + | |
| 22 | + private Integer code; | |
| 23 | + private String name; | |
| 24 | + | |
| 25 | + PayStatus(Integer code, String name){ | |
| 26 | + this.code = code; | |
| 27 | + this.name = name; | |
| 28 | + } | |
| 29 | + | |
| 30 | + public static String getName(Integer code) { | |
| 31 | + PayStatus[] values = PayStatus.values(); | |
| 32 | + for (PayStatus value : values) { | |
| 33 | + if (value.getCode().equals(code)) { | |
| 34 | + return value.getName(); | |
| 35 | + } | |
| 36 | + } | |
| 37 | + return ""; | |
| 38 | + } | |
| 39 | +} |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/mapper/LymsGoodsMapper.java
View file @
e3ed14a
| 1 | +package com.lyms.talkonlineweb.mapper; | |
| 2 | + | |
| 3 | +import com.baomidou.mybatisplus.core.mapper.BaseMapper; | |
| 4 | +import com.lyms.talkonlineweb.domain.LymsGoods; | |
| 5 | + | |
| 6 | +/** | |
| 7 | + * @Entity com.lyms.talkonlineweb.domain.LymsGoods | |
| 8 | + */ | |
| 9 | +public interface LymsGoodsMapper extends BaseMapper<LymsGoods> { | |
| 10 | + | |
| 11 | +} |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/mapper/LymsPatientMapper.java
View file @
e3ed14a
| ... | ... | @@ -2,11 +2,15 @@ |
| 2 | 2 | |
| 3 | 3 | import com.lyms.talkonlineweb.domain.LymsPatient; |
| 4 | 4 | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| 5 | +import org.apache.ibatis.annotations.Param; | |
| 6 | +import org.apache.ibatis.annotations.Update; | |
| 5 | 7 | |
| 6 | 8 | /** |
| 7 | 9 | * @Entity com.lyms.talkonlineweb.domain.LymsPatient |
| 8 | 10 | */ |
| 9 | 11 | public interface LymsPatientMapper extends BaseMapper<LymsPatient> { |
| 10 | 12 | |
| 13 | + @Update(" UPDATE lyms_patient SET ccnt = ccnt+#{cnt} WHERE id = #{id}") | |
| 14 | + int updatePatientCcnt(@Param("cnt") Integer cnt, @Param("id") Integer id); | |
| 11 | 15 | } |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/service/LymsGoodsService.java
View file @
e3ed14a
| 1 | +package com.lyms.talkonlineweb.service; | |
| 2 | + | |
| 3 | +import com.baomidou.mybatisplus.extension.service.IService; | |
| 4 | +import com.lyms.talkonlineweb.domain.LymsGoods; | |
| 5 | +import com.lyms.talkonlineweb.domain.LymsOrder; | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * | |
| 9 | + */ | |
| 10 | +public interface LymsGoodsService extends IService<LymsGoods> { | |
| 11 | + | |
| 12 | +} |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/service/LymsOrderService.java
View file @
e3ed14a
talkonlineweb/src/main/java/com/lyms/talkonlineweb/service/impl/LymsGoodsServiceImpl.java
View file @
e3ed14a
| 1 | +package com.lyms.talkonlineweb.service.impl; | |
| 2 | + | |
| 3 | +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | |
| 4 | +import com.lyms.talkonlineweb.domain.LymsGoods; | |
| 5 | +import com.lyms.talkonlineweb.mapper.LymsGoodsMapper; | |
| 6 | +import com.lyms.talkonlineweb.service.LymsGoodsService; | |
| 7 | +import org.springframework.stereotype.Service; | |
| 8 | + | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * | |
| 12 | + */ | |
| 13 | +@Service | |
| 14 | +public class LymsGoodsServiceImpl extends ServiceImpl<LymsGoodsMapper, LymsGoods> | |
| 15 | + implements LymsGoodsService { | |
| 16 | +} |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/service/impl/LymsOrderServiceImpl.java
View file @
e3ed14a
| 1 | 1 | package com.lyms.talkonlineweb.service.impl; |
| 2 | 2 | |
| 3 | +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | |
| 4 | +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | |
| 3 | 5 | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| 4 | 6 | import com.lyms.talkonlineweb.domain.LymsAttention; |
| 5 | 7 | import com.lyms.talkonlineweb.domain.LymsOrder; |
| 8 | +import com.lyms.talkonlineweb.domain.LymsPatient; | |
| 9 | +import com.lyms.talkonlineweb.domain.LymsTcard; | |
| 10 | +import com.lyms.talkonlineweb.enums.PayStatus; | |
| 6 | 11 | import com.lyms.talkonlineweb.mapper.LymsAttentionMapper; |
| 7 | 12 | import com.lyms.talkonlineweb.mapper.LymsOrderMapper; |
| 13 | +import com.lyms.talkonlineweb.mapper.LymsPatientMapper; | |
| 14 | +import com.lyms.talkonlineweb.mapper.LymsTcardMapper; | |
| 8 | 15 | import com.lyms.talkonlineweb.service.LymsAttentionService; |
| 9 | 16 | import com.lyms.talkonlineweb.service.LymsOrderService; |
| 17 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 10 | 18 | import org.springframework.stereotype.Service; |
| 19 | +import org.springframework.transaction.annotation.Transactional; | |
| 11 | 20 | |
| 21 | +import java.util.Date; | |
| 22 | + | |
| 12 | 23 | /** |
| 13 | 24 | * |
| 14 | 25 | */ |
| ... | ... | @@ -16,5 +27,42 @@ |
| 16 | 27 | public class LymsOrderServiceImpl extends ServiceImpl<LymsOrderMapper, LymsOrder> |
| 17 | 28 | implements LymsOrderService { |
| 18 | 29 | |
| 30 | + @Autowired | |
| 31 | + private LymsPatientMapper lymsPatientMapper; | |
| 32 | + | |
| 33 | + @Autowired | |
| 34 | + private LymsTcardMapper lymsTcardMapper; | |
| 35 | + | |
| 36 | + @Override | |
| 37 | + @Transactional(rollbackFor = Exception.class) | |
| 38 | + public boolean handleOrder(String payOrderId, String mchOrderNo) { | |
| 39 | + | |
| 40 | + //处理业务逻辑 | |
| 41 | + LambdaQueryWrapper<LymsOrder> wrapper = new QueryWrapper().lambda(); | |
| 42 | + wrapper.eq(LymsOrder::getOrderno, mchOrderNo); | |
| 43 | + wrapper.eq(LymsOrder::getPayorderid, payOrderId); | |
| 44 | + LymsOrder lymsOrders = getOne(wrapper); | |
| 45 | + if (lymsOrders != null) { | |
| 46 | + | |
| 47 | + LymsPatient patient = lymsPatientMapper.selectById(lymsOrders.getPid()); | |
| 48 | + if (patient == null) | |
| 49 | + { | |
| 50 | + return false; | |
| 51 | + } | |
| 52 | + | |
| 53 | + //TODO | |
| 54 | +// LymsTcard tcard = new LymsTcard(); | |
| 55 | +// lymsTcardMapper.insert(); | |
| 56 | + | |
| 57 | + //更新患者的卡的数量 | |
| 58 | + lymsPatientMapper.updatePatientCcnt(lymsOrders.getCnt(),patient.getId()); | |
| 59 | + | |
| 60 | + lymsOrders.setStatus(PayStatus.SUCCESS.getCode()); | |
| 61 | + lymsOrders.setUpdatedtime(new Date()); | |
| 62 | + updateById(lymsOrders); | |
| 63 | + } | |
| 64 | + | |
| 65 | + return false; | |
| 66 | + } | |
| 19 | 67 | } |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/Constant.java
View file @
e3ed14a
| ... | ... | @@ -8,5 +8,43 @@ |
| 8 | 8 | public static final int ROOT_MENU = 0;//一级菜单 |
| 9 | 9 | public static final String CUR_USER = "cur_user";//当前用户 |
| 10 | 10 | public static final String COMMON_PASSWD = "123456";//通用密码 |
| 11 | + | |
| 12 | + /** | |
| 13 | + * 患者端商户id | |
| 14 | + */ | |
| 15 | + public static final String MCHID = "1426009502"; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * 患者小程序appid | |
| 19 | + */ | |
| 20 | + public static final String PAT_APP_ID = "wxe3b5c34317a0f85b"; | |
| 21 | + | |
| 22 | + /** | |
| 23 | + * 签名加密 key | |
| 24 | + */ | |
| 25 | + public static final String REQ_KEY = "5fddsw5e7bfdeb99327435d125aebcs9"; | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * 微信服务器的域名 | |
| 29 | + */ | |
| 30 | + public static final String WEIXIN_SERVER = "https://api.weixin.qq.com"; | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * 微信支付订单url | |
| 34 | + */ | |
| 35 | + public static final String PAY_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder"; | |
| 36 | + | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * 认证类型填写为 authorization_code | |
| 40 | + */ | |
| 41 | + public static final String GRANT_TYPE = "authorization_code"; | |
| 42 | + | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * 小程序的 app secret | |
| 46 | + */ | |
| 47 | + public static final String WX_SECRET = "005ab68859ca2504b7217dac4c903cd2"; | |
| 48 | + | |
| 11 | 49 | } |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/DateUtil.java
View file @
e3ed14a
talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/HttpUtil.java
View file @
e3ed14a
| 1 | +package com.lyms.talkonlineweb.util; | |
| 2 | + | |
| 3 | +import java.io.IOException; | |
| 4 | + | |
| 5 | + | |
| 6 | +import lombok.extern.log4j.Log4j2; | |
| 7 | +import org.apache.http.HttpEntity; | |
| 8 | +import org.apache.http.HttpResponse; | |
| 9 | +import org.apache.http.client.config.RequestConfig; | |
| 10 | +import org.apache.http.client.methods.HttpGet; | |
| 11 | +import org.apache.http.client.methods.HttpPost; | |
| 12 | +import org.apache.http.entity.StringEntity; | |
| 13 | +import org.apache.http.impl.client.CloseableHttpClient; | |
| 14 | +import org.apache.http.impl.client.HttpClients; | |
| 15 | +import org.apache.http.util.EntityUtils; | |
| 16 | +/** | |
| 17 | + * @ProjectName: talkonline | |
| 18 | + * @Package: com.lyms.talkonlineweb.util | |
| 19 | + * @ClassName: HttpUtil | |
| 20 | + * @Author: lqy | |
| 21 | + * @Description: | |
| 22 | + * @Date: 2021-09-11 16:24 | |
| 23 | + * @Version: | |
| 24 | + */ | |
| 25 | +@Log4j2 | |
| 26 | +public class HttpUtil { | |
| 27 | + | |
| 28 | + // 连接超时时间,默认10秒 | |
| 29 | + private static int socketTimeout = 10000; | |
| 30 | + // 传输超时时间,默认30秒 | |
| 31 | + private static int connectTimeout = 30000; | |
| 32 | + // 请求器的配置 | |
| 33 | + private static RequestConfig requestConfig; | |
| 34 | + // HTTP请求器 | |
| 35 | + private static CloseableHttpClient httpClient; | |
| 36 | + static { | |
| 37 | + httpClient = HttpClients.custom().build(); | |
| 38 | + // 根据默认超时限制初始化requestConfig | |
| 39 | + requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build(); | |
| 40 | + } | |
| 41 | + | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * post 请求 | |
| 45 | + * @param url | |
| 46 | + * @param content | |
| 47 | + * @return | |
| 48 | + */ | |
| 49 | + public static String postData(String url, String content) { | |
| 50 | + | |
| 51 | + String result = null; | |
| 52 | + HttpPost httpPost = new HttpPost(url); | |
| 53 | + StringEntity postEntity = new StringEntity(content, "UTF-8"); | |
| 54 | + httpPost.addHeader("Content-Type", "text/xml"); | |
| 55 | + httpPost.setEntity(postEntity); | |
| 56 | + // 设置请求器的配置 | |
| 57 | + httpPost.setConfig(requestConfig); | |
| 58 | + try { | |
| 59 | + HttpResponse response = httpClient.execute(httpPost); | |
| 60 | + HttpEntity entity = response.getEntity(); | |
| 61 | + result = EntityUtils.toString(entity, "UTF-8"); | |
| 62 | + }catch (Exception e){ | |
| 63 | + log.error("post execute error,url : {}",url,e); | |
| 64 | + } finally { | |
| 65 | + httpPost.abort(); | |
| 66 | + } | |
| 67 | + return result; | |
| 68 | + } | |
| 69 | + | |
| 70 | + | |
| 71 | + /** | |
| 72 | + * get请求 | |
| 73 | + * @param url | |
| 74 | + * @return | |
| 75 | + */ | |
| 76 | + public static String getData(String url) { | |
| 77 | + String result = null; | |
| 78 | + HttpGet httpGet = new HttpGet(url); | |
| 79 | + httpGet.setConfig(requestConfig); | |
| 80 | + try { | |
| 81 | + HttpResponse response = httpClient.execute(httpGet); | |
| 82 | + HttpEntity entity = response.getEntity(); | |
| 83 | + result = EntityUtils.toString(entity, "UTF-8"); | |
| 84 | + }catch (Exception e){ | |
| 85 | + log.error("get execute error,url : {}",url,e); | |
| 86 | + } finally { | |
| 87 | + httpGet.abort(); | |
| 88 | + } | |
| 89 | + return result; | |
| 90 | + } | |
| 91 | + | |
| 92 | +} |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/IdWorker.java
View file @
e3ed14a
| 1 | +package com.lyms.talkonlineweb.util; | |
| 2 | + | |
| 3 | + | |
| 4 | +public class IdWorker { | |
| 5 | + | |
| 6 | + private final long workerId; | |
| 7 | + private final long epoch = 1403854494756L; | |
| 8 | + private final long workerIdBits = 10L; | |
| 9 | + private final long maxWorkerId = -1L ^ -1L << this.workerIdBits; | |
| 10 | + private long sequence = 0L; | |
| 11 | + private final long sequenceBits = 12L; | |
| 12 | + | |
| 13 | + private final long workerIdShift = this.sequenceBits; | |
| 14 | + private final long timestampLeftShift = this.sequenceBits + this.workerIdBits; | |
| 15 | + private final long sequenceMask = -1L ^ -1L << this.sequenceBits; | |
| 16 | + private long lastTimestamp = -1L; | |
| 17 | + | |
| 18 | + private IdWorker(long workerId) { | |
| 19 | + if (workerId > this.maxWorkerId || workerId < 0) { | |
| 20 | + throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", this.maxWorkerId)); | |
| 21 | + } | |
| 22 | + this.workerId = workerId; | |
| 23 | + } | |
| 24 | + | |
| 25 | + public synchronized long nextId() throws Exception { | |
| 26 | + long timestamp = this.timeGen(); | |
| 27 | + if (this.lastTimestamp == timestamp) { | |
| 28 | + this.sequence = this.sequence + 1 & this.sequenceMask; | |
| 29 | + if (this.sequence == 0) { | |
| 30 | + timestamp = this.tilNextMillis(this.lastTimestamp); | |
| 31 | + } | |
| 32 | + } else { | |
| 33 | + this.sequence = 0; | |
| 34 | + } | |
| 35 | + | |
| 36 | + if (timestamp < this.lastTimestamp) { | |
| 37 | + throw new Exception(String.format("clock moved backwards.Refusing to generate id for %d milliseconds", (this.lastTimestamp - timestamp))); | |
| 38 | + } | |
| 39 | + | |
| 40 | + this.lastTimestamp = timestamp; | |
| 41 | + return timestamp - this.epoch << this.timestampLeftShift | this.workerId << this.workerIdShift | this.sequence; | |
| 42 | + } | |
| 43 | + | |
| 44 | + private static IdWorker flowIdWorker = new IdWorker(1); | |
| 45 | + public static IdWorker getFlowIdWorkerInstance() { | |
| 46 | + return flowIdWorker; | |
| 47 | + } | |
| 48 | + | |
| 49 | + private long tilNextMillis(long lastTimestamp) { | |
| 50 | + long timestamp = this.timeGen(); | |
| 51 | + while (timestamp <= lastTimestamp) { | |
| 52 | + timestamp = this.timeGen(); | |
| 53 | + } | |
| 54 | + return timestamp; | |
| 55 | + } | |
| 56 | + | |
| 57 | + private long timeGen() { | |
| 58 | + return System.currentTimeMillis(); | |
| 59 | + } | |
| 60 | + | |
| 61 | + | |
| 62 | +} |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/JsonUtil.java
View file @
e3ed14a
| ... | ... | @@ -2,7 +2,10 @@ |
| 2 | 2 | |
| 3 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | 4 | |
| 5 | +import java.util.HashMap; | |
| 6 | +import java.util.Map; | |
| 5 | 7 | |
| 8 | + | |
| 6 | 9 | /** |
| 7 | 10 | * Created by Administrator on 2019-09-06. |
| 8 | 11 | */ |
| ... | ... | @@ -27,5 +30,37 @@ |
| 27 | 30 | return null; |
| 28 | 31 | } |
| 29 | 32 | |
| 33 | + /** | |
| 34 | + * json转换为map | |
| 35 | + * @param json | |
| 36 | + * @param hashMapClass | |
| 37 | + * @return | |
| 38 | + */ | |
| 39 | + public static Map<String, Object> str2Map(String json, Class<HashMap> hashMapClass) { | |
| 40 | + try { | |
| 41 | + ObjectMapper objectMapper = new ObjectMapper(); | |
| 42 | + return objectMapper.readValue(json,hashMapClass); | |
| 43 | + } catch (Exception e) { | |
| 44 | + | |
| 45 | + } | |
| 46 | + return null; | |
| 47 | + } | |
| 48 | + | |
| 49 | + /** | |
| 50 | + * 接送转换为对象 | |
| 51 | + * @param json | |
| 52 | + * @param clzz | |
| 53 | + * @param <T> | |
| 54 | + * @return | |
| 55 | + */ | |
| 56 | + public static <T> T str2Obj(String json, Class<T> clzz) { | |
| 57 | + try { | |
| 58 | + ObjectMapper objectMapper = new ObjectMapper(); | |
| 59 | + return objectMapper.readValue(json,clzz); | |
| 60 | + } catch (Exception e) { | |
| 61 | + | |
| 62 | + } | |
| 63 | + return null; | |
| 64 | + } | |
| 30 | 65 | } |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/NumberUtil.java
View file @
e3ed14a
| 1 | +package com.lyms.talkonlineweb.util; | |
| 2 | + | |
| 3 | +import java.util.Random; | |
| 4 | + | |
| 5 | +/** | |
| 6 | + * @ProjectName: talkonline | |
| 7 | + * @Package: com.lyms.talkonlineweb.util | |
| 8 | + * @ClassName: NumberUtil | |
| 9 | + * @Author: lqy | |
| 10 | + * @Description: 返回指定长度的随机数字字符串 | |
| 11 | + * @Date: 2021-09-11 11:29 | |
| 12 | + * @Version: | |
| 13 | + */ | |
| 14 | + | |
| 15 | +public class NumberUtil { | |
| 16 | + | |
| 17 | + private static final String RANDSTRING = "0123456789"; | |
| 18 | + /** | |
| 19 | + * 获取随机的字符 | |
| 20 | + */ | |
| 21 | + public static String getRandomString(int length){ | |
| 22 | + StringBuilder builder = new StringBuilder(length); | |
| 23 | + Random random = new Random(); | |
| 24 | + for(int i=0;i<length;i++){ | |
| 25 | + builder.append(RANDSTRING.charAt(random.nextInt(RANDSTRING.length()))); | |
| 26 | + } | |
| 27 | + return builder.toString(); | |
| 28 | + } | |
| 29 | + | |
| 30 | +} |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/PayDigestUtil.java
View file @
e3ed14a
| 1 | +package com.lyms.talkonlineweb.util; | |
| 2 | + | |
| 3 | +import java.io.UnsupportedEncodingException; | |
| 4 | +import java.lang.reflect.Field; | |
| 5 | +import java.security.MessageDigest; | |
| 6 | +import java.security.NoSuchAlgorithmException; | |
| 7 | +import java.util.ArrayList; | |
| 8 | +import java.util.Arrays; | |
| 9 | +import java.util.HashMap; | |
| 10 | +import java.util.Map; | |
| 11 | + | |
| 12 | +public class PayDigestUtil { | |
| 13 | + | |
| 14 | + private static String encodingCharset = "UTF-8"; | |
| 15 | + | |
| 16 | + /** | |
| 17 | + * @param aValue | |
| 18 | + * @param aKey | |
| 19 | + * @return | |
| 20 | + */ | |
| 21 | + public static String hmacSign(String aValue, String aKey) { | |
| 22 | + byte k_ipad[] = new byte[64]; | |
| 23 | + byte k_opad[] = new byte[64]; | |
| 24 | + byte keyb[]; | |
| 25 | + byte value[]; | |
| 26 | + try { | |
| 27 | + keyb = aKey.getBytes(encodingCharset); | |
| 28 | + value = aValue.getBytes(encodingCharset); | |
| 29 | + } catch (UnsupportedEncodingException e) { | |
| 30 | + keyb = aKey.getBytes(); | |
| 31 | + value = aValue.getBytes(); | |
| 32 | + } | |
| 33 | + | |
| 34 | + Arrays.fill(k_ipad, keyb.length, 64, (byte) 54); | |
| 35 | + Arrays.fill(k_opad, keyb.length, 64, (byte) 92); | |
| 36 | + for (int i = 0; i < keyb.length; i++) { | |
| 37 | + k_ipad[i] = (byte) (keyb[i] ^ 0x36); | |
| 38 | + k_opad[i] = (byte) (keyb[i] ^ 0x5c); | |
| 39 | + } | |
| 40 | + | |
| 41 | + MessageDigest md = null; | |
| 42 | + try { | |
| 43 | + md = MessageDigest.getInstance("MD5"); | |
| 44 | + } catch (NoSuchAlgorithmException e) { | |
| 45 | + | |
| 46 | + return null; | |
| 47 | + } | |
| 48 | + md.update(k_ipad); | |
| 49 | + md.update(value); | |
| 50 | + byte dg[] = md.digest(); | |
| 51 | + md.reset(); | |
| 52 | + md.update(k_opad); | |
| 53 | + md.update(dg, 0, 16); | |
| 54 | + dg = md.digest(); | |
| 55 | + return toHex(dg); | |
| 56 | + } | |
| 57 | + | |
| 58 | + public static String toHex(byte input[]) { | |
| 59 | + if (input == null) | |
| 60 | + { | |
| 61 | + return null; | |
| 62 | + } | |
| 63 | + StringBuffer output = new StringBuffer(input.length * 2); | |
| 64 | + for (int i = 0; i < input.length; i++) { | |
| 65 | + int current = input[i] & 0xff; | |
| 66 | + if (current < 16) | |
| 67 | + { | |
| 68 | + output.append("0"); | |
| 69 | + } | |
| 70 | + output.append(Integer.toString(current, 16)); | |
| 71 | + } | |
| 72 | + | |
| 73 | + return output.toString(); | |
| 74 | + } | |
| 75 | + | |
| 76 | + /** | |
| 77 | + * | |
| 78 | + * @param args | |
| 79 | + * @param key | |
| 80 | + * @return | |
| 81 | + */ | |
| 82 | + public static String getHmac(String[] args, String key) { | |
| 83 | + if (args == null || args.length == 0) { | |
| 84 | + return (null); | |
| 85 | + } | |
| 86 | + StringBuffer str = new StringBuffer(); | |
| 87 | + for (int i = 0; i < args.length; i++) { | |
| 88 | + str.append(args[i]); | |
| 89 | + } | |
| 90 | + return (hmacSign(str.toString(), key)); | |
| 91 | + } | |
| 92 | + | |
| 93 | + /** | |
| 94 | + * @param aValue | |
| 95 | + * @return | |
| 96 | + */ | |
| 97 | + public static String digest(String aValue) { | |
| 98 | + aValue = aValue.trim(); | |
| 99 | + byte value[]; | |
| 100 | + try { | |
| 101 | + value = aValue.getBytes(encodingCharset); | |
| 102 | + } catch (UnsupportedEncodingException e) { | |
| 103 | + value = aValue.getBytes(); | |
| 104 | + } | |
| 105 | + MessageDigest md = null; | |
| 106 | + try { | |
| 107 | + md = MessageDigest.getInstance("SHA"); | |
| 108 | + } catch (NoSuchAlgorithmException e) { | |
| 109 | + e.printStackTrace(); | |
| 110 | + return null; | |
| 111 | + } | |
| 112 | + return toHex(md.digest(value)); | |
| 113 | + | |
| 114 | + } | |
| 115 | + | |
| 116 | + public static String md5(String value, String charset) { | |
| 117 | + MessageDigest md = null; | |
| 118 | + try { | |
| 119 | + byte[] data = value.getBytes(charset); | |
| 120 | + md = MessageDigest.getInstance("MD5"); | |
| 121 | + byte[] digestData = md.digest(data); | |
| 122 | + return toHex(digestData); | |
| 123 | + } catch (NoSuchAlgorithmException e) { | |
| 124 | + e.printStackTrace(); | |
| 125 | + return null; | |
| 126 | + } catch (UnsupportedEncodingException e) { | |
| 127 | + e.printStackTrace(); | |
| 128 | + return null; | |
| 129 | + } | |
| 130 | + } | |
| 131 | + | |
| 132 | + public static String getSign(Map<String,String> map, String key){ | |
| 133 | + ArrayList<String> list = new ArrayList<String>(); | |
| 134 | + for(Map.Entry<String,String> entry:map.entrySet()){ | |
| 135 | + if(!"".equals(entry.getValue())){ | |
| 136 | + list.add(entry.getKey() + "=" + entry.getValue() + "&"); | |
| 137 | + } | |
| 138 | + } | |
| 139 | + int size = list.size(); | |
| 140 | + String[] arrayToSort = list.toArray(new String[size]); | |
| 141 | + Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER); | |
| 142 | + StringBuilder sb = new StringBuilder(); | |
| 143 | + for(int i = 0; i < size; i ++) { | |
| 144 | + sb.append(arrayToSort[i]); | |
| 145 | + } | |
| 146 | + String result = sb.toString(); | |
| 147 | + result += "key=" + key; | |
| 148 | + result = md5(result, encodingCharset).toUpperCase(); | |
| 149 | + return result; | |
| 150 | + } | |
| 151 | + | |
| 152 | + /** | |
| 153 | + * | |
| 154 | + * @param map | |
| 155 | + * @param key | |
| 156 | + * @param notContains 不包含的签名字段 | |
| 157 | + * @return | |
| 158 | + */ | |
| 159 | + public static String getSign(Map<String,String> map, String key, String... notContains){ | |
| 160 | + Map<String,String> newMap = new HashMap<String,String>(); | |
| 161 | + for(Map.Entry<String,String> entry:map.entrySet()){ | |
| 162 | + boolean isContain = false; | |
| 163 | + for(int i=0; i<notContains.length; i++) { | |
| 164 | + if(entry.getKey().equals(notContains[i])) { | |
| 165 | + isContain = true; | |
| 166 | + break; | |
| 167 | + } | |
| 168 | + } | |
| 169 | + if(!isContain) { | |
| 170 | + newMap.put(entry.getKey(), entry.getValue()); | |
| 171 | + } | |
| 172 | + } | |
| 173 | + return getSign(newMap, key); | |
| 174 | + } | |
| 175 | + | |
| 176 | +} |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/StringUtil.java
View file @
e3ed14a
| ... | ... | @@ -39,5 +39,20 @@ |
| 39 | 39 | } |
| 40 | 40 | return false; |
| 41 | 41 | } |
| 42 | + | |
| 43 | + /** | |
| 44 | + * 生成一个唯一编号 | |
| 45 | + * @return | |
| 46 | + */ | |
| 47 | + public static String getUniqueNo() | |
| 48 | + { | |
| 49 | + try | |
| 50 | + { | |
| 51 | + return String.format("%s%s%06d", "D", DateUtil.getSeqString(), IdWorker.getFlowIdWorkerInstance().nextId()); | |
| 52 | + }catch (Exception e){ | |
| 53 | + | |
| 54 | + } | |
| 55 | + return ""; | |
| 56 | + } | |
| 42 | 57 | } |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/WeiXinUtil.java
View file @
e3ed14a
| 1 | +package com.lyms.talkonlineweb.util; | |
| 2 | + | |
| 3 | +import lombok.extern.log4j.Log4j2; | |
| 4 | + | |
| 5 | +import java.util.HashMap; | |
| 6 | +import java.util.Map; | |
| 7 | + | |
| 8 | +/** | |
| 9 | + * @ProjectName: talkonline | |
| 10 | + * @Package: com.lyms.talkonlineweb.util | |
| 11 | + * @ClassName: WeiXinUtil | |
| 12 | + * @Author: lqy | |
| 13 | + * @Description: 微信接口调用 | |
| 14 | + * @Date: 2021-09-13 18:10 | |
| 15 | + * @Version: | |
| 16 | + */ | |
| 17 | +@Log4j2 | |
| 18 | +public class WeiXinUtil { | |
| 19 | + //微信返回的key定义 | |
| 20 | + private static final String OPEN_ID_KEY = "openid"; | |
| 21 | + /** | |
| 22 | + * 获取微信用户的openid | |
| 23 | + * | |
| 24 | + * @param code 登录时获取的code | |
| 25 | + * @return 微信方获取openid | |
| 26 | + */ | |
| 27 | + public static String getWxOpenId(String code) { | |
| 28 | + if (StringUtil.isEmpty(code)) { | |
| 29 | + return null; | |
| 30 | + } | |
| 31 | + try { | |
| 32 | + StringBuilder reqUrl = new StringBuilder(); | |
| 33 | + reqUrl.append("/sns/jscode2session?appid=").append(Constant.PAT_APP_ID). | |
| 34 | + append("&secret="). append(Constant.WX_SECRET).append("&js_code=").append(code). | |
| 35 | + append("&grant_type=").append(Constant.GRANT_TYPE); | |
| 36 | + log.info("getWxOpenId url : {}",reqUrl); | |
| 37 | + String response = HttpUtil.getData(Constant.WEIXIN_SERVER + reqUrl); | |
| 38 | + | |
| 39 | + log.info("getWxOpenId jscode2session response : {}" , response); | |
| 40 | + if (StringUtil.isNotEmpty(response)) | |
| 41 | + { | |
| 42 | + Map<String, Object> result = JsonUtil.str2Map(response, HashMap.class); | |
| 43 | + if (result.containsKey(OPEN_ID_KEY)) | |
| 44 | + { | |
| 45 | + return result.get(OPEN_ID_KEY).toString(); | |
| 46 | + } | |
| 47 | + } | |
| 48 | + } catch (Exception e) { | |
| 49 | + log.error("getWxOpenId by code: {}", code, e); | |
| 50 | + } | |
| 51 | + return null; | |
| 52 | + } | |
| 53 | + | |
| 54 | +} |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/XmlUtil.java
View file @
e3ed14a
| 1 | +package com.lyms.talkonlineweb.util; | |
| 2 | + | |
| 3 | +import org.w3c.dom.Node; | |
| 4 | +import org.w3c.dom.NodeList; | |
| 5 | + | |
| 6 | +import javax.xml.parsers.DocumentBuilder; | |
| 7 | +import javax.xml.parsers.DocumentBuilderFactory; | |
| 8 | +import javax.xml.transform.OutputKeys; | |
| 9 | +import javax.xml.transform.Transformer; | |
| 10 | +import javax.xml.transform.TransformerFactory; | |
| 11 | +import javax.xml.transform.dom.DOMSource; | |
| 12 | +import javax.xml.transform.stream.StreamResult; | |
| 13 | +import java.io.ByteArrayInputStream; | |
| 14 | +import java.io.InputStream; | |
| 15 | +import java.io.StringWriter; | |
| 16 | +import java.util.HashMap; | |
| 17 | +import java.util.Map; | |
| 18 | + | |
| 19 | +/** | |
| 20 | + * @ProjectName: talkonline | |
| 21 | + * @Package: com.lyms.talkonlineweb.util | |
| 22 | + * @ClassName: XmlUtil | |
| 23 | + * @Author: lqy | |
| 24 | + * @Description: xml转换 | |
| 25 | + * @Date: 2021-09-11 14:40 | |
| 26 | + * @Version: | |
| 27 | + */ | |
| 28 | + | |
| 29 | +public class XmlUtil { | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * 将Map转换为XML格式的字符串 | |
| 33 | + * | |
| 34 | + * @param data Map类型数据 | |
| 35 | + * @return XML格式的字符串 | |
| 36 | + * @throws Exception | |
| 37 | + */ | |
| 38 | + public static String mapToXml(Map<String, String> data) throws Exception{ | |
| 39 | + try { | |
| 40 | + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); | |
| 41 | + DocumentBuilder documentBuilder= documentBuilderFactory.newDocumentBuilder(); | |
| 42 | + org.w3c.dom.Document document = documentBuilder.newDocument(); | |
| 43 | + org.w3c.dom.Element root = document.createElement("xml"); | |
| 44 | + document.appendChild(root); | |
| 45 | + for (String key: data.keySet()) { | |
| 46 | + String value = data.get(key); | |
| 47 | + if (value == null) { | |
| 48 | + value = ""; | |
| 49 | + } | |
| 50 | + value = value.trim(); | |
| 51 | + org.w3c.dom.Element filed = document.createElement(String.valueOf(key)); | |
| 52 | + filed.appendChild(document.createTextNode(value)); | |
| 53 | + root.appendChild(filed); | |
| 54 | + } | |
| 55 | + TransformerFactory tf = TransformerFactory.newInstance(); | |
| 56 | + Transformer transformer = tf.newTransformer(); | |
| 57 | + DOMSource source = new DOMSource(document); | |
| 58 | + transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); | |
| 59 | + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); | |
| 60 | + StringWriter writer = new StringWriter(); | |
| 61 | + StreamResult result = new StreamResult(writer); | |
| 62 | + transformer.transform(source, result); | |
| 63 | + String output = writer.getBuffer().toString(); | |
| 64 | + writer.close(); | |
| 65 | + return output; | |
| 66 | + } catch (Exception e) { | |
| 67 | + throw e; | |
| 68 | + } | |
| 69 | + } | |
| 70 | + | |
| 71 | + | |
| 72 | + public static Map<String, String> xmlToMap(String strXML) throws Exception{ | |
| 73 | + InputStream stream = null; | |
| 74 | + try { | |
| 75 | + Map<String, String> data = new HashMap<String, String>(); | |
| 76 | + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); | |
| 77 | + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); | |
| 78 | + stream = new ByteArrayInputStream(strXML.getBytes("UTF-8")); | |
| 79 | + org.w3c.dom.Document doc = documentBuilder.parse(stream); | |
| 80 | + doc.getDocumentElement().normalize(); | |
| 81 | + NodeList nodeList = doc.getDocumentElement().getChildNodes(); | |
| 82 | + for (int idx = 0; idx < nodeList.getLength(); ++idx) { | |
| 83 | + Node node = nodeList.item(idx); | |
| 84 | + if (node.getNodeType() == Node.ELEMENT_NODE) { | |
| 85 | + org.w3c.dom.Element element = (org.w3c.dom.Element) node; | |
| 86 | + data.put(element.getNodeName(), element.getTextContent()); | |
| 87 | + } | |
| 88 | + } | |
| 89 | + return data; | |
| 90 | + } catch (Exception ex) { | |
| 91 | + throw ex; | |
| 92 | + }finally { | |
| 93 | + try { | |
| 94 | + stream.close(); | |
| 95 | + } catch (Exception ex) { | |
| 96 | + } | |
| 97 | + } | |
| 98 | + } | |
| 99 | + | |
| 100 | +} |
talkonlineweb/src/main/resources/application.yml
View file @
e3ed14a
talkonlineweb/src/main/resources/logback-spring.xml
View file @
e3ed14a