| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- const _Http = getApp().globalData.http;
- Page({
- data: {
- list: [],
- selectedCount: 0,
- selectedTotalPoints: 0,
- total: 0,
- sc_orderid: '',
- content: {
- nocache: true,
- pageNumber: 1,
- pageSize: 20,
- where: {
- condition: ""
- }
- }
- },
- onLoad(options) {
- this.setData({
- sc_orderid: options.sc_orderid || ''
- });
- getApp().globalData.Language.getLanguagePackage(this, '选择商品');
- this.getList();
- },
- // 搜索
- onSearch({ detail }) {
- this.data.content.where.condition = detail;
- this.getList(true);
- },
- // 获取商品列表
- getList(init = false) {
- if (init) this.selectComponent('#ListBox').goTop();
- _Http.init(this.data.content, init).then(content => {
- _Http.basic({
- id: 2026052510110006,
- content
- }).then(res => {
- this.selectComponent('#ListBox').RefreshToComplete();
- if (res.code != '1') return wx.showToast({ title: res.msg, icon: "none" });
- const list = (res.data || []).map(item => {
- // 处理图片URL
- try {
- if (item.attinfos && item.attinfos.length) {
- let att = item.attinfos[0];
- item.imgUrl = att.subfiles ? _Http.getSpecifiedImage(att) : (att.url || '');
- } else {
- item.imgUrl = '';
- }
- } catch (e) {
- item.imgUrl = '';
- }
- item.selected = false;
- item.qty = item.minimumorderqty || 1; // 初始化数量为起订量
- return item;
- });
- this.setData({
- content: _Http.paging(content, res),
- total: res.total || 0,
- list: res.pageNumber == 1 ? list : this.data.list.concat(list)
- });
- });
- });
- },
- // 切换选中状态
- onToggleSelect(e) {
- let index = e.currentTarget.dataset.index;
- let key = `list[${index}].selected`;
- this.setData({
- [key]: !this.data.list[index].selected
- });
- this.updateSelectedCount();
- },
- // 数量变化
- onQtyChange(e) {
- let qty = e.detail;
- let index = e.currentTarget.dataset.index;
- let item = this.data.list[index];
- if (!item) return;
- let minQty = item.minimumorderqty || 1;
- let increment = item.increment || 1;
- // 校验起订量
- if (qty < minQty) {
- qty = minQty;
- }
- // 校验增量
- if (increment > 1) {
- let remainder = (qty - minQty) % increment;
- if (remainder !== 0) {
- qty = qty + (increment - remainder);
- }
- }
- let key = `list[${index}].qty`;
- this.setData({ [key]: qty });
- this.updateSelectedCount();
- },
- // 阻止事件冒泡
- stopPropagation() {},
- // 更新选中数量和合计积分
- updateSelectedCount() {
- let selected = this.data.list.filter(v => v.selected);
- let count = selected.length;
- let totalPoints = selected.reduce((sum, v) => sum + (v.points || 0) * (v.qty || 0), 0);
- this.setData({ selectedCount: count, selectedTotalPoints: totalPoints });
- },
- // 确认添加
- onConfirm() {
- let selected = this.data.list.filter(v => v.selected);
- if (selected.length === 0) {
- return wx.showToast({ title: '请选择商品', icon: 'none' });
- }
- // 调用回调(回调中会处理API调用和页面跳转)
- if (getApp().globalData.handleSelect) {
- getApp().globalData.handleSelect(selected);
- }
- }
- });
|