classify.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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" @click="onMyInfo">
  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="buyCount" 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" @click="toBuy">去结算</view>
  73. </view>
  74. <cartPopupVue ref="cartPopup" :carList="carList" @selected-changed="handleSelectedChanged"
  75. @numChanged="numChanged" />
  76. </view>
  77. </template>
  78. <script>
  79. import cartPopupVue from '../../components/cartPopup.vue'
  80. import specPopup from '@/components/specPopup.vue'
  81. import iconCooking from '../../static/cooking.png'
  82. import iconPeople from '../../static/people.png'
  83. import shoppingCart from '../../static/shoppingCart.png'
  84. export default {
  85. components: {
  86. cartPopupVue,
  87. specPopup
  88. },
  89. data() {
  90. return {
  91. onIndex: 0,
  92. show: false,
  93. iconCooking: iconCooking,
  94. iconPeople: iconPeople,
  95. shoppingCart: shoppingCart,
  96. tags: 1,
  97. txts: 1,
  98. goods: [ // 购物车数据列表
  99. {
  100. id: 1,
  101. goods_name: "小乳酸菌牛奶酸奶",
  102. goods_price: 5.20,
  103. goods_type: '大份',
  104. goods_num: 1,
  105. src: '../../static/x0.jpg',
  106. },
  107. {
  108. id: 2,
  109. goods_name: "饮料整箱饮品",
  110. goods_price: 5.20,
  111. goods_type: '大份',
  112. goods_num: 1,
  113. src: '../../static/x1.jpg',
  114. },
  115. {
  116. id: 3,
  117. goods_name: "饮品早餐酸乳益生菌",
  118. goods_price: 5.20,
  119. goods_type: '小份',
  120. goods_num: 1,
  121. src: '../../static/x0.jpg',
  122. },
  123. {
  124. id: 4,
  125. goods_name: "酸奶饮料",
  126. goods_price: 5.20,
  127. goods_type: '小份',
  128. goods_num: 1,
  129. src: '../../static/x1.jpg',
  130. },
  131. {
  132. id: 5,
  133. goods_name: "小乳酸菌",
  134. goods_price: 5.20,
  135. goods_type: '小份',
  136. goods_num: 1,
  137. src: '../../static/x0.jpg',
  138. },
  139. {
  140. id: 6,
  141. goods_name: "酸乳益生菌",
  142. goods_price: 5.20,
  143. goods_type: '大份',
  144. goods_num: 1,
  145. src: '../../static/x1.jpg',
  146. }
  147. ],
  148. total: 0,
  149. carList: [],
  150. cardMsg: {},
  151. selectedList: [],
  152. buyCount: 0
  153. }
  154. },
  155. created() {
  156. },
  157. methods: {
  158. // 加购
  159. goCart(e) {
  160. console.log('================', e)
  161. this.cardMsg = e
  162. this.$refs.specPopup.open();
  163. //this.$refs.specPopup.change(true);
  164. },
  165. handleSelectedChanged(selectedItems) {
  166. console.log('选中的数据:', selectedItems)
  167. this.carList = selectedItems
  168. // this.buyCount = this.carList.length
  169. this.totalPrice();
  170. },
  171. handleUpdateShopMsg(shopMsg) {
  172. console.log('接收到的购物信息:', shopMsg);
  173. const specString = shopMsg.spec + ',' + shopMsg.specs;
  174. const item = this.goods.find(item => item.id === shopMsg.id);
  175. if (item) {
  176. // 检查购物车列表中是否已存在相同id和规格的商品
  177. const existingCartItem = this.carList.find(cartItem => cartItem.id === item.id && cartItem
  178. .goods_type === specString);
  179. if (existingCartItem) {
  180. // 如果已存在,则增加商品数量
  181. existingCartItem.goods_num += shopMsg.num;
  182. } else {
  183. // 如果不存在,则复制item并添加到购物车列表,同时设置商品数量和规格
  184. const newItem = {
  185. ...item
  186. };
  187. newItem.goods_num = shopMsg.num;
  188. newItem.goods_type = specString;
  189. this.carList.push(newItem);
  190. }
  191. // 您可以在这里执行其他逻辑,比如保存到本地存储或发送到服务器
  192. }
  193. console.log('购物车列表更新:', this.carList);
  194. this.totalPrice();
  195. },
  196. //数量的加减
  197. numChanged(value, index) {
  198. console.log('更新后的数量????????????:', value, '索引:', index);
  199. // 假设 carList 是一个 Map,其中 id 是键
  200. const item = this.carList.find(item => item.id === index)
  201. if (item) {
  202. // 更新对应商品的 goods_num
  203. item.goods_num = value;
  204. // 您可以在这里执行其他逻辑,比如保存到本地存储或发送到服务器
  205. console.log('更新后的数量:', value, '索引:', index);
  206. }
  207. this.totalPrice()
  208. },
  209. //计算selected为true的
  210. totalPrice() {
  211. let total = 0;
  212. for (let i = 0; i < this.carList.length; i++) {
  213. // 检查商品是否有 selected 属性
  214. if (this.carList[i].hasOwnProperty('selected')) {
  215. // 如果 selected 属性存在,只有当它为 true 时才计算
  216. if (this.carList[i].selected) {
  217. let priceInCents = Math.round(this.carList[i].goods_price * 100);
  218. let quantity = this.carList[i].goods_num;
  219. total += priceInCents * quantity;
  220. }
  221. } else {
  222. // 如果 selected 属性不存在,直接计算所有商品
  223. let priceInCents = Math.round(this.carList[i].goods_price * 100);
  224. let quantity = this.carList[i].goods_num;
  225. total += priceInCents * quantity;
  226. }
  227. }
  228. // 将总价格转换回浮点数(以元为单位)
  229. this.total = (total / 100).toFixed(2);
  230. console.log('total', this.total);
  231. },
  232. showCart() {
  233. this.$refs.cartPopup.setShow(true);
  234. },
  235. btnIc() {
  236. uni.navigateTo({
  237. url: `/pages/diningList/diningList`
  238. })
  239. },
  240. toDetial() {
  241. uni.navigateTo({
  242. url: `/pages/goodDetail/goodDetail`
  243. })
  244. },
  245. toBuy() {
  246. uni.navigateTo({
  247. url: `/pages/order/submitOrder/submitOrder`
  248. })
  249. },
  250. onMyInfo() {
  251. uni.navigateTo({
  252. url: `/pages/order/myOrder/myOrder`
  253. })
  254. }
  255. }
  256. }
  257. </script>
  258. <style scoped lang="scss">
  259. .goods-category {
  260. width: calc(100% - 100rpx);
  261. border-radius: 14rpx;
  262. overflow: hidden;
  263. .category-box {
  264. height: 70rpx;
  265. width: 100%;
  266. display: flex;
  267. align-items: center;
  268. .category-item {
  269. font-size: 14px;
  270. flex-shrink: 0;
  271. height: 100%;
  272. line-height: 70rpx;
  273. }
  274. .on-category {
  275. color: #FFF;
  276. background-color: #000;
  277. }
  278. }
  279. .classify-icon {
  280. width: 100rpx;
  281. display: flex;
  282. align-items: center;
  283. justify-content: flex-end;
  284. }
  285. }
  286. .goods-item {
  287. width: 48%;
  288. overflow: hidden;
  289. box-sizing: border-box;
  290. border: solid 4px #FFF;
  291. border-radius: 5px;
  292. box-shadow: 0px 3px 10px 0px rgba(213, 221, 232, 0.5);
  293. .img {
  294. width: 100%;
  295. height: 180rpx;
  296. overflow: hidden;
  297. background: #EEE;
  298. }
  299. .title {
  300. width: 100%;
  301. overflow: hidden;
  302. white-space: nowrap;
  303. text-overflow: ellipsis;
  304. padding: 10rpx;
  305. }
  306. }
  307. .cart-bottom {
  308. width: 100%;
  309. position: fixed;
  310. left: 0;
  311. bottom: 0;
  312. height: 130rpx;
  313. box-sizing: border-box;
  314. background-color: #FFF;
  315. box-shadow: 0px -4px 16px 0px rgba(0, 0, 0, 0.15);
  316. z-index: 10800;
  317. .cart {
  318. width: calc(100% - 220rpx);
  319. border-radius: 5px;
  320. background-color: #FEEEE4;
  321. display: flex;
  322. align-items: center;
  323. height: 100%;
  324. .cart-total {
  325. padding-left: 40rpx;
  326. font-size: 16px;
  327. font-weight: 700;
  328. }
  329. }
  330. .balance {
  331. width: 200rpx;
  332. height: 100%;
  333. background-color: #FF0000;
  334. font-size: $uni-font-size-lg;
  335. border-radius: 5px;
  336. color: #FFF;
  337. }
  338. }
  339. .spec-box {
  340. position: relative;
  341. z-index: 99999;
  342. }
  343. .classify-popup {
  344. width: 400rpx;
  345. }
  346. .container {
  347. display: flex;
  348. flex-direction: column;
  349. /* 垂直排列子元素 */
  350. align-items: center;
  351. /* 垂直居中子元素 */
  352. justify-content: center;
  353. /* 水平居中子元素 */
  354. }
  355. .icon-container-l {
  356. margin-top: 10px;
  357. margin-left: 15px;
  358. }
  359. .icon-container-r {
  360. margin-left: 20px;
  361. }
  362. </style>