index.vue 15 KB

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