search-record.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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_price || 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">
  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_price}}</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. goods_list: []
  190. }
  191. }
  192. },
  193. onLoad() {
  194. cartStore.setShopId();
  195. this.getList()
  196. },
  197. onShow() {
  198. this.getCartData();
  199. },
  200. mounted() {},
  201. methods: {
  202. scrolltolower() {
  203. if (this.goodsList.length < this.total) {
  204. this.page += 1;
  205. this.getSearch(this.search);
  206. }
  207. },
  208. openBuyCrad() {
  209. this.$refs.popupBuy.open()
  210. },
  211. getCartData() {
  212. // 获取购物车数据
  213. const shopCart = cartStore.getShopCart(this.shop_id);
  214. if (shopCart) {
  215. this.cartData = {
  216. total_price: shopCart.total_price || 0,
  217. total_num: shopCart.total_num || 0,
  218. goods_list: shopCart.goods_list || [],
  219. pack_price: shopCart.pack_price || 0,
  220. total_goods_money: shopCart.total_goods_money || 0
  221. };
  222. // 更新商品列表中的数量显示
  223. this.updateGoodsNumFromCart();
  224. }
  225. },
  226. // 更新商品列表中的数量显示
  227. updateGoodsNumFromCart() {
  228. if (!this.goodsList.length) return;
  229. const shopCart = cartStore.getShopCart(this.shop_id);
  230. if (!shopCart) return;
  231. // 创建商品数量映射
  232. const goodsQuantityMap = {};
  233. shopCart.goods_list.forEach(item => {
  234. if (!goodsQuantityMap[item.goods_id]) {
  235. goodsQuantityMap[item.goods_id] = 0;
  236. }
  237. goodsQuantityMap[item.goods_id] += item.goods_number;
  238. });
  239. // 更新商品列表数量
  240. this.goodsList.forEach(goods => {
  241. goods.num = goodsQuantityMap[goods.goods_id] || 0;
  242. });
  243. },
  244. toList(name) {
  245. let search = '';
  246. if (name) {
  247. search = name;
  248. this.search = name
  249. } else {
  250. search = this.search;
  251. }
  252. this.page = 1
  253. this.goodsList = []
  254. this.getSearch(search)
  255. },
  256. getSearch(search) {
  257. // 搜索商品列表
  258. getSearch({
  259. search: search,
  260. page: this.page
  261. }).then((res) => {
  262. if (res.code * 1 === 1) {
  263. const newGoods = res.data.items || [];
  264. // 为每个商品添加购物车数量
  265. newGoods.forEach(goods => {
  266. goods.num = 0; // 初始化为0
  267. goods.pack_price = parseFloat(goods.pack_price || 0);
  268. });
  269. this.total = res.data.total
  270. this.goodsList = [...this.goodsList, ...res.data.items]
  271. // 更新购物车数量显示
  272. this.updateGoodsNumFromCart();
  273. }
  274. })
  275. },
  276. addred(type, item) {
  277. if (type === 1) {
  278. // 减
  279. if (item.num <= 0) return;
  280. item.num--;
  281. if (item.num <= 0) {
  282. // 从购物车移除
  283. cartStore.removeFromCart(item.goods_id, item.goods_spec || []);
  284. } else {
  285. // 更新数量
  286. cartStore.updateQuantity(item.goods_id, item.goods_spec || [], item.num);
  287. }
  288. } else if (type === 2) {
  289. // 加
  290. item.num++;
  291. // 判断是否已存在购物车
  292. const currentQty = cartStore.getGoodsQuantity(item.goods_id, item.goods_spec || []);
  293. if (currentQty <= 0) {
  294. // 新商品,添加到购物车
  295. cartStore.addToCart(item, item.goods_spec || [], 1);
  296. } else {
  297. // 已存在,更新数量
  298. cartStore.updateQuantity(item.goods_id, item.goods_spec || [], item.num);
  299. }
  300. } else if (type === 3) {
  301. // 选规格
  302. uni.navigateTo({
  303. url: `/pages-takeout/new-specs-info?goods_id=${item.goods_id}&category_id=${item.category_id}`
  304. })
  305. }
  306. // 更新购物车数据
  307. this.getCartData();
  308. },
  309. setShoppingSave(item, type) {
  310. if (type === 1) {
  311. // 加
  312. item.goods_number = item.goods_number + 1
  313. } else {
  314. // 减
  315. item.goods_number = item.goods_number - 1
  316. }
  317. if (item.goods_number <= 0) {
  318. // 数量为0,移除商品
  319. cartStore.removeFromCart(item.goods_id, item.goods_spec || []);
  320. } else {
  321. // 更新商品数量
  322. cartStore.updateQuantity(
  323. item.goods_id,
  324. item.goods_spec || [],
  325. item.goods_number
  326. );
  327. }
  328. // 更新页面数据
  329. this.getShopIngList();
  330. },
  331. getShopIngList() {
  332. // 获取购物车列表
  333. const shopCart = cartStore.getShopCart(this.shop_id);
  334. if (shopCart) {
  335. this.cartData = {
  336. ...this.cartData,
  337. total_price: shopCart.total_price,
  338. total_num: shopCart.total_num,
  339. goods_list: shopCart.goods_list
  340. };
  341. // 同步更新商品列表中的数量
  342. this.updateGoodsNumFromCart();
  343. }
  344. },
  345. clearIcon() {
  346. this.search = '';
  347. this.showClearIcon = false;
  348. },
  349. clearInput(event) {
  350. this.search = event.detail.value;
  351. if (event.detail.value.length > 0) {
  352. this.showClearIcon = true;
  353. } else {
  354. this.showClearIcon = false;
  355. }
  356. },
  357. getList() {
  358. // 获取搜索历史内容
  359. getSearchList().then((res) => {
  360. if (res.code === 1) {
  361. this.list = res.data.my_search;
  362. }
  363. })
  364. },
  365. delList() {
  366. delSearch().then((res) => {
  367. if (res.code === 1) {
  368. tools.success('删除成功');
  369. this.list = [];
  370. }
  371. })
  372. },
  373. },
  374. }
  375. </script>
  376. <style scoped lang="scss">
  377. .buy-card-item {
  378. width: 100%;
  379. margin-bottom: 32rpx;
  380. }
  381. .right-only-right-2 {
  382. min-width: 50rpx;
  383. }
  384. .pop-shop-buttom-money {
  385. width: 100%;
  386. // height: 140rpx;
  387. background: #ECECEC;
  388. padding-left: 24rpx;
  389. padding-right: 24rpx;
  390. padding-bottom: calc(env(safe-area-inset-bottom) + 12rpx);
  391. padding-top: 22rpx;
  392. position: fixed;
  393. left: 0;
  394. bottom: 0;
  395. z-index: 5;
  396. }
  397. .goods_img {
  398. width: 152rpx;
  399. height: 152rpx;
  400. border-radius: 12rpx;
  401. margin-right: 20rpx;
  402. }
  403. .car-scroll-box {
  404. padding: 12rpx 24rpx calc(env(safe-area-inset-bottom) + 48rpx);
  405. }
  406. .buy-card {
  407. height: 60vh;
  408. border-radius: 24rpx 24rpx 0rpx 0rpx;
  409. }
  410. .buy-card-top {
  411. width: 100%;
  412. height: 92rpx;
  413. background: #FCF5DB;
  414. border-radius: 24rpx 24rpx 0rpx 0rpx;
  415. }
  416. .shop-buttom-money-1 {
  417. width: 100%;
  418. height: 96rpx;
  419. position: relative;
  420. }
  421. .qk-icon {
  422. width: 32rpx;
  423. height: 32rpx;
  424. }
  425. .shop-buttom-money {
  426. width: 100%;
  427. // height: 140rpx;
  428. }
  429. .shop-buttom-money-1 {
  430. width: 100%;
  431. height: 96rpx;
  432. position: relative;
  433. }
  434. .shop-buttom-money-2 {
  435. width: 200rpx;
  436. height: 80rpx;
  437. line-height: 80rpx;
  438. background: linear-gradient(270deg, #FF3333 0%, #FE6122 100%);
  439. }
  440. .gwc-img {
  441. width: 88rpx;
  442. height: 88rpx;
  443. }
  444. .shop-buttom-num {
  445. width: 32rpx;
  446. height: 32rpx;
  447. line-height: 32rpx;
  448. border-radius: 50%;
  449. position: absolute;
  450. top: 0rpx;
  451. left: 85rpx;
  452. border: 1rpx solid #FFFFFF;
  453. }
  454. .z-3 {
  455. position: relative;
  456. z-index: 3;
  457. }
  458. .order-bg {
  459. width: 100%;
  460. height: 17vh;
  461. position: fixed;
  462. left: 0;
  463. top: 0;
  464. }
  465. .hide-h {
  466. height: 232rpx !important;
  467. overflow: hidden !important;
  468. }
  469. .down {
  470. transform: rotate(-90deg);
  471. transition: all 0.5s;
  472. }
  473. .up {
  474. transform: rotate(90deg);
  475. transition: all 0.5s;
  476. }
  477. .view-x {
  478. width: 2rpx;
  479. height: 20rpx;
  480. background-color: #eee;
  481. margin: 0 24rpx;
  482. }
  483. .find-item {
  484. width: 48%;
  485. text-align: left;
  486. }
  487. .list-item {
  488. max-height: 688rpx;
  489. min-width: 10rpx;
  490. height: 56rpx;
  491. line-height: 56rpx;
  492. background: #F7F8FC;
  493. border-radius: 200rpx;
  494. margin: 0 8rpx 32rpx 0;
  495. box-sizing: border-box;
  496. padding: 0 24rpx;
  497. }
  498. .seacrh-bg {
  499. position: absolute;
  500. top: 0;
  501. left: 0;
  502. width: 100%;
  503. height: 376rpx;
  504. filter: blur(80rpx);
  505. background-image: linear-gradient(to right, #C9F7FC, #F9F6E0, #F6F5F3);
  506. z-index: -1;
  507. }
  508. .wh36 {
  509. width: 36rpx;
  510. height: 36rpx;
  511. }
  512. .input-box {
  513. width: 100%;
  514. }
  515. .input-buttom {
  516. width: 120rpx;
  517. height: 60rpx;
  518. background: linear-gradient(270deg, #FF3333 0%, #FE6122 100%);
  519. line-height: 60rpx;
  520. }
  521. .s-input {
  522. width: 100%;
  523. height: 72rpx;
  524. padding: 6rpx 6rpx 6rpx 24rpx;
  525. }
  526. .seacrh-info {
  527. margin-top: 20rpx;
  528. border-radius: 24rpx 24rpx 0rpx 0rpx;
  529. }
  530. .seacrh-record-index {
  531. position: relative;
  532. }
  533. .wh-44 {
  534. width: 44rpx;
  535. height: 44rpx;
  536. }
  537. .menu-list {
  538. width: 100%;
  539. .goods_img {
  540. width: 168rpx;
  541. height: 168rpx;
  542. border-radius: 12rpx;
  543. }
  544. }
  545. .menu-list-right {
  546. flex: 1;
  547. }
  548. .right-only-right-red {
  549. width: 44rpx;
  550. height: 44rpx;
  551. line-height: 40rpx;
  552. border-radius: 12rpx;
  553. border: 1rpx solid #41ABF5;
  554. color: #41ABF5;
  555. }
  556. .right-only-right-add {
  557. width: 44rpx;
  558. height: 44rpx;
  559. line-height: 40rpx;
  560. background: linear-gradient(117deg, #57CAFF 0%, #41ABF5 100%);
  561. border-radius: 12rpx;
  562. }
  563. .right-only-right {
  564. width: 116rpx;
  565. height: 44rpx;
  566. background: linear-gradient(270deg, #FF3333 0%, #FE6122 100%);
  567. line-height: 44rpx;
  568. position: relative;
  569. }
  570. .selectedNum {
  571. position: absolute;
  572. right: -10rpx;
  573. top: -22rpx;
  574. font-size: 24rpx;
  575. color: #fff;
  576. width: 32rpx;
  577. height: 32rpx;
  578. line-height: 32rpx;
  579. background: #FE6B2C;
  580. border-radius: 50%;
  581. border: 1rpx solid #FFFFFF;
  582. }
  583. </style>