JAVA打车小程序实现原理

打车小程序的核心功能包括用户端、司机端和后台管理系统。用户端实现叫车、订单管理、支付等功能;司机端实现接单、导航、收益管理等功能;后台管理系统负责订单监控、用户管理、数据统计等。

用户端功能模块包括地图定位、路线规划、订单创建、支付结算。司机端功能模块包括订单接收、导航、状态更新。后台管理系统模块包括订单管理、用户管理、数据分析。

技术架构通常采用微服务设计,前端使用Uniapp跨平台框架,后端使用Spring Boot提供RESTful API接口。数据库使用MySQL存储业务数据,Redis缓存高频访问数据。地图服务集成高德或百度地图API实现定位和导航功能。

Uniapp代码片段实现

地图定位功能
// 引入地图组件
import amap from '@/common/amap-wx.js'

export default {
  data() {
    return {
      latitude: 0,
      longitude: 0,
      markers: []
    }
  },
  onLoad() {
    this.initMap()
  },
  methods: {
    initMap() {
      const myAmap = new amap.AMapWX({
        key: '您的高德地图key'
      })
      myAmap.getRegeo({
        success: (data) => {
          this.latitude = data[0].latitude
          this.longitude = data[0].longitude
          this.markers = [{
            id: 0,
            latitude: this.latitude,
            longitude: this.longitude,
            iconPath: '/static/location.png',
            width: 30,
            height: 30
          }]
        }
      })
    }
  }
}

订单创建功能
createOrder() {
  const that = this
  uni.request({
    url: 'https://api.example.com/orders',
    method: 'POST',
    data: {
      start_latitude: this.startPoint.latitude,
      start_longitude: this.startPoint.longitude,
      end_latitude: this.endPoint.latitude,
      end_longitude: this.endPoint.longitude,
      user_id: uni.getStorageSync('user_id')
    },
    success(res) {
      if (res.data.code === 200) {
        uni.showToast({
          title: '订单创建成功',
          icon: 'success'
        })
        that.orderInfo = res.data.data
      }
    }
  })
}

司机接单功能
acceptOrder(orderId) {
  uni.request({
    url: `https://api.example.com/orders/${orderId}/accept`,
    method: 'POST',
    data: {
      driver_id: uni.getStorageSync('driver_id')
    },
    success(res) {
      if (res.data.code === 200) {
        uni.showToast({
          title: '接单成功',
          icon: 'success'
        })
        uni.navigateTo({
          url: '/pages/driver/navigation?order_id=' + orderId
        })
      }
    }
  })
}

后端JAVA实现示例

订单服务Controller
@RestController
@RequestMapping("/api/orders")
public class OrderController {
    
    @Autowired
    private OrderService orderService;
    
    @PostMapping
    public ResponseEntity<?> createOrder(@RequestBody OrderDTO orderDTO) {
        Order order = orderService.createOrder(orderDTO);
        return ResponseEntity.ok(ResponseResult.success(order));
    }
    
    @PutMapping("/{id}/accept")
    public ResponseEntity<?> acceptOrder(@PathVariable Long id, 
                                       @RequestBody DriverAcceptDTO acceptDTO) {
        Order order = orderService.acceptOrder(id, acceptDTO.getDriverId());
        return ResponseEntity.ok(ResponseResult.success(order));
    }
}

订单服务Service实现
@Service
public class OrderServiceImpl implements OrderService {
    
    @Autowired
    private OrderRepository orderRepository;
    
    @Override
    public Order createOrder(OrderDTO orderDTO) {
        Order order = new Order();
        // 设置订单属性
        order.setStartLatitude(orderDTO.getStartLatitude());
        order.setStartLongitude(orderDTO.getStartLongitude());
        order.setEndLatitude(orderDTO.getEndLatitude());
        order.setEndLongitude(orderDTO.getEndLongitude());
        order.setUserId(orderDTO.getUserId());
        order.setStatus(OrderStatus.CREATED);
        
        return orderRepository.save(order);
    }
    
    @Override
    public Order acceptOrder(Long orderId, Long driverId) {
        Order order = orderRepository.findById(orderId)
            .orElseThrow(() -> new ResourceNotFoundException("Order not found"));
        
        order.setDriverId(driverId);
        order.setStatus(OrderStatus.ACCEPTED);
        order.setAcceptTime(LocalDateTime.now());
        
        return orderRepository.save(order);
    }
}

数据库设计

订单表结构
CREATE TABLE `orders` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) NOT NULL,
  `driver_id` bigint(20) DEFAULT NULL,
  `start_latitude` decimal(10,7) NOT NULL,
  `start_longitude` decimal(10,7) NOT NULL,
  `end_latitude` decimal(10,7) NOT NULL,
  `end_longitude` decimal(10,7) NOT NULL,
  `status` varchar(20) NOT NULL,
  `create_time` datetime NOT NULL,
  `accept_time` datetime DEFAULT NULL,
  `start_time` datetime DEFAULT NULL,
  `end_time` datetime DEFAULT NULL,
  `price` decimal(10,2) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

支付功能集成

微信支付实现
requestPayment(orderInfo) {
  uni.requestPayment({
    provider: 'wxpay',
    orderInfo: JSON.stringify({
      appId: orderInfo.appId,
      timeStamp: orderInfo.timeStamp,
      nonceStr: orderInfo.nonceStr,
      package: orderInfo.packageValue,
      signType: orderInfo.signType,
      paySign: orderInfo.paySign
    }),
    success(res) {
      uni.showToast({
        title: '支付成功',
        icon: 'success'
      })
    },
    fail(err) {
      uni.showToast({
        title: '支付失败',
        icon: 'none'
      })
    }
  })
}

支付宝支付实现
requestAlipay(orderInfo) {
  uni.requestPayment({
    provider: 'alipay',
    orderInfo: orderInfo.tradeNo,
    success(res) {
      uni.showToast({
        title: '支付成功',
        icon: 'success'
      })
    },
    fail(err) {
      uni.showToast({
        title: '支付失败',
        icon: 'none'
      })
    }
  })
}

推荐开源项目

打车系统是一个基于Uniapp和Spring Boot的开源项目,包含完整的前后端代码。项目特点包括:

  • 前后端分离架构
  • 多端兼容(微信小程序、H5、App)
  • 完整的订单生命周期管理
  • 集成高德地图API
  • 支持微信支付和支付宝支付
  • 后台管理系统

项目地址通常托管在GitHub或Gitee平台,包含详细部署文档和API接口说明。开发者可以基于此项目进行二次开发,快速构建自己的打车应用系统。

更多推荐