PHP code example of anyuzhe / laravel-function-flow

1. Go to this page and download the library: Download anyuzhe/laravel-function-flow library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

anyuzhe / laravel-function-flow example snippets

`
public function bindRecord(Request $request)
{
	return \Flow::setLastFunc(['Response/flowReturn'])->setParam($request->all())->flow([
			['Store/getIdByNo'],
			['User/bindRecord'],
			['User/checkMemberExist'],
	])['response'];
}
`
public function beforeStore($id, $data)
{
	$data['cover_url'] = 'http://weiyicrm.oss-cn-shanghai.aliyuncs.com/ico-fuzhaung.png';
	$data['input_goods_no'] = isset($data['goods_no'])?$data['goods_no']:null;
	$data['id'] = $id;
	//setParam方法是设置运行中的参数 可以是数组 或者是两个参数键值的形式
	return \Flow::setParam($data)->flow([   //flow为主运行方法
			['Store/getOneId'],
			//  数组第二个参数是array型 可以设置函数的额外参数  第三个参数int型 是一个缓存的时间值可以设置是否缓存 单位为分钟
			['Qrcode/generateGoodsNoAndCreateQrcodeByNum',['created_pic'=>false]],
			['Picture/getIds'],
			['Picture/getModelByIds'],
			['Goods/getGoodsNo'],
			['Goods/findByNo'],
			['Goods/updateByStock'],
			['Goods/updateBySell'],
			['Goods/createOne'],
			['Qrcode/bingQrcodeGoodsNo'],// 
			['Qrcode/bingQrcodeLabelNo'],// 
			['GoodsRecord/updateOne'],
			['Response/flowReturn'],
	])['response'];
}
`
这一用来返回最后的reponse数组的方法。 判断在别的方法中是否输出了错误码和错误信息
public function flowReturn($res,$errcode,$errmsg,$msg)
{
	if(!$errcode){
			$data['status'] = 1;
			$data['data'] = $res;
			$data['msg'] = $msg?$msg:'Success';
	}else{
			$data['status'] = $errcode;
			$data['data'] = null;
			$data['msg'] = $errmsg?$errmsg:'Error';
	}
	return ['response'=>$data];
}
`
public function getOneId()
{
	$user = Auth::user();
	if($user) {
			$stores = $user->stores;
			if ($stores->count() == 1) {
					//最后输出的数组 会合并入方法容器的参数数组中 之后的方法中 可以申明参数 只要变量名与参数名相同 就可以自动传入
					return ['store_id'=>$stores->first()->id];
			}
	}
	//这里返回的skip是可以跳过之后的一些方法  直接运行想要的方法 可以作为报错后的处理   这里出错 所以返回了错误码和错误信息
	return ['skip'=>'flowReturn','store_id'=>null,'errcode'=>ErrorCode::$canNotFindStoreError['code'],'errmsg'=>ErrorCode::$canNotFindStoreError['msg']];
}
`
public function getIdByNo($store_no)
{
	if($store_no){
			$store = $this->findBy('identifying',$store_no);
			if($store){
					return ['store_id'=>$store->id];
			}
	}
	//这里返回的next为false 会让方法容器不执行之后的方法。直接运行lastFunc(如果定义了的话)ps:可以把最后的返回参数处理函数定义为lastFunc
	return ['next'=>false,'store_id'=>null,'errcode'=>ErrorCode::$canNotFindStoreError['code'],'errmsg'=>ErrorCode::$canNotFindStoreError['msg']];
}