index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <template>
  2. <!-- 下单页面 -->
  3. <view class="content">
  4. <!-- 订单卡片 -->
  5. <view class="card">
  6. <view class="card-header">
  7. <view class='tag'></view>
  8. <view class="card-header-title">订单信息</view>
  9. </view>
  10. <view class="card-content">
  11. <view class="info-item">
  12. <view class="info-item-label">就餐桌号</view>
  13. <view class="info-item-value">{{ orderInfo.tableId || '—' }}</view>
  14. </view>
  15. <view class="info-item">
  16. <view class="info-item-label">用餐人数</view>
  17. <view class="info-item-value">{{ orderInfo.diners || '—' }}人</view>
  18. </view>
  19. <view class="info-item">
  20. <view class="info-item-label">联系电话</view>
  21. <view class="info-item-value">{{ orderInfo.contactPhone || '—' }}</view>
  22. </view>
  23. <view class="info-item">
  24. <view class="info-item-label">备注信息</view>
  25. </view>
  26. <textarea
  27. v-model="orderInfo.remark"
  28. placeholder="请输入特殊需求,如少辣、不要香菜等(最多30字)"
  29. class="info-item-textarea"
  30. maxlength="30"
  31. :disabled="fromCheckout"
  32. :readonly="fromCheckout"
  33. ></textarea>
  34. </view>
  35. </view>
  36. <!-- 菜品清单 -->
  37. <view class="card">
  38. <view class="card-header">
  39. <view class='tag'></view>
  40. <view class="card-header-title">菜品清单</view>
  41. </view>
  42. <view class="card-content">
  43. <view class="info-food">
  44. <view v-for="(item, index) in foodList" :key="item.id || item.cuisineId || index" class="food-item">
  45. <!-- 菜品图片:兼容 cuisineImage/image,逗号分隔取首图 -->
  46. <image :src="getItemImage(item)" mode="aspectFill" class="food-item__image"></image>
  47. <!-- 菜品信息 -->
  48. <view class="food-item__info">
  49. <view class="food-item__name">{{ item.name || item.cuisineName }}</view>
  50. <view class="food-item__desc" v-if="item.tags && item.tags.length > 0">
  51. <text v-for="(tag, tagIndex) in item.tags" :key="tagIndex" class="food-item__tag">
  52. {{ tag.text }}<text v-if="tagIndex < item.tags.length - 1">,</text>
  53. </text>
  54. </view>
  55. </view>
  56. <!-- 价格和数量 -->
  57. <view class="food-item__right">
  58. <view class="food-item__price">
  59. <text class="price-main">¥{{ formatPrice(getItemPrice(item)) }}</text>
  60. </view>
  61. <view class="food-item__quantity">{{ item.quantity || 1 }}份</view>
  62. </view>
  63. </view>
  64. </view>
  65. </view>
  66. </view>
  67. <!-- 总价 -->
  68. <view class="card">
  69. <view class="card-header">
  70. <view class='tag'></view>
  71. <view class="card-header-title">价格明细</view>
  72. </view>
  73. <view class="card-content">
  74. <view class="info-item">
  75. <view class="info-item-label">菜品总价</view>
  76. <view class="info-item-value">¥{{ formatPrice(orderInfo.totalAmount) }}</view>
  77. </view>
  78. <view class="info-item">
  79. <view class="info-item-label">餐具费</view>
  80. <view class="info-item-value">¥{{ formatPrice(orderInfo.utensilFee ?? 0) }}</view>
  81. </view>
  82. <view class="info-item">
  83. <view class="info-item-label">优惠券</view>
  84. <view class="info-item-value">¥{{ formatPrice(orderInfo.discountAmount ?? 0) }}</view>
  85. </view>
  86. <view class="price-line">
  87. <view class="price-line-label">应付金额</view>
  88. <view class="price-line-value">¥{{ formatPrice(orderInfo.payAmount ?? orderInfo.totalAmount) }}</view>
  89. </view>
  90. </view>
  91. </view>
  92. <!-- 底部按钮:带金额(来自去结算)显示「确认支付」且备注不可改;不带金额显示「确认下单」且备注可改 -->
  93. <view class="bottom-button">
  94. <view class="bottom-button-text" hover-class="hover-active" @click="handleConfirmPay" v-if="fromCheckout">确认支付 ¥{{ formatPrice(orderInfo.payAmount ?? orderInfo.totalAmount) }}</view>
  95. <view class="bottom-button-text" hover-class="hover-active" @click="handleConfirmOrder" v-else>确认下单</view>
  96. </view>
  97. </view>
  98. </template>
  99. <script setup>
  100. import { onLoad } from "@dcloudio/uni-app";
  101. import { ref } from "vue";
  102. import { go } from "@/utils/utils.js";
  103. import { getFileUrl } from "@/utils/file.js";
  104. import { useUserStore } from "@/store/user.js";
  105. import { PostOrderCreate } from "@/api/dining.js";
  106. const payType = ref('confirmOrder'); // confirmOrder 确认下单 confirmPay 确认支付
  107. /** 仅当从订单列表点击「去结算」进入时为 true,底部确认下单按钮才显示金额 */
  108. const fromCheckout = ref(false);
  109. // 订单信息(从购物车带过来或 URL 参数)
  110. const orderInfo = ref({
  111. tableId: '',
  112. diners: '',
  113. contactPhone: '',
  114. remark: '',
  115. totalAmount: 0,
  116. utensilFee: 0,
  117. discountAmount: 0,
  118. payAmount: 0
  119. });
  120. // 菜品列表(从购物车带过来)
  121. const foodList = ref([]);
  122. // 菜品图片:兼容 cuisineImage/image,逗号分隔取首图
  123. function getItemImage(item) {
  124. const raw = item?.cuisineImage ?? item?.image ?? item?.imageUrl ?? '';
  125. const url = typeof raw === 'string' ? raw.split(',')[0].trim() : raw;
  126. return url ? getFileUrl(url) : '';
  127. }
  128. // 单品单价:列表展示用 unitPrice/price
  129. function getItemPrice(item) {
  130. const p = item?.unitPrice ?? item?.price ?? item?.totalPrice ?? item?.salePrice ?? 0;
  131. return Number(p) || 0;
  132. }
  133. function formatPrice(price) {
  134. const num = Number(price);
  135. return Number.isNaN(num) ? '0.00' : num.toFixed(2);
  136. }
  137. // 将存储的购物车项转为确认页展示格式(兼容 cuisineName/cuisineImage/unitPrice 等)
  138. function normalizeCartItem(item) {
  139. const image = item?.cuisineImage ?? item?.image ?? item?.imageUrl ?? '';
  140. const url = typeof image === 'string' ? image.split(',')[0].trim() : image;
  141. return {
  142. id: item?.id ?? item?.cuisineId,
  143. name: item?.name ?? item?.cuisineName,
  144. cuisineName: item?.cuisineName,
  145. price: item?.unitPrice ?? item?.price,
  146. image: url,
  147. cuisineImage: item?.cuisineImage,
  148. quantity: Number(item?.quantity) || 0,
  149. tags: item?.tags ?? [],
  150. subtotalAmount: item?.subtotalAmount,
  151. remark: item?.remark
  152. };
  153. }
  154. const handleConfirmOrder = async () => {
  155. const tableId = orderInfo.value.tableId;
  156. const contactPhone = orderInfo.value.contactPhone ?? '';
  157. const dinerCount = Number(orderInfo.value.diners) || 0;
  158. if (!tableId) {
  159. uni.showToast({ title: '请先选择桌号', icon: 'none' });
  160. return;
  161. }
  162. if (!contactPhone) {
  163. uni.showToast({ title: '请先填写联系电话', icon: 'none' });
  164. return;
  165. }
  166. const remark = (orderInfo.value.remark ?? '').trim().slice(0, 30);
  167. uni.showLoading({ title: '提交中...' });
  168. try {
  169. const res = await PostOrderCreate({
  170. tableId,
  171. contactPhone,
  172. couponId: null,
  173. dinerCount: dinerCount || undefined,
  174. immediatePay: 0,
  175. remark: remark || undefined
  176. });
  177. uni.hideLoading();
  178. const orderId = res?.id ?? res?.orderId ?? res?.data?.id ?? res?.data?.orderId;
  179. // 结果页「去结算」需带金额和备注,保存本次下单的桌号、人数、金额、备注
  180. const total = orderInfo.value.payAmount ?? orderInfo.value.totalAmount ?? 0;
  181. uni.setStorageSync('lastPlaceOrderInfo', JSON.stringify({
  182. tableId: orderInfo.value.tableId,
  183. diners: orderInfo.value.diners,
  184. totalAmount: total,
  185. remark: (orderInfo.value.remark ?? '').trim().slice(0, 30)
  186. }));
  187. if (orderId != null) {
  188. go(`/pages/result/index?id=${encodeURIComponent(orderId)}`);
  189. } else {
  190. go('/pages/result/index');
  191. }
  192. } catch (e) {
  193. uni.hideLoading();
  194. uni.showToast({ title: e?.message || '下单失败', icon: 'none' });
  195. }
  196. };
  197. const handleConfirmPay = () => {
  198. go('/pages/result/index?payType=confirmPay');
  199. };
  200. onLoad((options) => {
  201. if (options?.payType) payType.value = options.payType;
  202. const userStore = useUserStore();
  203. const contactPhone = userStore.getUserInfo?.phone ?? userStore.getUserInfo?.contactPhone ?? userStore.getUserInfo?.mobile ?? '';
  204. orderInfo.value.contactPhone = contactPhone;
  205. if (options?.tableId) orderInfo.value.tableId = options.tableId;
  206. if (options?.diners) orderInfo.value.diners = options.diners;
  207. if (options?.remark != null && options?.remark !== '') orderInfo.value.remark = decodeURIComponent(options.remark);
  208. if (options?.totalAmount != null && options?.totalAmount !== '') {
  209. fromCheckout.value = true;
  210. const total = Number(options.totalAmount) || 0;
  211. orderInfo.value.totalAmount = total;
  212. orderInfo.value.payAmount = total;
  213. }
  214. const raw = uni.getStorageSync('placeOrderCart');
  215. if (raw) {
  216. try {
  217. const data = JSON.parse(raw);
  218. const list = Array.isArray(data.list) ? data.list : [];
  219. foodList.value = list.map(normalizeCartItem).filter((item) => (item.quantity || 0) > 0);
  220. if (data.tableId != null) orderInfo.value.tableId = data.tableId;
  221. if (data.diners != null) orderInfo.value.diners = data.diners;
  222. if (data.remark != null) orderInfo.value.remark = data.remark;
  223. const total = Number(data.totalAmount) || 0;
  224. orderInfo.value.totalAmount = total;
  225. orderInfo.value.payAmount = total;
  226. } catch (e) {
  227. console.error('解析购物车数据失败:', e);
  228. }
  229. }
  230. if (orderInfo.value.tableId) uni.setStorageSync('currentTableId', String(orderInfo.value.tableId));
  231. });
  232. </script>
  233. <style lang="scss" scoped>
  234. .content {
  235. padding: 0 30rpx 300rpx;
  236. }
  237. .card {
  238. background-color: #fff;
  239. border-radius: 24rpx;
  240. padding: 30rpx 0;
  241. margin-top: 20rpx;
  242. .card-header {
  243. display: flex;
  244. justify-content: space-between;
  245. align-items: center;
  246. padding: 0 30rpx;
  247. height: 40rpx;
  248. position: relative;
  249. font-size: 27rpx;
  250. color: #151515;
  251. font-weight: bold;
  252. }
  253. .tag {
  254. width: 10rpx;
  255. height: 42rpx;
  256. background: linear-gradient(35deg, #FCB73F 0%, #FC733D 100%);
  257. border-radius: 0rpx 0rpx 0rpx 0rpx;
  258. position: absolute;
  259. left: 0;
  260. top: 0;
  261. }
  262. .card-content {
  263. padding: 0 30rpx;
  264. }
  265. .info-item {
  266. display: flex;
  267. justify-content: space-between;
  268. align-items: center;
  269. margin-top: 20rpx;
  270. font-size: 27rpx;
  271. .info-item-label {
  272. color: #666666;
  273. }
  274. .info-item-value {
  275. color: #151515;
  276. }
  277. }
  278. .info-item-textarea {
  279. width: 100%;
  280. height: 115rpx;
  281. border-radius: 8rpx;
  282. border: 1rpx solid #F2F2F2;
  283. margin-top: 20rpx;
  284. font-size: 23rpx;
  285. color: #AAAAAA;
  286. box-sizing: border-box;
  287. padding: 20rpx;
  288. }
  289. .info-food {
  290. margin-top: 20rpx;
  291. }
  292. .food-item {
  293. display: flex;
  294. align-items: center;
  295. padding: 20rpx 0;
  296. &:last-child {
  297. border-bottom: none;
  298. }
  299. &__image {
  300. width: 118rpx;
  301. height: 118rpx;
  302. border-radius: 8rpx;
  303. flex-shrink: 0;
  304. background-color: #f5f5f5;
  305. }
  306. &__info {
  307. flex: 1;
  308. margin-left: 20rpx;
  309. display: flex;
  310. flex-direction: column;
  311. justify-content: center;
  312. }
  313. &__name {
  314. font-size: 28rpx;
  315. font-weight: bold;
  316. color: #151515;
  317. margin-bottom: 8rpx;
  318. }
  319. &__desc {
  320. font-size: 24rpx;
  321. color: #666666;
  322. line-height: 1.5;
  323. }
  324. &__tag {
  325. font-size: 24rpx;
  326. color: #666666;
  327. }
  328. &__right {
  329. display: flex;
  330. flex-direction: column;
  331. align-items: flex-end;
  332. justify-content: center;
  333. margin-left: 20rpx;
  334. }
  335. &__price {
  336. display: flex;
  337. align-items: baseline;
  338. color: #151515;
  339. font-weight: bold;
  340. margin-bottom: 8rpx;
  341. .price-symbol {
  342. font-size: 20rpx;
  343. margin-right: 2rpx;
  344. }
  345. .price-main {
  346. font-size: 28rpx;
  347. }
  348. .price-decimal {
  349. font-size: 24rpx;
  350. }
  351. }
  352. &__quantity {
  353. font-size: 24rpx;
  354. color: #797979;
  355. }
  356. }
  357. .price-line {
  358. display: flex;
  359. justify-content: space-between;
  360. align-items: center;
  361. margin-top: 20rpx;
  362. font-size: 27rpx;
  363. color: #151515;
  364. border-top: 1rpx solid rgba(170, 170, 170, 0.15);
  365. font-weight: bold;
  366. padding-top: 20rpx;
  367. }
  368. }
  369. .bottom-button {
  370. width: 100%;
  371. background-color: #fff;
  372. padding: 20rpx 30rpx;
  373. box-sizing: border-box;
  374. position: fixed;
  375. bottom: 0;
  376. left: 0;
  377. right: 0;
  378. z-index: 999;
  379. padding-bottom: env(safe-area-inset-bottom);
  380. box-shadow: 0rpx -11rpx 46rpx 0rpx rgba(0, 0, 0, 0.05);
  381. .bottom-button-text {
  382. font-size: 32rpx;
  383. font-weight: bold;
  384. color: #fff;
  385. width: 695rpx;
  386. height: 80rpx;
  387. background: linear-gradient(90deg, #FCB73F 0%, #FC743D 100%);
  388. border-radius: 23rpx 23rpx 23rpx 23rpx;
  389. display: flex;
  390. align-items: center;
  391. justify-content: center;
  392. }
  393. }
  394. </style>