事务用法:首先介绍laravel5中的2中使用方式。
事务用法说明:
1、第一种方式:事务闭包
use Illuminate\Support\Facades\DB;
DB::transaction(function () {
//这里面写逻辑
});
2、第二种方式:常规方式
use Illuminate\Support\Facades\DB;
//开启事务
DB::beginTransaction();
try{
//中间逻辑代码
DB::commit();
}catch (\Exception $e) {
//接收异常处理并回滚
DB::rollBack();
}
下面代码,事务在payConfigAdd方法中使用!
getPageOffset(self::limitParam());
/********************* 筛选条件 *********************/
//商户类型(三方名称)
if (!empty($get['payId'])) {
PayConfig::$where['pay_id'] = $get['payId'];
}
//是否开启入款 1开启 2关闭
if (!empty($get['inState'])) {
PayConfig::$where['in_state'] = $get['inState'];
}
//是否开启出款 1开启 2关闭
if (!empty($get['outState'])) {
PayConfig::$where['out_state'] = $get['outState'];
}
//是否开启通道 1开启 2关闭
if (!empty($get['isStatus'])) {
PayConfig::$where['is_status'] = $get['isStatus'];
}
//ip白名单 1开启 2关闭
if (!empty($get['whiteListState'])) {
PayConfig::$where['whitelist_state'] = $get['whiteListState'];
}
//排序 按照id降序
PayConfig::$orderBy = 'count_use desc';
// 渴求式查询
PayConfig::$Craving = ['payTypes'];
//查询
$payConfigs = PayConfig::getList();
//计数
$count = PayConfig::getListCount();
//销毁
PayConfig::_destroy();
//数据伪装
$data = [];
if ($payConfigs) {
foreach ($payConfigs as $key => $payConfig) {
$data[$key]['payId'] = (int)$payConfig->pay_id;
$data[$key]['confName'] = (string)$payConfig->conf_name;
$data[$key]['confMod'] = (string)$payConfig->mod;
$data[$key]['whiteListState'] = (int)$payConfig->whitelist_state;
$data[$key]['payCode'] = (string)$payConfig->pay_code;
$data[$key]['isStatus'] = (int)$payConfig->is_status;
$data[$key]['inState'] = (int)$payConfig->in_state;
$data[$key]['outState'] = (int)$payConfig->out_state;
$data[$key]['countUse'] = (int)$payConfig->count_use;
//入款网关
$payType = PayType::typeList();
foreach ($payType as $type) {
$types = $type->english_name.'_url';
$data[$key][$type->english_name.'Url'] = (string)$payConfig->$types;
}
//自动出款款代付网关
$data[$key]['dispensingUrl'] = (string)$payConfig->dispensing_url;
$data[$key]['createdAt'] = (string)$payConfig->created_at;
$data[$key]['updatedAt'] = (string)$payConfig->updated_at;
//是否显示维护按钮
if(Maintain::display((int)$payConfig->pay_id) == 1){
$data[$key]['state'] = trans('lang.status')[1];
}else{
$data[$key]['state'] = trans('lang.status')[0];
}
if($payConfig->pay_code){
//字符串分割成一维数组
$payCode = explode(',',(string)$payConfig->pay_code);
//再次分割为二维数组
$codes = array();
foreach ($payCode as $v) {
//将数组的值赋给变量
list($typeId, $code) = explode('-',$v);
$codes[$typeId]['code'] = $code;
}
//支付类型(多对多) -- 组合分割的二维数组
foreach ($payConfig->payTypes as $payType) {
//判断type_id是否存在
if(isset($codes[$payType->type_id])){
$codes[$payType->type_id]['name'] = $payType->type_name;
$codes[$payType->type_id]['id'] = $payType->type_id;
}
$data[$key]['typeName'][] = $payType->type_name;
$data[$key]['typeId'][] = $payType->type_id;
}
$data[$key]['payCodeType'] = array_values($codes);
}else{ //当pay_code为空
$data[$key]['payCodeType'] = array();
foreach ($payConfig->payTypes as $payType) {
$data[$key]['typeName'][] = $payType->type_name;
$data[$key]['typeId'][] = $payType->type_id;
}
}
unset($codes);
}
}
self::setCount($count);
return $this->responseJson($data);
}
/**
* 新增三方
* @return \Illuminate\Http\JsonResponse
*/
public function payConfigAdd()
{
//开启事务
DB::beginTransaction();
try{
//参数验证
$post = self::requestParam(__FUNCTION__);
/******************* 获取提交数据并伪装 *************************/
PayConfig::$data = [
'conf_name' => $post['confName'], //商户类型(三方名称)
'mod' => $post['confMod'], //模型名称
'in_state' => $post['inState'], //是否开启入款1开启 2关闭
'out_state' => $post['outState'], //是否开启出款1开启 2关闭
'whitelist_state' => $post['whiteListState'], //是否开启IP白名单 1开启 2关闭
'is_status' => $post['isStatus'], //是否开启 1开启 2关闭
'pay_code' => !empty($post['payCode']) ? (string)$post['payCode'] : '', //三方扫码编码
'dispensing_url' => !empty($post['dispensingUrl']) ? (string)$post['dispensingUrl'] : '',//自动出款支付网关
'extend_name' => !empty($post['extendName']) ? (string)$post['extendName'] : '', //扩展字段名称
];
//入款网关
$payType = PayType::getList();
foreach ($payType as $type) {
PayConfig::$data[$type->english_name.'_url'] = !empty($post[$type->english_name.'Url']) ? (string)$post[$type->english_name.'Url'] : '';
}
//获取支付方式
PayConfig::$payTypeId = $post['typeId'];
//添加数据
$result = PayConfig::addToData();
//销毁
PayConfig::_destroy();
if ($result->pay_id) {
$payId = $result->pay_id;
//添加到维护
if($this->maintainAdd($payId)){
//添加成功返回数据伪装
$data = array([
'payId' => (int)$result->pay_id,
'confName' => (string)$result->conf_name,
'confMod' => (string)$result->mod,
'inState' => (int)$result->in_state,
'isStatus' => (int)$result->is_status,
'outState' => (int)$result->out_state,
'whiteListState' => (int)$result->whitelist_state,
'payCode' => (string)$result->pay_code,
'dispensingUrl' => (string)$result->dispensing_url,
'extendName' => (string)$result->extend_name,
//支付方式
'typeId' => (array)$post['typeId'],
'createdAt' => (string)$result->created_at,
'updatedAt' => (string)$result->updated_at,
]);
//入款网关
foreach ($payType as $type) {
$data[$type->english_name.'Url'] = !empty($post[$type->english_name.'_url']) ? (string)$post[$type->english_name.'_url'] : '';
}
}
//清除Redis
RedisConPool::del(PayConfig::$ConfigRedisKey);
//提交事务
DB::commit();
return $this->responseJson($this->translateInfo(Code::ADD_SUCCESS),$data);
}
}catch (\Exception $e) {
//事务回滚
DB::rollBack();
return $this->responseJson(Code::FAIL_TO_ADD);
}
}
/**
* 修改三方
* @return \Illuminate\Http\JsonResponse
*/
public function payConfigUpdate()
{
//参数验证
$put = self::requestParam( __FUNCTION__);
//判断条件商户ID pay_id
PayConfig::$where['pay_id'] = $put['payId'];
/******************* 获取提交数据并伪装 *************************/
PayConfig::$data = [
'conf_name' => $put['confName'], //商户类型(三方名称)
'mod' => $put['confMod'], //模型名称
'in_state' => $put['inState'], //是否开启入款1开启 2关闭
'out_state' => $put['outState'], //是否开启出款1开启 2关闭
'whitelist_state' => $put['whiteListState'], //是否开启IP白名单 1开启 2关闭
'is_status' => $put['isStatus'], //是否开启 1开启 2关闭
'pay_code' => !empty($put['payCode']) ? (string)$put['payCode'] : '', //三方扫码编码
'dispensing_url' => !empty($put['dispensingUrl']) ? (string)$put['dispensingUrl'] : '',//自动出款支付网关
'extend_name' => !empty($put['extendName']) ? (string)$put['extendName'] : '', //扩展字段名称
];
//入款网关
$payType = PayType::typeList();
foreach ($payType as $type) {
PayConfig::$data[$type->english_name.'_url'] = !empty($put[$type->english_name.'Url']) ? (string)$put[$type->english_name.'Url'] : '';
}
//获取支付方式
PayConfig::$payTypeId = (array)!empty($put['typeId']) ? (array)$put['typeId'] : null;
//修改数据
if (PayConfig::editToData($put['payId'])) {
//销毁
PayConfig::_destroy();
//清除Redis
RedisConPool::del(PayConfig::$ConfigRedisKey);
return $this->responseJson(Code::UPDATE_SUCCESS);
} else {
return $this->responseJson(Code::FAIL_TO_UPDATE);
}
}
/**
* 三方配置下拉数据
* @return mixed
*/
public function getConfigLists()
{
//要查询的字段
PayConfig::$field = array(
'pay_id',
'conf_name',
'extend_name'
);
$configLists = PayConfig::getList();
PayConfig::_destroy();
if (!$configLists) {
return $this->responseJson(Code::NULL_DATA);
}
$data = [];
foreach ($configLists as $k => $v) {
$data[$k]['payId'] = $v->pay_id;
$data[$k]['confName'] = $v->conf_name;
if (!empty($v->conf_name)) {
$data[$k]['extendName'] = $v->extend_name;
}
}
return $this->responseJson($data);
}
/**
* 添加维护 - 添加三方调用改方法
* @param $payId - 添加三方返回的pay_id
* @return \Illuminate\Http\JsonResponse
*/
public function maintainAdd($payId)
{
//查询是否已添加到维护
Maintain::$where['pay_id'] = $payId;
if(Maintain::getOne()){
return $this->responseJson(Code::HAVE_BEEN_MAINTAINED);
}
//提交参数
$addData = [
'pay_id' => $payId,
];
Maintain::$data = $addData;
if (Maintain::addToData()) {
//销毁
Maintain::_destroy();
return $this->responseJson($this->translateInfo(Code::ADD_SUCCESS));
} else {
return $this->responseJson(Code::FAIL_TO_ADD);
}
}
}
慢慢琢磨!