console.vue 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  1. <template>
  2. <div class="order-console">
  3. <el-drawer
  4. v-model="showOrderConsole"
  5. title="开始点餐"
  6. direction="rtl"
  7. size="100%"
  8. :before-close="beforeClose"
  9. >
  10. <div class="p-2 md:w-2/6 w-full md:h-full h-auto float-left">
  11. <el-card shadow="never" :body-style="{ padding: '10px' }">
  12. <template #header>
  13. <div
  14. class="justify-between items-center flex text-xl font-semibold font-mono"
  15. >
  16. <span>{{ '桌台:' + data.deskName }}</span>
  17. <div class="flex items-center">
  18. <span>{{ data.currentNum + '/' + data.deskCap }}</span>
  19. <el-icon class="ml-2 cursor-pointer" @click="refreshdishes">
  20. <Refresh />
  21. </el-icon>
  22. </div>
  23. </div>
  24. </template>
  25. <el-input
  26. v-model="params.remark"
  27. maxlength="100"
  28. placeholder="订单备注"
  29. show-word-limit
  30. type="textarea"
  31. />
  32. <el-divider />
  33. <el-scrollbar ref="scrollbarRef" class="scrollbar">
  34. <div ref="innerRef" v-loading="orderLoading">
  35. <el-empty
  36. v-if="data.selectGoods.length == 0 ? true : false"
  37. description="啥也没有,快点击商品添加吧"
  38. />
  39. <!-- 已选商品 -->
  40. <goods-item
  41. v-for="item in data.selectGoods"
  42. :key="item"
  43. class="scrollbar-demo-item"
  44. :goods-item="item"
  45. @add="add"
  46. @reduce="reduce"
  47. >
  48. </goods-item>
  49. </div>
  50. </el-scrollbar>
  51. <div class="flex justify-between items-center mt-2">
  52. <div
  53. class="flex-1 h-12 border rounded-md flex items-center justify-center cursor-pointer relative mr-2"
  54. :class="{
  55. 'border-primary text-primary bg-[#EDEFFF]':
  56. params.diningMethods === 1
  57. }"
  58. @click="params.diningMethods = 1"
  59. >
  60. <span>店内就餐</span>
  61. <el-icon
  62. v-if="params.diningMethods === 1"
  63. class="absolute right-0 bottom-0 text-primary"
  64. style="font-size: 16px"
  65. >
  66. <Check />
  67. </el-icon>
  68. </div>
  69. <div
  70. class="flex-1 h-12 border rounded-md flex items-center justify-center cursor-pointer relative ml-2"
  71. :class="{
  72. 'border-primary text-primary bg-[#EDEFFF]':
  73. params.diningMethods === 2
  74. }"
  75. @click="params.diningMethods = 2"
  76. >
  77. <span>打包外带</span>
  78. <el-icon
  79. v-if="params.diningMethods === 2"
  80. class="absolute right-0 bottom-0 text-primary"
  81. style="font-size: 16px"
  82. >
  83. <Check />
  84. </el-icon>
  85. </div>
  86. </div>
  87. </el-card>
  88. </div>
  89. <div class="p-2 md:w-1/6 w-full md:h-full h-auto float-left flex-wrap">
  90. <el-scrollbar>
  91. <div class="flex flex-col space-y-2 p-2">
  92. <el-button
  93. size="large"
  94. :type="buttonType == 'all' ? 'primary' : ''"
  95. @click="clickSortButton('all')"
  96. text
  97. bg
  98. >全部商品</el-button
  99. >
  100. <el-button
  101. class="cate-button"
  102. v-for="button in dishCate"
  103. :key="button.name"
  104. size="large"
  105. style="margin-left: 0"
  106. :type="buttonType == button.name ? 'primary' : ''"
  107. @click="clickSortButton(button.name)"
  108. text
  109. bg
  110. >{{ button.name }}</el-button
  111. >
  112. </div></el-scrollbar
  113. >
  114. </div>
  115. <div class="p-2 md:w-1/2 w-full md:h-full h-auto float-left flex-wrap">
  116. <div class="w-full h-auto">
  117. <div class="justify-end flex">
  118. <el-input
  119. v-model="search"
  120. class="md:w-40 w-1/2 m-2"
  121. placeholder="搜索菜品名字"
  122. :suffix-icon="Search"
  123. />
  124. </div>
  125. </div>
  126. <div class="w-full md:h-5/6 h-auto inline-block" style="height: calc(100% - 40px)">
  127. <!-- 菜单内容 -->
  128. <el-scrollbar height="100%">
  129. <div
  130. v-for="item in dishList"
  131. :key="item.id"
  132. class="float-left md:w-40 w-1/2 md:h-38 h-auto p-1"
  133. style="user-select: none"
  134. >
  135. <a
  136. href="javascript:void(0);"
  137. @click="addGoods(item)"
  138. :class="{ 'pointer-events-none': item.isShow == 0 }"
  139. ><el-card
  140. shadow="never"
  141. :body-style="{ padding: '0px' }"
  142. :class="{ relative: item.isShow == 0 }"
  143. >
  144. <div
  145. v-if="item.isShow == 0"
  146. class="absolute inset-0 bg-white z-10 pointer-events-none flex items-center justify-center select-none"
  147. style="opacity: 0.5; color: red; font-size: 25px"
  148. >
  149. <span class="text-gray-600 font-medium">已售罄</span>
  150. </div>
  151. <el-image
  152. style="width: 100%; height: 100px"
  153. :src="item.image"
  154. fit="cover"
  155. />
  156. <div class="text-2xs font-semibold p-1">
  157. <p>{{ item.title }}</p>
  158. <p style="color: coral">
  159. {{ item.summary + '元/份' }}
  160. </p>
  161. </div>
  162. </el-card></a
  163. >
  164. </div>
  165. </el-scrollbar>
  166. </div>
  167. </div>
  168. <template #footer>
  169. <div class="flex items-center px-4 space-x-5">
  170. <div class="relative mr-auto">
  171. <el-badge :value="orderData.sumNumSum" :max="99" class="mr-3">
  172. <el-icon size="50" style="width: 32px; height: 32px; font-size: 32px"
  173. ><ShoppingCart
  174. /></el-icon>
  175. </el-badge>
  176. <span class="ml-5 text-base"
  177. >合计: ¥{{ orderData.sumPriceSum.toFixed(2) }}</span
  178. >
  179. </div>
  180. <el-button
  181. type="primary"
  182. class="action-btn !w-[50px] !h-[40px]"
  183. @click="toEmpty()"
  184. :disabled="data.selectGoods.length <= 0"
  185. >清空</el-button
  186. >
  187. <el-badge :value="params.couponId ? 1 : ''" :hidden="!params.couponId">
  188. <el-button
  189. type="primary"
  190. class="action-btn !w-[200px] !h-[40px]"
  191. @click="showCouponDialog = true"
  192. >
  193. 优惠 {{ params.couponAmount !== '-' ? `¥${params.couponAmount}` : '' }}
  194. </el-button>
  195. </el-badge>
  196. <el-button
  197. type="danger"
  198. class="submit-btn !w-[300px] !h-[40px]"
  199. @click="submit()"
  200. >去结算 ¥{{ orderData.payAmount }}</el-button
  201. >
  202. </div>
  203. </template>
  204. </el-drawer>
  205. </div>
  206. <el-dialog
  207. v-model="specDialogVisible"
  208. title="选择规格"
  209. width="50%"
  210. :before-close="handleSpecDialogClose"
  211. >
  212. <div v-if="currentItem">
  213. <div
  214. v-for="(specs, name) in groupSpecsByName(currentItem.specsList)"
  215. :key="name"
  216. class="mb-4"
  217. >
  218. <div class="font-bold mb-2">{{ name }}</div>
  219. <div class="flex flex-wrap gap-2">
  220. <div
  221. v-for="spec in specs"
  222. :key="spec.id"
  223. class="flex-1 h-12 border rounded-md flex items-center justify-center cursor-pointer relative"
  224. :class="{
  225. 'border-primary text-primary bg-[#EDEFFF]':
  226. selectedSpecs[name] === spec.id
  227. }"
  228. @click="selectedSpecs[name] = spec.id"
  229. >
  230. <span>{{ spec.value }}</span>
  231. <el-icon
  232. v-if="selectedSpecs[name] === spec.id"
  233. class="absolute right-0 bottom-0 text-primary"
  234. style="font-size: 16px"
  235. >
  236. <Check />
  237. </el-icon>
  238. </div>
  239. </div>
  240. </div>
  241. </div>
  242. <template #footer>
  243. <span class="dialog-footer">
  244. <el-button @click="specDialogVisible = false">取消</el-button>
  245. <el-button type="primary" @click="handleSpecConfirm">确认</el-button>
  246. </span>
  247. </template>
  248. </el-dialog>
  249. <!-- 优惠券弹框 -->
  250. <el-dialog
  251. v-model="showCouponDialog"
  252. title="选择优惠券"
  253. width="50%"
  254. :close-on-click-modal="false"
  255. >
  256. <div class="mb-4" style="margin-top: 5px">
  257. <div class="flex items-center">
  258. <el-input
  259. v-model="data.phone"
  260. placeholder="请输入手机号查询优惠券"
  261. class="flex-1"
  262. maxlength="11"
  263. >
  264. <template #append>
  265. <el-button
  266. :icon="Search"
  267. @click="searchUserCoupons"
  268. :loading="searchLoading"
  269. />
  270. </template>
  271. </el-input>
  272. </div>
  273. </div>
  274. <div class="mt-2 coupon-container" v-loading="searchLoading">
  275. <el-scrollbar height="200px">
  276. <el-empty
  277. v-if="!data.couponList.length"
  278. description="暂无可用优惠券"
  279. :image-size="60"
  280. />
  281. <div v-else class="coupon-list" style="padding: 7px">
  282. <div
  283. v-for="coupon in data.couponList"
  284. :key="coupon.id"
  285. class="coupon-item"
  286. :class="{
  287. 'is-selected': params.couponId === coupon.ticketRecordId,
  288. 'is-disabled': orderData.sumPriceSum <= coupon.consumeScore / 100
  289. }"
  290. @click="handleCouponClick(coupon)"
  291. >
  292. <div class="left-part">
  293. <div class="amount-wrapper">
  294. <div class="amount">{{ coupon.consumeScore / 100 }}</div>
  295. </div>
  296. <!-- <div class="condition">满{{ coupon.consumeScore / 100 }}可用</div> -->
  297. </div>
  298. <div class="right-part">
  299. <div class="name">{{ coupon.productName }}</div>
  300. <div class="date">券码:{{ coupon.ticketNo }}</div>
  301. </div>
  302. </div>
  303. </div>
  304. </el-scrollbar>
  305. </div>
  306. <template #footer>
  307. <span class="dialog-footer">
  308. <el-button type="primary" @click="ticketConfirm">确认</el-button>
  309. </span>
  310. </template>
  311. </el-dialog>
  312. <el-dialog
  313. v-model="scanDialogVisible"
  314. title="请引导顾客扫码支付"
  315. width="300px"
  316. center
  317. :show-close="false"
  318. :close-on-click-modal="false"
  319. :close-on-press-escape="false"
  320. >
  321. <div class="text-center">
  322. <img
  323. src="@/assets/images/scan.gif"
  324. alt="扫码"
  325. class="mx-auto mb-4"
  326. style="width: 150px"
  327. />
  328. <b style="font-size: 20px">金额 {{ orderData.payAmount }} 元</b>
  329. </div>
  330. <template #footer>
  331. <div class="dialog-footer">
  332. <el-button @click="scanDialogVisible = false">取消支付</el-button>
  333. </div>
  334. </template>
  335. </el-dialog>
  336. <el-dialog
  337. v-model="payingDialogVisible"
  338. title="支付处理中"
  339. width="300px"
  340. :show-close="false"
  341. :close-on-click-modal="false"
  342. :close-on-press-escape="false"
  343. >
  344. <div class="flex flex-col items-center justify-center py-4">
  345. <el-icon class="is-loading mb-4" :size="32">
  346. <Loading />
  347. </el-icon>
  348. <p class="text-gray-600">正在支付中,请不要关闭或退出界面</p>
  349. </div>
  350. </el-dialog>
  351. <el-dialog
  352. v-model="takeNumberDialogVisible"
  353. title="取餐号"
  354. width="30%"
  355. :close-on-click-modal="false"
  356. >
  357. <el-input v-model="takeNumber" placeholder="请输入取餐号" maxlength="10" show-word-limit />
  358. <template #footer>
  359. <span class="dialog-footer">
  360. <el-button @click="takeNumberDialogVisible = false">取消</el-button>
  361. <el-button type="primary" @click="confirmSubmit">确认结算</el-button>
  362. </span>
  363. </template>
  364. </el-dialog>
  365. </template>
  366. <script setup lang="ts">
  367. import { ElScrollbar } from 'element-plus'
  368. import { Search, ShoppingCart } from '@element-plus/icons-vue'
  369. import goodsItem from './goodsItem.vue'
  370. import {
  371. dishAdd,
  372. dishCateAll,
  373. dishDec,
  374. dishDel,
  375. dishInc,
  376. dishListAll,
  377. toEmptyy,
  378. orderSubmit,
  379. deskOrderedDishListAll,
  380. searchCoupons,
  381. orderPay,
  382. queryPayStatus,
  383. cancelOrder
  384. } from '@/api/order'
  385. import feedback from '@/utils/feedback'
  386. // const route = useRoute()
  387. const innerRef = ref<HTMLDivElement>()
  388. const dishCate = ref<any[]>([])
  389. const dishList = ref<any[]>([])
  390. const showCouponDialog = ref(false)
  391. let dishListall: any[] = []
  392. const orderLoading = ref(false)
  393. const scrollbarRef = ref<InstanceType<typeof ElScrollbar>>()
  394. const search = ref('')
  395. const emit = defineEmits(['init'])
  396. const buttonType = ref('all')
  397. const showOrderConsole = ref(false)
  398. const scanDialogVisible = ref(false)
  399. const scanCode = ref('')
  400. // 添加支付中对话框的控制变量
  401. const payingDialogVisible = ref(false)
  402. const data = reactive<any>({
  403. deskName: '',
  404. deskCap: 0,
  405. currentNum: 0,
  406. selectGoods: [],
  407. oldGoods: [],
  408. phone: '', // 手机号
  409. couponList: [], // 优惠券列表
  410. canScan: false
  411. })
  412. const orderData = reactive<any>({
  413. priceSum: 0,
  414. sumPriceSum: 0,
  415. numSum: 0,
  416. sumNumSum: 0,
  417. payAmount: 0 // 新增支付金额
  418. })
  419. const params = reactive({
  420. remark: '', //订单备注
  421. number: '', //订单id
  422. userId: '', //用户id
  423. couponId: '', //优惠券id
  424. couponAmount: '-',
  425. diningMethods: 1 // 就餐方式:1店内就餐 2打包外带
  426. })
  427. // 添加取餐号相关的变量
  428. const takeNumberDialogVisible = ref(false)
  429. const takeNumber = ref('')
  430. const submit = () => {
  431. //出单
  432. if (data.selectGoods.length == 0) {
  433. feedback.alert('未点菜品!')
  434. return false
  435. }
  436. if (orderData.payAmount <= 0) {
  437. feedback.alert('支付金额需大于0!')
  438. return false
  439. }
  440. // 显示取餐号输入弹框
  441. takeNumberDialogVisible.value = true
  442. }
  443. // 添加确认提交函数
  444. const confirmSubmit = () => {
  445. if (!takeNumber.value) {
  446. feedback.msgError('请输入取餐号')
  447. return
  448. }
  449. takeNumberDialogVisible.value = false
  450. feedback.loading('正在出单...')
  451. orderSubmit({
  452. ...params,
  453. userId: params.userId || null,
  454. couponId: params.couponId || null,
  455. mealCode: takeNumber.value
  456. })
  457. .then(() => {
  458. // data.selectGoods.length = 0
  459. // orderData.priceSum = 0
  460. // orderData.numSum = 0
  461. //把所有餐品的状态修改为出单
  462. data.selectGoods.forEach((good: { status: number }) => {
  463. good.status = 1
  464. })
  465. // emit('init')
  466. feedback.closeLoading()
  467. // feedback.notifySuccess('出单成功')
  468. checkout()
  469. }).catch((e)=>{
  470. if (e.msg === "券不存在" || e.msg === "券不存在或已过期") {
  471. params.couponId = null
  472. params.couponAmount = '-'
  473. // 计算支付金额
  474. orderData.payAmount = Number(orderData.sumPriceSum).toFixed(2)
  475. searchUserCoupons()
  476. }
  477. })
  478. .finally(() => {
  479. feedback.closeLoading()
  480. });
  481. }
  482. // 添加扫码事件监听函数
  483. const handleScanInput = (event: KeyboardEvent) => {
  484. if (event.key === 'Enter') {
  485. const code = scanCode.value
  486. console.log('扫码内容:', code)
  487. if (!data.canScan) {
  488. feedback.notifyWarning('正在处理支付,请勿重复扫码')
  489. return
  490. }
  491. data.canScan = false
  492. payingDialogVisible.value = true // 显示支付中对话框
  493. document.removeEventListener('keydown', handleScanInput)
  494. // 修改支付处理逻辑
  495. orderPay({ oid: params.number, code: code })
  496. .then((res) => {
  497. console.log(res)
  498. // 实际上支付成功之后,res返回的是[],如果不是实时成功,返回的是个对象, 形如:{orderId: '123456789', sn: '7895004131583689'}
  499. if (res == null || res.length == 0) {
  500. feedback.notifySuccess('成功结账' + orderData.payAmount + '元')
  501. scanDialogVisible.value = false
  502. payingDialogVisible.value = false // 隐藏支付中对话框
  503. showOrderConsole.value = false
  504. emit('init')
  505. } else {
  506. console.log('支付中')
  507. return queryPayStatusWithRetry(res)
  508. .then((paySuccess) => {
  509. if (paySuccess) {
  510. feedback.notifySuccess('成功结账' + orderData.payAmount + '元')
  511. scanDialogVisible.value = false
  512. payingDialogVisible.value = false // 隐藏支付中对话框
  513. showOrderConsole.value = false
  514. emit('init')
  515. }
  516. })
  517. }
  518. })
  519. .catch((e) => {
  520. console.log(e)
  521. // 支付失败
  522. payingDialogVisible.value = false // 隐藏支付中对话框
  523. feedback.confirm('支付失败,请重试').then(() => {
  524. data.canScan = true
  525. scanDialogVisible.value = true
  526. // 添加键盘事件监听
  527. document.addEventListener('keydown', handleScanInput)
  528. })
  529. })
  530. scanCode.value = ''
  531. } else {
  532. scanCode.value += event.key
  533. }
  534. }
  535. // 定义查询支付状态的函数
  536. const queryPayStatusWithRetry = (data): Promise<boolean> => {
  537. const intervals = [3000, 5000, 10000, 10000, 10000, 10000, 10000]
  538. let currentIndex = 0
  539. payingDialogVisible.value = true
  540. const tryQuery = () => {
  541. return new Promise<boolean>((resolve) => {
  542. setTimeout(() => {
  543. queryPayStatus({ orderId: data.orderId, sn: data.sn })
  544. .then((result) => {
  545. if (
  546. result.result_code == '200' &&
  547. result.biz_response.data.order_status == 'SUCCESS'
  548. ) {
  549. resolve(true)
  550. } else if (currentIndex < intervals.length - 1) {
  551. currentIndex++
  552. tryQuery().then(resolve)
  553. } else {
  554. resolve(false)
  555. }
  556. })
  557. .catch(() => {
  558. if (currentIndex < intervals.length - 1) {
  559. currentIndex++
  560. tryQuery().then(resolve)
  561. } else {
  562. resolve(false)
  563. }
  564. })
  565. }, intervals[currentIndex])
  566. })
  567. }
  568. return tryQuery().then((success) => {
  569. payingDialogVisible.value = false
  570. if (!success) {
  571. return cancelOrder({ orderId: data.orderId, sn: data.sn })
  572. .then(() => {
  573. feedback.notifyWarning('支付超时,已自动撤销本次支付')
  574. return false
  575. })
  576. .catch((error) => {
  577. console.error('撤单失败:', error)
  578. feedback.notifyError('支付失败,请重试')
  579. return false
  580. })
  581. }
  582. return success
  583. })
  584. }
  585. const ticketConfirm = () => {
  586. if (data.phone == '') {
  587. params.couponId = null
  588. params.couponAmount = '-'
  589. params.userId = null
  590. // 计算支付金额
  591. orderData.payAmount = Number(orderData.sumPriceSum).toFixed(2)
  592. }
  593. showCouponDialog.value = false
  594. }
  595. const checkout = () => {
  596. refreshdishes()
  597. //结账
  598. console.warn('***结账***')
  599. // if (data.selectGoods || data.selectGoods.length == 0) {
  600. // feedback.notify('请点击商品开始点单吧!')
  601. // return false
  602. // }
  603. // feedback.confirm('结账金额为' + orderData.payAmount + ',确定要结账吗?').then(() => {
  604. data.canScan = true
  605. scanDialogVisible.value = true
  606. // 添加键盘事件监听
  607. document.addEventListener('keydown', handleScanInput)
  608. // })
  609. }
  610. // 添加优惠券点击处理方法
  611. const handleCouponClick = (coupon: any) => {
  612. if (orderData.sumPriceSum <= coupon.consumeScore / 100) {
  613. return
  614. }
  615. if (params.couponId === coupon.ticketRecordId) {
  616. params.couponId = ''
  617. params.couponAmount = '-'
  618. orderData.payAmount = orderData.sumPriceSum
  619. return
  620. }
  621. params.couponId = coupon.ticketRecordId
  622. params.couponAmount = (coupon.consumeScore / 100).toFixed(2)
  623. // 计算支付金额
  624. orderData.payAmount = (
  625. Number(orderData.sumPriceSum) - Number(coupon.consumeScore / 100)
  626. ).toFixed(2)
  627. }
  628. const open = (item: any, num?: number, orderNumber?: any) => {
  629. data.couponList = []
  630. showOrderConsole.value = true
  631. data.deskName = item.name
  632. data.deskCap = item.num
  633. data.currentNum = item.userNum ? item.userNum : num
  634. params.number = item.ordersId ? item.ordersId : orderNumber
  635. params.userId = ''
  636. params.couponId = ''
  637. takeNumber.value = ''
  638. //查询当前订单下的所有菜品
  639. deskOrderedDishListAll({ id: params.number }).then((res) => {
  640. res.forEach((good: { id: number; ordersDishId: number }) => {
  641. good.ordersDishId = good.id
  642. })
  643. console.log('***deskOrderedDishListAll***', res)
  644. data.selectGoods = res
  645. data.oldGoods = JSON.parse(JSON.stringify(res)) //记录下进入时当前餐桌已有的食物
  646. if (res.length > 0) {
  647. data.phone = res[0].mobile
  648. params.remark = res[0].remark
  649. params.userId = res[0].userId || null
  650. params.couponId = res[0].ticketId || null
  651. params.diningMethods = res[0].diningMethods || 1
  652. takeNumber.value = res[0].mealCode || ''
  653. if (params.couponId && params.userId) {
  654. searchUserCoupons()
  655. }
  656. params.couponAmount = res[0].ticketAmount
  657. ? Number(res[0].ticketAmount / 100).toFixed(2)
  658. : '-'
  659. const _list = res.filter((it: { status: number }) => it.status != 1) //找到没有出单的餐品来计算价格
  660. orderData.priceSum = _list.reduce(
  661. (accumulator: number, currentValue: { summary: number; num: number }) =>
  662. accumulator + currentValue.summary * currentValue.num,
  663. 0
  664. )
  665. orderData.numSum = _list.reduce(
  666. (accumulator: number, currentValue: { summary: number; num: number }) =>
  667. accumulator + currentValue.num,
  668. 0
  669. )
  670. //所有餐品的总价以及总数量
  671. orderData.sumPriceSum = res.reduce(
  672. (accumulator: number, currentValue: { summary: number; num: number }) =>
  673. accumulator + currentValue.summary * currentValue.num,
  674. 0
  675. )
  676. orderData.sumNumSum = res.reduce(
  677. (accumulator: number, currentValue: { summary: number; num: number }) =>
  678. accumulator + currentValue.num,
  679. 0
  680. )
  681. orderData.payAmount = Number(
  682. orderData.sumPriceSum -
  683. (params.couponAmount === '-' ? 0 : Number(params.couponAmount))
  684. ).toFixed(2)
  685. } else {
  686. orderData.priceSum = 0
  687. orderData.numSum = 0
  688. orderData.sumPriceSum = 0
  689. orderData.sumNumSum = 0
  690. data.phone = ''
  691. params.remark = ''
  692. params.userId = ''
  693. params.couponId = ''
  694. params.couponAmount = '-'
  695. orderData.payAmount = 0
  696. data.couponList = []
  697. }
  698. })
  699. dishList.value = dishListall //显示所有菜品
  700. }
  701. const refreshdishes = async () => {
  702. orderLoading.value = true
  703. await deskOrderedDishListAll({ id: params.number })
  704. .then((res) => {
  705. res.forEach((good: { id: number; ordersDishId: number }) => {
  706. good.ordersDishId = good.id
  707. })
  708. console.log('***refreshdishes***', res)
  709. data.selectGoods = res
  710. console.log('***res.length***', res.length)
  711. if (res.length > 0) {
  712. // const _list = res.filter((it: { status: number }) => it.status != 1) //找到没有出单的餐品来计算价格
  713. const _list = res
  714. orderData.priceSum = _list.reduce(
  715. (accumulator: number, currentValue: { summary: number; num: number }) =>
  716. accumulator + currentValue.summary * currentValue.num,
  717. 0
  718. )
  719. orderData.numSum = _list.reduce(
  720. (accumulator: number, currentValue: { summary: number; num: number }) =>
  721. accumulator + currentValue.num,
  722. 0
  723. )
  724. //所有餐品的总价以及总数量
  725. orderData.sumPriceSum = res.reduce(
  726. (accumulator: number, currentValue: { summary: number; num: number }) =>
  727. accumulator + currentValue.summary * currentValue.num,
  728. 0
  729. )
  730. orderData.sumNumSum = res.reduce(
  731. (accumulator: number, currentValue: { summary: number; num: number }) =>
  732. accumulator + currentValue.num,
  733. 0
  734. )
  735. console.log(
  736. '***res[0].payAmount***',
  737. res[0].payAmount,
  738. Number(res[0].payAmount / 100).toFixed(2)
  739. )
  740. orderData.payAmount = res[0].payAmount
  741. ? Number(res[0].payAmount / 100).toFixed(2)
  742. : Number(orderData.sumPriceSum).toFixed(2)
  743. console.log('***orderData***', orderData)
  744. } else {
  745. orderData.priceSum = 0
  746. orderData.numSum = 0
  747. orderData.sumPriceSum = 0
  748. orderData.sumNumSum = 0
  749. orderData.payAmount = 0
  750. }
  751. })
  752. .finally(() => {
  753. orderLoading.value = false
  754. })
  755. }
  756. watch(search, (value) => {
  757. if (!value) {
  758. if (buttonType.value == 'all') {
  759. dishList.value = dishListall
  760. } else {
  761. dishList.value = dishListall.filter((ele) => {
  762. return ele.category == buttonType.value
  763. })
  764. }
  765. console.warn('***watch***', value, buttonType.value, dishList.value)
  766. } else {
  767. dishList.value = dishList.value.filter((ele) => {
  768. return ele.title.includes(value)
  769. })
  770. }
  771. })
  772. const clickSortButton = (name: any) => {
  773. //重复点击不处理
  774. if (buttonType.value == name) {
  775. return false
  776. }
  777. search.value = ''
  778. buttonType.value = name
  779. if (name == 'all') {
  780. dishList.value = dishListall
  781. } else {
  782. dishList.value = dishListall.filter((ele) => {
  783. return ele.category == name
  784. })
  785. }
  786. }
  787. //清空订单下的菜品
  788. const toEmpty = () => {
  789. if (data.selectGoods.length != 0) {
  790. orderLoading.value = true
  791. toEmptyy({ id: params.number }).finally(() => {
  792. //清空未出单的餐品-保留出单餐品
  793. data.selectGoods = []
  794. orderData.sumPriceSum = data.selectGoods.reduce(
  795. (accumulator: number, currentValue: { summary: number; num: number }) =>
  796. accumulator + currentValue.summary * currentValue.num,
  797. 0
  798. )
  799. orderData.sumNumSum = data.selectGoods.reduce(
  800. (accumulator: number, currentValue: { summary: number; num: number }) =>
  801. accumulator + currentValue.num,
  802. 0
  803. )
  804. // data.selectGoods.length = 0
  805. orderData.priceSum = 0
  806. orderData.numSum = 0
  807. // orderData.sumPriceSum = 0
  808. // orderData.sumNumSum = 0
  809. // 检查优惠券条件
  810. if (params.couponId && orderData.sumPriceSum < Number(params.couponAmount)) {
  811. // 如果订单金额小于优惠券金额,清空优惠券
  812. params.couponId = ''
  813. params.couponAmount = '-'
  814. orderData.payAmount = orderData.sumPriceSum
  815. }
  816. orderData.payAmount = Number(
  817. orderData.sumPriceSum -
  818. (params.couponAmount === '-' ? 0 : Number(params.couponAmount))
  819. ).toFixed(2)
  820. orderLoading.value = false
  821. })
  822. }
  823. }
  824. const isEqual = (obj1: { [x: string]: any } | null, obj2: { [x: string]: any } | null) => {
  825. // 如果两个对象是同一个引用,直接返回 true
  826. if (obj1 === obj2) return true
  827. // 如果其中一个为 null 或者不是对象,返回 false
  828. if (obj1 === null || typeof obj1 !== 'object' || obj2 === null || typeof obj2 !== 'object') {
  829. return false
  830. }
  831. // 获取两个对象的键
  832. const keys1 = Object.keys(obj1)
  833. const keys2 = Object.keys(obj2)
  834. // 如果键的数量不同,返回 false
  835. if (keys1.length !== keys2.length) return false
  836. // 遍历所有键,递归比较每个键的值
  837. for (const key of keys1) {
  838. if (!keys2.includes(key) || !isEqual(obj1[key], obj2[key])) {
  839. return false
  840. }
  841. }
  842. // 如果所有键的值都相等,返回 true
  843. return true
  844. }
  845. const beforeClose = () => {
  846. //data.oldGoods
  847. // if(isEqual(data.selectGoods,data.oldGoods)){
  848. // showOrderConsole.value = false;
  849. // emit('init')
  850. // return false;
  851. // }
  852. // if (data.selectGoods && data.selectGoods.length > 0) {
  853. // feedback.confirm('所点菜品信息将不被保存,确认关闭?').then(() => {
  854. // toEmpty()
  855. // })
  856. // }
  857. showOrderConsole.value = false
  858. search.value = ''
  859. buttonType.value = 'all'
  860. emit('init')
  861. }
  862. const add = (item: any) => {
  863. // addGoods(item)
  864. console.log('***add***', item)
  865. orderLoading.value = true
  866. dishInc({ id: item.ordersDishId })
  867. .then(() => {
  868. data.selectGoods[data.selectGoods.indexOf(item)].num++
  869. addHandle(item)
  870. })
  871. .finally(() => {
  872. orderLoading.value = false
  873. nextTick(() => {
  874. scrollbarRef.value!.setScrollTop(innerRef.value!.clientHeight)
  875. })
  876. })
  877. }
  878. // 添加新的响应式变量
  879. const specDialogVisible = ref(false)
  880. const currentItem = ref<any>(null)
  881. const selectedSpecs = ref<any>({})
  882. // 添加新的方法
  883. const groupSpecsByName = (specsList: any[]) => {
  884. const groups: any = {}
  885. specsList?.forEach((spec) => {
  886. if (!groups[spec.name]) {
  887. groups[spec.name] = []
  888. }
  889. groups[spec.name].push(spec)
  890. })
  891. return groups
  892. }
  893. const handleSpecDialogClose = () => {
  894. specDialogVisible.value = false
  895. currentItem.value = null
  896. selectedSpecs.value = {}
  897. }
  898. const handleSpecConfirm = () => {
  899. // 检查是否所有规格都已选择
  900. const allSpecsSelected = Object.keys(groupSpecsByName(currentItem.value.specsList)).every(
  901. (name) => selectedSpecs.value[name]
  902. )
  903. if (!allSpecsSelected) {
  904. feedback.msgError('请选择所有规格')
  905. return
  906. }
  907. // 将选中的规格ID添加到商品中
  908. const item = { ...currentItem.value, selectedSpecIds: Object.values(selectedSpecs.value) }
  909. handleAddGoods(item)
  910. handleSpecDialogClose()
  911. }
  912. // 修改 addGoods 方法
  913. const addGoods = (item: any) => {
  914. if (item.specsList?.length > 0) {
  915. currentItem.value = item
  916. selectedSpecs.value = {}
  917. specDialogVisible.value = true
  918. return
  919. }
  920. handleAddGoods(item)
  921. }
  922. // 将原来的 addGoods 逻辑移到这个新方法中
  923. const handleAddGoods = (item: any) => {
  924. console.log('handleAddGoods', item)
  925. orderLoading.value = true
  926. //查找未出单的餐品列表
  927. const list = data.selectGoods.filter((element: any) => {
  928. return element.status != 1
  929. })
  930. console.log('list', list)
  931. let tempGoods: string | any[] = []
  932. //先从历史下单但是没有结单的餐品中找出要操作的餐品
  933. tempGoods = list.filter((element: any) => {
  934. //如果有specsIds,说明是有规格的餐品
  935. const specsIds: number[] = []
  936. element.specsList?.forEach((spec: any) => {
  937. specsIds.push(spec.specsId)
  938. })
  939. return (
  940. element.artId &&
  941. element.artId == item.id &&
  942. specsIds.sort((a: number, b: number) => a - b).join(',') ==
  943. (item.selectedSpecIds || []).sort((a: number, b: number) => a - b).join(',')
  944. )
  945. })
  946. console.log('tempGoods', tempGoods)
  947. if (tempGoods.length == 0) {
  948. item.num = 1
  949. dishAdd({
  950. orderId: params.number,
  951. dishId: item.id,
  952. specsIds: (item.selectedSpecIds || []).join(',') // 将数组转换为逗号分隔的字符串
  953. })
  954. .then((res) => {
  955. //拿到orders_dish的id
  956. item.ordersDishId = res
  957. item.specsList = item.selectedSpecIds?.map((specId: any) => {
  958. const o = item.specsList.find((spec: any) => spec.id == specId)
  959. o.specsId = specId
  960. return o
  961. })
  962. item.artId = item.id
  963. data.selectGoods.push(item)
  964. console.log('data.selectGoods', data.selectGoods)
  965. feedback.msgSuccess('成功添加商品' + item.title)
  966. addHandle(item)
  967. })
  968. .finally(() => {
  969. orderLoading.value = false
  970. nextTick(() => {
  971. scrollbarRef.value!.setScrollTop(innerRef.value!.clientHeight)
  972. })
  973. })
  974. } else {
  975. dishInc({ id: tempGoods[0].ordersDishId })
  976. .then(() => {
  977. data.selectGoods[data.selectGoods.indexOf(tempGoods[0])].num++
  978. addHandle(item)
  979. })
  980. .finally(() => {
  981. orderLoading.value = false
  982. nextTick(() => {
  983. scrollbarRef.value!.setScrollTop(innerRef.value!.clientHeight)
  984. })
  985. })
  986. }
  987. }
  988. //只有正常执行了接口才触发
  989. const addHandle = (item: { summary: any }) => {
  990. console.warn('***addGoods-dish***', item, data.selectGoods)
  991. console.warn('***orderData***', orderData)
  992. orderData.numSum++
  993. orderData.sumNumSum++
  994. orderData.priceSum += Number(item.summary)
  995. orderData.sumPriceSum += Number(item.summary)
  996. orderData.payAmount = Number(
  997. orderData.sumPriceSum - (params.couponAmount === '-' ? 0 : Number(params.couponAmount))
  998. ).toFixed(2)
  999. }
  1000. const reduce = (item: any) => {
  1001. orderLoading.value = true
  1002. //查找未出单的餐品列表
  1003. // const list = data.selectGoods.filter((element: any) => {return element.status != 1})
  1004. // let tempGoods: string | any[] = []
  1005. // //先从历史下单但是没有结单的餐品中找出要操作的餐品
  1006. // tempGoods = list.filter((element: any) => {
  1007. // const specsIds: number[] = [];
  1008. // element.specsList?.forEach((spec: any) => {
  1009. // specsIds.push(spec.specsId)
  1010. // })
  1011. // return element.artId && element.artId == item.artId &&
  1012. // specsIds.sort((a: number, b: number) => a - b).join(',') == (item.selectedSpecIds || []).sort((a: number, b: number) => a - b).join(',')
  1013. // })
  1014. // if(tempGoods.length==0){//历史没有,则从当次下单的餐品列表中找
  1015. // tempGoods = list.filter((element: any) => {
  1016. // return element.id == item.id && !element.artId
  1017. // })
  1018. // }
  1019. // console.log('tempGoods', tempGoods)
  1020. console.log('data.selectGoods', data.selectGoods)
  1021. const t = data.selectGoods.indexOf(item)
  1022. // const n = data.selectGoods[t].num
  1023. console.log('reduce', t, item)
  1024. if (item.num > 1) {
  1025. dishDec({ id: item.ordersDishId })
  1026. .then(() => {
  1027. data.selectGoods[t].num--
  1028. reduceHandle(item)
  1029. })
  1030. .finally(() => {
  1031. orderLoading.value = false
  1032. })
  1033. } else {
  1034. //餐品数量为0,要删减该餐品
  1035. dishDel({ id: item.ordersDishId })
  1036. .then(() => {
  1037. data.selectGoods.splice(t, 1)
  1038. reduceHandle(item)
  1039. })
  1040. .finally(() => {
  1041. orderLoading.value = false
  1042. })
  1043. }
  1044. }
  1045. //只有正常执行了接口才触发
  1046. const reduceHandle = (item: { summary: number }) => {
  1047. console.warn('***reduce-dish***', item, data.selectGoods)
  1048. orderData.numSum--
  1049. orderData.sumNumSum--
  1050. orderData.priceSum -= item.summary
  1051. orderData.sumPriceSum -= item.summary
  1052. orderData.payAmount = Number(
  1053. (
  1054. orderData.sumPriceSum - (params.couponAmount === '-' ? 0 : Number(params.couponAmount))
  1055. ).toFixed(2)
  1056. )
  1057. if (orderData.priceSum <= 0) {
  1058. orderData.priceSum = 0
  1059. }
  1060. if (orderData.numSum <= 0) {
  1061. orderData.numSum = 0
  1062. }
  1063. if (orderData.sumPriceSum <= 0) {
  1064. orderData.sumPriceSum = 0
  1065. }
  1066. if (orderData.sumNumSum <= 0) {
  1067. orderData.sumNumSum = 0
  1068. }
  1069. if (orderData.payAmount <= 0) {
  1070. orderData.payAmount = 0
  1071. }
  1072. }
  1073. // 添加搜索用户优惠券的方法
  1074. // ... 其他代码保持不变 ...
  1075. // 添加 loading 变量
  1076. const searchLoading = ref(false)
  1077. // 修改搜索方法
  1078. const searchUserCoupons = () => {
  1079. if (!data.phone) {
  1080. feedback.msgError('请输入手机号')
  1081. return
  1082. }
  1083. searchLoading.value = true
  1084. searchCoupons({ mobile: data.phone })
  1085. .then((res: any) => {
  1086. if (res) {
  1087. data.couponList = res ? res.couponList || [] : []
  1088. params.userId = res ? res.userId || null : null
  1089. if (data.couponList.length == 0) {
  1090. feedback.msgError('暂无可用优惠券')
  1091. return false
  1092. }
  1093. }
  1094. })
  1095. .finally(() => {
  1096. searchLoading.value = false
  1097. })
  1098. }
  1099. onMounted(() => {
  1100. dishCateAll().then((res) => {
  1101. dishCate.value = res
  1102. })
  1103. dishListAll().then((res) => {
  1104. dishList.value = res
  1105. dishListall = res
  1106. })
  1107. })
  1108. defineExpose({
  1109. open
  1110. })
  1111. </script>
  1112. <style lang="scss" scoped>
  1113. .el-alert__content {
  1114. width: 100%;
  1115. }
  1116. .scrollbar {
  1117. height: calc(100vh - 54px - 32px - 16px - 61px - 20px - 52px - 49px - 80px);
  1118. }
  1119. :deep(.el-divider--horizontal) {
  1120. margin: 8px 0;
  1121. }
  1122. .cate-buttons :deep(.el-button) {
  1123. margin: 0;
  1124. width: 100%;
  1125. justify-content: flex-start;
  1126. padding-left: 1rem;
  1127. }
  1128. .cate-buttons :deep(.el-button.is-text:focus),
  1129. .cate-buttons :deep(.el-button.is-text:hover),
  1130. .cate-buttons :deep(.el-button.is-text.is-active) {
  1131. color: var(--el-button-hover-border-color);
  1132. border-color: transparent;
  1133. background-color: var(--el-color-primary-light-9);
  1134. }
  1135. .cate-buttons :deep(.el-button.is-text) {
  1136. color: var(--el-text-color-regular);
  1137. }
  1138. .price-summary {
  1139. box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.05);
  1140. }
  1141. .el-button.el-button--danger {
  1142. background-color: #ff4d4f;
  1143. border-color: #ff4d4f;
  1144. }
  1145. .el-button.el-button--danger:hover {
  1146. background-color: #ff7875;
  1147. border-color: #ff7875;
  1148. }
  1149. </style>
  1150. <style>
  1151. .coupon-card {
  1152. display: flex;
  1153. align-items: center;
  1154. padding: 8px;
  1155. }
  1156. .coupon-item {
  1157. display: flex;
  1158. margin-bottom: 12px;
  1159. cursor: pointer;
  1160. position: relative;
  1161. background: #fff;
  1162. border-radius: 4px;
  1163. transition: all 0.3s;
  1164. border: 1px solid #e8e8e8;
  1165. &:last-child {
  1166. margin-bottom: 0;
  1167. }
  1168. &.is-selected {
  1169. border-color: coral;
  1170. .left-part {
  1171. background: coral;
  1172. }
  1173. }
  1174. &.is-disabled {
  1175. cursor: not-allowed;
  1176. opacity: 0.6;
  1177. .left-part {
  1178. background: #999;
  1179. }
  1180. }
  1181. }
  1182. .left-part {
  1183. width: 90px;
  1184. background: coral;
  1185. color: #fff;
  1186. padding: 12px 8px;
  1187. text-align: center;
  1188. position: relative;
  1189. display: flex;
  1190. align-items: center;
  1191. justify-content: center;
  1192. .amount-wrapper {
  1193. display: flex;
  1194. align-items: center;
  1195. justify-content: center;
  1196. height: 100%;
  1197. }
  1198. &::after {
  1199. content: '';
  1200. position: absolute;
  1201. right: 0;
  1202. top: 0;
  1203. bottom: 0;
  1204. width: 4px;
  1205. background-image: radial-gradient(circle at 0 6px, transparent 3px, #fff 3px);
  1206. background-size: 4px 12px;
  1207. background-repeat: repeat-y;
  1208. }
  1209. .amount {
  1210. font-size: 24px;
  1211. font-weight: bold;
  1212. line-height: 1;
  1213. }
  1214. .unit {
  1215. font-size: 12px;
  1216. margin-top: 2px;
  1217. }
  1218. .condition {
  1219. font-size: 12px;
  1220. margin-top: 6px;
  1221. opacity: 0.9;
  1222. }
  1223. }
  1224. .right-part {
  1225. flex: 1;
  1226. padding: 12px;
  1227. display: flex;
  1228. flex-direction: column;
  1229. justify-content: space-between;
  1230. .name {
  1231. font-size: 14px;
  1232. color: #333;
  1233. font-weight: 500;
  1234. }
  1235. .date {
  1236. font-size: 12px;
  1237. color: #999;
  1238. margin-top: 4px;
  1239. }
  1240. .check {
  1241. position: absolute;
  1242. right: 12px;
  1243. top: 50%;
  1244. transform: translateY(-50%);
  1245. color: #cd8e5f;
  1246. font-size: 12px;
  1247. }
  1248. }
  1249. .coupon-container {
  1250. border: 1px solid #ebeef5;
  1251. border-radius: 4px;
  1252. :deep(.el-scrollbar__wrap) {
  1253. padding: 8px;
  1254. }
  1255. }
  1256. .action-btn {
  1257. height: 28px;
  1258. padding: 5px 10px;
  1259. font-size: 12px;
  1260. border: 1px solid #dcdfe6;
  1261. border-radius: 4px;
  1262. background: transparent;
  1263. color: #606266;
  1264. }
  1265. .action-btn:hover {
  1266. color: var(--el-color-primary);
  1267. border-color: var(--el-color-primary);
  1268. background-color: var(--el-color-primary-light-9);
  1269. }
  1270. .submit-btn {
  1271. width: 100%;
  1272. height: 40px;
  1273. background-color: #ff4d4f;
  1274. border-color: #ff4d4f;
  1275. }
  1276. .submit-btn:hover {
  1277. background-color: #ff7875;
  1278. border-color: #ff7875;
  1279. }
  1280. </style>