laravel_002_签到API_001_路由和控制器

laravel_002_签到API_001_路由和控制器-2020-8-16

一、路由

$router->group(['prefix' => 'signin/'], function () use (&$router) {
    $router->get('new', 'SigninController@newActInfo');//新建活动
    $router->get('my', 'SigninController@myActInfo');//我的创建活动信息
    $router->get('delete', 'SigninController@deleteActInfo');//删除活动信息
    $router->get('find', 'SigninController@findActInfo');//查找活动
    $router->get('sign', 'SigninController@joinActInfo');//报名
    $router->get('join', 'SigninController@myjoinActInfo');//查找参加过的活动
    $router->get('joinout', 'SigninController@joinoutActInfo');//退出活动
    $router->get('actcode', 'SigninController@Actcode');//生成活动二维码保存并返回地址
    $router->get('signcode', 'SigninController@Signcode');//生成签到二维码保存并返回地址
    $router->get('offcode', 'SigninController@Offcode');//核销签到二维码
    $router->get('place', 'SigninController@place');//地点签到
    $router->get('xlsx/{id}', 'XlsxController@Xlsx');//下载活动人员名单
});

二、控制器

SigninController.php

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Maatwebsite\Excel\Facades\Excel;
use Maatwebsite\Excel\Concerns\FromArray;
class SigninController extends Controller
 {

    public function newActInfo( Request $request )
 {

        //获取值
        $act  =  $request->get( 'act' );
        $aname  =  $request->get( 'aname' );
        $tel  =  $request->get( 'tel' );
        $content =  $request->get( 'content' );
        $latitude  =  $request->get( 'latitude' );
        $longitude  =  $request->get( 'longitude' );
        $location = $request->get( 'location' );
        $wid = $request->get( 'wid' );
        //读取act表看有没有相同名,相同不太行
        $data = DB::table( 'act' )->where( 'act', $act )->first();
        if ( $data ) {
            $errors = '存在';
            $actid = '存在相同活动名';
        } else {
            DB::insert( "insert into act values(null,'$wid','$act','$aname','$tel','$content','$location','$latitude','$longitude')" );
            $data = DB::table( 'act' )->where( 'act', $act )->first();
            $id = $data->id;
            $errors = '成功';
            $actid = '您的活动ID:'.$id;

        }

        return response()->json( [
            'errors'=>$errors,
            'actid'=>$actid,

        ] );

    }

    public function myActInfo( Request $request )
 {

        $wid  =  $request->get( 'wid' );

        $data = DB::table( 'act' )->where( 'wid', $wid )->get();
        if ( $data ) {
            $errors = '存在';
        } else {

        }
        $errors = '成功';
        return response( $data );

    }

    public function deleteActInfo( Request $request )
 {
        $wid  =  $request->get( 'wid' );
        $aid = $request->get( 'aid' );
        $data = DB::table( 'act' )->where( 'id', $aid )->get();
        $wwid = $data->wwid;
        if ( $wwid = $wid )
 {
            $data = DB::delete( 'delete from act where id=?', [$aid] );
            $errors = '成功';
        } else {
            $errors = '越权操作';
        }

        return response( $data );

    }

    public function findActInfo( Request $request )
 {

        $aid  =  $request->get( 'aid' );

        $data = DB::table( 'act' )->where( 'id', $aid )->get();
        if ( $data ) {
            $errors = '存在';
        } else {
            $errors = '成功';
        }

        return response( $data );

    }

    public function joinActInfo( Request $request )
 {

        $aid  =  $request->get( 'aid' );
        $wid = $request->get( 'wid' );
        $name = $request->get( 'name' );
        $content = $request->get( 'content' );
        $data = DB::table( 'act_info' )->where( [
            'wid'    => $wid,
            'aid'    => $aid,
        ] )->first();
        $dataa = DB::table( 'act' )->where( 'aid', $aid )->first();
        $act = $dataa->act;
        $aname = $dataa->aname;
        $tel = $dataa->tel;
        $loction = $dataa->location;

        $acontent = $dataa->content;

        if ( $data ) {
            $errors = '已报名'.$act;
        } else {

            function make_code( $length = 4 )
 {
                // 密码字符集,可任意添加你需要的字符
                $chars = array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
                'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
                't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
                'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
                'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' );
                // 在 $chars 中随机取 $length 个数组元素键名
                $keys = array_rand( $chars, $length );

                $password = '';
                for ( $i = 0; $i < $length; $i++ )
 {
                    // 将 $length 个数组元素连接成字符串
                    $password .= $chars[$keys[$i]];
                }
                return $password;
            }

            $code = make_code();

            DB::table( 'act_info' )->insert( ['id'=>'0', 'wid'=>$wid, 'name'=>$name, 'content'=>$content, 'code'=>$code, 'latitude'=>'0', 'longitude'=>'0', 'location'=>'0', 'state'=>'0', 'aid'=>$aid] );

            $errors = '成功报名'.$act;
        }

        return response()->json( [
            'errors'=>$errors,
            'aid'=>$aid,
            'act'=>$act,

        ] );

    }

    public function myjoinActInfo( Request $request )
 {

        $wid  =  $request->get( 'wid' );
        $data = DB::table( 'act_info' )->where( 'wid', $wid )->get();

        if ( $data ) {
            $errors = '存在';
        } else {
            $errors = '成功';
            $data = '空';
        }

        return response( $data );

    }

    public function joinoutActInfo( Request $request )
 {

        $wid  =  $request->get( 'wid' );
        $aid = $request->get( 'aid' );

        $data = DB::table( 'act_info' )->where( [
            'wid'    => $wid,
            'aid'    => $aid,
        ] )->delete();

        if ( $data )
 {
            $errors = '成功删除';
        } else {
            $errors = '活动已被取消';
        }

        return response( $data );

    }

    public function Actcode( Request $request )
 {

        $wid  =  $request->get( 'wid' );
        $aid = $request->get( 'aid' );
        $qrcode_id = $aid;
        $qr_path = './Uploads/';
        if ( !file_exists( $qr_path.'act/' ) ) {
            mkdir( $qr_path.'act/', 0700, true );
            //判断保存目录是否存在,不存在自动生成文件目录
        }
        $filename = 'act/'.$aid.'.png';
        $file = $qr_path.$filename;

        $data = DB::table( 'access_token' )->where( 'id', 1 )->first();

        $access_token = $data->access_token;
        $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token;

        $qrcode = array(
            'scene'			=> 'id='.$qrcode_id.'c', //二维码所带参数
            'width'			=> 200,
            'page'			=> 'pages/index/index', //二维码跳转路径(要已发布小程序)
            'auto_color'	=> true
        );

        $result = $this->sendCmd( $url, json_encode( $qrcode ) );
        //请求微信接口

        $errcode = json_decode( $result, true )['errcode'];
        $errmsg = json_decode( $result, true )['errmsg'];
        if ( $errcode ) {
            $this->render( 0, $errmsg );
        }

        $res = file_put_contents( $file, $result );
        //将微信返回的图片数据流写入文件

        if ( $res === false ) {
            $this->render( 0, '生成二维码失败' );
        } else {
            // $this->return['data'] = $this->config->item( 'base_url' ).'/Uploads/'.$filename;
            //返回图片地址链接给前端
            return response()->json( [
                'errors'=>'https://ancientcloud.club'.'/Uploads/'.$filename,

            ] );
        }
    }

    public function Signcode( Request $request ) {

        $wid  =  $request->get( 'wid' );
        $aid = $request->get( 'aid' );
        $dataa = DB::table( 'act_info' )->where( [
            'wid'    => $wid,
            'aid'    => $aid,
        ] )->first();
        $code = $dataa->code;
        $qrcode_id = $aid.$code;
        $qr_path = './Uploads/';
        if ( !file_exists( $qr_path.'user/' ) ) {
            mkdir( $qr_path.'user/', 0700, true );
            //判断保存目录是否存在,不存在自动生成文件目录
        }
        $filename = 'user/'.$aid.$wid.'.png';
        $file = $qr_path.$filename;

        $data = DB::table( 'access_token' )->where( 'id', 1 )->first();

        $access_token = $data->access_token;
        $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token;

        $qrcode = array(
            'scene'			=> 'id='.$qrcode_id.'c', //二维码所带参数
            'width'			=> 200,
            'page'			=> 'pages/index/index', //二维码跳转路径(要已发布小程序)
            'auto_color'	=> true
        );

        $result = $this->sendCmd( $url, json_encode( $qrcode ) );
        //请求微信接口

        $errcode = json_decode( $result, true )['errcode'];
        $errmsg = json_decode( $result, true )['errmsg'];
        if ( $errcode ) {
            $this->render( 0, $errmsg );
        }

        $res = file_put_contents( $file, $result );
        //将微信返回的图片数据流写入文件

        if ( $res === false ) {
            $this->render( 0, '生成二维码失败' );
        } else {
            // $this->return['data'] = $this->config->item( 'base_url' ).'/Uploads/'.$filename;
            //返回图片地址链接给前端
            return response()->json( [
                'errors'=>'https://ancientcloud.club'.'/Uploads/'.$filename,

            ] );
        }
    }

    public function Offcode( Request $request ) {

        $wid  =  $request->get( 'wid' );
        $aid  =  $request->get( 'aid' );
        $code = $request->get( 'code' );

        $dataa = DB::table( 'act_info' )->where( 'code', $code )->first();
        $state = $dataa->state;

        $data = DB::table( 'act' )->where( 'aid', $aid )->first();
        $dwid = $data->wid;

        if ( $wid = $dwid ) {

            if ( $state == 2 ) {
                $errors = '核销失败,核销码不存在';
            } else {

                DB::table( 'act_info' )->where( 'code', '=', $code )->update( ['state'=>'2'] );
                $errors = '核销成功';
            }
        } else {
            $errors = '核销失败,无权限';
        }

        return response()->json( [
            'errors'=>$errors,

        ] );

    }

    public function place( Request $request )
 {

        $aid  =  $request->get( 'aid' );
        $wid  =  $request->get( 'wid' );
        $latitude  =  $request->get( 'latitude' );
        $longitude  =  $request->get( 'longitude' );
        $data = DB::table( 'act' )->where( 'aid', $aid )->first();
        if ( $data )
 {
            $dlatitude = $data->latitude ;
            $dlongitude = $data->longitude;
        } else {
            $dlatitude = $latitude;
            $dlongitude = $longitude;
        }

        function getdistance( $lng1, $lat1, $lng2, $lat2 ) {
            // 将角度转为狐度
            $radLat1 = deg2rad( $lat1 );
            //deg2rad()函数将角度转换为弧度
            $radLat2 = deg2rad( $lat2 );
            $radLng1 = deg2rad( $lng1 );
            $radLng2 = deg2rad( $lng2 );
            $a = $radLat1 - $radLat2;
            $b = $radLng1 - $radLng2;
            $s = 2 * asin( sqrt( pow( sin( $a / 2 ), 2 ) + cos( $radLat1 ) * cos( $radLat2 ) * pow( sin( $b / 2 ), 2 ) ) ) * 6378.137 * 1000;
            return $s;
        }

        $s = getdistance( $longitude, $latitude, $dlongitude, $dlatitude );
        if ( $s>100 ) {
            $errors = '距离过远签到失败,距离'.$s.'米';
        } else {

            if ( DB::table( 'act' )->where( 'aid', $aid )->first() ) {

                DB::table( 'act_info' )->where( [
                    'wid'    => $wid,
                    'aid'    => $aid,
                ] )->update( ['latitude'=>$latitude, 'longitude'=>$longitude, 'state'=>'1'] );
                $errors = '签到成功,距离'.$s.'米';
            } else {
                $errors = '活动已删除';
                $data = DB::table( 'act_info' )->where( [
                    'wid'    => $wid,
                    'aid'    => $aid,
                ] )->delete();

            }
        }
        return response()->json( [
            'errors'=>$errors,
            's'=>'距离'.$s,

        ] );

    }
    //开启curl post请求

    function sendCmd( $url, $data )
 {
        $curl = curl_init();
        // 启动一个CURL会话
        curl_setopt( $curl, CURLOPT_URL, $url );
        // 要访问的地址
        curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, 0 );
        // 对认证证书来源的检测
        curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
        // 从证书中检查SSL加密算法是否存在
        curl_setopt( $curl, CURLOPT_HTTPHEADER, array( 'Expect:' ) );
        //解决数据包大不能提交
        curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, 1 );
        // 使用自动跳转
        curl_setopt( $curl, CURLOPT_AUTOREFERER, 1 );
        // 自动设置Referer
        curl_setopt( $curl, CURLOPT_POST, 1 );
        // 发送一个常规的Post请求
        curl_setopt( $curl, CURLOPT_POSTFIELDS, $data );
        // Post提交的数据包
        curl_setopt( $curl, CURLOPT_TIMEOUT, 30 );
        // 设置超时限制防止死循
        curl_setopt( $curl, CURLOPT_HEADER, 0 );
        // 显示返回的Header区域内容
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
        // 获取的信息以文件流的形式返回

        $tmpInfo = curl_exec( $curl );
        // 执行操作
        if ( curl_errno( $curl ) ) {
            echo 'Errno'.curl_error( $curl );
        }
        curl_close( $curl );
        // 关键CURL会话
        return $tmpInfo;
        // 返回数据
    }
}

XlsxController.php

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Maatwebsite\Excel\Facades\Excel;
use Maatwebsite\Excel\Concerns\FromArray;

class TestExport implements FromArray
 {
    private $id;

    public function __construct( $id )
 {
        $this->id = $id;
    }

    public function array(): array
 {
        $wid = substr( $this->id, 4, 6 );
        $aid = substr( $this->id, 0, 4 );
        $dataa = DB::table( 'act_info' )->where( 'aid', $aid )->get();
        $d = [['id', '小程序id', '姓名', '备注', '核销码', '纬度', '经度', '地点', '状态','活动id']];
        $dat = DB::table( 'act' )->where( 'aid', $aid )->first();
        $dwid = $dat->wid ;
        if ( $wid = $dwid )
 {
            $data = json_decode( $dataa, true );
        } else {
            $data = [];
        }

        return array_merge( $d, $data );

    }
}

class EactController extends Controller
 {

    public function excel( $id ) {

        return Excel::download( new TestExport( $id ), 'test.xlsx' );

    }
}
©️2020 CSDN 皮肤主题: 数字50 设计师:CSDN官方博客 返回首页