小程序核销功能

html

<block wx:if="{{has_auth == true}}">
<view class="con">
    <view class="list">
        <view class="order_num">订单编号:{{outData.order_num}}</view>
        <view class="info">
            <view class="m_info">
                <image  src="{{outData.title_img}}"/>
                <view class="word">
                    <text>{{outData.title}}</text>
                    <text class="two">{{outData.sub_title}}</text>
                    <text>有效期:{{outData.valid_time}}</text>
                </view>
            </view>
            <view class="m_count">
                <text>共<text class="c_one">{{outData.p_num}}</text>张,已使用<text class="c_two">{{outData.used_num}}</text>张</text>
            </view>
        </view>
        <view class="count">
            <text >核销数量</text>
            <view class="button_change_num">
                    <block wx:if="{{check_num > 1}}">
                    <text class="reduce_num_dark" bind:tap="reduce_num">一</text>
                    </block>
                    <block wx:else>
                    <text class="reduce_num" bind:tap="reduce_num">一</text>
                    </block>
                    <text class="result_num">{{check_num}}</text>
                    <text class="add_num" bind:tap="add_num">+</text>
            </view>
        </view>
    </view>
</view>
<view class="button">
    <block wx:if="{{outData.status == 2}}">
    <button class="check_btn" data-order-id="{{outData.id}}" bind:tap="toCheck"> 
        确认核销
    </button>
    </block>
    <block wx:elif="{{outData.status == 5}}">
    <button class="check_btn_forbidden">
        已核销
    </button>
    </block>
    <block wx:elif="{{outData.status == 6}}">
    <button class="check_btn_forbidden">
        已过期
    </button>
    </block>
    <block wx:else>
    <button class="check_btn_forbidden">
        不可核销
    </button>
    </block>
 </view>
</block>
<block wx:if="{{has_auth == false}}">
 <view class="no-auth">
     {{msg}}
 </view>
 </block>

js

import { initNoPage } from "../../../common/requestData";
import Storage from "../../../common/auth/Storage";
import request from "../../../common/request";
import tips from "../../../common/tips";
const app = getApp();


