game2006admin_be/app/Http/Controllers/ApplyController.php
2022-08-08 14:05:32 +08:00

385 lines
13 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\ApplyModel;
use App\Models\MintModel;
use App\Models\NodeGroup;
use App\Models\User;
use App\Models\UserNodeLine;
use Illuminate\Http\Request;
use Illuminate\Mail\Message;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
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);
//当用户是申请组
if ($type==1){
$data = ApplyModel::with('mint')->where('creator_account',auth()->user()->account)->orderBy('id','desc')->paginate($size)->toArray();
if ($data['data'] ){
$data['data'] = $this->_changeExecuteState($data['data']);
}
return $this->success($data);
}
//当用户是初审核组
if ($type==2){
$data = ApplyModel::with('mint')->orderBy('id','desc')->paginate($size)->toArray();
if ($data['data'] ){
$data['data'] = $this->_changeExecuteState($data['data']);
}
return $this->success($data);
}
//当用户是终于审核组
if ($type==3){
$data = ApplyModel::with('mint')->where('audit_state_one',1)->orderBy('id','desc')->paginate($size)->toArray();
if ($data['data'] ){
$data['data'] = $this->_changeExecuteState($data['data']);
}
return $this->success($data);
}
//当用户是执行组
if ($type==4){
$data = ApplyModel::with('mint')->where('audit_state_one',1)->where('audit_state_two',1)->orderBy('id','desc')->paginate($size)->toArray();
if ($data['data'] ){
$data['data'] = $this->_changeExecuteState($data['data']);
}
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::create($data);
if(! $res){
return $this->error(ERROR_CODE_NO,'系统繁忙,稍后再试');
};
Cache::increment('unikey_id');
$msg = [
'role'=>'初审员',
'what' => '申请',
'do' => '审核',
'state' => 1
];
$this->_sendEmail('初审组',$msg);
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->audit_state_one || $model->audit_state_two){
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->audit_state_one){
return $this->error(ERROR_CODE_PARAM_INVALID,'无需审核');
}
$model->audit_account = auth()->user()->account;
$model->audit_time = time();
$model->audit_state_one = 1;
$res = $model->save();
if(! $res){
return $this->error(ERROR_CODE_NO,'系统繁忙,稍后再试');
};
$msg = [
'role'=>'终审员',
'what' => '审核',
'do' => '终审',
'state' => 1
];
$this->_sendEmail('终审组',$msg);
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->audit_state_one){
return $this->error(ERROR_CODE_PARAM_INVALID,'无需审核');
}
$model->audit_account = auth()->user()->account;
$model->audit_time = time();
$model->audit_state_one = 2;
$res = $model->save();
if(! $res){
return $this->error(ERROR_CODE_NO,'系统繁忙,稍后再试');
};
$msg = [
'role'=>'申请员',
'what' => '初审',
'do' => '',
'state' => 2
];
$user = User::where('account',$model->creator_account)->first();
// dump($model);die;
if ($user->email){
$this->_notPassSendEmail($user->email,$msg);
}
return $this->success([]);
}
//终审通过申请
public function passFinal($id){
if (! is_numeric($id)){
return $this->error(ERROR_CODE_PARAM_INVALID,'参数格式不对');
}
$model = ApplyModel::find($id);
if (!$model || $model->audit_state_two){
return $this->error(ERROR_CODE_PARAM_INVALID,'无需审核');
}
$model->audit_final_account = auth()->user()->account;
$model->audit_final_time = time();
$model->audit_state_two = 1;
$res = $model->save();
if(! $res){
return $this->error(ERROR_CODE_NO,'系统繁忙,稍后再试');
};
$msg = [
'role'=>'执行员',
'what' => '执行',
'do' => '执行',
'state' => 1
];
$this->_sendEmail('执行组',$msg);
return $this->success([]);
}
//终审不通过申请
public function noPassFinal($id){
if (! is_numeric($id)){
return $this->error(ERROR_CODE_PARAM_INVALID,'参数格式不对');
}
$model = ApplyModel::find($id);
if (!$model || ! $model->audit_state_one || $model->audit_state_two ){
return $this->error(ERROR_CODE_PARAM_INVALID,'无需审核');
}
$model->audit_final_account = auth()->user()->account;
$model->audit_final_time = time();
$model->audit_state_two = 2;
$res = $model->save();
if(! $res){
return $this->error(ERROR_CODE_NO,'系统繁忙,稍后再试');
};
$msg = [
'role'=>'申请员',
'what' => '终审',
'do' => '',
'state' => 2
];
$user = User::where('account',$model->creator_account)->first();
if ($user->email){
$this->_notPassSendEmail($user->email,$msg);
}
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();
$data->execute_state = 1;
$res = $data->save();
if(! $res){
return $this->error(ERROR_CODE_NO,'系统繁忙,稍后再试');
};
return $this->success([]);
}
//驳回道具申请
public function reject($id,Request $request){
if (! is_numeric($id)){
return $this->error(ERROR_CODE_PARAM_INVALID,'参数格式不对');
}
$validator = Validator::make($request->all(),[
'reject_reason' => 'required',
], [
'reject_reason.required' => '驳回原因不能为空',
]);
if ($validator->fails()){
return $this->error(ERROR_CODE_PARAM_INVALID,$validator->errors()->first());
}
$reject_reason = $request->get('reject_reason');
$data = ApplyModel::find($id);
if (!$data){
return $this->error(ERROR_CODE_NO,'数据不存在');
}
if ($data->execute_state != 0){
return $this->error(ERROR_CODE_NO,'无法驳回');
}
$data->execute_state = -1;
$data->reject_reason = $reject_reason;
$data->save();
$msg = [
'role'=>'申请员',
'what' => '申请',
'do' => '',
'state' => 3
];
$user = User::where('account',$data->creator_account)->first();
if ($user->email){
$this->_notPassSendEmail($user->email,$msg);
}
return $this->success();
}
//查看申请记录的执行状态
private function _changeExecuteState($arr){
foreach ($arr as $key=>$item){
if (! $arr[$key]['mint'] && $arr[$key]['execute_state'] != -1){
$arr[$key]['execute_state'] = 0;
}
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']);
}
return $arr;
}
private function _sendEmail($nodeGroup,$msg){
$data = NodeGroup::with('user')->where('name',$nodeGroup)->first();
if ($data){
$data = $data->toArray();
if ($data['user']){
foreach ($data['user'] as $val){
Mail::send('email',compact('msg'),function(Message $message) use ($val){
$message->to($val['email']);
$message -> subject('Nft申请通知');
});
}
}
Log::error($nodeGroup.'内不存在有权限的用户');
}
Log::error('无法识别出'.$nodeGroup);
}
private function _notPassSendEmail($email,$msg){
Mail::send('email',compact('msg'),function(Message $message) use ($email){
$message->to($email);
$message -> subject('Nft申请通知');
});
}
}