laravel_003_抽签API_sql、路由、控制器

laravel_003_抽签API_sql、路由、控制器-2020-8-16

目录

  • 一、sql
        • chouqian.sql
        • chouqian_info.sql
  • 二、路由
  • 三、控制器
        • DrawController.php

一、sql

laravel_003_抽签API_sql、路由、控制器_第1张图片
laravel_003_抽签API_sql、路由、控制器_第2张图片
laravel_003_抽签API_sql、路由、控制器_第3张图片

chouqian.sql

/*
 Navicat Premium Data Transfer

 Source Server         : 8.0.17
 Source Server Type    : MySQL
 Source Server Version : 80017
 Source Host           : localhost:3306
 Source Schema         : cloud

 Target Server Type    : MySQL
 Target Server Version : 80017
 File Encoding         : 65001

 Date: 16/08/2020 22:15:33
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for chouqian
-- ----------------------------
DROP TABLE IF EXISTS `chouqian`;
CREATE TABLE `chouqian`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `wid` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `qname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `lists` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `result` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `number` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

chouqian_info.sql

/*
 Navicat Premium Data Transfer

 Source Server         : 8.0.17
 Source Server Type    : MySQL
 Source Server Version : 80017
 Source Host           : localhost:3306
 Source Schema         : cloud

 Target Server Type    : MySQL
 Target Server Version : 80017
 File Encoding         : 65001

 Date: 16/08/2020 22:15:40
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for chouqian_info
-- ----------------------------
DROP TABLE IF EXISTS `chouqian_info`;
CREATE TABLE `chouqian_info`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `wid` varchar(0) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `nickname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `rid` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `rid`(`rid`) USING BTREE,
  CONSTRAINT `rid` FOREIGN KEY (`rid`) REFERENCES `cookie` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

二、路由

$router->group(['prefix' => 'draw/'], function () use (&$router) {
    $router->get('new', 'DrawController@new');
    $router->get('my', 'DrawController@my');
    $router->get('findlist', 'DrawController@findlist');
    $router->get('findname', 'DrawController@findname');
    $router->get('delete', 'DrawController@delete');
    $router->get('avatarlist', 'DrawController@avatarlist');
    $router->get('avatardelete', 'DrawController@avatardelete');
    $router->get('finddraw', 'DrawController@finddraw');
    $router->get('draw', 'DrawController@draw');
    $router->get('getresult', 'DrawController@getresult');
});

三、控制器

DrawController.php

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class DrawController extends Controller
 {
    public function new( Request $request )
 {

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

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

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

        DB::insert( "insert into chouqian values(null,'$wid','$qname','$lists','',0)" );
        $data = DB::table( 'chouqian' )->where( [
            'wid'    => $wid,
            'qname'    => $qname,
            'lists'    => $lists,
        ] )->first();
        $id = $data->id;

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

        ] );

    }

    public function my( Request $request )
 {

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

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

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

    }

    public function findlist( Request $request )
 {

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

        $data = DB::table( 'chouqian' )->where( 'id', $rid )->first();
        $lists = $data->lists ;
        if ( $data ) {
            $errors = '存在';
        } else {

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

    }

    public function findname( Request $request )
 {

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

        $data = DB::table( 'chouqian' )->where( 'id', $rid )->first();
        ;
        $lists = $data->lists ;
        $qname = $data->qname ;
        if ( $data ) {
            $errors = '存在';
        } else {

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

        ;

    }

    public function delete( Request $request )
 {

        $wid  =  $request->get( 'wid' );
        $rid = $request->get( 'rid' );
        $data = DB::delete( 'delete from chouqian where id=?', [$rid] );

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

    }

    public function avatarlist( Request $request )
 {

        $wid  =  $request->get( 'wid' );
        $rid  =  $request->get( 'rid' );
        $data = DB::table( 'chouqian' )->where( 'id', $rid )->first();
        $dataa = DB::table( 'chouqian_info' )->where( [
            'wid'    => $wid,
            'rid'    => $rid
        ] )->first();
        if ( $dataa )
 {
            $errors = '存在人';

        } else {

            $data_w = DB::table( 'user' )->where( 'wid', $wid )->first();
            $nickname = $data_w->nickname;
            $avatar = $data_w->avatar;

            DB::table( 'chouqian_info' )->insert( [
                'id' => null,
                'wid' => $wid,
                'nickname' => $nickname,
                'avatar' => $avatar,
                'rid'=>$rid,
            ] );
        }

        if ( $data ) {
            $errors = '存在房间';
            $d = DB::table( 'chouqian_info' )->where( 'rid', $rid )->get();

        } else {
            $errors = '不存在房间';
            $avatar = '无';
        }
        return response( $d );

    }

    public function avatardelete( Request $request )
 {

        $wid  =  $request->get( 'wid' );
        $rid  =  $request->get( 'rid' );
        $data = DB::table( 'chouqian_info' )->where( [
            'wid'    => $wid,
            'rid'    => $rid
        ] )->delete();
        return response( $data );

    }

    public function finddraw( Request $request )
 {

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

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

        if ( $data )
 {
            $errors = 1;
        } else {
            $errors = 0;
        }

        return response( $errors );

    }

    public function draw( Request $request )
 {
        $rid = $request->get( 'rid' );
        $result = $request->get( 'result' );
        $dataa = DB::table( 'chouqian' )->where( 'id', $rid )->first();
        $number = $dataa->number;
        $anumber = $number+1;
        $data = DB::table( 'chouqian' )->where( 'id', '=', $rid )->update( ['result' => $result] );
        $dataaa = DB::table( 'chouqian' )->where( 'id', '=', $rid )->update( ['number' =>$anumber] );
        if ( $data ) {
            $errors = '成功';
            if ( $dataaa ) {
                $errors = 'success';
            } else {
                $errors = 'fail';
            }
        } else {
            $errors = '失败';
        }

        return response( $errors );
    }

    public function getresult( Request $request )
 {

        $rid  =  $request->get( 'rid' );
        $data = DB::table( 'chouqian' )->where( 'id', $rid )->first();
        $result = $data->result;
        $number = $data->number;
        if ( $data )
 {
            $errors = '成功';
        } else {
            $errors = '失败';
        }

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

        ] );
        ;

    }
}

你可能感兴趣的:(#)