diff --git a/egg/app/controller/newShop.js b/egg/app/controller/newShop.js new file mode 100644 index 0000000000000000000000000000000000000000..dbae6dc86685d7baf491c75ef0f3406a5c3b3cca --- /dev/null +++ b/egg/app/controller/newShop.js @@ -0,0 +1,112 @@ +"use strict"; + +const { Controller } = require("egg"); + +class NewShopController extends Controller { + //添加到购物车 + async newShop() { + const { ctx, app } = this; + const list = ctx.request.body; + const flag = await app.mysql.get("carlist", { shopName: list.name }); + if (!flag) { + await app.mysql.insert("carlist", { + shopId: list.id, + shopName: list.name, + shopImgSrc: list.img, + weight: list.weight, + price: list.price, + num: 1, + flag: true, + }); + ctx.body = { + code: 200, + mag: "添加成功", + data: list, + }; + } else { + ctx.body = { + code: 200, + mag: "已经添加", + }; + } + } + + //获取购物车列表 + async getNewhop() { + const { ctx, app } = this; + let numAll = 0; + let priceAll = 0; + let count = 0; + let carlist = await app.mysql.select("carlist"); + carlist.forEach((item) => { + numAll += item.num * 1; + if (item.flag) { + count = item.num * item.price; + priceAll += count; + } + }); + ctx.body = { + code: 200, + list: carlist, + msg: "获取成功", + numAll: numAll, + priceAll: priceAll, + }; + } + + //点击全选框 + async clickCheckAll() { + const { ctx, app } = this; + const { checked } = ctx.request.body; + app.mysql.query(`UPDATE carlist SET flag = ${checked}`); + ctx.body = { + code: 200, + msg: "修改成功", + }; + } + //点击加号 + + async clickAdd() { + const { ctx, app } = this; + const { id, num } = ctx.request.body; + await app.mysql.update( + "carlist", + { + num: num * 1 + 1, + }, + { + where: { id: id }, + } + ); + ctx.body = { + code: 200, + msg: "修改成功", + }; + } + + //点击减号 + + async clickMinus() { + const { ctx, app } = this; + const { id, num } = ctx.request.body; + if (num === 1) { + await app.mysql.delete("carlist", { id: id }); + } else { + await app.mysql.update( + "carlist", + { + num: num * 1 - 1, + }, + { + where: { id: id }, + } + ); + } + ctx.body = { + code: 200, + msg: "修改成功", + }; + } +} + +module.exports = NewShopController; diff --git a/egg/app/mock/index.js b/egg/app/mock/index.js index 0169189ac4c405ba0fc5cfb8e0aa97d815e00dfd..6ceb2c352573555f6f3818880179d855abd8fb02 100644 --- a/egg/app/mock/index.js +++ b/egg/app/mock/index.js @@ -35,11 +35,13 @@ const userList = Mock.mock({ ], "rightlist|3-5": [ { + "id|+1": 1, name: "@cword(2,3)", //商品名字 img: "@image(50x50,@color)", //商品图片 "weight|50-300": 0, //商品重量 "MonthlyS|10-100": 0, //商品月售 "price|10-50": 0, //商品单价 + num: 0, }, ], }, diff --git a/egg/app/router.js b/egg/app/router.js index de6be394bf023c21c0edad8e5d35271844e9cc2c..d9601e9ff91d24887ede61cbbf3cf256c4b486e5 100644 --- a/egg/app/router.js +++ b/egg/app/router.js @@ -12,4 +12,9 @@ module.exports = (app) => { router.post("/api/get/shop", controller.shop.getShop); router.post("/api/get/shopDetail", controller.detail.getDetail); router.post("/api/get/shopDetaillist", controller.detail.getDetailList); + router.post("/api/carlist", controller.newShop.newShop); + router.post("/api/get/carlist", controller.newShop.getNewhop); + router.post("/api/click/carlist", controller.newShop.clickCheckAll); + router.post("/api/add/carlist", controller.newShop.clickAdd); + router.post("/api/min/carlist", controller.newShop.clickMinus); }; diff --git a/vue-app/components.d.ts b/vue-app/components.d.ts index e822b377bf1732a9f9347563dfec2b030a4889da..45759a30e3d00aff6bb1923903c52044fbc84cee 100644 --- a/vue-app/components.d.ts +++ b/vue-app/components.d.ts @@ -14,13 +14,16 @@ declare module 'vue' { RouterLink: typeof import('vue-router')['RouterLink'] RouterView: typeof import('vue-router')['RouterView'] Search: typeof import('./src/components/Search/Search.vue')['default'] + SearchResults: typeof import('./src/components/searchResults/searchResults.vue')['default'] VanButton: typeof import('vant/es')['Button'] VanCellGroup: typeof import('vant/es')['CellGroup'] + VanCheckbox: typeof import('vant/es')['Checkbox'] VanField: typeof import('vant/es')['Field'] VanForm: typeof import('vant/es')['Form'] VanIcon: typeof import('vant/es')['Icon'] VanList: typeof import('vant/es')['List'] VanNavBar: typeof import('vant/es')['NavBar'] + VanPopup: typeof import('vant/es')['Popup'] VanSearch: typeof import('vant/es')['Search'] VanSidebar: typeof import('vant/es')['Sidebar'] VanSidebarItem: typeof import('vant/es')['SidebarItem'] diff --git a/vue-app/src/api/index.js b/vue-app/src/api/index.js index cd785d261f6e4d6e1e1395081c2831ccf832d1c7..88b39ef787367f3939d0d5d9d3f6440eb7c5ddcb 100644 --- a/vue-app/src/api/index.js +++ b/vue-app/src/api/index.js @@ -44,3 +44,48 @@ export const apiGetShopDetailList = (data) => { data, }); }; + +//详情页购物车数据 +export const apiGetCarList = (data) => { + return request({ + url: "/api/carlist", + method: "post", + data, + }); +}; + +//获取购物车数据 +export const apiGetCarListFn = (data) => { + return request({ + url: "/api/get/carlist", + method: "post", + data, + }); +}; + +//点击全选框 +export const apiClickCke = (data) => { + return request({ + url: "/api/click/carlist", + method: "post", + data, + }); +}; + +//点击加号 +export const apiClickAdd = (data) => { + return request({ + url: "/api/add/carlist", + method: "post", + data, + }); +}; + +//点击减号 +export const apiClickMin = (data) => { + return request({ + url: "/api/min/carlist", + method: "post", + data, + }); +}; diff --git a/vue-app/src/components/Detail/Detail.vue b/vue-app/src/components/Detail/Detail.vue index 5616b4f3541825d55fcc20f32a207b7afb908d7d..300240a51b43d64d5ae6fb3a5ea6185cdf643268 100644 --- a/vue-app/src/components/Detail/Detail.vue +++ b/vue-app/src/components/Detail/Detail.vue @@ -1,9 +1,9 @@ - + - + @@ -75,11 +75,15 @@ @@ -87,13 +91,109 @@ + + + + + + + 数量:{{ numAll }} + 总计: + ¥{{ priceAll }} + + + 去结算 + + + + + + + + 全选 + + + 清空购物车 + + + + + + + + + + + + + + + + {{ item.shopName }}{{ item.weight }}g/份 + + + + ¥{{ item.price }} + + ¥{{ item.price }} + + + + + + + + + + + + + + + + + 数量:{{ numAll }} + 总计: + ¥{{ priceAll }} + + + 去结算 + + diff --git a/vue-app/src/components/searchResults/searchResults.vue b/vue-app/src/components/searchResults/searchResults.vue new file mode 100644 index 0000000000000000000000000000000000000000..6f72cb2c91bcd6240c153d0011080930e38da7e6 --- /dev/null +++ b/vue-app/src/components/searchResults/searchResults.vue @@ -0,0 +1,7 @@ + + + + + + +