在黄金回收类小程序中,实时金价和回收订单是系统设计的两个核心模块。黄金价格波动频繁,回收报价需要与权威金价源保持同步;同时,回收流程涉及克重、成色、损耗、扣费等复杂计算,订单状态流转也需要严谨的状态机控制。本文从技术实现角度,讲解黄金回收小程序的关键模块设计与代码实现。
一、业务特点与难点
黄金回收小程序与普通电商小程序最大的区别在于:商品价格不是固定的。黄金回收报价通常基于当日国际金价或国内基准金价,再结合回收成色、克重、损耗率等参数进行计算。因此,系统需要具备以下能力:
- 实时金价同步:定时从权威金价源拉取最新价格,并缓存到本地,避免每次请求都调用外部接口
- 回收价动态计算:根据金价、成色、克重、手续费等参数实时计算回收报价
- 订单状态流转:回收订单通常经历"提交预约 → 上门/到店检测 → 确认克重与报价 → 客户确认 → 打款 → 完成"等多个环节
- 价格快照机制:客户下单时的金价必须写入订单快照,后续金价波动不影响已确认订单
二、核心数据模型
回收订单表需要记录以下关键字段:订单号、用户ID、预约时间、预约方式(上门/到店)、黄金品类(足金/K金/铂金等)、申报克重、检测克重、成色、当日金价快照、回收单价、损耗率、扣费金额、最终结算金额、订单状态、创建时间、完成时间。其中,金价快照和回收单价是避免后续纠纷的核心字段。
三、核心代码实现
实时金价同步与缓存
以下代码实现了从外部金价接口同步价格并缓存到Redis的逻辑,同时提供本地查询接口,避免高频调用外部服务:
public class GoldPriceServiceImpl implements GoldPriceService { private StringRedisTemplate redisTemplate; private GoldPriceMapper priceMapper; private static final String PRICE_CACHE_KEY = "gold:price:latest"; private static final long CACHE_EXPIRE_HOURS = 1; /** * 获取最新金价,优先从缓存读取,缓存失效时同步外部接口 */ public Result<GoldPriceVO> getLatestPrice() { String cached = redisTemplate.opsForValue().get(PRICE_CACHE_KEY); if (cached != null) { return Result.success(JSON.parseObject(cached, GoldPriceVO.class)); } // 缓存未命中,调用外部金价接口 GoldPriceVO price = fetchFromExternalApi(); if (price == null) { return Result.fail("金价数据获取失败,请稍后重试"); } // 写入缓存和数据库 redisTemplate.opsForValue().set(PRICE_CACHE_KEY, JSON.toJSONString(price), CACHE_EXPIRE_HOURS, TimeUnit.HOURS); saveToDatabase(price); return Result.success(price); } /** * 定时任务:每小时同步一次金价 */ (cron = "0 0 * * * ?") public void syncGoldPrice() { GoldPriceVO price = fetchFromExternalApi(); if (price != null) { redisTemplate.opsForValue().set(PRICE_CACHE_KEY, JSON.toJSONString(price), CACHE_EXPIRE_HOURS, TimeUnit.HOURS); saveToDatabase(price); } } private GoldPriceVO fetchFromExternalApi() { // 实际项目中对接权威金价API,如上海黄金交易所、金投网等 return null; } private void saveToDatabase(GoldPriceVO price) { GoldPriceRecord record = new GoldPriceRecord(); record.setPriceDate(LocalDate.now()); record.setAu9999Price(price.getAu9999Price()); record.setAu950Price(price.getAu950Price()); record.setPt950Price(price.getPt950Price()); record.setCreateTime(LocalDateTime.now()); priceMapper.insert(record); } }
回收订单创建与价格计算
以下代码实现了客户提交回收预约时的订单创建逻辑,包含回收价格计算和金价快照保存:
public class RecycleOrderServiceImpl implements RecycleOrderService { private RecycleOrderMapper orderMapper; private GoldPriceService goldPriceService; /** * 创建回收订单 */ public Result<RecycleOrderVO> createOrder(RecycleOrderCreateDTO dto) { // 1. 参数校验 if (dto.getGoldType() == null || dto.getDeclaredWeight() == null || dto.getDeclaredWeight() <= 0) { return Result.fail("黄金品类和申报克重为必填项"); } // 2. 获取当日金价快照 Result<GoldPriceVO> priceResult = goldPriceService.getLatestPrice(); if (!priceResult.isSuccess()) { return Result.fail("金价数据获取失败,请稍后重试"); } GoldPriceVO currentPrice = priceResult.getData(); // 3. 计算回收报价 BigDecimal basePrice = getBasePriceByType(currentPrice, dto.getGoldType()); BigDecimal purityRate = getPurityRate(dto.getPurity()); BigDecimal recycleUnitPrice = basePrice.multiply(purityRate) .setScale(2, RoundingMode.HALF_DOWN); BigDecimal estimatedAmount = recycleUnitPrice .multiply(new BigDecimal(dto.getDeclaredWeight())) .setScale(2, RoundingMode.HALF_DOWN); // 4. 创建订单 RecycleOrder order = new RecycleOrder(); order.setOrderNo(generateOrderNo()); order.setUserId(dto.getUserId()); order.setGoldType(dto.getGoldType()); order.setDeclaredWeight(dto.getDeclaredWeight()); order.setPurity(dto.getPurity()); order.setPriceSnapshot(JSON.toJSONString(currentPrice)); order.setRecycleUnitPrice(recycleUnitPrice); order.setEstimatedAmount(estimatedAmount); order.setAppointmentTime(dto.getAppointmentTime()); order.setAppointmentType(dto.getAppointmentType()); order.setContactName(dto.getContactName()); order.setContactPhone(dto.getContactPhone()); order.setAddress(dto.getAddress()); order.setStatus(0); // 待检测 order.setCreateTime(LocalDateTime.now()); orderMapper.insert(order); RecycleOrderVO vo = new RecycleOrderVO(); vo.setOrderNo(order.getOrderNo()); vo.setRecycleUnitPrice(recycleUnitPrice); vo.setEstimatedAmount(estimatedAmount); vo.setStatusDesc("预约成功,等待检测"); return Result.success(vo); } private BigDecimal getBasePriceByType(GoldPriceVO price, String goldType) { switch (goldType) { case "au9999": return price.getAu9999Price(); case "au950": return price.getAu950Price(); case "pt950": return price.getPt950Price(); default: return price.getAu9999Price(); } } private BigDecimal getPurityRate(String purity) { if (purity == null) return new BigDecimal("0.99"); switch (purity) { case "999": return new BigDecimal("0.999"); case "990": return new BigDecimal("0.990"); case "950": return new BigDecimal("0.950"); case "750": return new BigDecimal("0.750"); default: return new BigDecimal("0.990"); } } private String generateOrderNo() { return "GR" + System.currentTimeMillis() + (int)(Math.random() * 10000); } }
四、设计要点
金价快照不可篡改:订单创建时必须将当时的金价、回收单价写入订单记录,后续金价波动不影响已确认订单。这是避免客户纠纷的关键设计。
检测克重与报价确认分离:客户申报的克重仅用于预估报价,实际结算以检测后的克重为准。检测完成后需要客户再次确认最终报价,确认后才进入打款环节。
并发与幂等:同一客户短时间内重复提交预约时,需要通过用户ID+时间窗口做幂等校验,避免产生重复订单。
上门回收的位置校验:若支持上门回收,建议在客户确认订单时校验其定位是否在平台服务范围内,超出范围的订单自动转为客户到店模式。
五、总结
黄金回收小程序的核心技术点在于实时金价的同步与缓存、回收价格的动态计算以及订单状态机的严谨设计。金价快照机制保证了价格数据的可追溯性,检测与确认分离的流程设计降低了交易纠纷风险。在实际开发中,还需要结合实名认证、人脸核验、打款回调等环节,构建完整的回收业务闭环。