ecshop适应PHP7的修改及事务处理

13 篇文章 0 订阅
2 篇文章 0 订阅

http://www.cnblogs.com/xiwang6428/p/5460155.html
但是自己没有用。这种方式,用的是:

<?php
$action = $_REQUEST['act'];
if ($action == 'withdraw') {
//show_message('请到移动端公众号进行提现!PC端提现即将开放');

    include_once(ROOT_PATH . 'includes/lib_clips.php');
//现在接收值
    $process_type = isset($_POST['process_type']) ? trim($_POST['process_type']) : 1;
    $mybank_id = isset($_POST['mybank_id']) ? intval($_POST['mybank_id']) : 0;
    $applicant = isset($_POST['applicant']) ? trim($_POST['applicant']) : '';
    $repay_money = isset($_POST['repay_money']) ? floatval($_POST['repay_money']) : 0;

/* 开始进行事务的处理 */
    $db->query('BEGIN');

    try {

        $user_id = $_SESSION['user_id'];
//查找的sql语句 select ...for  update (是在锁定表中的数据 user_money)
        $sql = " SELECT `user_money` from " . $ecs->table('users') . " WHERE `user_id`=$user_id  FOR UPDATE ";
        $user_moneys = $db->getOne($sql);
        if (!isset($user_moneys) || empty($user_moneys) || $user_moneys == 0) {
//抛出异常
            throw new \Exception("余额不足");
        }
        if ($mybank_id == 0) {
            throw new \Exception("请选择提现银行卡或者在下边区域进行银行卡绑定");
        }
        if ($applicant == '') {
            throw new \Exception("请填写申请人");
        }
        if ($repay_money == 0) {
            throw new \Exception("请填写提现金额");
        } else if ($repay_money > $user_moneys) {
            throw new \Exception("您要申请提现的金额超过了您现有的余额,此操作将不可进行!");
        } else if ($repay_money < 0) {
            throw new \Exception("提现金额不正确");
        }

//验证成功之后进行入库的操作
        $surplus = [
            'user_id' => $user_id,
            'amount' => "-" . $repay_money,     //申请金额
            'process_type' => $process_type,   //提现方式
            'user_note' => $applicant . "申请提现",
            'payment' => '银行转账',
            'mybank_id' => $mybank_id,         //选择提现银行卡
            'applicant' => $applicant,         //申请人姓名
        ];

        $surplus['rec_id'] = insert_user_account2($surplus);
//插入account_log表
        $account_log = array(
            'user_id' => $user_id,
            'user_money' => '-' . $repay_money,
            'frozen_money' => 0,
            'rank_points' => 0,
            'pay_points' => 0,
            'change_time' => gmtime(),
            'change_desc' => $applicant . "申请提现",
            'change_type' => 1
        );


        $account_log_id = insert_account_log2($account_log, $amount, $surplus['rec_id']);
//这一步开始更新ecs_user表  update user_money的值  修改之后  提交
        $update_amount = array(
            'user_id' => $user_id,
            'user_money' => $repay_money,
        );
        $update_id = updata_user_amount($update_amount);

        /* 如果成功提交 */
        if ($surplus['rec_id'] > 0) {
            $db->query('COMMIT');
            $content = $_LANG['surplus_appl_submit'];
            //分装的跳转信息
            show_message($content, $_LANG['back_account_log'], 'user.php?act=account_log', 'info');
        } else {
            throw new \Exception('添加失败');

        }

    } catch (Exception $e) {
        $db->query('ROLLBACK');
        $message = $e->getMessage() ? $e->getMessage() : "提现失败!";
        show_message($message);
    }
}

//插入account_log记录2
function insert_account_log2($account_log, $amount,$id)
{
    $ip=real_ip();
    $sql = 'INSERT INTO ' . $GLOBALS['ecs']->table('account_log') .
        ' (user_id, user_money, frozen_money, rank_points, pay_points, change_time, change_desc, change_type,ip,user_account_log_id)' .
        " VALUES ('$account_log[user_id]', '$account_log[user_money]', '$amount', '$account_log[rank_points]','$account_log[pay_points]','" . gmtime() . "', '$account_log[change_desc]', '$account_log[change_type]','$ip','$id')";
    $GLOBALS['db']->query($sql);

    return $GLOBALS['db']->insert_id();
}
//更新用户余额
function updata_user_amount($surplus)
{
    $sql = 'UPDATE ' . $GLOBALS['ecs']->table('users') . ' SET ' .
        "user_money     = user_money -$surplus[user_money]  " .
        "WHERE user_id   = '$surplus[user_id]'";
    $GLOBALS['db']->query($sql);

    return $surplus['rec_id'];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值