lavarel  curl   Ajax增删改查

lavarel  curl   Ajax增删改查

控制器 liveController

<?php

namespace App\Http\Controllers;
use App\live_lognModel;
use App\liveModel;
use Illuminate\Support\Facades\DB;
use QL\QueryList;
use Illuminate\Http\Request;

class liveController extends Controller
{
    //采集数据入库
    public function add(){
        $url = 'https://www.huya.com/majiajia';
    // 定义采集规则
        $rules = [
            'title' => ['h3','text'],
            'home' => ['#J_roomTitle','title'],
            'img' => ['#avatar-img','src'],
            'status'=>['#J_profileNoticeText','text'],
            'text'=>['.host-video','href']
        ];
        $rt = QueryList::get($url)->rules($rules)->query()->getData();
        $live = $rt->all();
        $live['title']=$rt['title'];
        $live['home']=$rt['home'];
        $live['img']=$rt['img'];
        $live['status']=$rt['status'];
        $live['text'] = $rt['text'];

        $data = liveModel::add($live);
        if ($data){
            return "添加成功";
        }else{
            return "添加失败";
        }
    }

    //展示列表,根据主播名搜索
    public function show(Request $request){
        $where = [];
        if ($request['title']){
            $where['title'] = $request['title'];
        }
        $data = liveModel::show($where);
        return view('live.show',['data'=>$data,'where'=>$where]);
    }

    //批量删除
    public function dels(Request $request){
        $id = $request['id'];
//        print_r($id);
        $data = liveModel::del($id);
        if ($data){
            return json_encode(['state'=>0,'info'=>'删除成功','data'=>'']);
        }else{
            return json_encode(['state'=>1,'info'=>'删除失败','data'=>'']);
        }
    }

    //删除
//    public function del($id){
//        $data = new liveModel();
//        $del = $data->find($id);
//        $delete = $del->delete();
//        if ($delete){
//            return redirect('liveShow');
//        }else{
//            return redirect('liveShow');
//        }
//    }

    //阅读量+1
    public function page(Request $request){
        $id = $request['id'];
        $data = liveModel::getnoe($id);
        $page = $data['page'];
        if ($data){
            $res = liveModel::up_page($id,['page'=>$page+1]);
            return redirect('live_desc');
        }else{
            die('没有数据');
        }
    }
    //修改状态
    public function upt(Request $request){
        $id = $request['id'];
        $data = liveModel::getnoe($id);
        if ($data['status']=='开启'){
            $data = liveModel::upt($id,['status'=>'禁用']);
            return json_encode(['status'=>202,'info'=>'修改成功','data'=>$data]);
        }else{
            $data = liveModel::upt($id,['status'=>'开启']);
            return json_encode(['status'=>200,'info'=>'修改成功','data'=>$data]);
        }
    }
    //根据状态判断删除
    public function del(Request $request){
        $id = $request['id'];
        $data = liveModel::getnoe($id);
        if ($data['status']=='开启'){
            return json_encode(['status'=>1,'info'=>'删除失败','data'=>'']);
        }else{
            $data = liveModel::del($id);
            return json_encode(['status'=>0,'info'=>'删除成功','data'=>'']);
        }
    }
    //修改
    public function up($id,Request $request){
        $obj = liveModel::find($id);


        if ($request->isMethod('post')){

            $data = $request->all();
            if ($request['img']){
                $data['img']= $request->file('img')->store('images');
            }
            $res = liveModel::up($id);
            if ($res){
                return redirect('liveShow');
            }else{
                return redirect('liveUp');
            }
        }else{
            return view('live.up',['data'=>$obj]);
        }

    }
    //登录
    public function logn(Request $request){
        $data = $request->all();
        $res = live_lognModel::logn($data['name']);
        if ($data['name']==$res['name']){
            if ($data['pwd']==$res['pwd']){
                return redirect('liveShow');
            }else{
                return "<script>alert('密码错误');window.location.href='live_logn'</script>";
            }
        }else{
            return "<script>alert('用户名错误');window.location.href='live_logn'</script>";
        }
    }
}

模型 liveModel

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class liveModel extends Model
{
    //
    protected $table = "live";
    public $timestamps = false;

    public static function add($live){
        return self::insert($live);
    }

    public static function show($where=[]){
        return self::where($where)->paginate(7);
    }

//    public static function del($id){
//        return self::destroy($id);
//    }

    public static function getnoe($id){
        return self::where('id',$id)->first();
    }

    public static function up_page($id,$where){
        return self::where('id',$id)->update($where);
    }

    public static function upt($id,$where){
        return self::where('id',$id)->update($where);
    }

    public static function del($id){
        return self::where('id',$id)->delete();
    }

    public static function up($id){

        return self::where('id','=',$id)->first();
    }
}

