search-record.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. <template>
  2. <view class="flex-common-css iPhone-bottom box-border-box bgc-f">
  3. <image class="order-bg" src="@/static/img/order-food/ss-bg.png" mode=""></image>
  4. <newNav textName="搜索商品" class="mb-20" titleColor='#333' leftIconColor='#333'></newNav>
  5. <view class="plr-24 box-border-box">
  6. <view class="s-input box-border-box bgc-f align-items-center flex-justify-space b-rad-200 z-3">
  7. <view class="align-items-center flex-1 mr-16">
  8. <image class="wh36 mr-16 flex-shrink0" src="/static/img/order-food/ss-icon.png" mode=""></image>
  9. <input class="input-box fs-32" placeholder-class="fc-9" v-model="search" type="text"
  10. placeholder="请搜索商品" @input="clearInput">
  11. </view>
  12. <view class="align-items-center">
  13. <image v-if="showClearIcon" @click="clearIcon" class="qk-icon mr-24"
  14. src="@/static/img/order-food/qk-icon.png" mode="">
  15. </image>
  16. <view class="input-buttom fs-28 fc-f fw-500 b-rad-200 text-align-center" @click="toList()">搜索</view>
  17. </view>
  18. </view>
  19. </view>
  20. <view class="scroll-view-css bgc-f seacrh-info box-border-box p-32" v-if="goodsList.length<=0">
  21. <view class="flex-justify-space mb-32">
  22. <view class="fs-32 fw-600 fc-3">搜索历史</view>
  23. <view class="align-items-center" v-if="list.length > 0">
  24. <view @click="isOpen=!isOpen" class="">{{!isOpen?'收起':'展开'}}</view>
  25. <view @click="isOpen=!isOpen" :class="!isOpen?'down':'up'">
  26. <uni-icons type="right" size="14" color="#999"></uni-icons>
  27. </view>
  28. <view class="view-x"></view>
  29. <uni-icons @click="delList()" type="trash" size="18" color="#999"></uni-icons>
  30. </view>
  31. </view>
  32. <view id='' :class="isOpen?'hide-h':''" class="flex-wrap-wrap mb-50 search-history" v-if="list.length > 0">
  33. <view class="fs-28 list-item text-overflow-ellipsis-one" @click="toList(item)" v-for="(item,i) in list"
  34. :key="i">{{item}}</view>
  35. </view>
  36. </view>
  37. <scroll-view scroll-y="true" class="scroll-view-css bgc-F5F4F9 p-32 box-border-box" v-else
  38. @scrolltolower="scrolltolower">
  39. <!-- <view class="scroll-view-css bgc-F5F4F9 p-32 box-border-box" v-else> -->
  40. <view class="menu-list flex-justify-space bgc-f p-24 box-border-box b-rad-20 mb-16"
  41. v-for="(info,k) in goodsList" :key="k" :id="'goods_id'+ info.goods_id">
  42. <image class="flex-shrink0 goods_img mr-20" :src="info.goods_image" mode="scaleToFill"></image>
  43. <view class="menu-list-right flex-direction flex-justify-space">
  44. <view class="mb-8">
  45. <view class="mb-4 goods-name text-overflow-ellipsis fs-32 fw-600">
  46. {{info.goods_name}}
  47. </view>
  48. <view class="text-overflow-ellipsis-one fw-400 fc-9 fs-24 mb-2 goods-name"
  49. v-if="info.spec_type == 1">
  50. {{info.goods_desc?info.goods_desc:''}}
  51. </view>
  52. </view>
  53. <view class="flex-justify-space align-items-flex-end">
  54. <view class="">
  55. <span class="fs-24 fw-400 fc-3">¥</span>
  56. <span class="fs-36 fw-b fc-3">{{info.seckill_money>0?info.seckill_money:info.price}}</span>
  57. <span class="fs-24 fc-9">/份起</span>
  58. </view>
  59. <view class="display-flex align-items-center" v-if="info.spec_type == 1">
  60. <image v-show="info.num > 0" class="wh-44" @click="addred(1,info)"
  61. src="@/static/img/order-food/reduce-img.png" mode=""></image>
  62. <view v-show="info.num > 0" class="right-only-right-2 text-align-center fs-28 fw-400">
  63. {{info.num}}
  64. </view>
  65. <image class="wh-44" src="@/static/img/order-food/add-img.png" @click="addred(2,info)"
  66. mode=""></image>
  67. </view>
  68. <view v-else class="right-only-right text-align-center b-rad-200" @click="addred(3,info)">
  69. <view class="fw-500 fs-28 fc-f">
  70. 选规格
  71. </view>
  72. <view class="selectedNum" v-if="info.num>0">
  73. {{info.num}}
  74. </view>
  75. </view>
  76. </view>
  77. </view>
  78. </view>
  79. <!-- </view> -->
  80. </scroll-view>
  81. <view v-if="goodsList.length>0" class="shop-buttom-money p-24 box-border-box bgc-F5F4F9" @click="openBuyCrad()">
  82. <view
  83. class="shop-buttom-money-1 bgc-f flex-justify-space align-items-center pl-24 box-border-box pr-8 b-rad-200">
  84. <view class="align-items-center">
  85. <image class="gwc-img mr-36" src="@/static/img/order-food/gwc-img.png" mode="scaleToFill"></image>
  86. <view class="shop-buttom-num bgc-FE6B2C fs-24 fc-f text-align-center">{{cartData.total_num || 0}}
  87. </view>
  88. <view class="align-items-baseline">
  89. <view class="fw-600 fs-28 fc-3 mr-4">¥</view>
  90. <view class="fw-600 fs-48 fc-3">{{cartData.total_goods_money || 0}}</view>
  91. </view>
  92. </view>
  93. <view class="shop-buttom-money-2 text-align-center fc-f fs-32 fw-600 b-rad-200"
  94. :class="cartData.total_num>0?'':'opacity-4'" @click.stop="toConfirmOrder()">去结算
  95. </view>
  96. </view>
  97. </view>
  98. <uni-popup ref="popupBuy" type="bottom" background-color="transparent" borderRadius='20px 20rpx 0 0'>
  99. <view class="buy-card flex-index-css">
  100. <view class="buy-card-top box-border-box plr-32 flex-justify-space align-items-center b-rad-20-20-0-0">
  101. <view class="">
  102. <span class="fs-32 fc-3 mr-12">已选商品</span>
  103. </view>
  104. <view class="fs-28 fw-400 fc-9 align-items-center" @click.stop="setShopIngDel">
  105. <view class="">
  106. <uni-icons type="trash" size="18" color="#999"></uni-icons>
  107. </view>
  108. <view class="fs-28 fw-400 fc-9">清空</view>
  109. </view>
  110. </view>
  111. <view class="scroll-view-css bgc-ECECEC box-border-box car-scroll-box">
  112. <view class="buy-card-item bgc-f b-rad-20 display-flex p-20 box-border-box"
  113. v-for="(goods,i) in cartData.goods_list" :key="i">
  114. <image class="goods_img" :src="goods.goods_image" mode="scaleToFill"></image>
  115. <view class="flex-1 flex-direction flex-justify-space">
  116. <view class="">
  117. <view class="w-514 text-overflow-ellipsis-one fs-34 fw-600 mb-8">
  118. {{goods.goods_name}}
  119. </view>
  120. <view class="fc-6" v-show="goods.goods_attr">
  121. {{goods.goods_attr}}
  122. </view>
  123. </view>
  124. <view class="flex-justify-space">
  125. <view class="">
  126. <span class="fs-24 fc-FB3D03 fw-400">¥</span>
  127. <span class="fs-40 fc-FB3D03 fw-b">{{goods.price}}</span>
  128. </view>
  129. <view class="display-flex align-items-center">
  130. <image class="wh-44" @click.stop="setShoppingSave(goods,2)"
  131. src="@/static/img/order-food/reduce-img.png" mode="">
  132. </image>
  133. <view class="right-only-right-2 text-align-center fs-28 fw-400">
  134. {{goods.goods_number}}
  135. </view>
  136. <image class="wh-44" src="/static/img/order-food/add-img.png"
  137. @click.stop="setShoppingSave(goods,1)" mode=""></image>
  138. </view>
  139. </view>
  140. </view>
  141. </view>
  142. <en-blank navBackgroundColor='#ECECEC' v-if="cartData.goods_list.length<=0"
  143. message="快去选购商品吧!~"></en-blank>
  144. </view>
  145. <view class="pop-shop-buttom-money box-border-box" @click="openBuyCrad()">
  146. <view
  147. class="shop-buttom-money-1 bgc-f flex-justify-space align-items-center pl-24 box-border-box pr-8 b-rad-200">
  148. <view class="align-items-center">
  149. <image class="gwc-img mr-36" src="@/static/img/order-food/gwc-img.png" mode="scaleToFill">
  150. </image>
  151. <view class="shop-buttom-num bgc-FE6B2C fs-24 fc-f text-align-center">{{cartData.total_num}}
  152. </view>
  153. <view class="align-items-baseline">
  154. <view class="fw-600 fs-28 fc-3 mr-4">¥</view>
  155. <view class="fw-600 fs-48 fc-3">{{cartData.total_goods_money}}</view>
  156. </view>
  157. </view>
  158. <view class="shop-buttom-money-2 text-align-center fc-f fs-32 fw-600 b-rad-200"
  159. :class="cartData.total_num>0?'':'opacity-4'" @click.stop="toConfirmOrder()">去结算
  160. </view>
  161. </view>
  162. </view>
  163. </view>
  164. </uni-popup>
  165. </view>
  166. </template>
  167. <script>
  168. import cartStore from '@/utils/cartStore.js';
  169. import tools from "@/service/tools";
  170. import {
  171. getSearch,
  172. getSearchList,
  173. delSearch
  174. } from '@/api/goods.js';
  175. export default {
  176. data() {
  177. return {
  178. search: '',
  179. isOpen: false,
  180. showClearIcon: false,
  181. list: [],
  182. goodsList: [],
  183. historyType: false,
  184. total: '',
  185. page: 1,
  186. cartData: { // 添加购物车数据
  187. total_price: 0,
  188. total_num: 0,
  189. pack_price: 0,
  190. total_goods_money: 0,
  191. goods_list: []
  192. }
  193. }
  194. },
  195. onLoad() {
  196. cartStore.setShopId();
  197. this.getList()
  198. },
  199. onShow() {
  200. this.getCartData();
  201. },
  202. mounted() {},
  203. methods: {
  204. toConfirmOrder() {
  205. uni.navigateTo({
  206. url: '/pages-takeout/goods-order'
  207. })
  208. },
  209. scrolltolower() {
  210. if (this.goodsList.length < this.total) {
  211. this.page += 1;
  212. this.getSearch(this.search);
  213. }
  214. },
  215. openBuyCrad() {
  216. this.$refs.popupBuy.open()
  217. },
  218. getCartData() {
  219. // 获取购物车数据
  220. const shopCart = cartStore.getShopCart(this.shop_id);
  221. if (shopCart) {
  222. this.cartData = {
  223. ...this.cartData,
  224. total_price: shopCart.total_price || 0,
  225. total_num: shopCart.total_num || 0,
  226. goods_list: shopCart.goods_list || [],
  227. pack_price: shopCart.pack_price || 0,
  228. total_goods_money: shopCart.total_goods_money || 0
  229. };
  230. // 更新商品列表中的数量显示
  231. this.updateGoodsNumFromCart();
  232. }
  233. },
  234. // 更新商品列表中的数量显示
  235. updateGoodsNumFromCart() {
  236. if (!this.goodsList.length) return;
  237. const shopCart = cartStore.getShopCart(this.shop_id);
  238. if (!shopCart) return;
  239. // 创建商品数量映射
  240. const goodsQuantityMap = {};
  241. shopCart.goods_list.forEach(item => {
  242. if (!goodsQuantityMap[item.goods_id]) {
  243. goodsQuantityMap[item.goods_id] = 0;
  244. }
  245. goodsQuantityMap[item.goods_id] += item.goods_number;
  246. });
  247. // 更新商品列表数量
  248. this.goodsList.forEach(goods => {
  249. goods.num = goodsQuantityMap[goods.goods_id] || 0;
  250. });
  251. },
  252. toList(name) {
  253. let search = '';
  254. if (name) {
  255. search = name;
  256. this.search = name
  257. } else {
  258. search = this.search;
  259. }
  260. this.page = 1
  261. this.goodsList = []
  262. this.getSearch(search)
  263. },
  264. getSearch(search) {
  265. // 搜索商品列表
  266. getSearch({
  267. search: search,
  268. page: this.page
  269. }).then((res) => {
  270. if (res.code * 1 === 1) {
  271. const newGoods = res.data.items || [];
  272. // 为每个商品添加购物车数量
  273. newGoods.forEach(goods => {
  274. goods.num = 0; // 初始化为0
  275. goods.pack_price = parseFloat(goods.pack_price || 0);
  276. });
  277. this.total = res.data.total
  278. this.goodsList = [...this.goodsList, ...res.data.items]
  279. // 更新购物车数量显示
  280. this.updateGoodsNumFromCart();
  281. }
  282. })
  283. },
  284. addred(type, item) {
  285. if (type === 1) {
  286. // 减
  287. if (item.num <= 0) return;
  288. item.num--;
  289. if (item.num <= 0) {
  290. // 从购物车移除
  291. cartStore.removeFromCart(item.goods_id, item.goods_spec || []);
  292. } else {
  293. // 更新数量
  294. cartStore.updateQuantity(item.goods_id, item.goods_spec || [], item.num);
  295. }
  296. } else if (type === 2) {
  297. // 加
  298. item.num++;
  299. // 判断是否已存在购物车
  300. const currentQty = cartStore.getGoodsQuantity(item.goods_id, item.goods_spec || []);
  301. if (currentQty <= 0) {
  302. // 新商品,添加到购物车
  303. cartStore.addToCart(item, item.goods_spec || [], 1);
  304. } else {
  305. // 已存在,更新数量
  306. cartStore.updateQuantity(item.goods_id, item.goods_spec || [], item.num);
  307. }
  308. } else if (type === 3) {
  309. // 选规格
  310. uni.navigateTo({
  311. url: `/pages-takeout/new-specs-info?goods_id=${item.goods_id}&category_id=${item.category_id}`
  312. })
  313. }
  314. // 更新购物车数据
  315. this.getCartData();
  316. },
  317. setShoppingSave(item, type) {
  318. if (type === 1) {
  319. // 加
  320. item.goods_number = item.goods_number + 1
  321. } else {
  322. // 减
  323. item.goods_number = item.goods_number - 1
  324. }
  325. if (item.goods_number <= 0) {
  326. // 数量为0,移除商品
  327. cartStore.removeFromCart(item.goods_id, item.goods_spec || []);
  328. } else {
  329. // 更新商品数量
  330. cartStore.updateQuantity(
  331. item.goods_id,
  332. item.goods_spec || [],
  333. item.goods_number
  334. );
  335. }
  336. // 更新页面数据
  337. this.getShopIngList();
  338. },
  339. getShopIngList() {
  340. // 获取购物车列表
  341. const shopCart = cartStore.getShopCart(this.shop_id);
  342. if (shopCart) {
  343. this.cartData = {
  344. ...this.cartData,
  345. total_price: shopCart.total_price,
  346. total_num: shopCart.total_num,
  347. goods_list: shopCart.goods_list
  348. };
  349. // 同步更新商品列表中的数量
  350. this.updateGoodsNumFromCart();
  351. }
  352. },
  353. clearIcon() {
  354. this.search = '';
  355. this.showClearIcon = false;
  356. },
  357. clearInput(event) {
  358. this.search = event.detail.value;
  359. if (event.detail.value.length > 0) {
  360. this.showClearIcon = true;
  361. } else {
  362. this.showClearIcon = false;
  363. }
  364. },
  365. getList() {
  366. // 获取搜索历史内容
  367. getSearchList().then((res) => {
  368. if (res.code === 1) {
  369. this.list = res.data.my_search;
  370. }
  371. })
  372. },
  373. delList() {
  374. delSearch().then((res) => {
  375. if (res.code === 1) {
  376. tools.success('删除成功');
  377. this.list = [];
  378. }
  379. })
  380. },
  381. },
  382. }
  383. </script>
  384. <style scoped lang="scss">
  385. .buy-card-item {
  386. width: 100%;
  387. margin-bottom: 32rpx;
  388. }
  389. .right-only-right-2 {
  390. min-width: 50rpx;
  391. }
  392. .pop-shop-buttom-money {
  393. width: 100%;
  394. // height: 140rpx;
  395. background: #ECECEC;
  396. padding-left: 24rpx;
  397. padding-right: 24rpx;
  398. padding-bottom: calc(env(safe-area-inset-bottom) + 12rpx);
  399. padding-top: 22rpx;
  400. position: fixed;
  401. left: 0;
  402. bottom: 0;
  403. z-index: 5;
  404. }
  405. .goods_img {
  406. width: 152rpx;
  407. height: 152rpx;
  408. border-radius: 12rpx;
  409. margin-right: 20rpx;
  410. }
  411. .car-scroll-box {
  412. padding: 12rpx 24rpx calc(env(safe-area-inset-bottom) + 48rpx);
  413. }
  414. .buy-card {
  415. height: 60vh;
  416. border-radius: 24rpx 24rpx 0rpx 0rpx;
  417. }
  418. .buy-card-top {
  419. width: 100%;
  420. height: 92rpx;
  421. background: #FCF5DB;
  422. border-radius: 24rpx 24rpx 0rpx 0rpx;
  423. }
  424. .shop-buttom-money-1 {
  425. width: 100%;
  426. height: 96rpx;
  427. position: relative;
  428. }
  429. .qk-icon {
  430. width: 32rpx;
  431. height: 32rpx;
  432. }
  433. .shop-buttom-money {
  434. width: 100%;
  435. // height: 140rpx;
  436. }
  437. .shop-buttom-money-1 {
  438. width: 100%;
  439. height: 96rpx;
  440. position: relative;
  441. }
  442. .shop-buttom-money-2 {
  443. width: 200rpx;
  444. height: 80rpx;
  445. line-height: 80rpx;
  446. background: linear-gradient(270deg, #FF3333 0%, #FE6122 100%);
  447. }
  448. .gwc-img {
  449. width: 88rpx;
  450. height: 88rpx;
  451. }
  452. .shop-buttom-num {
  453. width: 32rpx;
  454. height: 32rpx;
  455. line-height: 32rpx;
  456. border-radius: 50%;
  457. position: absolute;
  458. top: 0rpx;
  459. left: 85rpx;
  460. border: 1rpx solid #FFFFFF;
  461. }
  462. .z-3 {
  463. position: relative;
  464. z-index: 3;
  465. }
  466. .order-bg {
  467. width: 100%;
  468. height: 17vh;
  469. position: fixed;
  470. left: 0;
  471. top: 0;
  472. }
  473. .hide-h {
  474. height: 232rpx !important;
  475. overflow: hidden !important;
  476. }
  477. .down {
  478. transform: rotate(-90deg);
  479. transition: all 0.5s;
  480. }
  481. .up {
  482. transform: rotate(90deg);
  483. transition: all 0.5s;
  484. }
  485. .view-x {
  486. width: 2rpx;
  487. height: 20rpx;
  488. background-color: #eee;
  489. margin: 0 24rpx;
  490. }
  491. .find-item {
  492. width: 48%;
  493. text-align: left;
  494. }
  495. .list-item {
  496. max-height: 688rpx;
  497. min-width: 10rpx;
  498. height: 56rpx;
  499. line-height: 56rpx;
  500. background: #F7F8FC;
  501. border-radius: 200rpx;
  502. margin: 0 8rpx 32rpx 0;
  503. box-sizing: border-box;
  504. padding: 0 24rpx;
  505. }
  506. .seacrh-bg {
  507. position: absolute;
  508. top: 0;
  509. left: 0;
  510. width: 100%;
  511. height: 376rpx;
  512. filter: blur(80rpx);
  513. background-image: linear-gradient(to right, #C9F7FC, #F9F6E0, #F6F5F3);
  514. z-index: -1;
  515. }
  516. .wh36 {
  517. width: 36rpx;
  518. height: 36rpx;
  519. }
  520. .input-box {
  521. width: 100%;
  522. }
  523. .input-buttom {
  524. width: 120rpx;
  525. height: 60rpx;
  526. background: linear-gradient(270deg, #FF3333 0%, #FE6122 100%);
  527. line-height: 60rpx;
  528. }
  529. .s-input {
  530. width: 100%;
  531. height: 72rpx;
  532. padding: 6rpx 6rpx 6rpx 24rpx;
  533. }
  534. .seacrh-info {
  535. margin-top: 20rpx;
  536. border-radius: 24rpx 24rpx 0rpx 0rpx;
  537. }
  538. .seacrh-record-index {
  539. position: relative;
  540. }
  541. .wh-44 {
  542. width: 44rpx;
  543. height: 44rpx;
  544. }
  545. .menu-list {
  546. width: 100%;
  547. .goods_img {
  548. width: 168rpx;
  549. height: 168rpx;
  550. border-radius: 12rpx;
  551. }
  552. }
  553. .menu-list-right {
  554. flex: 1;
  555. }
  556. .right-only-right-red {
  557. width: 44rpx;
  558. height: 44rpx;
  559. line-height: 40rpx;
  560. border-radius: 12rpx;
  561. border: 1rpx solid #41ABF5;
  562. color: #41ABF5;
  563. }
  564. .right-only-right-add {
  565. width: 44rpx;
  566. height: 44rpx;
  567. line-height: 40rpx;
  568. background: linear-gradient(117deg, #57CAFF 0%, #41ABF5 100%);
  569. border-radius: 12rpx;
  570. }
  571. .right-only-right {
  572. width: 116rpx;
  573. height: 44rpx;
  574. background: linear-gradient(270deg, #FF3333 0%, #FE6122 100%);
  575. line-height: 44rpx;
  576. position: relative;
  577. }
  578. .selectedNum {
  579. position: absolute;
  580. right: -10rpx;
  581. top: -22rpx;
  582. font-size: 24rpx;
  583. color: #fff;
  584. width: 32rpx;
  585. height: 32rpx;
  586. line-height: 32rpx;
  587. background: #FE6B2C;
  588. border-radius: 50%;
  589. border: 1rpx solid #FFFFFF;
  590. }
  591. </style>