Page({
  toCheck({currentTarget: {
    dataset: { orderId }
  }}) {
    var that = this;
    if (that.data.lock_flag) {
      return;
    }
    that.setData({
      lock_flag: true
    });

    let check_num = that.data.check_num;
    const openid = app.globalData.openid || Storage.get().openid;
    request("checkOrder", { openid, order_id: orderId , check_num })
    .then(({ data }) => {  
      // 页面刷新  
      tips.showMsg('核销成功!');
      that.setData({
        lock_flag: false
      });
      that.onShow();
    })
    .catch(({ errdesc }) => {
      that.setData({
        lock_flag: false
      });
      return tips.showMsg(errdesc);
    });
  },
  reduce_num() {
    let check_num = this.data.check_num;
    if (check_num > 1) {
      this.setData(
        {
          check_num: --check_num
        }
      );
    }
  },
  add_num() {
    let check_num = this.data.check_num;
    let left_num = this.data.outData.left_num;
    if (check_num < left_num) {
      this.setData(
        {
          check_num: ++check_num
        }
      );
    } else {
      return tips.showMsg('当前最多可核销' + left_num + '件');
    }
  },
  /**
   * 页面的初始数据
   */
  data: {
    order_id:0,
    lock_flag:false,
    has_auth: false,
    check_num: 1,
    msg:'抱歉,您无核销权限!'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function ({ id }) {
    let that = this;
    that.setData({
      order_id: id
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const openid = app.globalData.openid || Storage.get().openid;
    let that = this;
    let order_id = that.data.order_id;
    request("getCheckOrderInfo", { openid, order_id })
    .then(({ data }) => {
      that.setData({
        has_auth: true,
        outData: data
      });
    })
    .catch(({ errdesc }) => {
      that.setData({
        msg: errdesc
      });
      // return tips.showMsg(errdesc);
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

php

// 获取核销订单信息
public function getCheckOrderInfo()
{
    $openid = trim($_POST['openid']);
    if (!$openid) {
        $this->json->E('缺少参数openid');
    }
    $user   = M('user');
    $user_info  = $user->where(array('openid' => $openid))->find();
    if (!$user_info) {
        $this->json->E('请先登录');
    }

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

    $order = M('order');
    $order_info = $order->where(['id' => $order_id])->find();
    // 获取商品信息
    $productService = new ProductService();
    $order_info['status_str'] = $productService->getOrderStatus($order_info['status']);
    $order_info['valid_time'] = date('Y-m-d', $order_info['valid_time']);
    // 获取商品信息
    $product = M('product');
    $product_info = $product->where(['id' => $order_info['pid']])->find();
    $order_info['title'] = $product_info['title'];
    $order_info['sub_title'] = $product_info['sub_title'];
    $order_info['title_img'] = $product_info['title_img'];
    $order_info['left_num'] = $order_info['p_num'] - $order_info['used_num'];

    // 判断是否有核销权限
    if ($product_info['merchant_id'] == 0) { // 自营
        if ($user_info['can_check_mid'] != -1) {
            $this->json->E('抱歉,您无核销权限!');
        }
    } else {
        if ($user_info['can_check_mid'] != $product_info['merchant_id']) {
            $this->json->E('抱歉,您无核销权限!');
        }
    }

    $this->json->S($order_info);
}

// 核销订单
public function checkOrder()
{
    $openid = trim($_POST['openid']);
    if (!$openid) {
        $this->json->E('缺少参数openid');
    }
    $user   = M('user');
    $user_info  = $user->where(array('openid' => $openid))->find();
    if (!$user_info) {
        $this->json->E('请先登录');
    }

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

    $order = M('order');
    $order_info = $order->where(['id' => $order_id])->find();

    if ($order_info['status'] != 2) {
        $this->json->E('当前订单状态不可核销');
    }
    $left_num = $order_info['p_num'] - $order_info['used_num'];
    if ($left_num <= 0) {
        $this->json->E('当前订单已核销完');
    }

    $check_num = (int) $_POST['check_num'];
    if ($check_num > $left_num) {
        $this->json->E('核销数超出剩余可核销数');
    }

    // 获取商品信息
    $product = M('product');
    $product_info = $product->where(['id' => $order_info['pid']])->find();
    // 判断是否有核销权限
    if ($product_info['merchant_id'] == 0) { // 自营
        if ($user_info['can_check_mid'] != -1) {
            $this->json->E('抱歉,您无核销权限!');
        }
    } else {
        if ($user_info['can_check_mid'] != $product_info['merchant_id']) {
            $this->json->E('抱歉,您无核销权限!');
        }
    }


    // 增加核销数
    if ($check_num == $left_num) { // 订单完成
        M()->startTrans();
        // 修改订单
        $edit_order_data = [
            'used_num' => $order_info['p_num'],
            'status'   => 5,
            'finish_time' => time()
        ];
        $order_flag = $order->where(['id' => $order_id])->save($edit_order_data);
        if ($order_flag === false) {
            M()->rollback();
            $this->json->E('服务器繁忙,请重试!');
        }

        // 增加用户积分,和累计消费金额
        $logService = new LogService();
        $log_flag = $logService->addIntegralAndLog($order_info['uid'], (int) $order_info['total_price'], '订单' . $order_info['order_num'] . '完成');
        if ($log_flag === false) {
            M()->rollback();
            $this->json->E('服务器繁忙,请重试!');
        }

        if ($order_info['pay_type'] == 1) {
            $edit_user_flag = $user->where(['id' => $order_info['uid']])->setInc('total_consume_money',$order_info['total_price']);
            if ($edit_user_flag === false) {
                M()->rollback();
                $this->json->E('服务器繁忙,请重试!');
            }
        }

        // 判断是否有推荐人
        $buyer_user_info = $user->where(['id' => $order_info['uid']])->find();
        if ($buyer_user_info['ruid'] != 0) {
            $log_flag = $logService->addIntegralAndLog($buyer_user_info['ruid'], (int) $order_info['total_price'], '推荐人' . $buyer_user_info['nickname'] . '消费奖励');
            if ($log_flag === false) {
                M()->rollback();
                $this->json->E('服务器繁忙,请重试!');
            }
        }

        // 增加日志
        $order_used_log = M('order_used_log');
        $add_data = [
            'order_id' => $order_id,
            'check_uid' => $user_info['id'],
            'used_num' => $check_num,
            'create_time' => time(),
        ];
        $add_flag = $order_used_log->add($add_data);
        if ($add_flag === false) {
            M()->rollback();
            $this->json->E('服务器繁忙,请重试!');
        }

        M()->commit();
        $this->json->S([], '核销成功');
    } else {
        M()->startTrans();
        $order_flag = $order->where(['id' => $order_id])->setInc('used_num', $check_num);
        if ($order_flag === false) {
            M()->rollback();
            $this->json->E('服务器繁忙,请重试!');
        }

        $order_used_log = M('order_used_log');
        $add_data = [
            'order_id' => $order_id,
            'check_uid' => $user_info['id'],
            'used_num' => $check_num,
            'create_time' => time(),
        ];
        $add_flag = $order_used_log->add($add_data);
        if ($add_flag === false) {
            M()->rollback();
            $this->json->E('服务器繁忙,请重试!');
        }

        M()->commit();
        $this->json->S([], '核销成功');
    }
}

思路,判断用户是否有核销权限。
判断订单状态是否可核销。
判断订单是否已核销完。
处理,积分赠送等操作。

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