一个练习项目,好玩的bbs-reactphp
php代码:
<?php //composer require react/event-loop react/http //composer install require __DIR__ . '/vendor/autoload.php'; function success($data){ return ['code' => 0, 'msg' => '', 'data' => $data]; } function error($code, $msg){ return ['code' => $code, 'msg' => $msg, 'data' => '']; } function getConn(){ $conn = mysqli_connect('127.0.0.1:3306', 'root', '123456', 'my_bbs'); mysqli_query($conn, "set names 'utf8'"); return $conn; } function getloginuserinfo($sessionId){ $conn = getConn(); $sql1 = "select id,username,nickname,addTime,sessionId from user where sessionId='{$sessionId}'"; $result = mysqli_query($conn, $sql1); $data = mysqli_fetch_assoc($result); return $data; } $server = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) { $url = $request->getUri()->getPath(); $params = $request->getQueryParams(); $secretKey = 'saacac3423@21212'; $pagesize = 20; $conn = getConn(); if($url == '/user/register'){ $username = $params['username']; $nickname = $params['nickname']; $password = $params['password']; $passwordMd5 = md5($password); $sql1 = "select id,username,nickname,addTime from user where username='{$params['username']}'"; $result = mysqli_query($conn, $sql1); $data = mysqli_fetch_assoc($result); if(empty($data)){ $sql2 = "insert into user(username, password, nickname) value('{$username}', '{$passwordMd5}', '{$nickname}')"; mysqli_query($conn, $sql2); $insertId = mysqli_insert_id($conn); return React\Http\Message\Response::json(success($insertId)); } else{ return React\Http\Message\Response::json(error(1, '用户名已经存在')); } } elseif($url == '/user/login'){ $username = $params['username']; $password = $params['password']; $passwordMd5 = md5($password); $sql1 = "select id,username,nickname,addTime from user where username='{$username}' and password='{$passwordMd5}'"; $result = mysqli_query($conn, $sql1); $data = mysqli_fetch_assoc($result); if(!empty($data)){ $data['sessionId'] = md5($secretKey . $data['id'] . $data['addTime']); $sql2 = "update user set sessionId='{$data['sessionId']}' where id={$data['id']}"; mysqli_query($conn, $sql2); return React\Http\Message\Response::json(success($data)); } else{ return React\Http\Message\Response::json(error(1, '用户名或者密码错误')); } } elseif($url == '/user/logout'){ $sessionId = $params['sessionId']; $data = getloginuserinfo($sessionId); $data['sessionId'] = ''; $sql1 = "update user set sessionId='' where sessionId='{$sessionId}'"; mysqli_query($conn, $sql1); return React\Http\Message\Response::json(success($data)); } elseif($url == '/user/getuserinfo'){ $sessionId = $params['sessionId']; $data = getloginuserinfo($sessionId); return React\Http\Message\Response::json(success($data)); } elseif($url == '/post/list'){ $page = $params['page']; $page = $page <= 0 ? 1 : $page; $keyword = !empty($params['keyword']) ? $params['keyword'] : ''; $start = ($page - 1) * $pagesize; $addsql = " isDel=0 "; if(!empty($keyword)){ $addsql = " isDel=0 and title like '%{$keyword}%' "; } $sql1 = "select count(1) as count from content where {$addsql}"; $result = mysqli_query($conn, $sql1); $data = mysqli_fetch_assoc($result); $count = $data['count']; $totalpage = ceil($count / floatval($pagesize)); $sql2 = "select id,title,userId,userNickename,replyNum,updateTime from content where {$addsql} order by updateTime desc limit {$start},{$pagesize}"; $result2 = mysqli_query($conn, $sql2); $rows = []; while($data2 = mysqli_fetch_assoc($result2)){ $rows[] = $data2; } return React\Http\Message\Response::json(success(['totalpage' => $totalpage, 'data' => $rows])); } elseif($url == '/post/detail'){ $id = $params['id']; $sql1 = "select id,title,content,userId,userNickename,replyNum,updateTime from content where isDel=0 and id={$id}"; $result = mysqli_query($conn, $sql1); $data = mysqli_fetch_assoc($result); return React\Http\Message\Response::json(success($data)); } elseif($url == '/post/add'){ $title = $params['title']; $content = $params['content']; $sessionId = $params['sessionId']; $userInfo = getloginuserinfo($sessionId); $userId = $userInfo['id']; $nickname = $userInfo['nickname']; $sql = "insert into content(title, content, userId, userNickename) value('{$title}', '{$content}', {$userId}, '{$nickname}')"; mysqli_query($conn, $sql); $insertId = mysqli_insert_id($conn); return React\Http\Message\Response::json(success($insertId)); } elseif($url == '/post/edit'){ $id = $params['id']; $title = $params['title']; $content = $params['content']; $sessionId = $params['sessionId']; $userInfo = getloginuserinfo($sessionId); $userId = $userInfo['id']; $nickname = $userInfo['nickname']; $sql = "update content set title='{$title}',content='{$content}',userId={$userId},userNickename='{$nickname}' where id={$id} and userId={$userId}"; mysqli_query($conn, $sql); return React\Http\Message\Response::json(success('')); } elseif($url == '/post/delete'){ $id = $params['id']; $sessionId = $params['sessionId']; $userInfo = getloginuserinfo($sessionId); $userId = $userInfo['id']; $nickname = $userInfo['nickname']; $sql = "update content set isDel=1 where id={$id} and userId={$userId}"; mysqli_query($conn, $sql); return React\Http\Message\Response::json(success('')); } elseif($url == '/reply/list'){ $page = $params['page']; $page = $page <= 0 ? 1 : $page; $contentId = !empty($params['contentId']) ? $params['contentId'] : ''; $start = ($page - 1) * $pagesize; $addsql = " isDel=0 and contentId={$contentId} "; $sql1 = "select count(1) as count from reply where {$addsql}"; $result = mysqli_query($conn, $sql1); $data = mysqli_fetch_assoc($result); $count = $data['count']; $totalpage = ceil($count / floatval($pagesize)); $sql2 = "select id,content,replyUserId,replyUserNickename,addTime from reply where {$addsql} order by id asc limit {$start},{$pagesize}"; $result2 = mysqli_query($conn, $sql2); $rows = []; while($data2 = mysqli_fetch_assoc($result2)){ $rows[] = $data2; } return React\Http\Message\Response::json(success(['totalpage' => $totalpage, 'data' => $rows])); } elseif($url == '/reply/detail'){ $id = $params['id']; $sql1 = "select id,content,replyUserId,replyUserNickename,addTime from reply where id={$id}"; $result = mysqli_query($conn, $sql1); $data = mysqli_fetch_assoc($result); return React\Http\Message\Response::json(success($data)); } elseif($url == '/reply/add'){ $contentId = $params['contentId']; $content = $params['content']; $sessionId = $params['sessionId']; $userInfo = getloginuserinfo($sessionId); $userId = $userInfo['id']; $nickname = $userInfo['nickname']; $sql1 = "update content set replyNum=replyNum+1 where id={$contentId}"; mysqli_query($conn, $sql1); $sql = "insert into reply(contentId, content, replyUserId, replyUserNickename) value({$contentId}, '{$content}', {$userId}, '{$nickname}')"; mysqli_query($conn, $sql); $insertId = mysqli_insert_id($conn); return React\Http\Message\Response::json(success($insertId)); } elseif($url == '/reply/edit'){ $id = $params['id']; $content = $params['content']; $sessionId = $params['sessionId']; $userInfo = getloginuserinfo($sessionId); $userId = $userInfo['id']; $nickname = $userInfo['nickname']; $sql = "update reply set content='{$content}',replyUserId={$userId},replyUserNickename='{$nickname}' where id={$id} and replyUserId={$userId}"; mysqli_query($conn, $sql); return React\Http\Message\Response::json(success('')); } elseif($url == '/reply/delete'){ $id = $params['id']; $sessionId = $params['sessionId']; $userInfo = getloginuserinfo($sessionId); $userId = $userInfo['id']; $nickname = $userInfo['nickname']; $sql1 = "select id,content,replyUserId,replyUserNickename,addTime,contentId from reply where id={$id}"; $result = mysqli_query($conn, $sql1); $data = mysqli_fetch_assoc($result); $sql1 = "update content set replyNum=replyNum-1 where id={$data['contentId']}"; mysqli_query($conn, $sql1); $sql = "update reply set isDel=1 where id={$id} and replyUserId={$userId}"; mysqli_query($conn, $sql); return React\Http\Message\Response::json(success('')); } else{ return React\Http\Message\Response::plaintext("此站接口使用php实现,<a href='api.html' target='_blank'>接口列表</a>"); } }); $socket = new React\Socket\SocketServer('127.0.0.1:1081'); $server->listen($socket); echo "Server running at http://127.0.0.1:1081" . PHP_EOL;
启动输出:
Server running at http://127.0.0.1:1081
本文来自博客园,作者:河北大学-徐小波,转载请注明原文链接:https://www.cnblogs.com/xuxiaobo/p/18268635