index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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.tableNumber || 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, PostOrderPay } from "@/api/dining.js";
  106. const payType = ref('confirmOrder'); // confirmOrder 确认下单 confirmPay 确认支付
  107. /** 仅当从订单列表/结果页等「去结算」进入时为 true,底部显示确认支付 */
  108. const fromCheckout = ref(false);
  109. /** 待支付订单ID,确认支付时用于调起微信支付 */
  110. const payOrderId = ref('');
  111. // 订单信息(从购物车带过来或 URL 参数)
  112. const orderInfo = ref({
  113. orderNo: '',
  114. tableId: '', // 桌号ID,接口入参用
  115. tableNumber: '', // 桌号展示(如 2),优先显示
  116. diners: '',
  117. contactPhone: '',
  118. remark: '',
  119. totalAmount: 0,
  120. utensilFee: 0,
  121. discountAmount: 0,
  122. payAmount: 0
  123. });
  124. // 菜品列表(从购物车带过来)
  125. const foodList = ref([]);
  126. // 菜品图片:兼容 cuisineImage/image,逗号分隔取首图
  127. function getItemImage(item) {
  128. const raw = item?.cuisineImage ?? item?.image ?? item?.imageUrl ?? '';
  129. const url = typeof raw === 'string' ? raw.split(',')[0].trim() : raw;
  130. return url ? getFileUrl(url) : '';
  131. }
  132. // 单品单价:列表展示用 unitPrice/price
  133. function getItemPrice(item) {
  134. const p = item?.unitPrice ?? item?.price ?? item?.totalPrice ?? item?.salePrice ?? 0;
  135. return Number(p) || 0;
  136. }
  137. function formatPrice(price) {
  138. const num = Number(price);
  139. return Number.isNaN(num) ? '0.00' : num.toFixed(2);
  140. }
  141. // 将存储的购物车项转为确认页展示格式(兼容 cuisineName/cuisineImage/unitPrice 等)
  142. function normalizeCartItem(item) {
  143. const image = item?.cuisineImage ?? item?.image ?? item?.imageUrl ?? '';
  144. const url = typeof image === 'string' ? image.split(',')[0].trim() : image;
  145. return {
  146. id: item?.id ?? item?.cuisineId,
  147. name: item?.name ?? item?.cuisineName,
  148. cuisineName: item?.cuisineName,
  149. price: item?.unitPrice ?? item?.price,
  150. image: url,
  151. cuisineImage: item?.cuisineImage,
  152. quantity: Number(item?.quantity) || 0,
  153. tags: item?.tags ?? [],
  154. subtotalAmount: item?.subtotalAmount,
  155. remark: item?.remark
  156. };
  157. }
  158. const handleConfirmOrder = async () => {
  159. const tableId = orderInfo.value.tableId;
  160. const contactPhone = orderInfo.value.contactPhone ?? '';
  161. const dinerCount = Number(orderInfo.value.diners) || 0;
  162. if (!tableId) {
  163. uni.showToast({ title: '请先选择桌号', icon: 'none' });
  164. return;
  165. }
  166. if (!contactPhone) {
  167. uni.showToast({ title: '请先填写联系电话', icon: 'none' });
  168. return;
  169. }
  170. const remark = (orderInfo.value.remark ?? '').trim().slice(0, 30);
  171. uni.showLoading({ title: '提交中...' });
  172. try {
  173. const res = await PostOrderCreate({
  174. tableId,
  175. contactPhone,
  176. couponId: null,
  177. dinerCount: dinerCount || undefined,
  178. immediatePay: 0,
  179. remark: remark || undefined
  180. });
  181. uni.hideLoading();
  182. const orderId = res?.id ?? res?.orderId ?? res?.data?.id ?? res?.data?.orderId;
  183. const orderNo = res?.orderNo ?? res?.data?.orderNo ?? orderId ?? '';
  184. const total = orderInfo.value.payAmount ?? orderInfo.value.totalAmount ?? 0;
  185. // 结果页「去结算」需带 orderId/orderNo/金额/备注,用于确认支付
  186. uni.setStorageSync('lastPlaceOrderInfo', JSON.stringify({
  187. orderId: orderId ?? undefined,
  188. orderNo: orderNo ?? undefined,
  189. tableId: orderInfo.value.tableId,
  190. tableNumber: orderInfo.value.tableNumber,
  191. diners: orderInfo.value.diners,
  192. totalAmount: total,
  193. remark: (orderInfo.value.remark ?? '').trim().slice(0, 30)
  194. }));
  195. if (orderId != null) {
  196. go(`/pages/result/index?id=${encodeURIComponent(orderId)}`);
  197. } else {
  198. go('/pages/result/index');
  199. }
  200. } catch (e) {
  201. uni.hideLoading();
  202. uni.showToast({ title: e?.message || '下单失败', icon: 'none' });
  203. }
  204. };
  205. // 确认支付:调起微信支付
  206. const handleConfirmPay = async () => {
  207. const orderId = payOrderId.value || '';
  208. if (!orderId) {
  209. uni.showToast({ title: '缺少订单ID', icon: 'none' });
  210. return;
  211. }
  212. const userStore = useUserStore();
  213. const openid = userStore.getOpenId || '';
  214. if (!openid) {
  215. uni.showToast({ title: '请先登录', icon: 'none' });
  216. return;
  217. }
  218. const priceAmount = Number(orderInfo.value.payAmount ?? orderInfo.value.totalAmount ?? 0) || 0;
  219. if (priceAmount <= 0) {
  220. uni.showToast({ title: '订单金额异常', icon: 'none' });
  221. return;
  222. }
  223. const orderNo = orderInfo.value.orderNo || '';
  224. if (!orderNo) {
  225. uni.showToast({ title: '缺少订单号', icon: 'none' });
  226. return;
  227. }
  228. // 后端要求金额乘 100 传入(分)
  229. const price = Math.round(priceAmount * 100);
  230. uni.showLoading({ title: '拉起支付...' });
  231. try {
  232. const res = await PostOrderPay({
  233. orderNo,
  234. payer: openid,
  235. price,
  236. subject: '订单支付'
  237. });
  238. uni.hideLoading();
  239. console.log(res)
  240. uni.requestPayment({
  241. provider: 'wxpay',
  242. timeStamp:res.timestamp,
  243. nonceStr:res.nonce,
  244. package: res.prepayId,
  245. signType:res.signType,
  246. paySign:res.sign,
  247. success: () => {
  248. uni.showToast({ title: '支付成功', icon: 'success' });
  249. setTimeout(() => go(`/pages/result/index?id=${encodeURIComponent(orderId)}`), 1500);
  250. },
  251. fail: (err) => {
  252. const msg = err?.errMsg ?? err?.message ?? '支付失败';
  253. if (String(msg).includes('cancel')) {
  254. uni.showToast({ title: '已取消支付', icon: 'none' });
  255. } else {
  256. uni.showToast({ title: msg || '支付失败', icon: 'none' });
  257. }
  258. }
  259. });
  260. } catch (e) {
  261. uni.hideLoading();
  262. uni.showToast({ title: e?.message ?? '获取支付参数失败', icon: 'none' });
  263. }
  264. };
  265. onLoad((options) => {
  266. if (options?.payType) payType.value = options.payType;
  267. const userStore = useUserStore();
  268. const contactPhone = userStore.getUserInfo?.phone ?? userStore.getUserInfo?.contactPhone ?? userStore.getUserInfo?.mobile ?? '';
  269. orderInfo.value.contactPhone = contactPhone;
  270. if (options?.orderId != null && options?.orderId !== '') payOrderId.value = options.orderId;
  271. if (options?.orderNo != null && options?.orderNo !== '') orderInfo.value.orderNo = options.orderNo;
  272. if (options?.tableId) orderInfo.value.tableId = options.tableId;
  273. if (options?.tableNumber != null && options?.tableNumber !== '') orderInfo.value.tableNumber = options.tableNumber;
  274. if (options?.diners) orderInfo.value.diners = options.diners;
  275. if (options?.remark != null && options?.remark !== '') orderInfo.value.remark = decodeURIComponent(options.remark);
  276. if (options?.totalAmount != null && options?.totalAmount !== '') {
  277. fromCheckout.value = true;
  278. const total = Number(options.totalAmount) || 0;
  279. orderInfo.value.totalAmount = total;
  280. orderInfo.value.payAmount = total;
  281. }
  282. const raw = uni.getStorageSync('placeOrderCart');
  283. if (raw) {
  284. try {
  285. const data = JSON.parse(raw);
  286. const list = Array.isArray(data.list) ? data.list : [];
  287. foodList.value = list.map(normalizeCartItem).filter((item) => (item.quantity || 0) > 0);
  288. if (data.tableId != null) orderInfo.value.tableId = data.tableId;
  289. if (data.tableNumber != null) orderInfo.value.tableNumber = data.tableNumber;
  290. if (data.diners != null) orderInfo.value.diners = data.diners;
  291. if (data.remark != null) orderInfo.value.remark = data.remark;
  292. const total = Number(data.totalAmount) || 0;
  293. orderInfo.value.totalAmount = total;
  294. orderInfo.value.payAmount = total;
  295. } catch (e) {
  296. console.error('解析购物车数据失败:', e);
  297. }
  298. }
  299. if (orderInfo.value.tableId) uni.setStorageSync('currentTableId', String(orderInfo.value.tableId));
  300. });
  301. </script>
  302. <style lang="scss" scoped>
  303. .content {
  304. padding: 0 30rpx 300rpx;
  305. }
  306. .card {
  307. background-color: #fff;
  308. border-radius: 24rpx;
  309. padding: 30rpx 0;
  310. margin-top: 20rpx;
  311. .card-header {
  312. display: flex;
  313. justify-content: space-between;
  314. align-items: center;
  315. padding: 0 30rpx;
  316. height: 40rpx;
  317. position: relative;
  318. font-size: 27rpx;
  319. color: #151515;
  320. font-weight: bold;
  321. }
  322. .tag {
  323. width: 10rpx;
  324. height: 42rpx;
  325. background: linear-gradient(35deg, #FCB73F 0%, #FC733D 100%);
  326. border-radius: 0rpx 0rpx 0rpx 0rpx;
  327. position: absolute;
  328. left: 0;
  329. top: 0;
  330. }
  331. .card-content {
  332. padding: 0 30rpx;
  333. }
  334. .info-item {
  335. display: flex;
  336. justify-content: space-between;
  337. align-items: center;
  338. margin-top: 20rpx;
  339. font-size: 27rpx;
  340. .info-item-label {
  341. color: #666666;
  342. }
  343. .info-item-value {
  344. color: #151515;
  345. }
  346. }
  347. .info-item-textarea {
  348. width: 100%;
  349. height: 115rpx;
  350. border-radius: 8rpx;
  351. border: 1rpx solid #F2F2F2;
  352. margin-top: 20rpx;
  353. font-size: 23rpx;
  354. color: #AAAAAA;
  355. box-sizing: border-box;
  356. padding: 20rpx;
  357. }
  358. .info-food {
  359. margin-top: 20rpx;
  360. }
  361. .food-item {
  362. display: flex;
  363. align-items: center;
  364. padding: 20rpx 0;
  365. &:last-child {
  366. border-bottom: none;
  367. }
  368. &__image {
  369. width: 118rpx;
  370. height: 118rpx;
  371. border-radius: 8rpx;
  372. flex-shrink: 0;
  373. background-color: #f5f5f5;
  374. }
  375. &__info {
  376. flex: 1;
  377. margin-left: 20rpx;
  378. display: flex;
  379. flex-direction: column;
  380. justify-content: center;
  381. }
  382. &__name {
  383. font-size: 28rpx;
  384. font-weight: bold;
  385. color: #151515;
  386. margin-bottom: 8rpx;
  387. }
  388. &__desc {
  389. font-size: 24rpx;
  390. color: #666666;
  391. line-height: 1.5;
  392. }
  393. &__tag {
  394. font-size: 24rpx;
  395. color: #666666;
  396. }
  397. &__right {
  398. display: flex;
  399. flex-direction: column;
  400. align-items: flex-end;
  401. justify-content: center;
  402. margin-left: 20rpx;
  403. }
  404. &__price {
  405. display: flex;
  406. align-items: baseline;
  407. color: #151515;
  408. font-weight: bold;
  409. margin-bottom: 8rpx;
  410. .price-symbol {
  411. font-size: 20rpx;
  412. margin-right: 2rpx;
  413. }
  414. .price-main {
  415. font-size: 28rpx;
  416. }
  417. .price-decimal {
  418. font-size: 24rpx;
  419. }
  420. }
  421. &__quantity {
  422. font-size: 24rpx;
  423. color: #797979;
  424. }
  425. }
  426. .price-line {
  427. display: flex;
  428. justify-content: space-between;
  429. align-items: center;
  430. margin-top: 20rpx;
  431. font-size: 27rpx;
  432. color: #151515;
  433. border-top: 1rpx solid rgba(170, 170, 170, 0.15);
  434. font-weight: bold;
  435. padding-top: 20rpx;
  436. }
  437. }
  438. .bottom-button {
  439. width: 100%;
  440. background-color: #fff;
  441. padding: 20rpx 30rpx;
  442. box-sizing: border-box;
  443. position: fixed;
  444. bottom: 0;
  445. left: 0;
  446. right: 0;
  447. z-index: 999;
  448. padding-bottom: env(safe-area-inset-bottom);
  449. box-shadow: 0rpx -11rpx 46rpx 0rpx rgba(0, 0, 0, 0.05);
  450. .bottom-button-text {
  451. font-size: 32rpx;
  452. font-weight: bold;
  453. color: #fff;
  454. width: 695rpx;
  455. height: 80rpx;
  456. background: linear-gradient(90deg, #FCB73F 0%, #FC743D 100%);
  457. border-radius: 23rpx 23rpx 23rpx 23rpx;
  458. display: flex;
  459. align-items: center;
  460. justify-content: center;
  461. }
  462. }
  463. </style>