功能详情
管理员登录到后台,点击批量发送账号密码,就给所有报名的选手发送账号密码。
单独给某人发邮件需要填写手动填写账号密码。
//管理员批量发送邮件
$api->post('sendpass','GamesController@sendpass')
->name('api.sendpass.pass');
//管理员单独给某人发邮件
$api->post('sendpassone','GamesController@sendpassone')
->name('api.sendpassone.sendpassone');
取一个表出来,单独分配模型
$baomingusers = Peoplebaoming::where('gameid',$gameid)->get(['schoolid','email']);
foreach ($baomingusers as $baominguser){
if($i==$cnt){
$flag=1;
$news = $news.','."'$baominguser->schoolid'".','."'$baominguser->email'";
break;
}
$i++;
}
//循环获取某个自己需要的东西
修改了上传账号密码时生成的数据表,加上了schoolid和email,所以取的时候直接取就可以了
public function sendpass(Request $request){
$this->validate($request,[
'key' => 'required',
'gameid' => 'required',
]);
$key = $request->key;
if($request->session()->has($key)){
//生成分配的账号密码数据表
$gameid = $request->gameid;
$dbpass = 'game_'.$gameid.'_pass';
$sql = 'select * from '.$dbpass;
$pass = DB::select($sql);
$i = 0 ;
foreach ($pass as $onepass){
//发送邮件
$email = $onepass->email;
Mail::send('sendpass',['schoolid' => $onepass->schoolid,'username' => $onepass->login_id,'password' => $onepass->password],function($message) use ($email){
$to = $email;
$message ->to($to)->subject('比赛账号密码');
});
}
return $this->response->array([
'info' => '发送成功',
'status_code' => 200,
])->setStatusCode(200);
}else{
return $this->response->array([
'info' => '请登录后重试',
'status_code' => 400,
])->setStatusCode(200);
}
}
修改后的创建上传的数据表
public function create_table($table_name,$arr_field,$chu_zai,$gameid)
{
$tmp = $table_name;
$va = $arr_field;
if($chu_zai==1) Schema::drop($tmp);
Schema::create("$tmp", function(Blueprint $table) use ($tmp,$va)
{
$fields = $va[0]; //列字段
//$fileds_count = 0; //列数
$table->increments('id');//主键
foreach($fields as $key => $value){
if($key == 0){
$table->string($fields[$key])->nullable();//->unique(); 唯一
}else{
$table->string($fields[$key])->nullable();
}
//$fileds_count = $fileds_count + 1;
}
$table->bigInteger('schoolid')->nullable();
$table->string('email')->nullable();
});
$value_str= array();
$id = 1;
//获取报名表的所有学号和邮箱
$baomingusers = Peoplebaoming::where('gameid',$gameid)->get(['schoolid','email']);
$cnt=0;
foreach($va as $key => $value){
if($key != 0){
$content = implode(",",$value);
$content2 = explode(",",$content);
foreach ( $content2 as $key => $val ) {
$value_str[] = "'$val'";
}
$news = implode(",",$value_str);
$news = "$id,".$news;
//从报名数据库拿出每个数据放进到这个数据库中
$i=0;
$flag=0;
foreach ($baomingusers as $baominguser){
if($i==$cnt){
$flag=1;
$news = $news.','."'$baominguser->schoolid'".','."'$baominguser->email'";
break;
}
$i++;
}
if($flag==0)continue;
DB::insert("insert into $tmp values ($news)");
//$value_str = '';
$value_str= array();
$id = $id + 1;
$cnt++;
}
}
return 1;
}
模板
学号为 {{$schoolid}} 同学您好,本次比赛您的账号为 {{$username}},密码为 {{$password}} ,请妥善保管您的账号密码,祝您生活愉快。
{
"info": "发送成功",
"status_code": 200
}
//如果这个用户有账号密码,就直接发,如果这个用户没有账号密码,就接收前端传来的账号密码
注意,用 pass=DB::select( p a s s = D B :: s e l e c t ( sql); 取出来的东西是一个对象数组,要使用的话必须用循环或者用 pass[ p a s s [ i] 的形式
使用 DB::insert(“insert into dbpassvalues( d b p a s s v a l u e s ( news)”); 的时候,自增的id没有自增,非常奇怪。只有从数据库中拿出当前最大的id为多少,然后 insert 进去了。
public function sendpassone(Request $request){
$this->validate($request,[
'key' => 'required',
'gameid' => 'required',
'schoolid' => 'required',
]);
//如果这个用户有账号密码,就直接发,如果这个用户没有账号密码,就接收前端传来的账号密码
$key = $request->key;
if($request->session()->has($key)){
$gameid = $request->gameid;
$dbpass = 'game_'.$gameid.'_pass';
$schoolid = $request->schoolid;
$where = 'where `schoolid`='.$schoolid;
$sql = 'select * from '.$dbpass.' '.$where;
$pass = DB::select($sql);
if($pass==null){
$login_id =$request->loginid;
$password = $request->password;
$email = $request->email;
$sql2 = 'select count(*) as total from '.$dbpass;
$numberpass = DB::select($sql2);
$number = $numberpass[0]->total+1;
$news = "$number,'your_own_nick','$login_id','$password',$schoolid,'$email'";
DB::insert("insert into $dbpass values ($news)");
$game = Game::where('id',$gameid)->first();
Mail::send('sendpass',['schoolid' => $schoolid,'username' => $login_id,'password' => $password,'gamename' => $game->gamename,'gametime' => $game->gametime,'address'=>$game->address],function($message) use ($email){
$to = $email;
$message ->to($to)->subject('比赛账号密码');
});
}else{
$email = $pass[0]->email;
$game = Game::where('id',$gameid)->first();
Mail::send('sendpass',['schoolid' => $schoolid,'username' => $pass[0]->login_id,'password' => $pass[0]->password,'gamename' => $game->gamename,'gametime' => $game->gametime,'address'=>$game->address],function($message) use ($email){
$to = $email;
$message ->to($to)->subject('比赛账号密码');
});
}
return $this->response->array([
'info' => '发送成功',
'status_code' => 200,
])->setStatusCode(200);
}else{
return $this->response->array([
'info' => '请登录后重试',
'status_code' => 400,
])->setStatusCode(200);
}
}
测试
{
"info": "发送成功",
"status_code": 200
}
路由
//账号密码对照表下载
$api->post('downloaduserpass','GamesController@downloaduserpass')
->name('api.downloaduserpass.downloaduserpass');
逻辑,可以仿照报名表的下载
public function downloaduserpass(Request $request){
$this->validate($request,[
'key' => 'required',
'gameid' => 'required',
]);
$key = $request->key;
if($request->session()->has($key)){
$gameid = $request->gameid;
$dbpass = 'game_'.$gameid.'_pass';
$sql = 'select * from '.$dbpass;
$pass = DB::select($sql);
$i = 0;
$pass2 = array();
foreach ($pass as $passone){
$pass2[$i]['id'] = $passone->id;
$pass2[$i]['team_name'] = $passone->team_name;
$pass2[$i]['login_id'] = $passone->login_id;
$pass2[$i]['password'] = $passone->password;
$pass2[$i]['schoolid'] = $passone->schoolid;
$pass2[$i]['email'] = $passone->email;
$i++;
}//只能接收二维数组,所以手动转换成二维数组
Excel::create('比赛账号密码对照表',function($excel) use ($pass2){
$excel->sheet('详细信息', function($sheet) use ($pass2){
$sheet->rows($pass2);
});
})->export('xls');
}else{
return $this->response->array([
'info' => '请登录后重试',
'status_code' => 400,
])->setStatusCode(200);
}
}
至此,用laravel开发网站博客就结束了,同学们可以把其他的接口补上,共勉。