game2006admin_be/app/Http/Controllers/ApplyController.php
2022-08-03 19:37:50 +08:00

209 lines
7.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\ApplyModel;
use App\Models\MintModel;
use App\Models\User;
use App\Models\UserNodeLine;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Http;
class ApplyController extends Controller
{
//
public function index($type,Request $request){
$size = $request->get('size', 10);
// $node_id = UserNodeLine::where('user_id',auth()->id())->pluck('node_group_id')->toArray();
//// dump($node_id);die;
//当用户是申请组
if ($type==1){
$data = ApplyModel::withTrashed()->where('creator_account',auth()->user()->account)->orderBy('id','desc')->paginate($size)->toArray();
return $this->success($data);
}
//当用户是审核组
if ($type==2){
$data = ApplyModel::orderBy('id','desc')->paginate($size)->toArray();
return $this->success($data);
}
//当用户是执行组
if ($type==3){
$data = ApplyModel::with('mint')->where('state',1)->orderBy('id','desc')->paginate($size)->toArray();
if ($data['data'] ){
$arr = $data['data'];
foreach ($arr as $key=>$item){
if ($arr[$key]['mint'] && ! $arr[$key]['mint']['suspend'] && ! $arr[$key]['mint']['done']){
$arr[$key]['execute_state'] = 1;
}
if ($arr[$key]['mint'] && $arr[$key]['mint']['done']){
$arr[$key]['execute_state'] = 2;
}
if ($arr[$key]['mint'] && $arr[$key]['mint']['suspend']){
$arr[$key]['execute_state'] = 3;
}
unset($arr[$key]['mint']);
}
// dump($arr);
$data['data'] = $arr;
}
return $this->success($data);
}
}
public function create(Request $request){
$validator = Validator::make($request->all(),[
'item_id' => 'required',
'send_account' => 'required',
'reason' => 'required',
], [
'item_id.required' => '道具id不能为空',
'send_account.required' => '接收账号不能为空',
'reason.required' => '申请原因不能为空',
]);
if ($validator->fails()){
return $this->error(ERROR_CODE_PARAM_INVALID,$validator->errors()->first());
}
$data = $request->all();
$param1 = [
'c' => 'BcService',
'a' => 'isValidItem',
'item_id' => $data['item_id'],
];
$response1 = Http::get(env('WEB3_HELPER_URL'), $param1);
if (!$response1->successful()) {
return $this->error(ERROR_CODE_INTERNAL_ERROR, 'WEB3服务出现错误');
}
$response1 = $response1->json();
if ($response1['errcode']){
return $this->error(ERROR_CODE_PARAM_INVALID, $response1['errmsg']);
}
$param2 = [
'c' => 'BcService',
'a' => 'isValidAccount',
'account' => $data['send_account'],
];
$response2 = Http::get(env('WEB3_HELPER_URL'), $param2);
if (!$response2->successful()) {
return $this->error(ERROR_CODE_INTERNAL_ERROR, 'WEB3服务出现错误');
}
$response2 = $response2->json();
if ($response2['errcode']){
return $this->error(ERROR_CODE_PARAM_INVALID, $response2['errmsg']);
}
$data['creator_account'] = auth()->user()->account;
if (Cache::has('unikey_id')){
$unikey_id = Cache::get("unikey_id");
}else{
$unikey_id = 1;
Cache::add("unikey_id",$unikey_id);
}
$data['unikey'] = date('ymd_his',time()).'_'.$unikey_id;
$data['create_time'] = time();
$res = ApplyModel::insert($data);
if(! $res){
return $this->error(ERROR_CODE_NO,'系统繁忙,稍后再试');
};
Cache::increment('unikey_id');
return $this->success([]);
}
public function show($id){
if (! is_numeric($id)){
return $this->error(ERROR_CODE_PARAM_INVALID,'参数格式不对');
}
$data = ApplyModel::withTrashed()->find($id);
return $this->success($data);
}
public function destroy($id){
if (! is_numeric($id)){
return $this->error(ERROR_CODE_PARAM_INVALID,'参数格式不对');
}
$model = ApplyModel::find($id);
if (!$model || $model->state){
return $this->error(ERROR_CODE_PARAM_INVALID,'无法撤销');
}
$res = $model->delete();
if(! $res){
return $this->error(ERROR_CODE_NO,'系统繁忙,稍后再试');
};
return $this->success([]);
}
public function pass($id){
if (! is_numeric($id)){
return $this->error(ERROR_CODE_PARAM_INVALID,'参数格式不对');
}
$model = ApplyModel::find($id);
if (!$model || $model->state){
return $this->error(ERROR_CODE_PARAM_INVALID,'无需审核');
}
$model->audit_account = auth()->user()->account;
$model->audit_time = time();
$model->state = 1;
$res = $model->save();
if(! $res){
return $this->error(ERROR_CODE_NO,'系统繁忙,稍后再试');
};
return $this->success([]);
}
public function noPass($id){
if (! is_numeric($id)){
return $this->error(ERROR_CODE_PARAM_INVALID,'参数格式不对');
}
$model = ApplyModel::find($id);
if (!$model || $model->state){
return $this->error(ERROR_CODE_PARAM_INVALID,'无需审核');
}
$model->audit_account = auth()->user()->account;
$model->audit_time = time();
$model->state = 2;
$res = $model->save();
if(! $res){
return $this->error(ERROR_CODE_NO,'系统繁忙,稍后再试');
};
return $this->success([]);
}
public function execute($id){
if (! is_numeric($id)){
return $this->error(ERROR_CODE_PARAM_INVALID,'参数格式不对');
}
$data = ApplyModel::find($id);
if (!$data){
return $this->error(ERROR_CODE_NO,'数据不存在');
}
if (MintModel::where('unikey',$data->unikey)->count()>0){
return $this->error(ERROR_CODE_NO,'无法再次执行');
}
$param = [
'c' => 'BcService',
'a' => 'presentItem',
'account' => $data->send_account,
'unikey' => $data->unikey,
'item_id' => $data->item_id
];
$response = Http::get(env('WEB3_HELPER_URL'), $param);
if (!$response->successful()) {
return $this->error(ERROR_CODE_INTERNAL_ERROR, 'WEB3服务出现错误');
}
$response = $response->json();
if ($response['errcode']){
return $this->error(ERROR_CODE_PARAM_INVALID, $response['errmsg']);
}
$data->execute_account = auth()->user()->account;
$data->execute_time = time();
$res = $data->save();
if(! $res){
return $this->error(ERROR_CODE_NO,'系统繁忙,稍后再试');
};
return $this->success([]);
}
}