| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- // 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,
- selectedHospital: null,
- selectedCase: null,
- selectedMedicines: [],
- totalPrice: 0
- },
- 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
- })
- },
- // 增加药品数量
- 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
- })
- },
- // 确认下单
- 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
- }
- // 创建订单数据
- 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],
- droneTracking: {
- status: '配送中',
- steps: ['订单确认', '药品准备', '无人机起飞', '配送中', '待送达']
- }
- }
- // 保存订单到全局变量和本地存储
- const app = getApp()
- app.addOrder(order)
- // 模拟下单成功
- wx.showModal({
- title: '下单成功',
- content: `订单总价:¥${this.data.totalPrice}\n药品将尽快通过无人机配送`,
- showCancel: false,
- success: () => {
- this.closeMedicineModal()
- // 跳转到我的页面查看订单
- setTimeout(() => {
- wx.switchTab({
- url: '/pages/profile/profile'
- })
- }, 500)
- }
- })
- },
- // 选择热门药品
- 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
-
- wx.showModal({
- title: '确认下单',
- content: `确认在${priceInfo.hospital}购买${medicineName},价格:¥${priceInfo.price}`,
- success: (res) => {
- if (res.confirm) {
- // 创建订单数据
- const order = {
- id: Date.now(),
- hospital: priceInfo.hospital,
- medicines: [medicineName],
- totalPrice: priceInfo.price,
- status: '配送中',
- date: new Date().toISOString().split('T')[0],
- droneTracking: {
- status: '配送中',
- steps: ['订单确认', '药品准备', '无人机起飞', '配送中', '待送达']
- }
- }
- // 保存订单到全局变量和本地存储
- const app = getApp()
- app.addOrder(order)
- wx.showToast({
- title: '下单成功',
- icon: 'success',
- success: () => {
- // 跳转到我的页面查看订单
- setTimeout(() => {
- wx.switchTab({
- url: '/pages/profile/profile'
- })
- }, 500)
- }
- })
- }
- }
- })
- }
- })
|