|
@@ -1,12 +1,16 @@
|
|
|
package com.jpsoft.epay.modules.business.controller;
|
|
|
|
|
|
-import com.jpsoft.epay.modules.base.entity.RechargeRecord;
|
|
|
-import com.jpsoft.epay.modules.base.entity.RoomInfo;
|
|
|
-import com.jpsoft.epay.modules.base.service.RechargeRecordService;
|
|
|
-import com.jpsoft.epay.modules.base.service.RoomInfoService;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.jpsoft.epay.config.RedisConfig;
|
|
|
+import com.jpsoft.epay.modules.base.entity.*;
|
|
|
+import com.jpsoft.epay.modules.base.service.*;
|
|
|
import com.jpsoft.epay.modules.business.service.RechargeService;
|
|
|
import com.jpsoft.epay.modules.common.dto.MessageResult;
|
|
|
+import com.jpsoft.epay.modules.common.utils.StringUtils;
|
|
|
+import com.jpsoft.epay.modules.communication.server.ChannelWrapper;
|
|
|
import com.jpsoft.epay.modules.communication.server.protocol.MeterReceivePacket;
|
|
|
+import com.jpsoft.epay.modules.communication.server.protocol.MeterSendPacket;
|
|
|
+import com.jpsoft.epay.modules.pay.properties.WxPayProperties;
|
|
|
import com.jpsoft.epay.modules.sys.entity.DataDictionary;
|
|
|
import com.jpsoft.epay.modules.sys.service.DataDictionaryService;
|
|
|
import io.swagger.annotations.ApiImplicitParam;
|
|
@@ -14,13 +18,15 @@ import io.swagger.annotations.ApiImplicitParams;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.data.redis.core.ValueOperations;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import javax.net.ssl.HttpsURLConnection;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.net.URL;
|
|
|
+import java.text.DecimalFormat;
|
|
|
+import java.util.*;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@Slf4j
|
|
@@ -33,12 +39,17 @@ public class MobileApiController {
|
|
|
@Autowired
|
|
|
private RechargeService rechargeService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private DataDictionaryService dataDictionaryService;
|
|
|
+
|
|
|
@Autowired
|
|
|
private RechargeRecordService rechargeRecordService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ ValueOperations<String, Object> valueOperations;
|
|
|
|
|
|
@Autowired
|
|
|
- private DataDictionaryService dataDictionaryService;
|
|
|
+ private WxPayProperties wxPayProperties;
|
|
|
|
|
|
/**
|
|
|
* 查询房间
|
|
@@ -153,4 +164,197 @@ public class MobileApiController {
|
|
|
|
|
|
return msgResult;
|
|
|
}
|
|
|
+
|
|
|
+ private RechargeRecord createRechargeRecord(String roomId, String buyType, Integer num, BigDecimal amount) throws Exception{
|
|
|
+ String serialNumber = StringUtils.getOutTradeNo();
|
|
|
+
|
|
|
+ RoomInfo room = roomInfoService.get(roomId);
|
|
|
+
|
|
|
+ //查询用电类型及价格
|
|
|
+ DataDictionary dd = dataDictionaryService.get(room.getUseType());
|
|
|
+
|
|
|
+ BigDecimal price = new BigDecimal(dd.getValue());
|
|
|
+
|
|
|
+ if(num<=0){
|
|
|
+ throw new Exception("充电度数只能为正整数!");
|
|
|
+ }
|
|
|
+
|
|
|
+ BigDecimal totalAmount = price.multiply(new BigDecimal(num));
|
|
|
+
|
|
|
+ DecimalFormat df = new DecimalFormat("#.##");
|
|
|
+
|
|
|
+ if(!totalAmount.equals(amount)){
|
|
|
+ throw new Exception("支付金额应为:" + df.format(totalAmount) + "!");
|
|
|
+ }
|
|
|
+
|
|
|
+ RechargeRecord record = new RechargeRecord();
|
|
|
+ record.setId(UUID.randomUUID().toString());
|
|
|
+ record.setDelFlag(false);
|
|
|
+ record.setCreateTime(new Date());
|
|
|
+ record.setSerialNumber(serialNumber);
|
|
|
+ record.setProductTheme("电费充值(测试)");
|
|
|
+ record.setPaymentStatus("10");//10为未支付 20为支付成功
|
|
|
+ record.setChargingStatus("10");//10为未充电 20为充电成功
|
|
|
+ record.setRoomId(roomId);
|
|
|
+ record.setBuyElectricity(num);
|
|
|
+ record.setBuyAmount(totalAmount);
|
|
|
+ record.setBuyType(buyType); //weipay alipay cash
|
|
|
+
|
|
|
+ int affectCount = rechargeRecordService.insert(record);
|
|
|
+
|
|
|
+ if (affectCount>0){
|
|
|
+ return record;
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getCommonAccessToken() throws Exception{
|
|
|
+ String accessToken = null;
|
|
|
+
|
|
|
+ accessToken = (String)valueOperations.get(RedisConfig.WX_COMMON_ACCESS_TOKEN);
|
|
|
+
|
|
|
+ if(StringUtils.isEmpty(accessToken)){
|
|
|
+ URL realUrl = new URL(String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",
|
|
|
+ wxPayProperties.getAppId(),wxPayProperties.getAppSecret()));
|
|
|
+
|
|
|
+ HttpsURLConnection conn = (HttpsURLConnection) realUrl.openConnection();
|
|
|
+
|
|
|
+ conn.connect();
|
|
|
+
|
|
|
+ InputStream input = conn.getInputStream();
|
|
|
+
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+
|
|
|
+ Map resultMap = objectMapper.readValue(input, Map.class);
|
|
|
+
|
|
|
+ accessToken = (String)resultMap.get("access_token");
|
|
|
+ Integer expiresIn = Integer.valueOf(resultMap.get("expires_in").toString()); //默认是7200秒过期
|
|
|
+
|
|
|
+ valueOperations.set(RedisConfig.WX_COMMON_ACCESS_TOKEN,accessToken,expiresIn,TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
+ return accessToken;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getJSApiTicket() throws Exception{
|
|
|
+ String ticket = null;
|
|
|
+
|
|
|
+ ticket = (String)valueOperations.get(RedisConfig.JS_API_TICKET);
|
|
|
+
|
|
|
+ if(StringUtils.isEmpty(ticket)){
|
|
|
+ String accessToken = getCommonAccessToken();
|
|
|
+
|
|
|
+ URL realUrl = new URL(String.format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=%s",
|
|
|
+ accessToken));
|
|
|
+
|
|
|
+ HttpsURLConnection conn = (HttpsURLConnection) realUrl.openConnection();
|
|
|
+
|
|
|
+ conn.connect();
|
|
|
+
|
|
|
+ InputStream input = conn.getInputStream();
|
|
|
+
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+
|
|
|
+ Map resultMap = objectMapper.readValue(input, Map.class);
|
|
|
+
|
|
|
+ Integer errcode = (Integer)resultMap.get("errcode");
|
|
|
+
|
|
|
+ if(errcode.equals(0)){
|
|
|
+ ticket = (String)resultMap.get("ticket");
|
|
|
+ Integer expiresIn = Integer.valueOf(resultMap.get("expires_in").toString()); //默认是7200秒过期
|
|
|
+
|
|
|
+ valueOperations.set(RedisConfig.JS_API_TICKET,ticket,expiresIn,TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return ticket;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map getAuthAccessToken(String code) throws Exception{
|
|
|
+ String tokenUrl = String.format("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code",
|
|
|
+ wxPayProperties.getAppId(),wxPayProperties.getAppSecret(),code);
|
|
|
+ String key = "AUTH_" + code;
|
|
|
+
|
|
|
+ Map tokenMap = (Map)valueOperations.get(key);
|
|
|
+
|
|
|
+ if(tokenMap==null){
|
|
|
+ URL realUrl = new URL(tokenUrl);
|
|
|
+
|
|
|
+ HttpsURLConnection conn = (HttpsURLConnection) realUrl.openConnection();
|
|
|
+
|
|
|
+ conn.connect();
|
|
|
+
|
|
|
+ InputStream input = conn.getInputStream();
|
|
|
+
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+
|
|
|
+ tokenMap = objectMapper.readValue(input, Map.class);
|
|
|
+
|
|
|
+ valueOperations.set(key,tokenMap,5,TimeUnit.MINUTES);
|
|
|
+ }
|
|
|
+
|
|
|
+ return tokenMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping(value="prepareWXPay")
|
|
|
+ public MessageResult<Map> prepareWXPay(String roomId, String buyType, Integer num, BigDecimal amount){
|
|
|
+ MessageResult<Map> msgResult = new MessageResult<>();
|
|
|
+
|
|
|
+ try {
|
|
|
+ Map<String,Object> dataMap = new HashMap<>();
|
|
|
+
|
|
|
+ dataMap.put("appId",wxPayProperties.getAppId());
|
|
|
+ RechargeRecord record = createRechargeRecord(roomId,buyType,num,amount);
|
|
|
+
|
|
|
+ if(record!=null){
|
|
|
+ dataMap.put("recordId",record.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ msgResult.setResult(true);
|
|
|
+
|
|
|
+ msgResult.setData(dataMap);
|
|
|
+ }
|
|
|
+ catch(Exception ex){
|
|
|
+ log.error(ex.getMessage(),ex);
|
|
|
+
|
|
|
+ msgResult.setMessage(ex.getMessage());
|
|
|
+ msgResult.setResult(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ return msgResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping(value="getWXConfigParam")
|
|
|
+ public MessageResult<Map> getWXConfigParam(String code){
|
|
|
+ MessageResult<Map> msgResult = new MessageResult<>();
|
|
|
+
|
|
|
+ try {
|
|
|
+ Map<String,Object> dataMap = new HashMap<>();
|
|
|
+
|
|
|
+ dataMap.put("appId",wxPayProperties.getAppId());
|
|
|
+ dataMap.put("ticket", getJSApiTicket());
|
|
|
+
|
|
|
+ Map tokenMap = getAuthAccessToken(code);
|
|
|
+
|
|
|
+ String accessToken = (String)tokenMap.get("access_token");
|
|
|
+ String openId = (String)tokenMap.get("openid");
|
|
|
+
|
|
|
+ dataMap.put("accessToken",accessToken);
|
|
|
+ dataMap.put("openId",openId);
|
|
|
+
|
|
|
+ msgResult.setResult(true);
|
|
|
+
|
|
|
+ msgResult.setData(dataMap);
|
|
|
+ }
|
|
|
+ catch(Exception ex){
|
|
|
+ log.error(ex.getMessage(),ex);
|
|
|
+
|
|
|
+ msgResult.setMessage(ex.getMessage());
|
|
|
+ msgResult.setResult(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ return msgResult;
|
|
|
+ }
|
|
|
}
|