shop.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. <template>
  2. <view class="container">
  3. <navbar :config="config" backColor="#666"></navbar>
  4. <!-- 有数据 -->
  5. <view v-if="shopList.length >0">
  6. <view class="topStatus u-flex-center-sb u-plr30" :style="{top:tabTop+'px'}">
  7. <text class="u-1A1A1A u-font30">共{{totalNum}}件商品</text>
  8. <text class="u-1A1A1A u-font30" @click="editorBtn">{{editorStatus?'完成':'编辑'}}</text>
  9. </view>
  10. <view class="carbox">
  11. <view class="shopList u-mt20" v-for="(item,index) in shopList" :key="item.source_shop_id">
  12. <view class="u-flex-center-sb">
  13. <view class="u-flex-center">
  14. <view class="check u-mr10" @click="shopChecked(index)">
  15. <text class="iconfont iconbox u-font32 u-FF0000 " v-if="item.flag">&#xe646;</text>
  16. <view class="radios" v-else></view>
  17. </view>
  18. <text class="iconfont3 u-bold u-ml10" style="color: #FA6138;">&#xe655;</text>
  19. <text class="u-1A1A1A u-font30 u-ml15 u-text1 name-width">{{item.source_shop_name}}</text>
  20. <text class="u-CCC right-icon iconfont">&#xe6c7;</text>
  21. </view>
  22. <!-- <view class="">
  23. {{item.proxy?'代销商品':'自营商品'}}
  24. </view> -->
  25. </view>
  26. <view class="goodsItem u-border-one-one u-flex-center"
  27. v-for="(children,childrenIndex) in item.user_cart_list" :key="children.id">
  28. <view class="check u-mr10" @click="goodsChecked(index,children.id)">
  29. <text class="iconfont iconbox u-font32 u-FF0000 " v-if="children.flag">&#xe646;</text>
  30. <view class="radios" v-else></view>
  31. </view>
  32. <view class="u-goods200 u-ml15 positionRetave">
  33. <view class="positinAble" v-if="!children.goods_valid||!children.sku_valid">已失效</view>
  34. <image :src="children.goods_cover" class="u-goods200" mode="scaleToFill"></image>
  35. </view>
  36. <!-- u-flex-column-start -->
  37. <view class="u-flex1 right-box rightText u-ml30">
  38. <view class="u-text2 u-1A1A1A u-font28">
  39. <view class="tipsStatus" v-if="children.platform_store">自营</view>
  40. <view class="thirdTag" v-if="children.third">第三方</view>
  41. <text>{{children.goods_title}}</text>
  42. </view>
  43. <view class="u-flex-column-start">
  44. <text class="u-font22 u-999" v-if="children.sku_set_name">规格: {{children.sku_set_name}}</text>
  45. <view class="u-flex-center-sb botPrice">
  46. <view class="u-FF0000 u-font32 u-flex1">
  47. <rich-text :nodes="$mUtil.priceBigSmall(children.sale_price)"></rich-text>
  48. </view>
  49. <view class="rightAddRes u-flex-center">
  50. <text class=" btnTag" @click="resNum(children.id)">-</text>
  51. <text class="u-flex1 centertext u-font28 u-999">{{children.num}}</text>
  52. <text class="btnTag" @click="addNum(children.id)">+</text>
  53. </view>
  54. </view>
  55. </view>
  56. </view>
  57. </view>
  58. </view>
  59. </view>
  60. <!-- 底部 -->
  61. <view v-if="ispay" class="botfixed u-flex-center-sb">
  62. <view class="leftAll u-flex-center" @click="allChecked">
  63. <view class="check u-mr10">
  64. <text class="iconfont iconbox u-font32 u-FF0000 " v-if="checkAggrement">&#xe646;</text>
  65. <view class="radios" v-else></view>
  66. </view>
  67. <text class="u-ml5 u-font24 u-999">{{checkAggrement?'反选':'全选'}}</text>
  68. </view>
  69. <view class="rightP u-flex-center">
  70. <view class="u-flex-center" v-if="!editorStatus">
  71. <text class="u-font30 u-333 u-bold">合计:</text>
  72. <view class="u-FF0000 u-bold">
  73. <rich-text :nodes="$mUtil.priceBigSmall(totalPrice)"></rich-text>
  74. </view>
  75. </view>
  76. <view class="u-btn btnWidth u-ml30" v-if="!editorStatus" @click="surePay">去结算({{checkNum}})</view>
  77. <view class="u-btn btnWidth u-ml30" v-else @click="delCart">删除</view>
  78. </view>
  79. </view>
  80. </view>
  81. <!-- 没有数据 -->
  82. <view class="nogoods u-mt30 u-flex-column-center" v-if="shopList.length==0">
  83. <image src="/static/common/noGoods.png" mode="" class="noImg"></image>
  84. <text class="u-font26 u-999">您的购物车空空如也~</text>
  85. <view class="btnview" @click="gotoshop">
  86. <button class="u-DCCDA4 shop-btn u-font30 ">{{isLogin?'去逛逛':'去登录'}}</button>
  87. </view>
  88. </view>
  89. <!--页面加载动画-->
  90. <ldLoading isFullScreen :active="loading"></ldLoading>
  91. </view>
  92. </template>
  93. <script>
  94. var app = getApp()
  95. export default {
  96. data() {
  97. return {
  98. totalPrice: 0,
  99. totalNum: 0,
  100. checkNum: 0,
  101. checkAggrement: false,
  102. noGoods: this.$mAssetsPath.noGoods,
  103. config: {
  104. back: true, //false是tolbar页面 是则不写
  105. title: '购物车',
  106. color: '#1A1A1A',
  107. //背景颜色;参数一:透明度(0-1);参数二:背景颜色(array则为线性渐变,string为单色背景)
  108. backgroundColor: [1, "#fff"],
  109. statusBarFontColor: '#000'
  110. },
  111. loading: false,
  112. shopList: [],
  113. arrLenid: [], //选中的购物车id
  114. editorStatus: false,
  115. isLogin: false,
  116. tabTop: 0,
  117. ispay: app.globalData.openPay
  118. }
  119. },
  120. onShow() {
  121. let shop = uni.getStorageSync('shop')
  122. if (shop) {
  123. this.shop = shop
  124. this.shopId = shop.id
  125. this.cartList(shop.id)
  126. this.rest()
  127. }
  128. let token = uni.getStorageSync("apiToken")
  129. if (token) {
  130. this.isLogin = true
  131. } else {
  132. this.isLogin = false
  133. }
  134. },
  135. onLoad() {
  136. this.tabTop = app.globalData.barHeight + 44;
  137. },
  138. methods: {
  139. gotoshop() {
  140. if (this.isLogin) {
  141. uni.redirectTo({
  142. url: '/pages/index/lidaPage'
  143. })
  144. } else {
  145. uni.navigateTo({
  146. url: '/pages/login/index'
  147. })
  148. }
  149. },
  150. //选择店铺
  151. shopChecked(index) {
  152. this.shopList[index].flag = !this.shopList[index].flag
  153. this.shopList[index].user_cart_list.forEach(j => {
  154. if (this.shopList[index].flag) {
  155. j.flag = true
  156. } else {
  157. j.flag = false
  158. }
  159. })
  160. this.checkTotalNum()
  161. },
  162. //选择商品
  163. goodsChecked(index, id) {
  164. this.shopList[index].user_cart_list.forEach(j => {
  165. if (j.id == id) {
  166. j.flag = !j.flag
  167. }
  168. })
  169. this.shopHeightLaign(index)
  170. this.checkTotalNum()
  171. },
  172. //全选
  173. allChecked() {
  174. this.checkAggrement = !this.checkAggrement
  175. this.shopList.forEach(k => {
  176. k.flag = this.checkAggrement
  177. k.user_cart_list.forEach(j => {
  178. j.flag = this.checkAggrement
  179. })
  180. })
  181. this.checkTotalNum()
  182. },
  183. //减
  184. resNum(id) {
  185. this.shopList.forEach(k => {
  186. k.user_cart_list.forEach(j => {
  187. if (j.id == id) {
  188. if (j.num > 1) {
  189. j.num--
  190. } else {
  191. this.$mUtil.toast('不能再减了~')
  192. }
  193. }
  194. })
  195. })
  196. this.checkTotalNum()
  197. },
  198. //加
  199. addNum(id) {
  200. this.shopList.forEach(k => {
  201. k.user_cart_list.forEach(j => {
  202. if (j.id == id) {
  203. if (j.num >= j.limit_buy_num && j.limit_buy_num > 0) {
  204. this.$mUtil.toast('商品库存只有' + j.limit_buy_num + '件')
  205. } else {
  206. j.num++
  207. }
  208. }
  209. })
  210. })
  211. this.checkTotalNum()
  212. },
  213. //编辑
  214. editorBtn() {
  215. this.editorStatus = !this.editorStatus
  216. },
  217. //计算选中的数量和总价
  218. checkTotalNum() {
  219. this.checkNum = 0
  220. let arrLenid = []
  221. let total = 0
  222. this.shopList.forEach(k => {
  223. k.user_cart_list.forEach(j => {
  224. if (j.flag) {
  225. arrLenid.push(j.id)
  226. total += j.sale_price * j.num
  227. }
  228. })
  229. })
  230. this.totalPrice = total.toFixed(2)
  231. this.arrLenid = arrLenid
  232. this.checkNum = arrLenid.length
  233. if (this.checkNum == this.totalNum) {
  234. this.checkAggrement = true
  235. } else {
  236. this.checkAggrement = false
  237. }
  238. },
  239. //查询当前店铺的商品是否被全选中
  240. shopHeightLaign(index) {
  241. let arr = []
  242. this.shopList[index].user_cart_list.forEach(j => {
  243. arr.push(j.flag)
  244. })
  245. if (arr.indexOf(false) == -1) {
  246. this.shopList[index].flag = true
  247. } else {
  248. this.shopList[index].flag = false
  249. }
  250. },
  251. // 判断结算数组是否存在混合商品
  252. getIsHaveOnline() {
  253. return new Promise((resolve, reject) => {
  254. let arr = []
  255. for (let item of this.shopList) {
  256. for (let el of item.user_cart_list) {
  257. for (let i of this.arrLenid) {
  258. if (i == el.id) {
  259. arr.push(el.third)
  260. }
  261. }
  262. }
  263. }
  264. let value = arr.every((item) => {
  265. return item == arr[0];
  266. })
  267. resolve(value)
  268. })
  269. },
  270. //结算
  271. async surePay() {
  272. if (this.arrLenid.length > 0) {
  273. let isBoolean = await this.getIsHaveOnline()
  274. if (!isBoolean) return this.$mUtil.toast('本地商品与第三方商品不能混合购买')
  275. let flagKeys = []
  276. this.shopList.map((res, i) => {
  277. res.user_cart_list.map((keys, j) => {
  278. if (keys.flag) {
  279. if (!keys.goods_valid || (!keys.sku_valid)) {
  280. flagKeys.push('false')
  281. } else {
  282. flagKeys.push('true')
  283. }
  284. }
  285. })
  286. })
  287. if (flagKeys.indexOf('false') == -1) {
  288. this.psotJson()
  289. } else {
  290. this.$mUtil.toast('已失效的商品不能结算')
  291. }
  292. } else {
  293. this.$mUtil.toast('请选择需要购买的商品')
  294. }
  295. },
  296. //删除
  297. delCart() {
  298. let that = this
  299. if (that.arrLenid.length > 0) {
  300. uni.showModal({
  301. title: '提示',
  302. content: '确定删除选中的商品?',
  303. success: function(e) {
  304. if (e.confirm) {
  305. that.$http.delete(`/cart/delete`, that.arrLenid)
  306. .then(res => {
  307. if (res && res.code == 200) {
  308. that.$mUtil.toast('删除成功')
  309. that.rest()
  310. that.cartList(that.shopId)
  311. }
  312. })
  313. }
  314. }
  315. });
  316. } else {
  317. that.$mUtil.toast('请选择需要删除的商品')
  318. }
  319. },
  320. // 重置状态
  321. rest() {
  322. this.arrLenid = []
  323. this.totalPrice = 0
  324. this.checkNum = 0
  325. this.checkAggrement = false
  326. },
  327. //列表
  328. cartList(id) {
  329. let nums = 0
  330. this.$http.get(`/cart/list/${id}`)
  331. .then(res => {
  332. this.loading = false
  333. if (res && res.code == 200) {
  334. if (res.list.length > 0) {
  335. res.list.forEach(k => {
  336. k.flag = false
  337. nums += k.user_cart_list.length
  338. k.user_cart_list.forEach(j => {
  339. j.flag = false
  340. })
  341. })
  342. this.totalNum = nums
  343. this.shopList = res.list
  344. } else {
  345. this.shopList = []
  346. }
  347. }
  348. })
  349. },
  350. psotJson() {
  351. let shop_orders = []
  352. let parendArr = []
  353. let childArr = [
  354. [],
  355. [],
  356. [],
  357. [],
  358. [],
  359. [],
  360. [],
  361. [],
  362. []
  363. ]
  364. let listArr = []
  365. let dataJson = {}
  366. this.shopList.map((res, i) => {
  367. res.user_cart_list.map((keys, j) => {
  368. if (keys.flag) {
  369. childArr[i].push({
  370. user_cart_id: keys.id,
  371. activity_goods_id: '0',
  372. goods_id: keys.goods_id,
  373. num: keys.num,
  374. sku_hash_code: keys.sku_hash_code,
  375. })
  376. }
  377. })
  378. if (childArr[i].length > 0) {
  379. parendArr.push({
  380. shop_id: res.shop_id,
  381. source_shop_id: res.source_shop_id,
  382. activity_id: 0, //活动id
  383. sponsor_activity_id: 0, //拼团id
  384. user_remark: '',
  385. items: childArr[i]
  386. })
  387. }
  388. })
  389. dataJson = {
  390. is_cart: true, //是否来源于购物车
  391. receipt_id: null,
  392. shipment_mode: 0, //0物流,1自提
  393. shop_orders: parendArr,
  394. marketing_type: 0, //0无活动,1秒杀,2拼团
  395. }
  396. uni.setStorageSync('dataJson', dataJson)
  397. uni.navigateTo({
  398. url: '../product/surePay/surePay'
  399. })
  400. // let shop = uni.getStorageSync('shop')
  401. // let shopid=shop.id?shop.id:''
  402. // this.$http.post(`/order/build`,dataJson).then(res=>{
  403. // if(res&&res.code==200){
  404. // uni.setStorageSync('sureData',res.data)
  405. // }
  406. // })
  407. },
  408. }
  409. }
  410. </script>
  411. <style lang="scss">
  412. page {
  413. background-color: #f5f5f5;
  414. }
  415. .shop-btn {
  416. width: 300rpx;
  417. height: 86rpx;
  418. background: #FA6138;
  419. border-radius: 43rpx;
  420. font-size: 30rpx;
  421. color: #fff;
  422. text-align: center;
  423. line-height: 86rpx;
  424. margin-top: 30rpx;
  425. }
  426. .right-icon {
  427. font-size: 25rpx;
  428. }
  429. .border {
  430. border-bottom: 3rpx solid #E7E7E7;
  431. }
  432. .right-box {
  433. display: flex;
  434. flex-direction: column;
  435. // align-items: flex-start;
  436. justify-content: space-between;
  437. }
  438. .name-width {
  439. max-width: 420rpx;
  440. }
  441. .positionRetave {
  442. position: relative;
  443. .positinAble {
  444. position: absolute;
  445. width: 200rpx;
  446. height: 40rpx;
  447. text-align: center;
  448. background-color: rgba($color: #000000, $alpha: .5);
  449. color: #fff;
  450. font-size: 26rpx;
  451. line-height: 40rpx;
  452. bottom: 0;
  453. left: 0;
  454. }
  455. }
  456. .tipsStatus {
  457. display: inline-block;
  458. // width: 102rpx;
  459. // height: 28rpx;
  460. background: #ff0000;
  461. border-radius: 10rpx 0px 10rpx 0px;
  462. line-height: 28rpx;
  463. text-align: center;
  464. margin-right: 10rpx;
  465. color: #fff;
  466. font-size: 20rpx;
  467. }
  468. .thirdTag {
  469. display: inline-block;
  470. background: #30b885;
  471. border-radius: 10rpx;
  472. line-height: 28rpx;
  473. text-align: center;
  474. margin-right: 10rpx;
  475. padding: 6rpx 10rpx;
  476. color: #fff;
  477. font-size: 20rpx;
  478. }
  479. .check {
  480. position: relative;
  481. width: 32rpx;
  482. height: 32rpx;
  483. .radios {
  484. width: 30rpx;
  485. height: 30rpx;
  486. display: block;
  487. border: 1px solid #ccc;
  488. border-radius: 50%;
  489. position: absolute;
  490. left: 0;
  491. top: 0;
  492. z-index: 1;
  493. }
  494. .iconbox {
  495. position: relative;
  496. z-index: 2;
  497. }
  498. }
  499. .topStatus {
  500. height: 80rpx;
  501. background-color: #fff;
  502. position: fixed;
  503. z-index: 9;
  504. width: 100%;
  505. box-sizing: border-box;
  506. }
  507. .goodsItem {
  508. padding: 60rpx 0;
  509. border-bottom: 3rpx solid #E7E7E7;
  510. }
  511. .goodsItem:last-child {
  512. border-bottom: none;
  513. }
  514. .noImg {
  515. width: 490rpx;
  516. height: 556rpx;
  517. }
  518. .btnview {
  519. // width: 300rpx;
  520. // margin: 30rpx auto 0;
  521. }
  522. .carbox {
  523. padding: 80rpx 0 160rpx 0;
  524. .shopList {
  525. background-color: #fff;
  526. padding: 30rpx 30rpx 0;
  527. }
  528. }
  529. .rightText {
  530. justify-content: space-between;
  531. height: 200rpx;
  532. }
  533. .bottext,
  534. .botPrice {
  535. width: 100%;
  536. }
  537. .bottext {
  538. margin-top: 36rpx;
  539. }
  540. .rightAddRes {
  541. width: 172rpx;
  542. height: 50rpx;
  543. border-radius: 8rpx;
  544. border: 1px solid #E5E5E5;
  545. .centertext {
  546. text-align: center;
  547. }
  548. .btnTag {
  549. width: 50rpx;
  550. height: 50rpx;
  551. line-height: 50rpx;
  552. text-align: center;
  553. background-color: #dedede;
  554. font-size: 46rpx;
  555. color: #888;
  556. }
  557. }
  558. .btnWidth {
  559. width: 208rpx;
  560. }
  561. .botfixed {
  562. position: fixed;
  563. background-color: #fff;
  564. width: 100%;
  565. height: 138rpx;
  566. bottom: 0;
  567. left: 0;
  568. z-index: 9;
  569. padding: 0 30rpx;
  570. box-sizing: border-box;
  571. }
  572. </style>