index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. <template>
  2. <view class="container">
  3. <!--店铺切换-->
  4. <Location v-if="storeInfo" :storeInfo="storeInfo"/>
  5. <!-- 搜索框 -->
  6. <Search tips="请输入搜索关键字..." @event="$navTo('pages/search/index')" />
  7. <view class="cate-content dis-flex" v-if="list.length > 0">
  8. <!-- 左侧 分类 -->
  9. <scroll-view class="cate-left f-28" :scroll-y="true" :style="{ height: `${scrollHeight}px` }">
  10. <view v-for="(item, index) in list" :key="index">
  11. <text class="cart-badge" v-if="item.total">{{ item.total }}</text>
  12. <view class="type-nav" :class="{ selected: curIndex == index }" @click="handleSelectNav(index)">
  13. <image class="logo" lazy-load :lazy-load-margin="0" v-if="item.logo" :src="item.logo"></image>
  14. <view class="name">{{ item.name }}</view>
  15. </view>
  16. </view>
  17. </scroll-view>
  18. <!-- 右侧 商品 -->
  19. <scroll-view class="cate-right b-f" :scroll-top="scrollTop" :scroll-y="true" :style="{ height: `${scrollHeight}px` }">
  20. <view v-if="list[curIndex]">
  21. <view class="cate-right-cont">
  22. <view class="cate-two-box">
  23. <view v-if="list[curIndex].goodsList.length" class="cate-cont-box">
  24. <view class="flex-five item" v-for="(item, idx) in list[curIndex].goodsList" :key="idx">
  25. <view class="cate-img">
  26. <image v-if="item.logo" lazy-load :lazy-load-margin="0" :src="item.logo" @click="onTargetGoods(item.id)"></image>
  27. </view>
  28. <view class="cate-info">
  29. <view class="base">
  30. <text class="name text">{{ item.name }}</text>
  31. <text class="stock text">库存:{{ item.stock ? item.stock : 0 }} 已售:{{ item.initSale ? item.initSale : 0 }}</text>
  32. </view>
  33. <view class="action">
  34. <text class="price">¥{{ item.price ? item.price : 0 }}</text>
  35. <view class="cart">
  36. <view v-if="item.isSingleSpec === 'Y'" class="singleSpec">
  37. <view class="ii do-minus" v-if="item.buyNum" @click="onSaveCart(item.id, '-')"></view>
  38. <view class="ii num" v-if="item.buyNum">{{ (item.buyNum != undefined) ? item.buyNum : 0 }}</view>
  39. <view class="ii do-add" v-if="item.stock > 0" @click="onSaveCart(item.id, '+')"></view>
  40. </view>
  41. <view v-if="item.isSingleSpec === 'N'" class="multiSpec">
  42. <text class="num-badge" v-if="item.buyNum">{{ item.buyNum }}</text>
  43. <view class="select-spec" @click="onShowSkuPopup(2, item.id)">选规格</view>
  44. </view>
  45. </view>
  46. </view>
  47. </view>
  48. </view>
  49. </view>
  50. <empty v-if="!list[curIndex].goodsList.length" :isLoading="isLoading" tips="暂无商品~"></empty>
  51. </view>
  52. </view>
  53. </view>
  54. </scroll-view>
  55. </view>
  56. <!-- 商品SKU弹窗 -->
  57. <SkuPopup v-if="!isLoading" v-model="showSkuPopup" :skuMode="skuMode" :goods="goods" @addCart="onAddCart"/>
  58. <view class="flow-fixed-footer b-f m-top10">
  59. <view class="dis-flex chackout-box">
  60. <view class="chackout-left pl-12">
  61. <view class="col-amount-do">总金额:<text class="amount">¥{{ totalPrice.toFixed(2) }}</text></view>
  62. <view class="col-amount-view">共计:{{ totalNum }} 件</view>
  63. </view>
  64. <view class="chackout-right" @click="doSubmit()">
  65. <view class="flow-btn f-32">去结算</view>
  66. </view>
  67. </view>
  68. </view>
  69. <empty v-if="!list.length" :isLoading="isLoading" />
  70. </view>
  71. </template>
  72. <script>
  73. import { setCartTabBadge, setCartTotalNum } from '@/utils/app'
  74. import * as CartApi from '@/api/cart'
  75. import * as GoodsApi from '@/api/goods'
  76. import * as settingApi from '@/api/setting'
  77. import Search from '@/components/search'
  78. import Empty from '@/components/empty'
  79. import SkuPopup from './components/SkuPopup'
  80. import Location from '@/components/page/location'
  81. const App = getApp()
  82. export default {
  83. components: {
  84. Search,
  85. SkuPopup,
  86. Empty,
  87. Location
  88. },
  89. data() {
  90. return {
  91. orderId: 0,
  92. goodsCart: [],
  93. totalNum: 0,
  94. totalPrice: 0.00,
  95. // 列表高度
  96. scrollHeight: 500,
  97. // 一级分类:指针
  98. curIndex: 0,
  99. // 内容区竖向滚动条位置
  100. scrollTop: 0,
  101. // 分类列表
  102. list: [],
  103. // 正在加载中
  104. isLoading: true,
  105. showSkuPopup: false,
  106. skuMode: 1,
  107. goods: {},
  108. storeInfo: null
  109. }
  110. },
  111. /**
  112. * 生命周期函数--监听页面加载
  113. */
  114. onLoad({ tableId, orderId }) {
  115. const app = this;
  116. tableId = tableId ? parseInt(tableId) : 0;
  117. if (tableId > 0) {
  118. uni.setStorageSync('tableId', tableId);
  119. }
  120. app.orderId = orderId;
  121. // 设置分类列表高度
  122. app.setListHeight();
  123. console.log('orderId = ', app.orderId);
  124. },
  125. /**
  126. * 生命周期函数--监听页面显示
  127. */
  128. onShow() {
  129. const app = this;
  130. // 获取页面数据
  131. app.getPageData();
  132. app.onGetStoreInfo();
  133. uni.getLocation({
  134. type: 'gcj02',
  135. success(res){
  136. uni.setStorageSync('latitude', res.latitude);
  137. uni.setStorageSync('longitude', res.longitude);
  138. app.onGetStoreInfo();
  139. },
  140. fail(e) {
  141. // empty
  142. }
  143. })
  144. },
  145. methods: {
  146. /**
  147. * 获取页面数据
  148. */
  149. getPageData() {
  150. const app = this
  151. app.isLoading = true
  152. Promise.all([
  153. // 获取分类列表
  154. GoodsApi.cateList(),
  155. // 获取购物车列表
  156. CartApi.list()
  157. ])
  158. .then(result => {
  159. // 初始化分类列表数据
  160. app.list = result[0].data;
  161. app.totalNum = result[1].data.totalNum;
  162. app.goodsCart = result[1].data.list;
  163. setCartTotalNum(app.totalNum);
  164. setCartTabBadge();
  165. })
  166. .finally(() => {
  167. app.isLoading = false
  168. app.totalPrice = 0
  169. app.list.forEach(function(item, index) {
  170. let total = 0
  171. item.goodsList.forEach(function(goods, key) {
  172. let totalBuyNum = 0
  173. app.goodsCart.forEach(function(cart){
  174. if (goods.id == cart.goodsId) {
  175. total = total + cart.num
  176. totalBuyNum = totalBuyNum + cart.num
  177. app.totalPrice = app.totalPrice + (cart.goodsInfo.price * cart.num)
  178. }
  179. })
  180. app.$set(app.list[index].goodsList[key], 'buyNum', totalBuyNum)
  181. })
  182. app.$set(app.list[index], 'total', total)
  183. })
  184. })
  185. },
  186. /**
  187. * 获取默认店铺
  188. * */
  189. onGetStoreInfo() {
  190. const app = this
  191. settingApi.systemConfig()
  192. .then(result => {
  193. app.storeInfo = result.data.storeInfo
  194. })
  195. },
  196. /**
  197. * 跳转商品详情
  198. */
  199. onTargetGoods(goodsId) {
  200. this.$navTo(`pages/goods/detail`, { goodsId })
  201. },
  202. /**
  203. * 设置分类列表高度
  204. */
  205. setListHeight() {
  206. const app = this
  207. uni.getSystemInfo({
  208. success(res) {
  209. app.scrollHeight = res.windowHeight - 120
  210. }
  211. })
  212. },
  213. // 一级分类:选中分类
  214. handleSelectNav(index) {
  215. const app = this;
  216. app.curIndex = index;
  217. app.scrollTop = 0;
  218. },
  219. // 更新购物车
  220. onSaveCart(goodsId, action) {
  221. const app = this
  222. return new Promise((resolve, reject) => {
  223. CartApi.save(goodsId, action)
  224. .then(result => {
  225. app.getPageData();
  226. resolve(result);
  227. })
  228. .catch(err => {
  229. console.log(err);
  230. })
  231. })
  232. },
  233. // 更新购物车数量
  234. onAddCart(total) {
  235. this.getPageData();
  236. this.$toast("添加购物车成功");
  237. },
  238. // 结算
  239. doSubmit() {
  240. if (this.totalPrice > 0) {
  241. this.$navTo('pages/cart/index')
  242. } else {
  243. this.$error("请先选择商品")
  244. }
  245. },
  246. onShowSkuPopup(skuMode, goodsId) {
  247. const app = this
  248. app.isLoading = true
  249. return new Promise((resolve, reject) => {
  250. GoodsApi.detail(goodsId)
  251. .then(result => {
  252. const goodsData = result.data
  253. if (goodsData.skuList) {
  254. goodsData.skuList.forEach(function(sku, index) {
  255. goodsData.skuList[index].specIds = sku.specIds.split('-')
  256. goodsData.skuList[index].skuId = sku.id
  257. })
  258. }
  259. app.goods = goodsData
  260. app.skuMode = skuMode
  261. app.showSkuPopup = !app.showSkuPopup
  262. console.log(app.skuMode)
  263. app.isLoading = false
  264. resolve(result)
  265. })
  266. .catch(err => reject(err))
  267. })
  268. },
  269. },
  270. /**
  271. * 设置分享内容
  272. */
  273. onShareAppMessage() {
  274. const app = this
  275. return {
  276. title: _this.templet.shareTitle,
  277. path: '/pages/category/index?' + app.$getShareUrlParams()
  278. }
  279. },
  280. /**
  281. * 分享到朋友圈
  282. * 本接口为 Beta 版本,暂只在 Android 平台支持,详见分享到朋友圈 (Beta)
  283. * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
  284. */
  285. onShareTimeline() {
  286. const app = this
  287. return {
  288. title: _this.templet.shareTitle,
  289. path: '/pages/category/index?' + app.$getShareUrlParams()
  290. }
  291. }
  292. }
  293. </script>
  294. <style>
  295. page {
  296. background: #fff;
  297. }
  298. </style>
  299. <style lang="scss" scoped>
  300. .cate-content {
  301. background: #fff;
  302. margin-top: 118rpx;
  303. /* #ifdef H5 */
  304. margin-top: 124rpx;
  305. /* #endif */
  306. }
  307. .cate-wrapper {
  308. padding: 0 20rpx 20rpx 20rpx;
  309. box-sizing: border-box;
  310. }
  311. /* 分类内容 */
  312. .cate-content {
  313. width: 100%;
  314. }
  315. .cate-left {
  316. flex-direction: column;
  317. display: flex;
  318. width: 200rpx;
  319. color: #444;
  320. height: 100%;
  321. background: #f8f8f8;
  322. margin-bottom: 120rpx;
  323. .cart-badge {
  324. position: absolute;
  325. right: 1rpx;
  326. margin-top: 10rpx;
  327. margin-right: 5rpx;
  328. font-size: 18rpx;
  329. background: #fa5151;
  330. z-index: 999;
  331. text-align: center;
  332. line-height: 28rpx;
  333. color: #ffffff;
  334. border-radius: 50%;
  335. min-width: 32rpx;
  336. padding: 5rpx 13rpx 5rpx 13rpx;
  337. }
  338. }
  339. .cate-right {
  340. display: flex;
  341. flex-direction: column;
  342. width: 100%;
  343. height: 100%;
  344. overflow: hidden;
  345. margin-bottom: 80rpx;
  346. }
  347. .cate-right-cont {
  348. width: 100%;
  349. display: flex;
  350. flex-flow: row wrap;
  351. align-content: flex-start;
  352. padding-top: 10rpx;
  353. }
  354. .type-nav {
  355. position: relative;
  356. height: 140rpx;
  357. text-align: center;
  358. z-index: 10;
  359. display: block;
  360. font-size: 26rpx;
  361. padding: 20rpx 0rpx 126rpx 0rpx;
  362. .logo {
  363. width: 60rpx;
  364. height: 60rpx;
  365. border-radius: 60rpx;
  366. margin: 0rpx;
  367. padding: 0rpx;
  368. }
  369. .name {
  370. margin-top: 2rpx;
  371. width: 100%;
  372. overflow-x: hidden;
  373. height: 40rpx;
  374. line-height: 40rpx;
  375. text-align: center;
  376. }
  377. }
  378. .type-nav.selected {
  379. color: #666666;
  380. background: #ffffff;
  381. border-right: none;
  382. border-left: solid 10rpx #f03c3c;
  383. font-weight: bold;
  384. font-size: 28rpx;
  385. }
  386. .cate-cont-box {
  387. margin-bottom: 10rpx;
  388. padding-bottom: 10rpx;
  389. overflow: hidden;
  390. height: auto;
  391. display: block;
  392. .item {
  393. height: 220rpx;
  394. display: block;
  395. padding-top: 5rpx;
  396. border-radius: 3rpx;
  397. margin-bottom: 5rpx;
  398. }
  399. }
  400. .cate-cont-box .cate-img {
  401. padding: 13rpx 10rpx 4rpx 10rpx;
  402. display: block;
  403. }
  404. .cate-cont-box .cate-img image {
  405. width: 160rpx;
  406. height: 150rpx;
  407. float: left;
  408. border-radius: 5rpx;
  409. display: block;
  410. border: #cccccc solid 1rpx;
  411. margin-top: 5rpx;
  412. }
  413. .cate-cont-box .cate-info {
  414. text-align: left;
  415. display: flex;
  416. flex-direction: column;
  417. font-size: 26rpx;
  418. margin-left: 168rpx;
  419. padding-bottom: 14rpx;
  420. color: #444;
  421. padding: 0 15rpx 30rpx 15rpx;
  422. .base {
  423. height: 100%;
  424. display: block;
  425. .text {
  426. display: block;
  427. float: left;
  428. width: 100%;
  429. }
  430. .name {
  431. font-weight: bold;
  432. width: 100%;
  433. font-size: 26rpx;
  434. overflow: hidden;
  435. display: -webkit-box;
  436. -webkit-box-orient: vertical;
  437. -webkit-line-clamp: 2;
  438. }
  439. .stock {
  440. margin-top: 10rpx;
  441. color: #999;
  442. }
  443. }
  444. .action {
  445. display: block;
  446. height: 50rpx;
  447. .price {
  448. margin-top: 20rpx;
  449. color: #f03c3c;
  450. float: left;
  451. font-size: 32rpx;
  452. font-weight: bold;
  453. }
  454. .cart {
  455. margin-top: 20rpx;
  456. float: right;
  457. font-size: 30rpx;
  458. height: 60rpx;
  459. .ii {
  460. float: left;
  461. text-align: center;
  462. width: 60rpx;
  463. cursor: pointer;
  464. }
  465. .do-add {
  466. background: url('~@/static/icon/add.png') no-repeat;
  467. background-size: 100% 100%;
  468. width: 45rpx;
  469. height: 45rpx;
  470. }
  471. .do-minus {
  472. background-image: url('~@/static/icon/minus.png');
  473. background-size: 100% 100%;
  474. width: 45rpx;
  475. height: 45rpx;
  476. }
  477. .multiSpec {
  478. .num-badge {
  479. position: absolute;
  480. margin-top: 10rpx;
  481. margin-right: 25rpx;
  482. font-size: 18rpx;
  483. background: #f03c3c;
  484. text-align: center;
  485. line-height: 36rpx;
  486. color: #ffffff;
  487. border-radius: 50%;
  488. min-width: 36rpx;
  489. padding: 2rpx;
  490. }
  491. .select-spec {
  492. border: solid 1rpx $fuint-theme;
  493. padding: 10rpx 20rpx 10rpx 36rpx;
  494. font-size: 25rpx;
  495. border-radius: 5rpx;
  496. color: #ffffff;
  497. background: $fuint-theme;
  498. }
  499. }
  500. }
  501. }
  502. }
  503. .cate-two-box {
  504. width: 100%;
  505. padding: 0 2px;
  506. }
  507. // 底部操作栏
  508. .flow-fixed-footer {
  509. position: fixed;
  510. bottom: var(--window-bottom);
  511. width: 100%;
  512. background: #fff;
  513. border-top: 1px solid #eee;
  514. z-index: 11;
  515. padding-top: 8rpx;
  516. .chackout-left {
  517. font-size: 28rpx;
  518. height: 98rpx;
  519. color: #777;
  520. flex: 4;
  521. padding-left: 12px;
  522. text-align: right;
  523. padding-right: 40rpx;
  524. .col-amount-do {
  525. font-size: 35rpx;
  526. margin-top: 5rpx;
  527. margin-bottom:5rpx;
  528. .amount {
  529. color: #f03c3c;
  530. font-weight: bold;
  531. }
  532. }
  533. }
  534. .chackout-right {
  535. font-size: 34rpx;
  536. flex: 2;
  537. }
  538. // 提交按钮
  539. .flow-btn {
  540. background: linear-gradient(to right, $fuint-theme, $fuint-theme);
  541. color: #fff;
  542. text-align: center;
  543. line-height: 92rpx;
  544. display: block;
  545. font-size: 28rpx;
  546. border-radius: 5rpx;
  547. margin-right: 20rpx;
  548. // 禁用按钮
  549. &.disabled {
  550. background: #ff9779;
  551. }
  552. }
  553. }
  554. </style>