模型 live_lognModel

 

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class live_lognModel extends Model
{
    //
    protected $table = "live_logn";
    public $timestamps = false;

    public static function logn($name){
        return self::where('name',$name)->first();
    }
}

 

视图层 live/show.blade.php

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <!-- 最新版本的 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">
    <title>Document</title>
</head>
<body>
<form action="">
    <input type="text" name="title" placeholder="请根据关键字搜索">
    <input type="submit" value="搜索">
    <button id="batch">批量删除</button>
</form>
<table  class="table">
    <tr class="info">
        <th>选择</th>
        <th>序号</th>
        <th>主播名</th>
        <th>房间名</th>
        <th>主播头像</th>
        <th>动态</th>
        <th>连接地址</th>
        <th>阅览量</th>
        <th>状态</th>
        <th>操作</th>


    </tr>
    @foreach($data as $key)
        <tr class="danger">
            <td>
                    <input type="checkbox" checkid="{{$key['id']}}" name="check">
            </td>
            <td>
                {{$key['id']}}
            </td>
            <td align="left">
                <a href="livePage?id={{$key['id']}}" >{{$key['title']}}</a>
            </td>
            <td>
                {{$key['home']}}
            </td>
            <td>
                <img src="{{$key['img']}}" width="60px" height="60px">
            </td>
            <td>
                {{$key['status']}}
            </td>
            <td>
                <a href="{{$key['text']}}">{{$key['text']}}</a>
            </td>
            <td>
                {{$key['page']}}
            </td>
            <td>
                <button class="status" t_id="{{$key['id']}}">{{$key['state']}}</button>
            </td>
            <td>
                <button class="del" d_id="{{$key['id']}}">删除</button>
                <a href="{{url('liveUp')}}/{{$key['id']}}">修改</a>
            </td>
        </tr>
    @endforeach
</table>
{{ $data->withquerystring()->links() }}
</body>
</html>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
    $('#batch').click(function () {
        var arr=[];
       var al = $("input[name='check']:checked").each(function () {
            arr.push($(this).attr("checkid"))
        })
        var id = arr
        $.get('liveDels',{id:id},function (res) {
            if (res.state==0){
                al.parent().parent().remove();
            }else{
                alert(res.info)
            }
        },'json')
    })

    $('.status').click(function () {
        let _this = $(this);
        let id = $(this).attr('t_id')
        $.get('liveUpt',{id:id},function (res) {
            if(res.status==200){
                _this.text('开启')
            }else{
                _this.text('禁用')
            }
        },'json')
    })

    $('.del').click(function () {
        let _this = $(this);
        let id = $(this).attr('d_id')
        $.get('liveDel',{id:id},function (res) {
            if(res.status!=0){
                alert('状态为正常,删除失败')
            }else{
                _this.parent().parent().remove()
            }
        },'json')
    })
</script>

视图层  live/up.blade.php

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <!-- 最新版本的 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">

    <title>Document</title>
</head>
<body>

<form   action="{{url('liveUp')}}/{{$data->id}}" method="post" enctype="multipart/form-data" >
    @csrf
    <div class="form-group">
        <label >主播名</label>
        <input type="text" class="form-control" name="title" value="{{$data->title}}">
    </div>
    <div class="form-group">
        <label >房间名</label>
        <input type="text" class="form-control" name="home" value="{{$data->home}}">
    </div>
    <div class="form-group">
        <label>主播头像</label>
        <input type="file" name="img">
    </div>
    <div class="form-group">
        <label>动态</label>
        <input type="text" class="form-control" name="status" value="{{$data->status}}">
    </div>
    <div class="form-group">
        <label >连接地址</label>
        <input type="text" class="form-control" name="text" value="{{$data->text}}">
    </div>
    <button type="submit" class="btn btn-default">确认修改</button>

</form >

</body>
</html>

路由

Route::get('liveAdd','liveController@add');
Route::get('liveShow','liveController@show');
Route::get('liveDel','liveController@del');
Route::get('liveDels','liveController@dels');
Route::get('livePage','liveController@page');
Route::view('live_desc','live.desc');
Route::get('liveUpt','liveController@upt');
Route::any('liveUp/{id}','liveController@Up');
Route::view('live_logn','live.logn');
Route::any('liveLogn','liveController@logn');

 

以上就是一整套增删改查(curl)登录 还有一点Ajax。

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值