• 积分抵扣逻辑


    使用优惠券后,重置积分。
    积分使用,采用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('操作失败');
            }
        }
    }
    

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

  • 相关阅读:
    STM32F407Discovery开发板使用环境搭建
    NIO初识
    Mac下Boost环境搭建
    Android Studio增加NDK代码编译支持--Mac环境
    LNMP平台搭建---PHP安装篇
    LNMP平台搭建---MySQL安装篇
    支付系统流程
    从html字符串中获取div内容---jquery
    记一次进入新公司快速融入开发团队经历
    DataTable复制自身行
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/12794380.html
Copyright © 2020-2023  润新知