order.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 hospitalLocation = this.getHospitalLocation(this.data.selectedHospital.name)
  149. const mapTracking = this.generateMapTracking(hospitalLocation)
  150. // 创建订单数据
  151. const order = {
  152. id: Date.now(),
  153. hospital: this.data.selectedHospital.name,
  154. medicines: selectedMedicines.map(m => m.name),
  155. totalPrice: this.data.totalPrice,
  156. status: '配送中',
  157. date: new Date().toISOString().split('T')[0],
  158. droneTracking: {
  159. status: '配送中',
  160. steps: ['订单确认', '药品准备', '无人机起飞', '配送中', '待送达']
  161. },
  162. mapTracking: mapTracking
  163. }
  164. // 保存订单到全局变量和本地存储
  165. const app = getApp()
  166. app.addOrder(order)
  167. // 模拟下单成功
  168. wx.showModal({
  169. title: '下单成功',
  170. content: `订单总价:¥${this.data.totalPrice}\n药品将尽快通过无人机配送`,
  171. showCancel: false,
  172. success: () => {
  173. this.closeMedicineModal()
  174. // 跳转到我的页面查看订单
  175. setTimeout(() => {
  176. wx.switchTab({
  177. url: '/pages/profile/profile'
  178. })
  179. }, 500)
  180. }
  181. })
  182. },
  183. // 获取医院位置
  184. getHospitalLocation(hospitalName) {
  185. const locations = {
  186. '北京协和医院': { latitude: 39.9139, longitude: 116.3917 },
  187. '上海瑞金医院': { latitude: 31.2304, longitude: 121.4737 },
  188. '广州中山医院': { latitude: 23.1291, longitude: 113.2644 }
  189. }
  190. return locations[hospitalName] || { latitude: 39.9042, longitude: 116.4074 }
  191. },
  192. // 生成地图轨迹数据
  193. generateMapTracking(hospitalLocation) {
  194. const points = []
  195. const numPoints = 5
  196. for (let i = 0; i < numPoints; i++) {
  197. const latOffset = (i * 0.004) / numPoints
  198. const lngOffset = (i * 0.007) / numPoints
  199. points.push({
  200. latitude: hospitalLocation.latitude + latOffset,
  201. longitude: hospitalLocation.longitude + lngOffset
  202. })
  203. }
  204. return {
  205. polyline: [
  206. {
  207. points: points,
  208. color: '#007AFF',
  209. width: 4,
  210. dottedLine: false
  211. }
  212. ],
  213. markers: [
  214. {
  215. id: 1,
  216. latitude: hospitalLocation.latitude,
  217. longitude: hospitalLocation.longitude,
  218. iconPath: '/images/hospital-marker.png',
  219. width: 30,
  220. height: 30,
  221. title: '医院位置'
  222. },
  223. {
  224. id: 2,
  225. latitude: points[points.length - 1].latitude,
  226. longitude: points[points.length - 1].longitude,
  227. iconPath: '/images/drone-marker.png',
  228. width: 30,
  229. height: 30,
  230. title: '无人机位置'
  231. }
  232. ]
  233. }
  234. },
  235. // 选择热门药品
  236. selectHotMedicine(e) {
  237. const medicineName = e.currentTarget.dataset.medicine
  238. this.setData({
  239. medicineKeyword: medicineName
  240. })
  241. if (mockData.medicinePrices[medicineName]) {
  242. this.setData({
  243. priceResults: mockData.medicinePrices[medicineName]
  244. })
  245. }
  246. },
  247. // 通过价格选择医院
  248. selectByPrice(e) {
  249. const priceInfo = e.currentTarget.dataset.price
  250. const medicineName = this.data.medicineKeyword
  251. wx.showModal({
  252. title: '确认下单',
  253. content: `确认在${priceInfo.hospital}购买${medicineName},价格:¥${priceInfo.price}`,
  254. success: (res) => {
  255. if (res.confirm) {
  256. // 根据医院生成地图轨迹数据
  257. const hospitalLocation = this.getHospitalLocation(priceInfo.hospital)
  258. const mapTracking = this.generateMapTracking(hospitalLocation)
  259. // 创建订单数据
  260. const order = {
  261. id: Date.now(),
  262. hospital: priceInfo.hospital,
  263. medicines: [medicineName],
  264. totalPrice: priceInfo.price,
  265. status: '配送中',
  266. date: new Date().toISOString().split('T')[0],
  267. droneTracking: {
  268. status: '配送中',
  269. steps: ['订单确认', '药品准备', '无人机起飞', '配送中', '待送达']
  270. },
  271. mapTracking: mapTracking
  272. }
  273. // 保存订单到全局变量和本地存储
  274. const app = getApp()
  275. app.addOrder(order)
  276. wx.showToast({
  277. title: '下单成功',
  278. icon: 'success',
  279. success: () => {
  280. // 跳转到我的页面查看订单
  281. setTimeout(() => {
  282. wx.switchTab({
  283. url: '/pages/profile/profile'
  284. })
  285. }, 500)
  286. }
  287. })
  288. }
  289. }
  290. })
  291. }
  292. })