JAVA扫码点餐(2)-项目过程
项目框架
maven项目:分为三个项目:
online-foods-services:公共框架,封装13张表的增删改查且一些业务封装方法。
online-foods-oms:后台管理操作。
online-foods-controller:前端页面展示,可以在支付宝扫一扫中打开的H5页面。
代码片段
public class AlipayClientHelper {
private static Logger LOGGER = LoggerFactory.getLogger(AlipayClientHelper.class);
private static String url = null;
private static String appId = null;
private static String appPrivateKey = null;
private static String format = null;
private static String charset = null;
private static String alipayPublicKey = null;
private static String signType = null;
static {
url = Properties.getString(PropertiesEnum.ALIPAY_URL.getIndex());
appId = Properties.getString(PropertiesEnum.ALIPAY_APP_ID.getIndex());
appPrivateKey = Properties.getString(PropertiesEnum.ALIPAY_APP_PRIVATE_KEY.getIndex());
format = Properties.getString(PropertiesEnum.ALIPAY_FORMAT.getIndex());
charset = Properties.getString(PropertiesEnum.ALIPAY_CHARSET.getIndex());
alipayPublicKey = Properties.getString(PropertiesEnum.ALIPAY_ALIPAY_PUBLIC_KEY.getIndex());
signType = Properties.getString(PropertiesEnum.ALIPAY_SIGN_TYPE.getIndex());
}
public static AlipayClient getAlipayClient() {
LOGGER.info("url = {}, appId = {}, appPrivateKey={}, format = {}, charset={}, alipayPublicKey = {}, signType = {}", url, appId, "", format, charset, "", signType);
// AlipayClient alipayClient = new DefaultAlipayClient(url, appId, appPrivateKey, "json", "UTF-8", alipayPublicKey, "RSA2");
AlipayClient alipayClient = new DefaultAlipayClient(url, appId, appPrivateKey, format, charset, alipayPublicKey, signType);
return alipayClient;
}
}
订单倒计时实现:
public class CountDownThread extends Thread {
private static Logger LOGGER = LoggerFactory.getLogger(CountDownThread.class);
public volatile boolean exit = false;
private String orderNo;
private int countdown;
public CountDownThread() {
}
public CountDownThread(String orderNo) {
this.orderNo = orderNo;
// 默认15分钟倒计时
this.countdown = 900;
}
public CountDownThread(String orderNo, int countdown) {
this.orderNo = orderNo;
this.countdown = countdown;
}
public String getOrderNo() {
return orderNo;
}
public int getCountdown() {
return countdown;
}
@Override
public void run() {
while (countdown > 0) {
if (exit) {
break;
}
countdown--;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
LOGGER.error("订单倒计时线程发生错误:", e);
}
}
OnlineFoodOrderService onlineFoodOrderService = (OnlineFoodOrderService) SpringHelper.getBean(OnlineFoodOrderService.class);
OnlineFoodOrder onlineFoodOrder1 = new OnlineFoodOrder();
onlineFoodOrder1.setOrderNo(orderNo);
onlineFoodOrder1.setOrderState(OrderStateEnum.OrderState_1.getIndex());
OnlineFoodOrder onlineFoodOrder2 = new OnlineFoodOrder();
onlineFoodOrder2.setOrderState(OrderStateEnum.OrderState_2.getIndex());
int count = onlineFoodOrderService.updateOnlineFoodOrderByCriteriaSelective(onlineFoodOrder1, onlineFoodOrder2);
LOGGER.warn("订单倒计时取消,订单号:{} ,取消到:{} 条", orderNo, count);
}
}
订单倒计时Cache:
public class AppcationCache {
private static AppcationCache appcationCache;
private Map<String, CountDownThread> orderCountDownMap = new ConcurrentHashMap<String, CountDownThread>();
private Map<String, String> phoneMap = new ConcurrentHashMap<String, String>();
public static AppcationCache getInstance() {
if (appcationCache == null) {
synchronized (AppcationCache.class) {
if (appcationCache == null) {
appcationCache = new AppcationCache();
}
}
}
return appcationCache;
}
public Map<String, CountDownThread> getOrderCountDownMap() {
return orderCountDownMap;
}
public Map<String, String> getPhoneMap() {
return phoneMap;
}
}
为啥要贴着3段代码?
- 很多人在调用 支付宝支付的时候,总会有一些奇奇怪怪的问题,所以我这里封装起来了。注意:支付宝公钥,不是应用的公钥,一定是要支付宝的公钥。要不然调用就会出现sign验证不通过。
- 订单倒计时,我百度了一圈,没看到实现得较好的代码,所以我这里用一种最简单的方案,自己缓存倒计时。可能有人会问,为啥不用redis,我阿里云ECS 1核2G, 不想引用一些第三方的组件。
有意做类似软件的可以私信我,买源码也可以私信我。
转载请注明出处:【https://blog.****.net/hncdyj/article/details/83650155】