| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- // order.js
- const mockData = require('../../utils/mockData.js')
- Page({
- data: {
- currentTab: 0,
- hospitalKeyword: '',
- medicineKeyword: '',
- hospitals: mockData.hospitals,
- filteredHospitals: mockData.hospitals,
- priceResults: [],
- hotMedicines: [
- { name: '阿莫西林胶囊', minPrice: 24.8 },
- { name: '布洛芬缓释胶囊', minPrice: 17.5 },
- { name: '氯雷他定片', minPrice: 30.5 },
- { name: '奥美拉唑肠溶胶囊', minPrice: 45.0 }
- ],
- showCaseModal: false,
- showMedicineModal: false,
- showPriceConfirmModal: false,
- selectedHospital: null,
- selectedCase: null,
- selectedMedicines: [],
- selectedPriceItem: null,
- totalPrice: 0,
- deliveryAddress: ''
- },
- onLoad() {
- console.log('下单页面加载完成')
- },
- // 切换选项卡
- switchTab(e) {
- const tab = parseInt(e.currentTarget.dataset.tab)
- this.setData({
- currentTab: tab
- })
- },
- // 医院搜索
- onHospitalSearch(e) {
- const keyword = e.detail.value
- const filteredHospitals = this.data.hospitals.filter(hospital =>
- hospital.name.includes(keyword) || hospital.address.includes(keyword)
- )
- this.setData({
- hospitalKeyword: keyword,
- filteredHospitals: filteredHospitals
- })
- },
- // 药品搜索
- onMedicineSearch(e) {
- const keyword = e.detail.value
- this.setData({
- medicineKeyword: keyword
- })
- if (keyword && mockData.medicinePrices[keyword]) {
- this.setData({
- priceResults: mockData.medicinePrices[keyword]
- })
- } else {
- this.setData({
- priceResults: []
- })
- }
- },
- // 选择医院
- selectHospital(e) {
- const hospital = e.currentTarget.dataset.hospital
- this.setData({
- selectedHospital: hospital,
- showCaseModal: true
- })
- },
- // 关闭病例单模态框
- closeCaseModal() {
- this.setData({
- showCaseModal: false,
- selectedHospital: null
- })
- },
- // 选择病例单
- selectCase(e) {
- const selectedCase = e.currentTarget.dataset.case
- // 初始化药品数量
- const medicines = selectedCase.medicines.map(medicine => ({
- ...medicine,
- quantity: 0
- }))
-
- this.setData({
- selectedCase: { ...selectedCase, medicines },
- showCaseModal: false,
- showMedicineModal: true
- })
- },
- // 关闭药品模态框
- closeMedicineModal() {
- this.setData({
- showMedicineModal: false,
- selectedCase: null,
- selectedMedicines: [],
- totalPrice: 0,
- deliveryAddress: ''
- })
- },
- // 增加药品数量
- increaseQuantity(e) {
- const medicine = e.currentTarget.dataset.medicine
- const medicines = this.data.selectedCase.medicines.map(item => {
- if (item.id === medicine.id) {
- const quantity = (item.quantity || 0) + 1
- return { ...item, quantity }
- }
- return item
- })
- const totalPrice = medicines.reduce((total, item) => total + (item.price * (item.quantity || 0)), 0)
-
- this.setData({
- 'selectedCase.medicines': medicines,
- totalPrice: totalPrice
- })
- },
- // 减少药品数量
- decreaseQuantity(e) {
- const medicine = e.currentTarget.dataset.medicine
- const medicines = this.data.selectedCase.medicines.map(item => {
- if (item.id === medicine.id && (item.quantity || 0) > 0) {
- const quantity = item.quantity - 1
- return { ...item, quantity }
- }
- return item
- })
- const totalPrice = medicines.reduce((total, item) => total + (item.price * (item.quantity || 0)), 0)
-
- this.setData({
- 'selectedCase.medicines': medicines,
- totalPrice: totalPrice
- })
- },
- // 地址输入处理
- onAddressInput(e) {
- this.setData({
- deliveryAddress: e.detail.value
- })
- },
- // 使用默认地址
- useDefaultAddress() {
- const app = getApp()
- const userInfo = app.globalData.userInfo || mockData.userInfo
-
- if (userInfo && userInfo.defaultAddress) {
- this.setData({
- deliveryAddress: userInfo.defaultAddress
- })
- wx.showToast({
- title: '已使用默认地址',
- icon: 'success'
- })
- } else {
- wx.showToast({
- title: '暂无默认地址',
- icon: 'none'
- })
- }
- },
- // 确认下单
- confirmOrder() {
- const selectedMedicines = this.data.selectedCase.medicines
- .filter(item => item.quantity > 0)
- .map(item => ({
- name: item.name,
- price: item.price,
- quantity: item.quantity
- }))
- if (selectedMedicines.length === 0) {
- wx.showToast({
- title: '请选择至少一种药品',
- icon: 'none'
- })
- return
- }
- // 验证送货地址
- if (!this.data.deliveryAddress || this.data.deliveryAddress.trim() === '') {
- wx.showToast({
- title: '请输入送货地址',
- icon: 'none'
- })
- return
- }
- // 根据医院生成地图轨迹数据
- const hospitalLocation = this.getHospitalLocation(this.data.selectedHospital.name)
- const mapTracking = this.generateMapTracking(hospitalLocation)
- // 创建订单数据
- const order = {
- id: Date.now(),
- hospital: this.data.selectedHospital.name,
- medicines: selectedMedicines.map(m => m.name),
- totalPrice: this.data.totalPrice,
- status: '配送中',
- date: new Date().toISOString().split('T')[0],
- deliveryAddress: this.data.deliveryAddress,
- droneTracking: {
- status: '配送中',
- steps: ['订单确认', '药品准备', '无人机起飞', '配送中', '待送达']
- },
- mapTracking: mapTracking
- }
- // 保存订单到全局变量和本地存储
- const app = getApp()
- app.addOrder(order)
- // 模拟下单成功
- wx.showModal({
- title: '下单成功',
- content: `订单总价:¥${this.data.totalPrice}\n送货地址:${this.data.deliveryAddress}\n药品将尽快通过无人机配送`,
- showCancel: false,
- success: () => {
- this.closeMedicineModal()
- // 跳转到我的页面查看订单
- setTimeout(() => {
- wx.switchTab({
- url: '/pages/profile/profile'
- })
- }, 500)
- }
- })
- },
- // 获取医院位置
- getHospitalLocation(hospitalName) {
- const locations = {
- '北京协和医院': { latitude: 39.9139, longitude: 116.3917 },
- '上海瑞金医院': { latitude: 31.2304, longitude: 121.4737 },
- '广州中山医院': { latitude: 23.1291, longitude: 113.2644 }
- }
- return locations[hospitalName] || { latitude: 39.9042, longitude: 116.4074 }
- },
- // 生成地图轨迹数据
- generateMapTracking(hospitalLocation) {
- const points = []
- const numPoints = 5
-
- for (let i = 0; i < numPoints; i++) {
- const latOffset = (i * 0.004) / numPoints
- const lngOffset = (i * 0.007) / numPoints
- points.push({
- latitude: hospitalLocation.latitude + latOffset,
- longitude: hospitalLocation.longitude + lngOffset
- })
- }
- return {
- polyline: [
- {
- points: points,
- color: '#007AFF',
- width: 4,
- dottedLine: false
- }
- ],
- markers: [
- {
- id: 1,
- latitude: hospitalLocation.latitude,
- longitude: hospitalLocation.longitude,
- iconPath: '/images/hospital-marker.png',
- width: 30,
- height: 30,
- title: '医院位置'
- },
- {
- id: 2,
- latitude: points[points.length - 1].latitude,
- longitude: points[points.length - 1].longitude,
- iconPath: '/images/drone-marker.png',
- width: 30,
- height: 30,
- title: '无人机位置'
- }
- ]
- }
- },
- // 选择热门药品
- selectHotMedicine(e) {
- const medicineName = e.currentTarget.dataset.medicine
- this.setData({
- medicineKeyword: medicineName
- })
-
- if (mockData.medicinePrices[medicineName]) {
- this.setData({
- priceResults: mockData.medicinePrices[medicineName]
- })
- }
- },
- // 通过价格选择医院
- selectByPrice(e) {
- const priceInfo = e.currentTarget.dataset.price
- const medicineName = this.data.medicineKeyword
-
- // 显示确认模态框
- this.setData({
- showPriceConfirmModal: true,
- selectedPriceItem: {
- medicine: medicineName,
- hospital: priceInfo.hospital,
- price: priceInfo.price
- },
- deliveryAddress: ''
- })
- },
- // 关闭比价确认模态框
- closePriceConfirmModal() {
- this.setData({
- showPriceConfirmModal: false,
- selectedPriceItem: null,
- deliveryAddress: ''
- })
- },
- // 确认比价下单
- confirmPriceOrder() {
- // 验证送货地址
- if (!this.data.deliveryAddress || this.data.deliveryAddress.trim() === '') {
- wx.showToast({
- title: '请输入送货地址',
- icon: 'none'
- })
- return
- }
- const selectedPriceItem = this.data.selectedPriceItem
-
- // 根据医院生成地图轨迹数据
- const hospitalLocation = this.getHospitalLocation(selectedPriceItem.hospital)
- const mapTracking = this.generateMapTracking(hospitalLocation)
- // 创建订单数据
- const order = {
- id: Date.now(),
- hospital: selectedPriceItem.hospital,
- medicines: [selectedPriceItem.medicine],
- totalPrice: selectedPriceItem.price,
- status: '配送中',
- date: new Date().toISOString().split('T')[0],
- deliveryAddress: this.data.deliveryAddress,
- droneTracking: {
- status: '配送中',
- steps: ['订单确认', '药品准备', '无人机起飞', '配送中', '待送达']
- },
- mapTracking: mapTracking
- }
- // 保存订单到全局变量和本地存储
- const app = getApp()
- app.addOrder(order)
- // 保存地址用于显示
- const savedAddress = this.data.deliveryAddress
- // 关闭模态框
- this.setData({
- showPriceConfirmModal: false,
- selectedPriceItem: null,
- deliveryAddress: ''
- })
- wx.showModal({
- title: '下单成功',
- content: `订单总价:¥${selectedPriceItem.price}\n送货地址:${savedAddress}\n药品将尽快通过无人机配送`,
- showCancel: false,
- success: () => {
- // 跳转到我的页面查看订单
- setTimeout(() => {
- wx.switchTab({
- url: '/pages/profile/profile'
- })
- }, 500)
- }
- })
- }
- })
|