supermarket.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <template>
  2. <view class="content">
  3. <empty v-if="list.length === 0"></empty>
  4. <view class="guess" v-if="list.length !=0">
  5. <view v-for="(item, index) in list" :key="index" class="guess-item" @click="navToDetailPage(item)">
  6. <image :src="item.image"></image>
  7. <view class="guess-box">
  8. <view class="title clamp">{{ item.store_name }}</view>
  9. <view class="guessbtm-box flex">
  10. <view class="price">¥{{ item.price }} <text class="yuan">¥{{ item.ot_price }}</text> </view>
  11. <image @click.stop="toggleSpec(item.id)" class="btn" src="../../static/icon/cart.png" mode="">
  12. </image>
  13. </view>
  14. </view>
  15. </view>
  16. <uni-load-more :status="loadingType"></uni-load-more>
  17. </view>
  18. <view class="cart" @click="navtab">
  19. <view class="text" v-if="total>0">
  20. {{total}}
  21. </view>
  22. <image class="cart-icon" src="/static/img/cartIcon.png" mode="scaleToFill"></image>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
  28. import empty from '@/components/empty';
  29. import {
  30. getCartList,
  31. } from '@/api/cart.js';
  32. import {
  33. getProducts,
  34. cartAdd
  35. } from '@/api/product.js';
  36. export default {
  37. data() {
  38. return {
  39. page: 1,
  40. limit: 20,
  41. loadingType: 'more',
  42. list: [],
  43. total: 0
  44. };
  45. },
  46. components: {
  47. empty,
  48. uniLoadMore
  49. },
  50. onLoad() {
  51. this.loadData();
  52. },
  53. onShow() {
  54. this.loadCart();
  55. },
  56. onReachBottom() {
  57. this.loadData();
  58. },
  59. onReady() {},
  60. methods: {
  61. navtab() {
  62. uni.switchTab({
  63. url: '/pages/cart/cart'
  64. })
  65. },
  66. async loadCart() {
  67. let obj = this;
  68. getCartList({
  69. type: 0
  70. })
  71. .then(function(e) {
  72. obj.total = e.data.valid.length;
  73. })
  74. .catch(function(e) {
  75. console.log(e);
  76. });
  77. },
  78. navToDetailPage(opt) {
  79. uni.navigateTo({
  80. url: '/pages/product/product?id=' + opt.id
  81. })
  82. },
  83. loadData() {
  84. if (this.loadingType == 'loading' || this.loadingType == 'nomore') {
  85. return
  86. }
  87. this.loadingType = 'loading'
  88. getProducts({
  89. page: this.page,
  90. limit: this.limit,
  91. cid: 1
  92. }).then(({
  93. data
  94. }) => {
  95. this.list = this.list.concat(data)
  96. this.page++
  97. if (data.length != this.limit) {
  98. this.loadingType = 'nomore'
  99. } else {
  100. this.loadingType = 'more'
  101. }
  102. })
  103. },
  104. toggleSpec(str) {
  105. const that = this;
  106. that.total++
  107. cartAdd({
  108. cartNum: 1, //商品数量
  109. uniqueId: '', //商品标签
  110. new: 0, //商品是否新增加到购物车1为不加入0为加入
  111. mer_id: 0,
  112. is_consumer: 0,
  113. productId: str
  114. }).then(e => {
  115. uni.showToast({
  116. title: '成功加入购物车',
  117. type: 'top',
  118. duration: 2000
  119. });
  120. that.loadCart();
  121. }).catch((e)=>{
  122. that.total--
  123. })
  124. },
  125. }
  126. };
  127. </script>
  128. <style lang="scss">
  129. page,
  130. .content {
  131. min-height: 100%;
  132. height: auto;
  133. }
  134. .cart {
  135. position: fixed;
  136. right: 30rpx;
  137. bottom: 60rpx;
  138. .cart-icon {
  139. width: 150rpx;
  140. height: 150rpx;
  141. }
  142. .text {
  143. background-color: $color-red;
  144. color: #FFF;
  145. position: absolute;
  146. top: 0;
  147. right: 0;
  148. width: 40rpx;
  149. height: 40rpx;
  150. z-index: 999;
  151. text-align: center;
  152. border-radius: 99rpx;
  153. }
  154. }
  155. .guess {
  156. display: flex;
  157. justify-content: space-between;
  158. flex-wrap: wrap;
  159. padding: 20rpx;
  160. }
  161. .guess-item {
  162. margin-top: 20rpx;
  163. width: 345rpx;
  164. background: #FFFFFF;
  165. box-shadow: 0rpx 0rpx 20rpx 0rpx rgba(50, 50, 52, 0.06);
  166. border-radius: 10rpx;
  167. image {
  168. width: 345rpx;
  169. height: 345rpx;
  170. border-radius: 10rpx;
  171. }
  172. .guess-box {
  173. padding: 28rpx 20rpx;
  174. width: 345rpx;
  175. .title {
  176. font-size: 30rpx;
  177. font-family: PingFang SC;
  178. font-weight: bold;
  179. color: #333333;
  180. }
  181. .price {
  182. font-size: 36rpx;
  183. font-family: PingFang SC;
  184. font-weight: bold;
  185. color: #FF6B2E;
  186. .yuan {
  187. display: inline-block;
  188. margin-left: 10rpx;
  189. font-size: 26rpx;
  190. font-family: PingFang SC;
  191. font-weight: 500;
  192. text-decoration: line-through;
  193. color: #989898;
  194. }
  195. }
  196. .btn {
  197. width: 44rpx;
  198. height: 44rpx;
  199. background: #00ca95;
  200. border-radius: 50rpx;
  201. }
  202. }
  203. }
  204. </style>