order.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. const order = {
  149. id: Date.now(),
  150. hospital: this.data.selectedHospital.name,
  151. medicines: selectedMedicines.map(m => m.name),
  152. totalPrice: this.data.totalPrice,
  153. status: '配送中',
  154. date: new Date().toISOString().split('T')[0],
  155. droneTracking: {
  156. status: '配送中',
  157. steps: ['订单确认', '药品准备', '无人机起飞', '配送中', '待送达']
  158. }
  159. }
  160. // 保存订单到全局变量和本地存储
  161. const app = getApp()
  162. app.addOrder(order)
  163. // 模拟下单成功
  164. wx.showModal({
  165. title: '下单成功',
  166. content: `订单总价:¥${this.data.totalPrice}\n药品将尽快通过无人机配送`,
  167. showCancel: false,
  168. success: () => {
  169. this.closeMedicineModal()
  170. // 跳转到我的页面查看订单
  171. setTimeout(() => {
  172. wx.switchTab({
  173. url: '/pages/profile/profile'
  174. })
  175. }, 500)
  176. }
  177. })
  178. },
  179. // 选择热门药品
  180. selectHotMedicine(e) {
  181. const medicineName = e.currentTarget.dataset.medicine
  182. this.setData({
  183. medicineKeyword: medicineName
  184. })
  185. if (mockData.medicinePrices[medicineName]) {
  186. this.setData({
  187. priceResults: mockData.medicinePrices[medicineName]
  188. })
  189. }
  190. },
  191. // 通过价格选择医院
  192. selectByPrice(e) {
  193. const priceInfo = e.currentTarget.dataset.price
  194. const medicineName = this.data.medicineKeyword
  195. wx.showModal({
  196. title: '确认下单',
  197. content: `确认在${priceInfo.hospital}购买${medicineName},价格:¥${priceInfo.price}`,
  198. success: (res) => {
  199. if (res.confirm) {
  200. // 创建订单数据
  201. const order = {
  202. id: Date.now(),
  203. hospital: priceInfo.hospital,
  204. medicines: [medicineName],
  205. totalPrice: priceInfo.price,
  206. status: '配送中',
  207. date: new Date().toISOString().split('T')[0],
  208. droneTracking: {
  209. status: '配送中',
  210. steps: ['订单确认', '药品准备', '无人机起飞', '配送中', '待送达']
  211. }
  212. }
  213. // 保存订单到全局变量和本地存储
  214. const app = getApp()
  215. app.addOrder(order)
  216. wx.showToast({
  217. title: '下单成功',
  218. icon: 'success',
  219. success: () => {
  220. // 跳转到我的页面查看订单
  221. setTimeout(() => {
  222. wx.switchTab({
  223. url: '/pages/profile/profile'
  224. })
  225. }, 500)
  226. }
  227. })
  228. }
  229. }
  230. })
  231. }
  232. })