123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834 |
- <template>
- <mobile-frame :frameStyle="frameStyle" @toPath="toPath">
- <view class="main">
- <view class="top" v-if="list.length !=0">
- <view class="text_1">
- <checkbox class="checkbox" @click="selectAll(false)" :checked="isAll">全选</checkbox>
- </view>
- <view class="text_2" v-if="num==0" @click="edit(1)">编辑</view>
- <view class="text_2" v-if="num==1" @click="edit(0)">完成</view>
- </view>
- <view class="one" v-if="!user._id">
- <view class="logo"><text class="iconfont icon-geren2"></text></view>
- <view class="one_1">您还没有登录</view>
- <view class="btn">
- <button type="primary" size="mini" @click="toCommon('/pages/login/index')">去登录</button>
- </view>
- </view>
- <view class="one" v-if="list.length==0&&user._id">
- <view class="logo"><text class="iconfont icon-gouwuche"></text></view>
- <view class="one_1">购物车空空如也~</view>
- <view class="btn">
- <button type="primary" size="mini" @click="toCommon('/pages/home/index')">去逛逛</button>
- </view>
- </view>
- <view class="two" v-if="list.length !=0">
- <scroll-view scroll-y="true" class="scroll-view">
- <view class="list-scroll-view">
- <checkbox-group name="checkbox" @change="marketChange">
- <view class="list" v-for="(item, index) in list" :key="index">
- <view class="list_1" v-if="item.is_set=='0'">
- <view class="title">
- <checkbox :value="item._id" :checked="item.check">
- 优惠套装---{{item.name||'暂无名称'}}</checkbox>
- </view>
- <view class="content" v-for="(gs,indexx) in item.goods" :key="indexx">
- <view class="img">
- <image :src="getFile(gs)"></image>
- </view>
- <view class="one_1" @click="toCommon('/pagesHome/order/detail',gs)">
- <view class="name"><text class="set">套装</text> {{gs.goods_name}}
- </view>
- <view class="info">
- <view class="title_1" v-if="gs.spec_name">
- <text>规格:{{gs.spec_name}}</text>
- </view>
- </view>
- </view>
- <view class="money">
- <view>{{gs.set_num}}件/套x{{gs.set_num}}</view>
- </view>
- </view>
- <view class="total">
- <view class="total_1"> <text>套装价</text>¥{{item.sell_money||0}} </view>
- <view class="total_2">
- <view class="num">
- <uni-number-box @change="changeValue(item)" name="num" value="item"
- :min="1" v-model="item.num" />
- </view>
- <view class="del">
- <text class="iconfont icon-del-copy" @click="toDel(item)"></text>
- </view>
- </view>
- </view>
- </view>
- <view class="list_2" v-else>
- <view class="title">
- <checkbox :value="item.shop" :checked="item.check">
- <text class="iconfont icon-shangdian"></text>
- {{item.shop_name}}
- </checkbox>
- </view>
- <checkbox-group name="checkbox" @change="goodsChange">
- <view class="content" v-for="gs in item.goods" :key="gs.goodsSpec_id">
- <view class="box">
- <checkbox :value="gs.goodsSpec_id" :checked="gs.check" />
- </view>
- <view class="img">
- <image :src="getFile(gs)"></image>
- </view>
- <view class="one_1" v-if="num==0"
- @click="toCommon('/pagesHome/order/detail',gs)">
- <view class="name">{{gs.goods_name}}</view>
- <view class="info">
- <view class="title_1" v-if="gs.goodsSpec_name">
- <text>规格:{{gs.goodsSpec_name}}</text>
- </view>
- </view>
- <view class="info">
- <text v-if="gs.gift&&gs.gift.length>0" class="gift">赠品</text>
- <text v-if="gs.sp_price" class="act">特价</text>
- </view>
- </view>
- <view class="money" v-if="num==0">
- <view>¥{{gs.price}}</view>
- <view>x{{gs.num}}</view>
- </view>
- <view class="num" v-if="num==1">
- <uni-number-box @change="changeValue(gs)" name="num" value="gs" :min="1"
- v-model="gs.num" />
- </view>
- <view class="del" v-if="num==1">
- <text class="iconfont icon-del-copy" @click="toDel(gs)"></text>
- </view>
- </view>
- </checkbox-group>
- </view>
- </view>
- </checkbox-group>
- </view>
- </scroll-view>
- </view>
- <view class="foot" v-if="list.length !=0">
- <view class="total">总价:<text>¥{{totalMoney}}(不含运费)</text></view>
- <view class="btn" v-if="hasCheck()">
- <button type="primary" size="mini" @click="toSettle()" v-if="num==0">提交订单</button>
- <button type="primary" size="mini" @click="toDel()" v-if="num==1">删除</button>
- </view>
- </view>
- </view>
- </mobile-frame>
- </template>
- <script>
- export default {
- data() {
- return {
- frameStyle: {
- useBar: true
- },
- // 用户
- user: {},
- // 购物车列表
- list: [],
- // 全选
- isAll: false,
- // 编辑/完成按钮传的数字
- num: 0,
- // 总额
- totalMoney: 0,
- };
- },
- onLoad() {},
- onShow() {
- const that = this;
- // 监听登录
- that.watchLogin();
- },
- methods: {
- // 监听登录
- watchLogin() {
- const that = this;
- uni.getStorage({
- key: 'token',
- success: (res) => {
- let user = that.$jwt(res.data);
- if (user) {
- that.$set(that, `user`, user)
- that.searchMarket();
- }
- },
- fail: (err) => {}
- })
- },
- // 查询购物车信息
- async searchMarket() {
- const that = this;
- const res = await that.$api(`/cart/selfCart`, 'GET', {
- customer: that.user.id
- });
- if (res.errcode == '0') {
- that.$set(that, `list`, res.data)
- }
- },
- // 编辑
- edit(num) {
- const that = this;
- that.$set(that, `num`, num)
- },
- //全选
- selectAll(e) {
- const that = this;
- const list = that.list;
- const isAll = that.isAll;
- let data = [];
- for (const val of list) {
- let a = isAll ? false : true;
- val.check = a;
- for (let s of val.goods) {
- s.check = a
- }
- data.push(val);
- }
- that.$set(that, `list`, data)
- // 计算总额
- that.countMoney();
- // 赋值是否全选
- that.$set(that, `isAll`, isAll ? false : true)
- },
- // 选择店铺
- marketChange(e) {
- const that = this;
- const list = this.list;
- const {
- value
- } = e.detail;
- for (const p1 of list) {
- let p2;
- // 判断是套装还是正常商品
- if (p1.is_set == '0') p2 = value.find((i) => i == p1._id);
- else p2 = value.find((i) => i == p1.shop);
- let a = p2 ? true : false;
- p1.check = a;
- for (let s of p1.goods) {
- s.check = a
- }
- }
- that.$set(that, `list`, list);
- that.marketAllChange();
- // 计算总额
- that.countMoney();
- },
- //店铺全部选择true,全选自动选择,
- marketAllChange() {
- const that = this;
- let list = that.list;
- for (let val of list) {
- if (val.check == true) {
- that.$set(that, `isAll`, true)
- } else {
- that.$set(that, `isAll`, false)
- }
- }
- that.$set(that, `list`, list)
- // 计算总额
- that.countMoney();
- },
- //选择商品
- goodsChange(e) {
- const that = this;
- let list = that.list;
- const {
- value
- } = e.detail;
- let shop = list.find(f => f.goods.find(i => value.find(s => s == i.goodsSpec_id)))
- if (shop) {
- for (let val of shop.goods) {
- let p2 = value.find((i) => i == val.goodsSpec_id);
- if (p2) val.check = true;
- else val.check = false;
- }
- } else {
- list = list.map(i => {
- i.goods = i.goods.map(g => ({
- ...g,
- check: false
- }))
- return i;
- })
- }
- that.$set(that, `list`, list)
- that.goodsAllChange();
- // 计算总额
- that.countMoney();
- },
- //商品全部选择true,店铺自动选择,
- // 所有店铺为true,则全选为true
- goodsAllChange() {
- const that = this;
- let list = that.list;
- list = list.map(i => {
- const isAllSelect = i.goods.every(f => f.check);
- if (isAllSelect) i.check = true;
- else i.check = false;
- return i;
- })
- const allSelect = list.every(e => e.check)
- if (allSelect) this.$set(this, `isAll`, true)
- else this.$set(this, `isAll`, false)
- // 计算总额
- that.countMoney();
- },
- // 加减商品数量
- async changeValue(value) {
- const that = this;
- this.$nextTick(async () => {
- let res;
- // 判断是套装还是正常商品
- if (value.is_set == '0') {
- const {
- _id: set_id,
- num,
- cart_id: cartId
- } = value
- res = await that.$api(`/cart/checkGoodsNum`, 'GET', {
- cartId,
- set_id,
- num
- });
- } else {
- const {
- goodsSpec_id: goodsSpecId,
- num,
- cart_id: cartId
- } = value
- res = await that.$api(`/cart/checkGoodsNum`, 'GET', {
- cartId,
- goodsSpecId,
- num
- });
- }
- if (res.errcode === 0) {
- const {
- enough,
- total
- } = res.data
- if (!enough) {
- uni.showToast({
- title: `库存最大为${total}`,
- icon: 'error',
- });
- // 将该商品的库存量修改为最大值
- value.num = total;
- }
- }
- // 计算总额
- that.countMoney();
- })
- },
- // 删除, 接口,购物车删除,然后将该数据移除
- async toDel(e) {
- let list = this.list;
- let set_list = [];
- let goods_list = [];
- uni.showModal({
- title: '提示',
- content: '请选择要删除的商品',
- success: async (res) => {
- if (!res.confirm) return
- if (e?.cart_id) {
- const result = await this.$api(`/cart/${e.cart_id}`, 'Delete');
- if (result.errcode === 0) {
- // 判断是套装还是正常商品
- set_list = list.filter(i => i.is_set == '0')
- goods_list = list.filter(i => i.is_set !== '0')
- set_list = set_list.filter(i => i.cart_id !== e.cart_id)
- goods_list = goods_list.map(i => ({
- ...i,
- goods: i.goods.filter(f => f.cart_id !== e.cart_id)
- }))
- this.$set(this, `list`, [...set_list, ...goods_list]);
- // 检查店铺内是否还有商品
- this.checkShopGoodsExist();
- // 计算总额
- this.countMoney();
- }
- } else {
- let goodsList = [];
- let cartIds = [];
- for (let val of this.list) {
- // 判断是套装还是正常商品
- if (val.is_set == '0') goodsList.push(val)
- else goodsList.push(val.goods)
- }
- for (let val of goodsList) {
- // 判断是套装还是正常商品
- if (val.is_set == '0') {
- if (val.check) cartIds.push(val.cart_id)
- } else {
- for (let set of val) {
- if (set.check) cartIds.push(set.cart_id)
- }
- }
- }
- set_list = list.filter(i => i.is_set == '0')
- goods_list = list.filter(i => i.is_set !== '0')
- for (let val of cartIds) {
- const result = await this.$api(`/cart/${val}`, 'Delete');
- if (result.errcode === 0) {
- // 判断是套装还是正常商品
- set_list = set_list.filter(i => i.cart_id !== val)
- goods_list = goods_list.map(i => ({
- ...i,
- goods: i.goods.filter(f => f.cart_id !== val)
- }))
- this.$set(this, `list`, [...set_list, ...goods_list]);
- // 检查店铺内是否还有商品
- this.checkShopGoodsExist();
- // 计算总额
- this.countMoney();
- }
- }
- }
- }
- });
- },
- //检查店铺内是否还有商品
- checkShopGoodsExist() {
- let list = this.list;
- list = list.filter(f => f.goods && f.goods.length > 0)
- this.$set(this, `list`, list);
- },
- // 计算总额
- countMoney() {
- const that = this;
- const list = that.list;
- let totalMoney = 0;
- // 渲染结束执行下面方法
- that.$nextTick(() => {
- for (const val of list) {
- // 判断是套装还是正常商品
- if (val.is_set == '0') {
- if (val.check == true) {
- let total = that.$multiply(val.sell_money, val.num);
- totalMoney += Number(total);
- }
- } else {
- for (let s of val.goods) {
- if (s.check == true) {
- let total = that.$multiply(s.price, s.num);
- totalMoney += Number(total);
- }
- }
- }
- }
- that.$set(that, `totalMoney`, totalMoney.toFixed(2))
- })
- },
- getFile(data) {
- const file = data.file;
- if (!file) return '';
- if (file.length && file.length > 0) return file[0].url
- },
- // 去结算
- async toSettle() {
- // 将选中的购物车放到数组中,传回服务端进行检查.然后拿着key去订单页请求数据
- let goodsList = [];
- let cartIds = [];
- for (let val of this.list) {
- // 判断是套装还是正常商品
- if (val.is_set == '0') goodsList.push(val)
- else goodsList.push(val.goods)
- }
- for (let val of goodsList) {
- // 判断是套装还是正常商品
- if (val.is_set == '0') {
- if (val.check) cartIds.push(val.cart_id)
- } else {
- for (let set of val) {
- if (set.check) cartIds.push(set.cart_id)
- }
- }
- }
- const res = await this.$api(`/util/checkCartBuy`, 'POST', {
- cartIds
- });
- if (res.errcode == '0') {
- const {
- data
- } = res
- if (data.result) {
- const key = data.key;
- uni.navigateTo({
- url: `/pagesHome/order/order?key=${key}`
- })
- }
- }
- },
- // 是否选中商品,控制提交订单按钮
- hasCheck() {
- return this.list.some(e => e.goods.some(eg => eg.check))
- },
- // 公共跳转
- toCommon(route, e) {
- uni.navigateTo({
- url: `${route}?id=${e && e.goods_id}`
- })
- },
- toPath(e) {
- let url = `/${e.route}`;
- if (e.type == '0') uni.redirectTo({
- url
- })
- else {
- uni.navigateTo({
- url
- })
- }
- }
- }
- }
- </script>
- <style lang="scss">
- .main {
- display: flex;
- flex-direction: column;
- width: 100vw;
- height: 92vh;
- background-color: var(--footColor);
- .top {
- display: flex;
- flex-direction: row;
- background-color: var(--mainColor);
- height: 35px;
- margin: 0 0 4px 0;
- padding: 5px 4vw;
- .text_1 {
- flex-grow: 1;
- line-height: 30px;
- }
- .text_2 {
- line-height: 35px;
- }
- }
- .one {
- text-align: center;
- margin: 2vw 0;
- .logo {
- margin: 10vw 0 2vw 0;
- .iconfont {
- font-size: 35vw;
- }
- }
- .one_1 {
- margin: 3vw 0;
- }
- button {
- background-color: var(--fFB1Color);
- }
- }
- .two {
- position: relative;
- flex-grow: 1;
- margin: 0 2vw;
- .list_1 {
- background-color: var(--mainColor);
- margin: 2vw 0 2vw 0;
- padding: 2vw 3vw;
- border-radius: 4px;
- .title {
- border-bottom: 1px solid var(--fcColor);
- padding: 0 0 2vw 0;
- font-size: var(--font18Size);
- text {
- margin: 0 1vw;
- }
- }
- .content {
- display: flex;
- flex-direction: row;
- padding: 2vw 0;
- font-size: var(--font16Size);
- border-bottom: 1px dashed var(--fcColor);
- .img {
- width: 20vw;
- height: 20vw;
- border-radius: 2vw;
- border: 1px solid var(--fcColor);
- image {
- width: 20vw;
- height: 20vw;
- }
- }
- .one_1 {
- margin: 0 2.5vw;
- flex-grow: 1;
- .name {
- .set {
- margin: 0 1vw 0 0;
- font-size: 12px;
- border-radius: 5px;
- padding: 0 1vw;
- color: #ffffff;
- background-color: #FF6347;
- border: 1px solid #FFA500;
- }
- }
- .info {
- width: 100%;
- display: flex;
- flex-direction: row;
- .title_1 {
- font-size: 12px;
- color: #666;
- margin-top: 10px;
- text {
- background-color: #eee;
- padding: 5px;
- }
- }
- .gift {
- margin: 2vw 1vw 0 0;
- font-size: 12px;
- color: #FFA500;
- border: 1px solid #FFA500;
- border-radius: 5px;
- padding: 0 1vw;
- }
- .act {
- margin: 2vw 0 0 0;
- font-size: 12px;
- border-radius: 5px;
- padding: 0 1vw;
- border: 1px solid var(--fFB1Color);
- color: var(--fFB1Color);
- }
- }
- }
- .money {
- font-size: 14px;
- margin-top: 2vw;
- text-align: right;
- flex-grow: 1;
- }
- }
- .total {
- display: flex;
- justify-content: space-between;
- padding: 1vw;
- .total_1 {
- font-size: 20px;
- color: var(--fFB1Color);
- text {
- font-size: 12px;
- }
- }
- .total_2 {
- display: flex;
- justify-content: space-between;
- align-items: center;
- .del {
- margin: 0 0 0 3vw;
- }
- }
- }
- }
- .list_2 {
- background-color: var(--mainColor);
- margin: 2vw 0 2vw 0;
- padding: 2vw 3vw;
- border-radius: 4px;
- .title {
- border-bottom: 1px solid var(--fcColor);
- padding: 0 0 2vw 0;
- font-size: var(--font18Size);
- text {
- margin: 0 1vw;
- }
- }
- .content {
- display: flex;
- flex-direction: row;
- padding: 2vw 0;
- font-size: var(--font16Size);
- border-bottom: 1px dashed var(--fcColor);
- .box {
- line-height: 20vw;
- }
- .img {
- width: 20vw;
- height: 20vw;
- border-radius: 2vw;
- border: 1px solid var(--fcColor);
- image {
- width: 20vw;
- height: 20vw;
- }
- }
- .one_1 {
- margin: 0 2.5vw;
- flex-grow: 1;
- .info {
- width: 100%;
- display: flex;
- flex-direction: row;
- .title_1 {
- font-size: 12px;
- color: #666;
- margin-top: 10px;
- text {
- background-color: #eee;
- padding: 5px;
- }
- }
- .gift {
- margin: 2vw 1vw 0 0;
- font-size: 12px;
- color: #FFA500;
- border: 1px solid #FFA500;
- border-radius: 5px;
- padding: 0 1vw;
- }
- .act {
- margin: 2vw 0 0 0;
- font-size: 12px;
- border-radius: 5px;
- padding: 0 1vw;
- border: 1px solid var(--fFB1Color);
- color: var(--fFB1Color);
- }
- }
- }
- .money {
- margin-top: 2vw;
- text-align: right;
- flex-grow: 1;
- }
- .num {
- margin: 6vw 4vw;
- font-size: 20px;
- }
- .del {
- margin: 6vw 0;
- text-align: right;
- flex-grow: 1;
- }
- }
- }
- }
- .foot {
- background-color: var(--fffColor);
- display: flex;
- flex-direction: row;
- justify-content: space-between;
- height: 44px;
- padding: 0 0 0 6vw;
- border-right: 1px solid var(--f99Color);
- .total {
- flex-grow: 1;
- display: flex;
- align-content: flex-end;
- line-height: 40px;
- text {
- color: var(--fFB1Color);
- }
- }
- .btn {
- button {
- width: 30vw;
- height: 44px;
- line-height: 44px;
- border-radius: 0px;
- background-color: var(--fFB1Color);
- text-align: center;
- font-weight: normal;
- font-size: var(--font15Size);
- color: var(--fffColor);
- }
- }
- }
- }
- .scroll-view {
- position: absolute;
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- .list-scroll-view {
- display: flex;
- flex-direction: column;
- }
- }
- // 复选框样式
- checkbox .wx-checkbox-input {
- width: 40rpx;
- height: 40rpx;
- border-radius: 50%;
- border-color: var(--f99Color);
- background-color: var(--mainColor);
- }
- // 复选框选中样式
- checkbox .wx-checkbox-input.wx-checkbox-input-checked {
- border-color: var(--fFB1Color);
- background-color: var(--fFB1Color);
- }
- // 复选框选中之后对号的样式
- checkbox .wx-checkbox-input.wx-checkbox-input-checked::before {
- color: var(--fffColor);
- }
- </style>
|