首页 > 其他 > 详细

tp3.2 事务

时间:2019-03-07 16:29:31      阅读:173      评论:0      收藏:0      [点我收藏+]
public function exchangeTransfer($user_id, $type, $money, $config, $other_id = 0)
    {
        $r[‘code‘] = ERROR1;
        $r[‘msg‘] = L(‘parameter_error‘);
        $r[‘result‘] = [];
        if (intval($user_id) && !empty($money)) {
            $uuc_money = M(‘UucMoney‘)->where([‘user_id‘=>$user_id])->find();
            if(!$uuc_money && !$this->otherMoneyInit($user_id)){
                $r[‘code‘] = ERROR4;
                $r[‘msg‘] = $this->logic_error;
                return $r;
            }
            // 互转时给对方账户初始化
            if($other_id>0 && !$this->otherMoneyInit($other_id)){
                $r[‘code‘] = ERROR5;
                $r[‘msg‘] = $this->logic_error;
                return $r;
            }
            $balance = $uuc_money[‘exchange_money‘]; // 账户余额
            try {
                $model = M();
                $model->startTrans();
                switch($type){
                    case 1: // 后台充值
                        if(!$this->exchangeRecharge($user_id, $money, $config)){
                            throw new Exception($this->logic_error);
                        }
                        break;
                    case 2: // 后台扣除
                        if(!$this->exchangeDeduction($user_id, $money, $balance, $config)){
                            throw new Exception($this->logic_error);
                        }
                        break;
                    case 4:  // 转出其他用户
                        if(!$this->exchangeToOtherExchange($user_id, $money, $balance, $config, $other_id)){
                            throw new Exception($this->logic_error);
                        }
                        break;
                    default:
                        throw new Exception(L(‘type_error‘));
                }

                $model->commit();
                $r[‘msg‘] = L(‘success_operation‘);
                $r[‘code‘] = SUCCESS;
            } catch (Exception $e) {
                $model->rollback();
                $r[‘msg‘] = $e->getMessage();
                $r[‘code‘] = ERROR3;
            }
        }
        return $r;
    }

tp3.2 事务

原文:https://www.cnblogs.com/sgm4231/p/10490067.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!