路由:
Route::view('login','login');
Route::post('logins','LoginController@login');
Route::post('add','LoginController@add');
Route::get('show','LoginController@show');
Route::get('del/{id}','LoginController@del');
Route::get('up/{id}','LoginController@getone');
Route::post('ups','LoginController@up');
api路由:
Route::post('post','LoginController@dian');
Route::post('dele','LoginController@dele');
登录页面:
<?php
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="{{url('logins')}}" method="post">
@csrf
<div class="form-group">
<label for="exampleInputEmail1">账号</label>
<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="name">
{{-- <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>--}}
</div>
<div class="form-group">
<label for="exampleInputPassword1">密码</label>
<input type="password" class="form-control" id="exampleInputPassword1" name="pwd">
</div>
<button type="submit" class="btn btn-primary">登录</button>
</form>
</body>
</html>
添加页面:
<?php
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<form action="{{url('add')}}" method="post" enctype="multipart/form-data">
@csrf
<div class="form-group">
<label for="exampleInputEmail1">标题</label>
<input type="text" class="form-control" id="exampleInputEmail1" name="live_title" >
</div>
<div class="form-group">
<label for="exampleInputPassword1">房间名称</label>
<input type="text" class="form-control" id="exampleInputPassword1" name="live_name">
</div>
<div class="form-group">
<label for="exampleInputPassword1">房间号</label>
<input type="text" class="form-control" id="exampleInputPassword1" name="live_room">
</div>
<div class="form-group">
<label for="exampleInputPassword1">观看人数</label>
<input type="text" class="form-control" id="exampleInputPassword1" name="live_number">
</div>
<div class="form-group">
<label for="exampleInputPassword1">直播状态</label>
<input type="radio" class="form-control" id="exampleInputPassword1" name="live_status" value="直播">
<input type="radio" class="form-control" id="exampleInputPassword1" name="live_status" value="未直播">
</div>
<div class="form-group">
<label for="exampleInputPassword1">主播头像</label>
<input type="file" class="form-control" id="exampleInputPassword1" name="live_img">
</div>
<button type="submit" class="btn btn-default">添加</button>
</form>
</body>
</html>
展示页面:
<?php
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<!-- CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body>
<form action="">
<input type="name" name="live_room">
<input type="submit" 搜索>
</form>
<table class="table table-dark">
<thead>
<th scope="col">序号</th>
<th scope="col">标题</th>
<th scope="col">名称</th>
<th scope="col">房间号</th>
<th scope="col">状态</th>
{{-- <th scope="col">观看人数</th>--}}
<th scope="col">图片</th>
<th scope="col">操作<th>
@foreach($data as $vel)
<tr>
<td><input type="checkbox" name='check' value="{{$vel['id']}}">{{$vel['id']}}</td>
<td>{{$vel['live_title']}}</td>
<td>{{$vel['live_name']}}</td>
<td>{{$vel['live_room']}}</td>
<td>@if($vel['live_status'] == '直播') <span class="span1" onclick="btn({{$vel['id']}})">直播</span> @else <span class="span2" onclick="btn({{$vel['id']}})">未直播</span> @endif</td>
{{-- <td>{{$vel['live_number']}}</td>--}}
{{-- <td dataid="{{$vel['id']}}"><span class="btn" >{{$vel['live_status']}}</span></td>--}}
<td dataid="{{$vel['id']}}"><span class="btn">{{$vel['live_status']}}</span></td>
<td><img src="{{$vel['live_img']}}" alt="" width="100px"></td>
<td><a href="del/{{$vel['id']}}">删除</a>|<a href="up/{{$vel['id']}}">修改</a>|<a href="{{url('login')}}">添加</a></td>
</tr>
@endforeach
</thead>
</table>
<button onclick="qx()">全选</button> <button onclick="qbx()">全不选</button> <button onclick="dele()">批量删除</button>
{{$data->appends($where)->links()}}
</body>
</html>
<script>
function btn(id) {
var vals = $(this).text();
var _this=$(this)
$.post(''),{id:id},function(res){
}
$.post('http://www.yang.com/api/post',{id:id},function(res){
console.log(vals);
console.log(res);
if(res.code == 200){
window.location.href = 'show';
}else{
}
})
}
function qx(){
//获取复选框的对象
var check=document.getElementsByName('check');
for(var i=0;i<check.length;i++){
check[i].checked=true;
}
}
function qbx(){
//获取复选框的对象
var check=document.getElementsByName('check');
for(var i=0;i<check.length;i++){
check[i].checked=false;
}
}
function dele() {
var check=document.getElementsByName('check');
var arr = [];
for(var i=0;i<check.length;i++){
if(check[i].checked == true){
arr.push(check[i].value)
}
}
$.post('http://www.yang.com/api/dele',{id:arr},function(res){
// console.log(res)
if(res.code == 200){
window.location.href = 'show';
}else{
}
})
}
</script>
修改页面:
<?php
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<form action="{{url('ups')}}" method="post" enctype="multipart/form-data">
@csrf
<input type="hidden" name="id" value="{{$data['id']}}">
<div class="form-group">
<label for="exampleInputEmail1">标题</label>
<input type="text" class="form-control" id="exampleInputEmail1" name="live_title" value="{{$data['live_title']}}">
</div>
<div class="form-group">
<label for="exampleInputPassword1">房间名称</label>
<input type="text" class="form-control" id="exampleInputPassword1" name="live_name" value="{{$data['live_name']}}">
</div>
<div class="form-group">
<label for="exampleInputPassword1">房间号</label>
<input type="text" class="form-control" id="exampleInputPassword1" name="live_room" value="{{$data['live_room']}}">
</div>
<div class="form-group">
<label for="exampleInputPassword1">观看人数</label>
<input type="text" class="form-control" id="exampleInputPassword1" name="live_number" value="{{$data['live_number']}}">
</div>
<div class="form-group">
<label for="exampleInputPassword1">直播状态</label>
<input type="radio" class="form-control" id="exampleInputPassword1" name="live_status" value="直播" @if ($data['live_status'] =='直播') checked @endif > 直播
<input type="radio" class="form-control" id="exampleInputPassword1" name="live_status" value="未直播" @if ($data['live_status'] == '未直播') checked @endif > 未直播
</div>
<div class="form-group">
<label for="exampleInputPassword1">主播头像</label>
<input type="file" class="form-control" id="exampleInputPassword1" name="live_img" value="{{$data['live_img']}}">
</div>
<button type="submit" class="btn btn-default">修改</button>
</form>
</body>
</html>
登录模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class PwdModel extends Model
{
//
protected $table = 'live_pwd';
public $timestamps = false;
public static function login(){
return self::first();
}
}
主模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class LoginModel extends Model
{
//
protected $table = 'live';
public $timestamps = false;
public static function add($data){
return self::insert($data);
}
public static function show($where = []){
return self::where($where)->paginate(2);
}
public static function del($id){
return self::find($id)->delete();
}
public static function getone($id){
return self::find($id);
}
public static function up($where,$data){
return self::where($where)->update($data);
}
}
主控制器:
<?php
namespace App\Http\Controllers;
use App\LoginModel;
use App\PwdModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class LoginController extends Controller
{
//
public function login(Request $request){
$res = $request->all();
$obj = PwdModel::login();
$time = date('H:i:s');
$time1 = '08:00:00';
$time2 = '12:00:00';
if ($time < $time1 || $time > $time2){
if ($res['name'] == $obj['name']){
if ($res['pwd'] == $obj['pwd']){
return view('add').'登陆成功';
}else{
return redirect('login').'密码错误';
}
}else{
return redirect('login').'账号错误';
}
}else if ($time > $time1 || $time < $time2){
return redirect('login').'未到登录时间';
}
}
public function add(Request $request){
$res = $request->all();
// print_r($res);
// die();
$data['live_title'] = $request['live_title'];
$data['live_name'] = $request['live_name'];
$data['live_room'] = $request['live_room'];
$data['live_number'] = $request['live_number'];
$data['live_status'] = $request['live_status'];
$data['live_img'] = $request->file('live_img')->store('img');
$obj = LoginModel::add($data);
if ($obj){
return redirect('show');
}else{
return redirect('add');
}
}
public function show(Request $request){
$where = [];
if ($request['live_room']){
$where['live_room'] = $request['live_room'];
}
$res = LoginModel::show($where);
return view('show',['data'=>$res,'where'=>$where]);
}
public function del($id){
$res = LoginModel::del($id);
if ($res){
return redirect('show');
}else{
return redirect('show');
}
}
public function getone(Request $request){
$id = $request['id'];
$res = LoginModel::getone($id);
return view('up',['data'=>$res]);
}
public function up(Request $request){
$data['live_title'] = $request['live_title'];
$data['live_name'] = $request['live_name'];
$data['live_room'] = $request['live_room'];
$data['live_number'] = $request['live_number'];
$data['live_status'] = $request['live_status'];
$where['id'] = $request['id'];
if ($request['live_img']){
$data['live_img'] = $request->file('live_img')->store('img');
}
$res = LoginModel::up($where,$data);
if ($res){
return redirect('show');
}else{
return redirect('show');
}
}
public function dian(Request $request){
$id = $request['id'];
$data = LoginModel::getone($id);
$status=$data->live_status;
if ($status == '直播'){
$status = '未直播';
}else{
$status = '直播';
}
$where = ['id'=>$id];
$data = ['live_status'=>$status];
$res = LoginModel::up($where,$data);
if($res){
return response()->json(['code'=>200,'msg'=>'成功','data'=>$status]);
}else{
return response()->json(['code'=>500,'msg'=>'未成功']);
}
}
public function dele(Request $request){
$id = $request['id'];
if(!empty($id)){
for ($i = 0;$i<count($id);$i++){
$res = LoginModel::del($id[$i]);
}
}else{
}
if ($res){
return response()->json(['code'=>200,'data'=>'删除成功']);
}else{
return response()->json(['code'=>500,'data'=>'删除失败']);
}
}
}