classify.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <template>
  2. <view class="w-full">
  3. <view>
  4. <u-row justify="space-between" customStyle="margin-bottom: 10px">
  5. <u-col span="3">
  6. <view class="icon-container-l">
  7. <uni-badge size="normal" :text="txts" absolute="rightTop">
  8. <u-icon :name="iconCooking" label="自提" labelPos="bottom" size="32"></u-icon>
  9. </uni-badge>
  10. </view>
  11. </u-col>
  12. <view class="container">
  13. <u-tag plain :text="tags ? '营业中' : '离线'" :type="tags ? 'success' : 'error'" size="mini"></u-tag>
  14. <view @click="btnIc">
  15. <u-icon label="当前餐车名称" labelPos="left" size="16" name="arrow-down"></u-icon>
  16. </view>
  17. </view>
  18. <u-col span="3">
  19. <view class="icon-container-r">
  20. <u-icon label="我的" labelPos="bottom" size="32" :name="iconPeople"></u-icon>
  21. </view>
  22. </u-col>
  23. </u-row>
  24. </view>
  25. <!-- <top-search :isSearch="false" @searchClick="searchClick"></top-search>
  26. <view class="w-full padding-lr-sm box-sizing-b dflex-b">
  27. <scroll-view class="goods-category" scroll-x>
  28. <view class="category-box">
  29. <view class="category-item padding-lr-lg" v-for="item in 8" :key="item" :class="{'on-category':onIndex === item}">分类{{item+1}}</view>
  30. </view>
  31. </scroll-view>
  32. <view class="classify-icon">
  33. <u-icon name="grid-fill" size="30" @click="show = true" />
  34. </view>
  35. </view>
  36. <u-popup :show="show" mode="left" @close="show = false">
  37. <view class="classify-popup padding-lr-lg padding-tb box-sizing-b">
  38. <view class="padding-tb-sm">全部</view>
  39. <view class="padding-tb-sm" v-for="index in 8">分类{{index+1}}</view>
  40. </view>
  41. </u-popup> -->
  42. <view class="w-full dflex-b padding-xs box-sizing-b dflex-wrap-w">
  43. <view class="goods-item margin-bottom-sm" v-for="(item, gindex) in goods" :key="gindex">
  44. <view class="img dflex-c" @click="toDetial">
  45. <image mode="aspectFit" :src="'../../static/x'+ gindex%2 +'.jpg'"></image>
  46. </view>
  47. <view class="title">{{item.goods_name}}</view>
  48. <view class="dflex padding-bottom-xs">
  49. <u-tag plain text="销量: 199" borderColor="#FF6D20" color="#FF6D20" size="mini"></u-tag>
  50. </view>
  51. <view class="w-full dflex-b">
  52. <view class="price">{{item.goods_price}}</view>
  53. <view>
  54. <u-button type="primary" shape="circle" size="small"
  55. color="linear-gradient(to right, #F54319, #FF6D20)" @click="goCart(item)">
  56. <u-icon name="shopping-cart-fill" size="28" color="#fff"></u-icon>
  57. </u-button>
  58. </view>
  59. </view>
  60. </view>
  61. </view>
  62. <my-gap :height="130" />
  63. <specPopup class='spec-box' ref="specPopup" :cardMsg="cardMsg" @update-shopmsg="handleUpdateShopMsg">
  64. </specPopup>
  65. <view class="cart-bottom padding-sm dflex-b">
  66. <view class="cart padding-lr">
  67. <uni-badge size="small" :text="100" absolute="rightTop">
  68. <u-icon name="shopping-cart-fill" size="28" color="#FF873D" @click="showCart"></u-icon>
  69. </uni-badge>
  70. <text class="cart-total">总计:¥{{total}}</text>
  71. </view>
  72. <view class="balance dflex-c background-gradient">去结算</view>
  73. </view>
  74. <cartPopupVue ref="cartPopup" :carList="carList" @numChanged="numChanged" />
  75. </view>
  76. </template>
  77. <script>
  78. import cartPopupVue from '../../components/cartPopup.vue'
  79. import specPopup from '@/components/specPopup.vue'
  80. import iconCooking from '../../static/cooking.png'
  81. import iconPeople from '../../static/people.png'
  82. import shoppingCart from '../../static/shoppingCart.png'
  83. export default {
  84. components: {
  85. cartPopupVue,
  86. specPopup
  87. },
  88. data() {
  89. return {
  90. onIndex: 0,
  91. show: false,
  92. iconCooking: iconCooking,
  93. iconPeople: iconPeople,
  94. shoppingCart: shoppingCart,
  95. tags: 1,
  96. txts: 1,
  97. goods: [ // 购物车数据列表
  98. {
  99. id: 1,
  100. goods_name: "小乳酸菌牛奶酸奶",
  101. goods_price: "5.20",
  102. goods_type: '大份',
  103. goods_num: 1,
  104. src: '../../static/x0.jpg',
  105. },
  106. {
  107. id: 2,
  108. goods_name: "饮料整箱饮品",
  109. goods_price: "5.20",
  110. goods_type: '大份',
  111. goods_num: 1,
  112. src: '../../static/x1.jpg',
  113. },
  114. {
  115. id: 3,
  116. goods_name: "饮品早餐酸乳益生菌",
  117. goods_price: "5.20",
  118. goods_type: '小份',
  119. goods_num: 1,
  120. src: '../../static/x0.jpg',
  121. },
  122. {
  123. id: 4,
  124. goods_name: "酸奶饮料",
  125. goods_price: "5.20",
  126. goods_type: '小份',
  127. goods_num: 1,
  128. src: '../../static/x1.jpg',
  129. },
  130. {
  131. id: 5,
  132. goods_name: "小乳酸菌",
  133. goods_price: "5.20",
  134. goods_type: '小份',
  135. goods_num: 1,
  136. src: '../../static/x0.jpg',
  137. },
  138. {
  139. id: 6,
  140. goods_name: "酸乳益生菌",
  141. goods_price: "5.20",
  142. goods_type: '大份',
  143. goods_num: 1,
  144. src: '../../static/x1.jpg',
  145. }
  146. ],
  147. total: 0,
  148. carList: [],
  149. cardMsg: {}
  150. }
  151. },
  152. created() {
  153. },
  154. methods: {
  155. // 加购
  156. goCart(e) {
  157. console.log('================', e)
  158. this.cardMsg = e
  159. this.$refs.specPopup.open();
  160. //this.$refs.specPopup.change(true);
  161. },
  162. handleUpdateShopMsg(shopMsg) {
  163. console.log('接收到的购物信息:', shopMsg);
  164. const specString = shopMsg.spec + ',' + shopMsg.specs;
  165. const item = this.goods.find(item => item.id === shopMsg.id);
  166. if (item) {
  167. // 检查购物车列表中是否已存在相同id和规格的商品
  168. const existingCartItem = this.carList.find(cartItem => cartItem.id === item.id && cartItem.goods_type === specString);
  169. if (existingCartItem) {
  170. // 如果已存在,则增加商品数量
  171. existingCartItem.goods_num += shopMsg.num;
  172. } else {
  173. // 如果不存在,则复制item并添加到购物车列表,同时设置商品数量和规格
  174. const newItem = { ...item };
  175. newItem.goods_num = shopMsg.num;
  176. newItem.goods_type = specString;
  177. this.carList.push(newItem);
  178. }
  179. // 您可以在这里执行其他逻辑,比如保存到本地存储或发送到服务器
  180. }
  181. console.log('购物车列表更新:', this.carList);
  182. this.totalPrice();
  183. },
  184. numChanged(value, index) {
  185. console.log('更新后的数量????????????:', value, '索引:', index);
  186. // 假设 carList 是一个 Map,其中 id 是键
  187. const item = this.carList.find(item => item.id === index)
  188. if (item) {
  189. // 更新对应商品的 goods_num
  190. item.goods_num = value;
  191. // 您可以在这里执行其他逻辑,比如保存到本地存储或发送到服务器
  192. console.log('更新后的数量:', value, '索引:', index);
  193. }
  194. this.totalPrice()
  195. },
  196. totalPrice() {
  197. let total = 0;
  198. for (let i = 0; i < this.carList.length; i++) {
  199. total += this.carList[i].goods_price * this.carList[i].goods_num;
  200. //goods_num += this.carList[i].goods_num
  201. }
  202. this.total = total
  203. console.log('total', this.total);
  204. },
  205. showCart() {
  206. this.$refs.cartPopup.setShow(true);
  207. },
  208. btnIc() {
  209. uni.navigateTo({
  210. url: `/pages/diningList/diningList`
  211. })
  212. },
  213. toDetial() {
  214. uni.navigateTo({
  215. url: `/pages/goodDetail/goodDetail`
  216. })
  217. }
  218. }
  219. }
  220. </script>
  221. <style scoped lang="scss">
  222. .goods-category {
  223. width: calc(100% - 100rpx);
  224. border-radius: 14rpx;
  225. overflow: hidden;
  226. .category-box {
  227. height: 70rpx;
  228. width: 100%;
  229. display: flex;
  230. align-items: center;
  231. .category-item {
  232. font-size: 14px;
  233. flex-shrink: 0;
  234. height: 100%;
  235. line-height: 70rpx;
  236. }
  237. .on-category {
  238. color: #FFF;
  239. background-color: #000;
  240. }
  241. }
  242. .classify-icon {
  243. width: 100rpx;
  244. display: flex;
  245. align-items: center;
  246. justify-content: flex-end;
  247. }
  248. }
  249. .goods-item {
  250. width: 48%;
  251. overflow: hidden;
  252. box-sizing: border-box;
  253. border: solid 4px #FFF;
  254. border-radius: 5px;
  255. box-shadow: 0px 3px 10px 0px rgba(213, 221, 232, 0.5);
  256. .img {
  257. width: 100%;
  258. height: 180rpx;
  259. overflow: hidden;
  260. background: #EEE;
  261. }
  262. .title {
  263. width: 100%;
  264. overflow: hidden;
  265. white-space: nowrap;
  266. text-overflow: ellipsis;
  267. padding: 10rpx;
  268. }
  269. }
  270. .cart-bottom {
  271. width: 100%;
  272. position: fixed;
  273. left: 0;
  274. bottom: 0;
  275. height: 130rpx;
  276. box-sizing: border-box;
  277. background-color: #FFF;
  278. box-shadow: 0px -4px 16px 0px rgba(0, 0, 0, 0.15);
  279. z-index: 10800;
  280. .cart {
  281. width: calc(100% - 220rpx);
  282. border-radius: 5px;
  283. background-color: #FEEEE4;
  284. display: flex;
  285. align-items: center;
  286. height: 100%;
  287. .cart-total {
  288. padding-left: 40rpx;
  289. font-size: 16px;
  290. font-weight: 700;
  291. }
  292. }
  293. .balance {
  294. width: 200rpx;
  295. height: 100%;
  296. background-color: #FF0000;
  297. font-size: $uni-font-size-lg;
  298. border-radius: 5px;
  299. color: #FFF;
  300. }
  301. }
  302. .spec-box {
  303. position: relative;
  304. z-index: 99999;
  305. }
  306. .classify-popup {
  307. width: 400rpx;
  308. }
  309. .container {
  310. display: flex;
  311. flex-direction: column;
  312. /* 垂直排列子元素 */
  313. align-items: center;
  314. /* 垂直居中子元素 */
  315. justify-content: center;
  316. /* 水平居中子元素 */
  317. }
  318. .icon-container-l {
  319. margin-top: 10px;
  320. margin-left: 15px;
  321. }
  322. .icon-container-r {
  323. margin-left: 20px;
  324. }
  325. </style>