| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- // order.js
- const mockData = require('../../utils/mockData.js')
- Page({
- data: {
- currentTab: 0,
- hospitalKeyword: '',
- medicineKeyword: '',
- hospitals: mockData.hospitals,
- filteredHospitals: mockData.hospitals,
- priceResults: [],
- selectedMedicine: null,
- hotMedicines: [
- { name: '阿莫西林胶囊', minPrice: 24.8 },
- { name: '布洛芬缓释胶囊', minPrice: 17.5 },
- { name: '氯雷他定片', minPrice: 30.5 },
- { name: '奥美拉唑肠溶胶囊', minPrice: 45.0 }
- ],
- otherMedicines: [],
- selectedMedicineQuantity: {},
- showCaseModal: false,
- showMedicineModal: false,
- showPriceConfirmModal: false,
- selectedHospital: null,
- selectedCase: null,
- selectedMedicines: [],
- selectedPriceItem: null,
- totalPrice: 0,
- deliveryAddress: ''
- },
- onLoad() {
- console.log('下单页面加载完成')
- this.loadOtherMedicines()
- },
- loadOtherMedicines() {
- const hotMedicineNames = this.data.hotMedicines.map(m => m.name)
- const otherMedicines = []
-
- for (const medicineName in mockData.medicinePrices) {
- if (!hotMedicineNames.includes(medicineName)) {
- const prices = mockData.medicinePrices[medicineName]
- const minPrice = Math.min(...prices.map(p => p.price))
- otherMedicines.push({
- name: medicineName,
- minPrice: minPrice
- })
- }
- }
-
- this.setData({
- otherMedicines: otherMedicines
- })
- },
- // 切换选项卡
- 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.trim()
- this.setData({
- medicineKeyword: keyword,
- selectedMedicine: null,
- priceResults: []
- })
- if (!keyword) {
- // 清空搜索时,重新加载所有药品信息
- this.loadOtherMedicines()
- return
- }
- // 模糊搜索:查找包含关键词的药品
- const matchedMedicines = []
- for (const medicineName in mockData.medicinePrices) {
- if (medicineName.includes(keyword)) {
- const prices = mockData.medicinePrices[medicineName]
- const minPrice = Math.min(...prices.map(p => p.price))
- matchedMedicines.push({
- name: medicineName,
- minPrice: minPrice
- })
- }
- }
- // 将搜索结果显示在药品信息模块中
- this.setData({
- otherMedicines: matchedMedicines
- })
- },
- // 选择医院
- 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)
- }
- })
- },
- // 选择药品
- selectMedicine(e) {
- const medicineName = e.currentTarget.dataset.medicine
- this.setData({
- selectedMedicine: medicineName
- })
-
- if (mockData.medicinePrices[medicineName]) {
- const priceResults = mockData.medicinePrices[medicineName].map(item => {
- const hospital = this.data.hospitals.find(h => h.name === item.hospital)
- return {
- ...item,
- distance: hospital ? hospital.distance : '未知'
- }
- })
- this.setData({
- priceResults: priceResults
- })
- }
- },
- // 获取医院位置
- getHospitalLocation(hospitalName) {
- const locations = {
- '北京协和医院': { latitude: 39.9139, longitude: 116.3917 },
- '上海瑞金医院': { latitude: 31.2304, longitude: 121.4737 },
- '广州中山医院': { latitude: 23.1291, longitude: 113.2644 },
- '北京大学第一医院': { latitude: 39.9289, longitude: 116.3883 },
- '上海华山医院': { latitude: 31.2165, longitude: 121.4449 },
- '广东省人民医院': { latitude: 23.1256, longitude: 113.2732 },
- '北京301医院': { latitude: 39.9135, longitude: 116.2766 },
- '上海仁济医院': { latitude: 31.2269, longitude: 121.4997 },
- '广州南方医院': { latitude: 23.1825, longitude: 113.2767 },
- '北京中日友好医院': { latitude: 39.9792, longitude: 116.4472 },
- '上海长征医院': { latitude: 31.2317, longitude: 121.4708 },
- '广东省中医院': { latitude: 23.1215, longitude: 113.2665 },
- '北京天坛医院': { latitude: 39.8726, longitude: 116.4168 },
- '上海第九人民医院': { latitude: 31.2304, longitude: 121.4851 },
- '广州华侨医院': { latitude: 23.1358, longitude: 113.3245 },
- '北京安贞医院': { latitude: 39.9432, longitude: 116.4134 },
- '上海新华医院': { latitude: 31.2576, longitude: 121.5119 },
- '广州医科大学附属第一医院': { latitude: 23.1258, longitude: 113.2587 },
- '北京朝阳医院': { latitude: 39.9299, longitude: 116.4443 },
- '上海第六人民医院': { latitude: 31.1884, longitude: 121.4367 }
- }
- 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: '无人机位置'
- }
- ]
- }
- },
- // 返回药品列表
- backToMedicines() {
- this.setData({
- selectedMedicine: null,
- priceResults: []
- })
- },
- // 通过价格选择医院
- selectByPrice(e) {
- const priceInfo = e.currentTarget.dataset.price
- const medicineName = this.data.selectedMedicine
-
- // 显示确认模态框
- this.setData({
- showPriceConfirmModal: true,
- selectedPriceItem: {
- medicine: medicineName,
- hospital: priceInfo.hospital,
- price: priceInfo.price,
- distance: priceInfo.distance
- },
- deliveryAddress: ''
- })
- },
- // 增加比价药品数量
- increasePriceQuantity(e) {
- const hospital = e.currentTarget.dataset.hospital
- const selectedMedicineQuantity = { ...this.data.selectedMedicineQuantity }
- selectedMedicineQuantity[hospital] = (selectedMedicineQuantity[hospital] || 0) + 1
-
- this.setData({
- selectedMedicineQuantity
- })
- },
- // 减少比价药品数量
- decreasePriceQuantity(e) {
- const hospital = e.currentTarget.dataset.hospital
- const selectedMedicineQuantity = { ...this.data.selectedMedicineQuantity }
- if ((selectedMedicineQuantity[hospital] || 0) > 0) {
- selectedMedicineQuantity[hospital] = selectedMedicineQuantity[hospital] - 1
-
- this.setData({
- selectedMedicineQuantity
- })
- }
- },
- // 关闭比价确认模态框
- 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 quantity = this.data.selectedMedicineQuantity[selectedPriceItem.hospital] || 1
-
- // 根据医院生成地图轨迹数据
- 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 * quantity,
- quantity: quantity,
- 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: '',
- selectedMedicineQuantity: {}
- })
- wx.showModal({
- title: '下单成功',
- content: `订单总价:¥${selectedPriceItem.price * quantity}\n药品数量:${quantity}\n送货地址:${savedAddress}\n药品将尽快通过无人机配送`,
- showCancel: false,
- success: () => {
- // 跳转到我的页面查看订单
- setTimeout(() => {
- wx.switchTab({
- url: '/pages/profile/profile'
- })
- }, 500)
- }
- })
- }
- })
|