search-record.vue 18 KB

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