| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- import DictionariesBehavior from "../../../../core/behavior/dictionaries.behavior";
- import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
- import tickleBehavior, {
- getTickleContext,
- } from "../../../../core/behavior/tickle.behavior";
- // module/order/pages/select-goods/select-goods.ts
- Component({
- behaviors: [PageContainerBehavior, DictionariesBehavior, tickleBehavior],
- lifetimes: {
- attached() {
- // 初始化商品数据(示例数据,实际应从接口获取)
- this.initGoodsData();
- },
- },
- properties: {
- schemeId: { type: String, value: "" },
- },
- data: {
- goodsList: [] as Array<{
- category: string;
- goods: Array<{
- id: string;
- name: string;
- description?: string;
- image: string;
- price: number;
- quantity: number;
- checked: boolean;
- badge?: string;
- }>;
- }>,
- selectAll: true,
- selectedCount: 0,
- totalPrice: 0,
- },
- observers: {
- 'goodsList': function() {
- this.calculateSummary();
- }
- },
- methods: {
- // 初始化商品数据
- initGoodsData() {
- // 示例数据,实际应从接口获取
- const goodsList = [
- {
- category: "穴位贴敷",
- goods: [
- {
- id: "1",
- name: "肝血虚穴位贴",
- description: "20贴",
- image: "/assets/icon/icon_file@3x.png",
- price: 100,
- quantity: 1,
- checked: true,
- }
- ]
- },
- {
- category: "运动",
- goods: [
- {
- id: "2",
- name: "五禽戏",
- image: "/assets/icon/icon_file@3x.png",
- price: 0,
- quantity: 1,
- checked: true,
- badge: "1",
- },
- {
- id: "3",
- name: "颈椎操",
- image: "/assets/icon/icon_file@3x.png",
- price: 0,
- quantity: 1,
- checked: true,
- }
- ]
- },
- {
- category: "茶饮",
- goods: [
- {
- id: "4",
- name: "元气茶",
- description: "30包",
- image: "/assets/icon/icon_file@3x.png",
- price: 80,
- quantity: 1,
- checked: true,
- },
- {
- id: "5",
- name: "芡实米仁燕麦粥",
- description: "21袋",
- image: "/assets/icon/icon_file@3x.png",
- price: 1.5,
- quantity: 1,
- checked: true,
- }
- ]
- }
- ];
-
- this.setData({ goodsList });
- this.calculateSummary();
- },
-
- // 商品复选框变化
- onGoodsCheckChange(e: any) {
- const { categoryIndex, goodsIndex } = e.currentTarget.dataset;
- const checked = e.detail.checked;
- const goodsList = this.data.goodsList;
- const goods = goodsList[categoryIndex].goods[goodsIndex];
-
- goods.checked = checked;
-
- // 如果勾选且数量为0,设置为1
- if (checked && goods.quantity === 0) {
- goods.quantity = 1;
- }
-
- // 单价为0的商品,如果勾选,确保数量为1
- if (checked && goods.price === 0 && goods.quantity !== 1) {
- goods.quantity = 1;
- }
-
- this.setData({ goodsList });
- this.calculateSummary();
- },
-
- // 全选变化
- onSelectAllChange(e: any) {
- const checked = e.detail.checked;
- const goodsList = this.data.goodsList.map(category => ({
- ...category,
- goods: category.goods.map(goods => ({
- ...goods,
- checked: checked,
- quantity: checked && goods.quantity === 0 ? 1 : goods.quantity
- }))
- }));
-
- this.setData({
- goodsList,
- selectAll: checked
- });
- this.calculateSummary();
- },
-
- // 数量变化(加减按钮)
- onQuantityChange(e: any) {
- const { categoryIndex, goodsIndex, type } = e.currentTarget.dataset;
- const goodsList = this.data.goodsList;
- const goods = goodsList[categoryIndex].goods[goodsIndex];
-
- // 单价为0的商品数量只能是1
- if (goods.price === 0) {
- if (type === "plus") {
- // 已经是1,不能再加
- return;
- } else if (type === "minus") {
- // 减到0时自动取消勾选,但不能小于1(如果已勾选)
- if (goods.quantity === 1) {
- goods.quantity = 0;
- goods.checked = false;
- } else {
- goods.quantity = 1; // 确保始终为1
- }
- }
- } else {
- // 非0价格商品的正常逻辑
- let newQuantity = goods.quantity;
-
- if (type === "plus") {
- newQuantity = goods.quantity + 1;
- } else if (type === "minus") {
- newQuantity = Math.max(0, goods.quantity - 1);
- }
-
- goods.quantity = newQuantity;
-
- // 如果数量为0,自动取消勾选
- if (newQuantity === 0) {
- goods.checked = false;
- } else if (!goods.checked) {
- // 如果数量大于0且未勾选,自动勾选
- goods.checked = true;
- }
- }
-
- this.setData({ goodsList });
- this.calculateSummary();
- },
-
- // 数量输入
- onQuantityInput(e: any) {
- const { categoryIndex, goodsIndex } = e.currentTarget.dataset;
- const value = parseInt(e.detail.value) || 0;
- const goodsList = this.data.goodsList;
- const goods = goodsList[categoryIndex].goods[goodsIndex];
-
- // 单价为0的商品数量只能是1
- if (goods.price === 0) {
- if (value > 1) {
- goods.quantity = 1;
- } else if (value < 0) {
- goods.quantity = 0;
- } else {
- goods.quantity = value;
- }
- } else {
- goods.quantity = Math.max(0, value);
- }
-
- this.setData({ goodsList });
- },
-
- // 数量输入失焦
- onQuantityBlur(e: any) {
- const { categoryIndex, goodsIndex } = e.currentTarget.dataset;
- const goodsList = this.data.goodsList;
- const goods = goodsList[categoryIndex].goods[goodsIndex];
-
- // 单价为0的商品数量只能是1
- if (goods.price === 0) {
- if (goods.quantity > 1) {
- goods.quantity = 1;
- } else if (goods.quantity < 1 && goods.checked) {
- // 如果已勾选但数量小于1,设置为1
- goods.quantity = 1;
- } else if (goods.quantity === 0) {
- // 数量为0,取消勾选
- goods.checked = false;
- }
- } else {
- // 非0价格商品的逻辑
- if (goods.quantity === 0) {
- goods.checked = false;
- } else if (!goods.checked) {
- // 如果数量大于0且未勾选,自动勾选
- goods.checked = true;
- }
- }
-
- this.setData({ goodsList });
- this.calculateSummary();
- },
-
- // 计算汇总信息
- calculateSummary() {
- const goodsList = this.data.goodsList;
- let selectedCount = 0;
- let totalPrice = 0;
- let allChecked = true;
-
- goodsList.forEach((category: any) => {
- category.goods.forEach((goods: any) => {
- if (goods.checked && goods.quantity > 0) {
- selectedCount++; // 已选件数 = 选中的商品种类数
- totalPrice += goods.price * goods.quantity;
- }
- if (!goods.checked || goods.quantity === 0) {
- allChecked = false;
- }
- });
- });
-
- this.setData({
- selectedCount,
- totalPrice: totalPrice.toFixed(2),
- selectAll: allChecked
- });
- },
-
- // 结算
- onCheckout() {
- const goodsList = this.data.goodsList;
- const selectedGoods: any[] = [];
-
- // 收集选中的商品
- goodsList.forEach((category: any) => {
- category.goods.forEach((goods: any) => {
- if (goods.checked && goods.quantity > 0) {
- selectedGoods.push({
- ...goods,
- category: category.category
- });
- }
- });
- });
-
- if (selectedGoods.length === 0) {
- getTickleContext.call(this).showWarnMessage("请至少选择一件商品");
- return;
- }
-
- // 跳转到确认订单页面(需要创建该页面)
- // 暂时将选中的商品数据存储,供确认订单页面使用
- wx.setStorageSync('selectedGoods', selectedGoods);
- wx.setStorageSync('totalPrice', this.data.totalPrice);
-
- wx.navigateTo({
- url: `/module/order/pages/confirm-order/confirm-order`,
- fail: (err) => {
- // 如果确认订单页面不存在,提示用户
- getTickleContext.call(this).showWarnMessage("确认订单页面不存在,请先创建该页面");
- }
- });
- },
-
- // 隐私相关方法
- onPrivacySetting() {},
- onAgree() {},
- onDisagree() {},
- },
- });
|