积分抵扣逻辑

使用优惠券后,重置积分。
积分使用,采用switch选择。

<block wx:if="{{orderData.user_integral > 0}}">
<view class="integral_box">
    <image class="logo" src="/images/order/jf.png"/> 积分,共 {{orderData.user_integral}},可用{{orderData.can_use_integral}},可抵扣¥{{orderData.deduction_price}}
    <block wx:if="{{orderData.can_use_integral > 0}}">
    <switch class="checkbox" checked="{{integralChecked}}" bindchange="integralChange"/>
    </block>
</view>
</block>
<block wx:else>
<view class="integral_box_no">
    <image class="logo" src="/images/order/jf.png"/> 暂无积分
</view>
</block>

切换switch触发事件

integralChange({ detail: {
  value: res
} }) {
  // console.log(e.detail.value);
  if (res) {
    var use_integral = 1;
  } else {
    var use_integral = 2;
  }
  request("setUseIntegral", {
    order_id: this.orderId,
    use_integral: use_integral
  }).then(() => {
    this.getOrderData();
  });
}

后台处理数据

// 设置积分使用
public function setUseIntegral()
{
    if (!$use_integral = trim($_POST['use_integral'])) {
        $this->json->E('缺少参数use_integral');
    }

    if (!$order_id = trim($_POST['order_id'])) {
        $this->json->E('缺少订单id');
    }

    // 获取用户积分
    $order = D('Order');
    $order_info = $order->where(['id' => $order_id])->find();
    if ((int) $order_info['status'] !== 0) {
        $this->json->E('订单状态有误');
    }

    if ((int) $order_info['status'] !== 0) {
        $this->json->E('订单状态有误');
    }

    if ($use_integral == 1) { // 使用积分
        $user      = M('user');
        $user_info = $user->where(['id' => $order_info['uid']])->find();
        if (!$user_info) {
            $this->json->E('用户不存在');
        }

        // 预计奖励积分
        $configs      = D('Configs');
        $configs_data = $configs->queryKeys('per_yuan_deduction');
        $integral = $user_info['integral'];

        // 商品实际需支付金额
        $total_need_price = $order->getNeedPayPrice($order_id, 2);
        $deduction_price = $integral / $configs_data['per_yuan_deduction'];
        if ($deduction_price >  $total_need_price) {
            $can_use_integral = $total_need_price * $configs_data['per_yuan_deduction'];
            $deduction_price = $total_need_price;
        } else {
            $can_use_integral = $integral;
            $deduction_price = $deduction_price;
        }

        $order_save_data = [
            'integral' => $can_use_integral,
            'integral_price' => $deduction_price * 100,
        ];
        $order = M('order');
        $order_save_flag = $order->where(['id' => $order_id])->save($order_save_data);
        if ($order_save_flag !== false) {
            $this->json->S();
        } else {
            $this->json->E('操作失败');
        }
    } else { // 取消积分使用
        $order_save_data = [
            'integral' => 0,
            'integral_price' => 0,
        ];
        $order = M('order');
        $order_save_flag = $order->where(['id' => $order_id])->save($order_save_data);
        if ($order_save_flag !== false) {
            $this->json->S();
        } else {
            $this->json->E('操作失败');
        }
    }
}

奖励的积分,只可以是实际支付金额对应的积分。

原文地址:https://www.cnblogs.com/jiqing9006/p/12794380.html