123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538 |
- <template>
- <app-layout>
- <view class="app-cart">
- <view class="app-announcement dir-left-nowrap main-between cross-center">
- <text class="app-announcement-text">商品库存有限,请尽快下单</text>
- <app-form-id @click="editSwitch">
- <text class="app-edit-text">{{editStatus ? '完成' : '编辑'}}</text>
- </app-form-id>
- </view>
- <view class="app-item" v-for="(mch, index) in listObj" :key="index" v-if="listObj.length > 0">
- <app-shop-product @change="change" :mch="mch" :editStatus="editStatus" @changeSingleRadio="changeSingleRadio" @changeRadioAll="changeRadioAll" ></app-shop-product>
- </view>
- <view class="no-cart" v-if="listObj.length === 0">
- <view class="cart-icon">
- <image class="cart-image" src="../../static/image/icon/nav-icon-cart.png"></image>
- </view>
- <view class="cart-text">购物车还是空的哦</view>
- </view>
- <view>
- <app-empty-bottom backgroundColor="#f7f7f7" :botBool="botBool" :height="Number(100)"></app-empty-bottom>
- </view>
- <view v-if="!tabbarbool" :style="{background: 'white', position: 'fixed', bottom: 0, height: `${getEmpty}rpx`, width: '750rpx'}"></view>
- <view class="app-settlement dir-left-nowrap main-between cross-center" :style="{bottom: `${tabbarbool ? BotHeight : getEmpty}rpx`}">
- <view class="app-radio dir-left-nowrap main-right cross-center">
- <app-radio type="round" @click="setALl" v-model="all"></app-radio>
- <text class="app-all-text">全选</text>
- <text class="app-price" v-if="!editStatus">总计: ¥{{priceNum}}</text>
- </view>
- <button :disabled="submitDis" :class="submitDis ? 'delete-disabled' : ''" class="app-delete" v-if="editStatus" @click="deleteProduct">
- 删除
- </button>
- <button :disabled="submitDis" :class="submitDis ? 'disabled-background' : ''" class="app-settlement-button" v-else @click="settlement">
- 去结算
- </button>
- </view>
- </view>
- </app-layout>
- </template>
- <script>
- import { mapState, mapGetters } from 'vuex';
- import appShopProduct from './components/app-shop-product/app-shop-product.vue';
- import appRadio from '../../components/basic-component/app-radio/app-radio.vue';
- import appIphoneX from '../../components/basic-component/app-iphone-x/app-iphone-x.vue';
- import appEmptyBottom from '../../components/basic-component/app-empty-bottom/app-empty-bottom.vue';
-
- export default {
- name: 'cart',
- components:{
- 'app-shop-product': appShopProduct,
- 'app-radio': appRadio,
- 'app-iphone-x': appIphoneX,
- 'app-empty-bottom': appEmptyBottom,
- },
- data() {
- return {
- editStatus: false,
- listObj: [],
- all: false,
- editList: [],
- priceNum: 0,
- edit: false,
- botBool: true,
- currentRoute: this.$platDiff.route(),
- tabbarbool: false,
- spike: -1,
- submitDis: true,
- }
- },
- computed:{
- ...mapState({
- tabBarHeight: (state) => {
- return state.gConfig.tabBarHeight;
- },
- iphoneHeight: (state) =>{
- return state.gConfig.iphoneHeight;
- },
- iphone: (state) => {
- return state.gConfig.iphone
- }
- }),
- ...mapGetters('iPhoneX', {
- BotHeight: 'getBotHeight',
- getEmpty: 'getEmpty',
- }),
- ...mapState({
- tabBarNavs: state => state.mallConfig.navbar.navs,
- }),
- },
- methods: {
- setALl(data) {
- this.selectAll(data);
- },
-
- // 编辑
- editSwitch() {
- this.all = false;
- for (let i = 0; i < this.listObj.length; i++) {
- this.listObj[i].is_active = false;
- for (let j = 0; j < this.listObj[i].goods_list.length; j++) {
- this.listObj[i].goods_list[j].is_active = false;
- }
- }
- this.editStatus = !this.editStatus;
- },
-
- async getProductList() {
- uni.showLoading({
- title: '加载中'
- });
- const res = await this.$request({
- url: this.$api.cart.list,
- method: 'get',
- });
- if (res.code === 0) {
- this.listObj = res.data.list;
- this.spikeTime(res.data.list);
- }
- uni.hideLoading();
- },
-
- // 商城全选
- changeRadioAll(data) {
- for (let i = 0; i < this.listObj.length; i++) {
- let {mch_id , goods_list } = this.listObj[i];
- if (data.mch_id === mch_id) {
- for (let j = 0; j < goods_list.length; j++) {
- if (!this.editStatus) {
- if (goods_list[j].new_status === 0) goods_list[j].is_active = !data.is_active;
- } else {
- goods_list[j].is_active = !data.is_active;
- }
- }
- this.listObj[i].is_active = !data.is_active;
- }
- }
- },
-
- // 单选
- changeSingleRadio(mch, item) {
- for (let i = 0; i < this.listObj.length; i++) {
- if (this.listObj[i].mch_id === mch.mch_id) {
- let goods_list_len = mch.goods_list.length;
- let active_num = 0;
- for (let j = 0; j < this.listObj[i].goods_list.length; j++) {
-
- if (item.id === this.listObj[i].goods_list[j].id) {
- this.listObj[i].goods_list[j].is_active = !this.listObj[i].goods_list[j].is_active;
- }
-
- if (this.listObj[i].goods_list[j].is_active) {
- active_num++;
- }
-
- if (this.editStatus === false && this.listObj[i].goods_list[j].new_status !== 0) {
- goods_list_len --;
- }
- }
- if (goods_list_len === active_num) {
- this.listObj[i].is_active = true;
- } else {
- this.listObj[i].is_active = false;
- }
- }
- }
- },
-
- selectAll(bool) {
- this.listObj.map(item => {
- item.is_active = bool;
- item.goods_list.map(good => {
- if (this.editStatus === false) {
- if (good.new_status === 0) {
- good.is_active = bool;
- }
- } else {
- good.is_active = bool;
- }
- })
- });
- },
- // 结算
- settlement() {
- let all_product = [];
- let is_miaosha = false;
- for (let i = 0; i < this.listObj.length; i++) {
- let mch = {
- mch_id: this.listObj[i].mch_id,
- goods_list: [],
- };
- for (let j = 0; j < this.listObj[i].goods_list.length; j++) {
- let good = this.listObj[i].goods_list[j];
- if (good.new_status === 0 && good.is_active) {
- if (good.sign === 'miaosha') is_miaosha = true;
- let product = {
- id: good.goods_id,
- attr: [
- ],
- num: good.num,
- cart_id: good.id,
- goods_attr_id: good.attr_id,
- };
- for (let n in good.attrs.attr) {
- let attr = {
- attr_id: good.attrs.attr[n].attr_id,
- attr_group_id: good.attrs.attr[n].attr_group_id,
- };
- product.attr.push(attr);
- }
- mch.goods_list.push(product);
- }
- }
- if (mch.goods_list.length > 0) {
- all_product.push(mch);
- }
- }
- let jump_url = `/pages/order-submit/order-submit?mch_list=${JSON.stringify(all_product)}`;
- if (is_miaosha) {
- jump_url += `&preview_url=${encodeURIComponent(this.$api.miaosha.order_preview)}&submit_url=${encodeURIComponent(this.$api.miaosha.order_submit)}`;
- }
- this.$jump({
- open_type: 'navigate',
- url: jump_url,
- });
- setTimeout(()=> {
- this.listObj = [];
- }, 1000);
- },
- editNum() {
- let editList = [];
- for (let i = 0; i < this.listObj.length; i++) {
- let goods = this.listObj[i].goods_list;
- for (let j = 0; j < goods.length; j++) {
- if (goods[j].new_status === 0) {
- editList.push({
- goods_id: goods[j].goods_id,
- num: goods[j].num,
- attr: goods[j].attr_id,
- })
- }
- }
- }
- if (!editList.length) return;
- this.$request({
- method: 'post',
- url: this.$api.cart.edit,
- data: {
- list: JSON.stringify(editList)
- }
- }).then(() => {
- })
- },
-
- // 删除商品
- async deleteProduct() {
- let product_list = [];
-
- for (let i = 0 ; i < this.listObj.length; i++) {
- let goods = this.listObj[i].goods_list;
- for (let j = 0 ; j < goods.length; j++) {
- if (goods[j].is_active) {
- product_list.push({
- mch_id: goods[j].mch_id,
- id: goods[j].id,
- })
- }
- }
- }
-
- if (!product_list.length) return;
- const res = await this.$request({
- method: 'post',
- url: this.$api.cart.delete,
- data: {
- cart_id_list: JSON.stringify(product_list),
- }
- });
- if (res.code === 0) {
- this.editStatus = false;
- for (let j = 0; j < this.listObj.length; j++) {
- for (let i = 0; i < product_list.length; i++) {
- if (this.listObj[j].mch_id == product_list[i].mch_id) {
- for (let k = 0; k < this.listObj[j].goods_list.length; k++) {
- if (this.listObj[j].goods_list[k].id == product_list[i].id) {
- this.$delete(this.listObj[j].goods_list, k);
- }
- }
- if (this.listObj[j].goods_list.length === 0) {
- this.$delete(this.listObj, j);
- }
- }
- }
- }
- }
- },
- b() {
- let currentRoute = this.currentRoute;
- for (let i = 0; i < this.tabBarNavs.length; i++) {
- if(currentRoute.includes(this.tabBarNavs[i].url.split('?')[0])) {
- return this.tabbarbool = true;
- }
- }
- return this.tabbarbool = false;
- },
- change(number, id, mch_id) {
- for (let i = 0; i < this.listObj.length; i++) {
- if (this.listObj[i].mch_id === mch_id) {
- for (let j = 0; j < this.listObj[i].goods_list.length; j++) {
- if (this.listObj[i].goods_list[j].id === id) {
- this.listObj[i].goods_list[j].num = Number(number);
- return;
- }
- }
- }
- }
- },
-
- setTime(data) {
- let is_spike = false;
- for (let i = 0; i < data.length; i++) {
- for (let j = 0; j < data[i].goods_list.length; j++) {
- if (data[i].goods_list[j].sign === 'miaosha' && data[i].goods_list[j].miaosha_status === 1) {
- is_spike = true;
- data[i].goods_list[j].miaosha_time--;
- let second = parseInt(data[i].goods_list[j].miaosha_time);
- let minute = 0;
- let hour = 0;
- if (second > 60) {
- minute = parseInt(second / 60);
- second = parseInt(second % 60);
- if (minute > 60) {
- hour = parseInt(minute / 60);
- minute = parseInt(minute % 60);
- }
- }
- let timeDaily = {
- h: hour < 10 ? ('0' + hour) : hour,
- m: minute < 10 ? ('0' + minute) : minute,
- s: second < 10 ? ('0' + second) : second,
- };
- this.$set(this.listObj[i].goods_list[j], 'miaosha_string', `${timeDaily.h + ':' + timeDaily.m + ':' + timeDaily.s}`);
- }
- }
- }
- return is_spike;
- },
- spikeTime(data) {
- clearInterval(this.spike);
- let is_spike = this.setTime(data);
- if (!is_spike) return;
- this.spike = setInterval(() => {
- let is_spike = this.setTime(data);
- if (!is_spike) {
- clearInterval(this.spike);
- }
- }, 1000);
- }
- },
- onLoad() {
- setTimeout(() => {
- this.getProductList();
- this.all = false;
- }, 1000);
- },
- onShow() {
- this.submitDis = true;
- this.getProductList();
- this.all = false;
- },
- onHide() {
- this.editNum();
- clearInterval(this.spike);
- },
- onUnload() {
- this.editNum();
- clearInterval(this.spike);
- },
- watch:{
- listObj: {
- handler(listObj) {
- this.priceNum = 0;
- let listNum = 0;
- let activeIndex = 0;
- for (let i = 0; i < listObj.length; i++) {
- listNum += listObj[i].goods_list.length;
- let status = listObj[i].goods_list.length;
- let newStatus = 0;
- for (let j = 0; j < listObj[i].goods_list.length;j++) {
- if (listObj[i].goods_list[j].new_status !== 0) {
- newStatus++;
- }
- if (this.editStatus === false && listObj[i].goods_list[j].new_status !== 0) {
- listNum --;
- }
- if (listObj[i].goods_list[j].is_active) {
- if (!this.editStatus) {
- let num = Number(listObj[i].goods_list[j].attrs.price) * Number(listObj[i].goods_list[j].num);
- this.priceNum += num;
- }
- activeIndex++;
- }
- }
- if (status === newStatus) {
- this.listObj[i].new_status = 1;
- }
- }
- if (activeIndex === listNum) {
- if (listNum === 0 && activeIndex === 0) {
- this.all = false;
- } else {
- this.all = true;
- }
- } else {
- this.all = false;
- }
-
- if (activeIndex > 0) {
- this.submitDis = false;
- } else {
- this.submitDis = true;
- }
- this.priceNum = this.priceNum.toFixed(2);
- },
- deep: true,
- },
- tabBarNavs: {
- handler: function() {
- this.b();
- },
- immediate: true,
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .app-cart {
- background-color: #f7f7f7;
- position: absolute;
- top: 0;
- left: 0;
- width: #{750rpx};
- .app-announcement {
- width: 100%;
- height: #{72rpx};
- background-color: #ffffff;
- .app-announcement-text {
- font-size: #{26rpx};
- color: #999999;
- margin-left: #{24rpx};
- }
- .app-edit-text {
- font-size: #{26rpx};
- color: #353535;
- margin-right: #{32rpx};
- }
- }
- .app-settlement {
- width: 100%;
- height: #{100rpx};
- z-index: 1500;
- position: fixed;
- left: 0;
- border-top: #{1rpx} solid #e2e2e2;
- background-color: white;
- .app-radio {
- padding-left: #{23rpx};
- .app-price {
- margin-left: #{24rpx};
- font-size: #{28rpx};
- color: #ff4544;
- }
- }
- .app-all-text {
- margin-left: #{9rpx};
- font-size: #{25rpx};
- color: #3f3f3f;
- }
- .app-delete {
- width: #{140rpx};
- height: #{64rpx};
- line-height: #{64rpx};
- text-align: center;
- background-color: white;
- border-radius: #{32rpx};
- border: #{1rpx} solid #ff4544;
- color: #ff4544;
- font-size: #{28rpx};
- margin: #{0 24rpx 0 0};
- padding: 0;
- }
- .delete-disabled {
- color: #989898;
- border: #{1rpx} solid #989898;
- }
- .app-settlement-button {
- background-color: #ff4544;
- height: #{100rpx};
- width: #{250rpx};
- color: #ffffff;
- font-size: #{30rpx};
- line-height: #{100rpx};
- text-align: center;
- border-radius: 0;
- margin: 0;
- padding: 0;
- border: none;
- }
-
- .disabled-background {
- background-color: #989898;
- }
- }
- .no-cart {
- width: 100%;
- .cart-icon {
- width: #{160rpx};
- height: #{160rpx};
- border-radius: 50%;
- background-color: rgba(0, 0, 0, 0.1);
- margin: #{150rpx auto 40rpx};
- .cart-image {
- height: #{80rpx};
- width: #{80rpx};
- margin: #{40rpx};
- }
- }
- .cart-text {
- font-size: #{30rpx};
- color: #888;
- text-align: center;
-
- }
- }
- }
- </style>
|