order.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. showPriceConfirmModal: false,
  20. selectedHospital: null,
  21. selectedCase: null,
  22. selectedMedicines: [],
  23. selectedPriceItem: null,
  24. totalPrice: 0,
  25. deliveryAddress: ''
  26. },
  27. onLoad() {
  28. console.log('下单页面加载完成')
  29. },
  30. // 切换选项卡
  31. switchTab(e) {
  32. const tab = parseInt(e.currentTarget.dataset.tab)
  33. this.setData({
  34. currentTab: tab
  35. })
  36. },
  37. // 医院搜索
  38. onHospitalSearch(e) {
  39. const keyword = e.detail.value
  40. const filteredHospitals = this.data.hospitals.filter(hospital =>
  41. hospital.name.includes(keyword) || hospital.address.includes(keyword)
  42. )
  43. this.setData({
  44. hospitalKeyword: keyword,
  45. filteredHospitals: filteredHospitals
  46. })
  47. },
  48. // 药品搜索
  49. onMedicineSearch(e) {
  50. const keyword = e.detail.value
  51. this.setData({
  52. medicineKeyword: keyword
  53. })
  54. if (keyword && mockData.medicinePrices[keyword]) {
  55. this.setData({
  56. priceResults: mockData.medicinePrices[keyword]
  57. })
  58. } else {
  59. this.setData({
  60. priceResults: []
  61. })
  62. }
  63. },
  64. // 选择医院
  65. selectHospital(e) {
  66. const hospital = e.currentTarget.dataset.hospital
  67. this.setData({
  68. selectedHospital: hospital,
  69. showCaseModal: true
  70. })
  71. },
  72. // 关闭病例单模态框
  73. closeCaseModal() {
  74. this.setData({
  75. showCaseModal: false,
  76. selectedHospital: null
  77. })
  78. },
  79. // 选择病例单
  80. selectCase(e) {
  81. const selectedCase = e.currentTarget.dataset.case
  82. // 初始化药品数量
  83. const medicines = selectedCase.medicines.map(medicine => ({
  84. ...medicine,
  85. quantity: 0
  86. }))
  87. this.setData({
  88. selectedCase: { ...selectedCase, medicines },
  89. showCaseModal: false,
  90. showMedicineModal: true
  91. })
  92. },
  93. // 关闭药品模态框
  94. closeMedicineModal() {
  95. this.setData({
  96. showMedicineModal: false,
  97. selectedCase: null,
  98. selectedMedicines: [],
  99. totalPrice: 0,
  100. deliveryAddress: ''
  101. })
  102. },
  103. // 增加药品数量
  104. increaseQuantity(e) {
  105. const medicine = e.currentTarget.dataset.medicine
  106. const medicines = this.data.selectedCase.medicines.map(item => {
  107. if (item.id === medicine.id) {
  108. const quantity = (item.quantity || 0) + 1
  109. return { ...item, quantity }
  110. }
  111. return item
  112. })
  113. const totalPrice = medicines.reduce((total, item) => total + (item.price * (item.quantity || 0)), 0)
  114. this.setData({
  115. 'selectedCase.medicines': medicines,
  116. totalPrice: totalPrice
  117. })
  118. },
  119. // 减少药品数量
  120. decreaseQuantity(e) {
  121. const medicine = e.currentTarget.dataset.medicine
  122. const medicines = this.data.selectedCase.medicines.map(item => {
  123. if (item.id === medicine.id && (item.quantity || 0) > 0) {
  124. const quantity = item.quantity - 1
  125. return { ...item, quantity }
  126. }
  127. return item
  128. })
  129. const totalPrice = medicines.reduce((total, item) => total + (item.price * (item.quantity || 0)), 0)
  130. this.setData({
  131. 'selectedCase.medicines': medicines,
  132. totalPrice: totalPrice
  133. })
  134. },
  135. // 地址输入处理
  136. onAddressInput(e) {
  137. this.setData({
  138. deliveryAddress: e.detail.value
  139. })
  140. },
  141. // 使用默认地址
  142. useDefaultAddress() {
  143. const app = getApp()
  144. const userInfo = app.globalData.userInfo || mockData.userInfo
  145. if (userInfo && userInfo.defaultAddress) {
  146. this.setData({
  147. deliveryAddress: userInfo.defaultAddress
  148. })
  149. wx.showToast({
  150. title: '已使用默认地址',
  151. icon: 'success'
  152. })
  153. } else {
  154. wx.showToast({
  155. title: '暂无默认地址',
  156. icon: 'none'
  157. })
  158. }
  159. },
  160. // 确认下单
  161. confirmOrder() {
  162. const selectedMedicines = this.data.selectedCase.medicines
  163. .filter(item => item.quantity > 0)
  164. .map(item => ({
  165. name: item.name,
  166. price: item.price,
  167. quantity: item.quantity
  168. }))
  169. if (selectedMedicines.length === 0) {
  170. wx.showToast({
  171. title: '请选择至少一种药品',
  172. icon: 'none'
  173. })
  174. return
  175. }
  176. // 验证送货地址
  177. if (!this.data.deliveryAddress || this.data.deliveryAddress.trim() === '') {
  178. wx.showToast({
  179. title: '请输入送货地址',
  180. icon: 'none'
  181. })
  182. return
  183. }
  184. // 根据医院生成地图轨迹数据
  185. const hospitalLocation = this.getHospitalLocation(this.data.selectedHospital.name)
  186. const mapTracking = this.generateMapTracking(hospitalLocation)
  187. // 创建订单数据
  188. const order = {
  189. id: Date.now(),
  190. hospital: this.data.selectedHospital.name,
  191. medicines: selectedMedicines.map(m => m.name),
  192. totalPrice: this.data.totalPrice,
  193. status: '配送中',
  194. date: new Date().toISOString().split('T')[0],
  195. deliveryAddress: this.data.deliveryAddress,
  196. droneTracking: {
  197. status: '配送中',
  198. steps: ['订单确认', '药品准备', '无人机起飞', '配送中', '待送达']
  199. },
  200. mapTracking: mapTracking
  201. }
  202. // 保存订单到全局变量和本地存储
  203. const app = getApp()
  204. app.addOrder(order)
  205. // 模拟下单成功
  206. wx.showModal({
  207. title: '下单成功',
  208. content: `订单总价:¥${this.data.totalPrice}\n送货地址:${this.data.deliveryAddress}\n药品将尽快通过无人机配送`,
  209. showCancel: false,
  210. success: () => {
  211. this.closeMedicineModal()
  212. // 跳转到我的页面查看订单
  213. setTimeout(() => {
  214. wx.switchTab({
  215. url: '/pages/profile/profile'
  216. })
  217. }, 500)
  218. }
  219. })
  220. },
  221. // 获取医院位置
  222. getHospitalLocation(hospitalName) {
  223. const locations = {
  224. '北京协和医院': { latitude: 39.9139, longitude: 116.3917 },
  225. '上海瑞金医院': { latitude: 31.2304, longitude: 121.4737 },
  226. '广州中山医院': { latitude: 23.1291, longitude: 113.2644 }
  227. }
  228. return locations[hospitalName] || { latitude: 39.9042, longitude: 116.4074 }
  229. },
  230. // 生成地图轨迹数据
  231. generateMapTracking(hospitalLocation) {
  232. const points = []
  233. const numPoints = 5
  234. for (let i = 0; i < numPoints; i++) {
  235. const latOffset = (i * 0.004) / numPoints
  236. const lngOffset = (i * 0.007) / numPoints
  237. points.push({
  238. latitude: hospitalLocation.latitude + latOffset,
  239. longitude: hospitalLocation.longitude + lngOffset
  240. })
  241. }
  242. return {
  243. polyline: [
  244. {
  245. points: points,
  246. color: '#007AFF',
  247. width: 4,
  248. dottedLine: false
  249. }
  250. ],
  251. markers: [
  252. {
  253. id: 1,
  254. latitude: hospitalLocation.latitude,
  255. longitude: hospitalLocation.longitude,
  256. iconPath: '/images/hospital-marker.png',
  257. width: 30,
  258. height: 30,
  259. title: '医院位置'
  260. },
  261. {
  262. id: 2,
  263. latitude: points[points.length - 1].latitude,
  264. longitude: points[points.length - 1].longitude,
  265. iconPath: '/images/drone-marker.png',
  266. width: 30,
  267. height: 30,
  268. title: '无人机位置'
  269. }
  270. ]
  271. }
  272. },
  273. // 选择热门药品
  274. selectHotMedicine(e) {
  275. const medicineName = e.currentTarget.dataset.medicine
  276. this.setData({
  277. medicineKeyword: medicineName
  278. })
  279. if (mockData.medicinePrices[medicineName]) {
  280. this.setData({
  281. priceResults: mockData.medicinePrices[medicineName]
  282. })
  283. }
  284. },
  285. // 通过价格选择医院
  286. selectByPrice(e) {
  287. const priceInfo = e.currentTarget.dataset.price
  288. const medicineName = this.data.medicineKeyword
  289. // 显示确认模态框
  290. this.setData({
  291. showPriceConfirmModal: true,
  292. selectedPriceItem: {
  293. medicine: medicineName,
  294. hospital: priceInfo.hospital,
  295. price: priceInfo.price
  296. },
  297. deliveryAddress: ''
  298. })
  299. },
  300. // 关闭比价确认模态框
  301. closePriceConfirmModal() {
  302. this.setData({
  303. showPriceConfirmModal: false,
  304. selectedPriceItem: null,
  305. deliveryAddress: ''
  306. })
  307. },
  308. // 确认比价下单
  309. confirmPriceOrder() {
  310. // 验证送货地址
  311. if (!this.data.deliveryAddress || this.data.deliveryAddress.trim() === '') {
  312. wx.showToast({
  313. title: '请输入送货地址',
  314. icon: 'none'
  315. })
  316. return
  317. }
  318. const selectedPriceItem = this.data.selectedPriceItem
  319. // 根据医院生成地图轨迹数据
  320. const hospitalLocation = this.getHospitalLocation(selectedPriceItem.hospital)
  321. const mapTracking = this.generateMapTracking(hospitalLocation)
  322. // 创建订单数据
  323. const order = {
  324. id: Date.now(),
  325. hospital: selectedPriceItem.hospital,
  326. medicines: [selectedPriceItem.medicine],
  327. totalPrice: selectedPriceItem.price,
  328. status: '配送中',
  329. date: new Date().toISOString().split('T')[0],
  330. deliveryAddress: this.data.deliveryAddress,
  331. droneTracking: {
  332. status: '配送中',
  333. steps: ['订单确认', '药品准备', '无人机起飞', '配送中', '待送达']
  334. },
  335. mapTracking: mapTracking
  336. }
  337. // 保存订单到全局变量和本地存储
  338. const app = getApp()
  339. app.addOrder(order)
  340. // 保存地址用于显示
  341. const savedAddress = this.data.deliveryAddress
  342. // 关闭模态框
  343. this.setData({
  344. showPriceConfirmModal: false,
  345. selectedPriceItem: null,
  346. deliveryAddress: ''
  347. })
  348. wx.showModal({
  349. title: '下单成功',
  350. content: `订单总价:¥${selectedPriceItem.price}\n送货地址:${savedAddress}\n药品将尽快通过无人机配送`,
  351. showCancel: false,
  352. success: () => {
  353. // 跳转到我的页面查看订单
  354. setTimeout(() => {
  355. wx.switchTab({
  356. url: '/pages/profile/profile'
  357. })
  358. }, 500)
  359. }
  360. })
  361. }
  362. })