tp3.2 事务

时间:2024-01-19 16:35:02
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;
}