order.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // order.js
  2. const mockData = require('../../utils/mockData.js')
  3. Page({
  4. data: {
  5. currentTab: 0,
  6. hospitalKeyword: '',
  7. medicineKeyword: '',
  8. hospitals: mockData.hospitals,
  9. filteredHospitals: mockData.hospitals,
  10. priceResults: [],
  11. hotMedicines: [
  12. { name: '阿莫西林胶囊', minPrice: 24.8 },
  13. { name: '布洛芬缓释胶囊', minPrice: 17.5 },
  14. { name: '氯雷他定片', minPrice: 30.5 },
  15. { name: '奥美拉唑肠溶胶囊', minPrice: 45.0 }
  16. ],
  17. showCaseModal: false,
  18. showMedicineModal: false,
  19. selectedHospital: null,
  20. selectedCase: null,
  21. selectedMedicines: [],
  22. totalPrice: 0
  23. },
  24. onLoad() {
  25. console.log('下单页面加载完成')
  26. },
  27. // 切换选项卡
  28. switchTab(e) {
  29. const tab = parseInt(e.currentTarget.dataset.tab)
  30. this.setData({
  31. currentTab: tab
  32. })
  33. },
  34. // 医院搜索
  35. onHospitalSearch(e) {
  36. const keyword = e.detail.value
  37. const filteredHospitals = this.data.hospitals.filter(hospital =>
  38. hospital.name.includes(keyword) || hospital.address.includes(keyword)
  39. )
  40. this.setData({
  41. hospitalKeyword: keyword,
  42. filteredHospitals: filteredHospitals
  43. })
  44. },
  45. // 药品搜索
  46. onMedicineSearch(e) {
  47. const keyword = e.detail.value
  48. this.setData({
  49. medicineKeyword: keyword
  50. })
  51. if (keyword && mockData.medicinePrices[keyword]) {
  52. this.setData({
  53. priceResults: mockData.medicinePrices[keyword]
  54. })
  55. } else {
  56. this.setData({
  57. priceResults: []
  58. })
  59. }
  60. },
  61. // 选择医院
  62. selectHospital(e) {
  63. const hospital = e.currentTarget.dataset.hospital
  64. this.setData({
  65. selectedHospital: hospital,
  66. showCaseModal: true
  67. })
  68. },
  69. // 关闭病例单模态框
  70. closeCaseModal() {
  71. this.setData({
  72. showCaseModal: false,
  73. selectedHospital: null
  74. })
  75. },
  76. // 选择病例单
  77. selectCase(e) {
  78. const selectedCase = e.currentTarget.dataset.case
  79. // 初始化药品数量
  80. const medicines = selectedCase.medicines.map(medicine => ({
  81. ...medicine,
  82. quantity: 0
  83. }))
  84. this.setData({
  85. selectedCase: { ...selectedCase, medicines },
  86. showCaseModal: false,
  87. showMedicineModal: true
  88. })
  89. },
  90. // 关闭药品模态框
  91. closeMedicineModal() {
  92. this.setData({
  93. showMedicineModal: false,
  94. selectedCase: null,
  95. selectedMedicines: [],
  96. totalPrice: 0
  97. })
  98. },
  99. // 增加药品数量
  100. increaseQuantity(e) {
  101. const medicine = e.currentTarget.dataset.medicine
  102. const medicines = this.data.selectedCase.medicines.map(item => {
  103. if (item.id === medicine.id) {
  104. const quantity = (item.quantity || 0) + 1
  105. return { ...item, quantity }
  106. }
  107. return item
  108. })
  109. const totalPrice = medicines.reduce((total, item) => total + (item.price * (item.quantity || 0)), 0)
  110. this.setData({
  111. 'selectedCase.medicines': medicines,
  112. totalPrice: totalPrice
  113. })
  114. },
  115. // 减少药品数量
  116. decreaseQuantity(e) {
  117. const medicine = e.currentTarget.dataset.medicine
  118. const medicines = this.data.selectedCase.medicines.map(item => {
  119. if (item.id === medicine.id && (item.quantity || 0) > 0) {
  120. const quantity = item.quantity - 1
  121. return { ...item, quantity }
  122. }
  123. return item
  124. })
  125. const totalPrice = medicines.reduce((total, item) => total + (item.price * (item.quantity || 0)), 0)
  126. this.setData({
  127. 'selectedCase.medicines': medicines,
  128. totalPrice: totalPrice
  129. })
  130. },
  131. // 确认下单
  132. confirmOrder() {
  133. const selectedMedicines = this.data.selectedCase.medicines
  134. .filter(item => item.quantity > 0)
  135. .map(item => ({
  136. name: item.name,
  137. price: item.price,
  138. quantity: item.quantity
  139. }))
  140. if (selectedMedicines.length === 0) {
  141. wx.showToast({
  142. title: '请选择至少一种药品',
  143. icon: 'none'
  144. })
  145. return
  146. }
  147. // 模拟下单成功
  148. wx.showModal({
  149. title: '下单成功',
  150. content: `订单总价:¥${this.data.totalPrice}\n药品将尽快通过无人机配送`,
  151. showCancel: false,
  152. success: () => {
  153. this.closeMedicineModal()
  154. // 可以跳转到订单详情页面
  155. }
  156. })
  157. },
  158. // 选择热门药品
  159. selectHotMedicine(e) {
  160. const medicineName = e.currentTarget.dataset.medicine
  161. this.setData({
  162. medicineKeyword: medicineName
  163. })
  164. if (mockData.medicinePrices[medicineName]) {
  165. this.setData({
  166. priceResults: mockData.medicinePrices[medicineName]
  167. })
  168. }
  169. },
  170. // 通过价格选择医院
  171. selectByPrice(e) {
  172. const priceInfo = e.currentTarget.dataset.price
  173. wx.showModal({
  174. title: '确认下单',
  175. content: `确认在${priceInfo.hospital}购买${this.data.medicineKeyword},价格:¥${priceInfo.price}`,
  176. success: (res) => {
  177. if (res.confirm) {
  178. wx.showToast({
  179. title: '下单成功',
  180. icon: 'success'
  181. })
  182. }
  183. }
  184. })
  185. }
  186. })