小程序核銷功能


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([], '核銷成功');
    }
}

思路,判斷用戶是否有核銷權限。
判斷訂單狀態是否可核銷。
判斷訂單是否已核銷完。
處理,積分贈送等操作。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM