首页 > 编程语言 >一个练习项目,好玩的bbs-perl-dancer

一个练习项目,好玩的bbs-perl-dancer

时间:2024-09-02 14:13:58浏览次数:11  
标签:ref bbs perl content dancer mysql my id row

代码:

#! D:/software/Strawberry/perl/bin/perl.exe

BEGIN {
    push (@INC,'D:/workspace/studys/study_bbs');
}

use utf8;
use Dancer;
use Dancer::Serializer::JSON;
use Net::MySQL;
use Encode;
use POSIX;
use JSON qw/encode_json decode_json/;
use Digest;

our $pagesize = 20;
our $secretKey = 'saacac3423@21212';

sub getloginuserinfo{
    my $sessionId = shift;
    
    my $mysql = Net::MySQL->new(
        hostname => '127.0.0.1',
        port => '3306',
        database => 'my_bbs',
        user     => 'root',
        password => '123456'
    );
    $mysql->query("SET NAMES 'utf8mb4'");
    
    $mysql->query("SELECT id,username,nickname,addTime,sessionId FROM user where sessionId='".$sessionId."'");
    my $record_set = $mysql->create_record_iterator;
    
    my %row = ();
    while (my $ref = $record_set->each) {
        $ref->[1] = decode("utf-8", $ref->[1]);
        $ref->[2] = decode("utf-8", $ref->[2]);
        $row{"id"} = $ref->[0];
        $row{"username"} = $ref->[1];
        $row{"nickname"} = $ref->[2];
        $row{"addTime"} = $ref->[3];
    }
    
    return %row;
}

get '/' => sub {
    headers 'Server' => 'perl-dancer';
    content_type 'text/html; charset=utf-8';
    
    return "此站接口使用perl.dancer实现,<a href='api.html' target='_blank'>接口列表</a>";
};

get '/user/register' => sub {
    content_type 'application/json; charset=utf-8';
    
    my $username = param "username";
    my $password = param "password";
    my $nickname = param "nickname";
    
    my $passwordmd5 = Digest->new('MD5')->add($password)->hexdigest;
    $username = encode("utf-8", $username);
    $nickname = encode("utf-8", $nickname);
    
    my $mysql = Net::MySQL->new(
        hostname => '127.0.0.1',
        port => '3306',
        database => 'my_bbs',
        user     => 'root',
        password => '123456'
    );
    $mysql->query("SET NAMES 'utf8mb4'");
    
    $mysql->query("SELECT id,username,nickname,addTime FROM user where username='".$username."'");
    my $record_set = $mysql->create_record_iterator;
    
    my %row = ();
    while (my $ref = $record_set->each) {
        $ref->[1] = decode("utf-8", $ref->[1]);
        $ref->[2] = decode("utf-8", $ref->[2]);
        $ref->[3] = decode("utf-8", $ref->[3]);
        $row{"id"} = $ref->[0];
        $row{"username"} = $ref->[1];
        $row{"nickname"} = $ref->[2];
        $row{"addTime"} = $ref->[3];
    }
    
    my $insertid = 0;
    if(keys %row == 0){
        my $tmpstr = $secretKey . $row{"id"} . $row{"addTime"};
        my $sessionId = Digest->new('MD5')->add($tmpstr)->hexdigest;
        $mysql->query("insert into user(username, password, nickname) value('".$username."', '".$passwordmd5."', '".$nickname."')");
        $insertid = $mysql->get_insert_id();
    
        my $res_data = {'code' => 0, 'msg' => '', 'data' => $insertid};
        return to_json($res_data);
    }
    else{
        my $res_data = {'code' => 1, 'msg' => '用户名已经存在', 'data' => 0};
        return to_json($res_data);
    }
};

get '/user/login' => sub {
    content_type 'application/json; charset=utf-8';
    
    my $username = param "username";
    my $password = param "password";
    
    my $passwordmd5 = Digest->new('MD5')->add($password)->hexdigest;
    
    my $mysql = Net::MySQL->new(
        hostname => '127.0.0.1',
        port => '3306',
        database => 'my_bbs',
        user     => 'root',
        password => '123456'
    );
    $mysql->query("SET NAMES 'utf8mb4'");
    
    $mysql->query("SELECT id,username,nickname,addTime FROM user where username='".$username."' and password='".$passwordmd5."'");
    my $record_set = $mysql->create_record_iterator;
    
    my %row = ();
    while (my $ref = $record_set->each) {
        $ref->[1] = decode("utf-8", $ref->[1]);
        $ref->[2] = decode("utf-8", $ref->[2]);
        $ref->[3] = decode("utf-8", $ref->[3]);
        $row{"id"} = $ref->[0];
        $row{"username"} = $ref->[1];
        $row{"nickname"} = $ref->[2];
        $row{"addTime"} = $ref->[3];
    }
    
    my $res_data;
    if(keys %row != 0){
        my $tmpstr = $secretKey . $row{"id"} . $row{"addTime"};
        my $sessionId = Digest->new('MD5')->add($tmpstr)->hexdigest;
        $mysql->query("update user set sessionId='".$sessionId."' where id=".$row{"id"});
        $row{"sessionId"} = $sessionId;
        $res_data = {'code' => 0, 'msg' => '', 'data' => \%row};
    }
    else{
        $res_data = {'code' => 1, 'msg' => '用户名或者密码错误', 'data' => ''};
    }
    
    return to_json($res_data);
};

get '/user/logout' => sub {
    content_type 'application/json; charset=utf-8';
    
    my $sessionId = param "sessionId";
    
    my %row = getloginuserinfo($sessionId);
    
    my $mysql = Net::MySQL->new(
        hostname => '127.0.0.1',
        port => '3306',
        database => 'my_bbs',
        user     => 'root',
        password => '123456'
    );
    $mysql->query("SET NAMES 'utf8mb4'");
    
    if(keys %row != 0){
        $mysql->query("update user set sessionId='' where sessionId=".$sessionId);
        $row{"sessionId"} = '';
    }
    
    my $res_data = {'code' => 0, 'msg' => '', 'data' => \%row};
    return to_json($res_data);
};

get '/user/getuserinfo' => sub {
    content_type 'application/json; charset=utf-8';
    
    my $sessionId = param "sessionId";
    
    my %row = getloginuserinfo($sessionId);
    
    my $res_data = {'code' => 0, 'msg' => '', 'data' => \%row};
    return to_json($res_data);
};

get '/post/list' => sub {
    content_type 'application/json; charset=utf-8';
    
    my $page = param "page";
    my $keyword = param "keyword";
    
    if(!$page){
        $page = 1;
    }
    my $start = ($page - 1) * $pagesize;
    
    my $mysql = Net::MySQL->new(
        hostname => '127.0.0.1',
        port => '3306',
        database => 'my_bbs',
        user     => 'root',
        password => '123456'
    );
    $mysql->query("SET NAMES 'utf8mb4'");
    
    my $addsql = " isDel=0 ";
    if($keyword){
        $addsql = " isDel=0 and title like '%".$keyword."%' ";
    }
    
    $mysql->query("SELECT count(1) as count FROM content where ".$addsql);
    my $record_set = $mysql->create_record_iterator;
    my $count = 0;
    while (my $ref = $record_set->each) {
        $count = $ref->[0];
    }
    my $totalpage = int(ceil($count / $pagesize));
    
    $mysql->query("SELECT id,title,userNickename,replyNum,updateTime,userId FROM content where ".$addsql." order by updateTime desc limit ".$start.",".$pagesize);
    $record_set = $mysql->create_record_iterator;
    
    my @post_lists = ();
    while (my $ref = $record_set->each) {
        $ref->[1] = decode("utf-8", $ref->[1]);
        $ref->[2] = decode("utf-8", $ref->[2]);
        $ref->[4] = decode("utf-8", $ref->[4]);
        push(@post_lists, {"id" => $ref->[0], "title" => $ref->[1], "userNickename" => $ref->[2], "replyNum" => $ref->[3], "updateTime" => $ref->[4], "userId" => $ref->[5]})
    }
    
    my $res_data = {'code' => 0, 'msg' => '', 'data' => {'totalpage' => $totalpage, 'data' => \@post_lists}};
    return to_json($res_data);
};

get '/post/detail' => sub {
    content_type 'application/json; charset=utf-8';
    
    my $id = param "id";
    
    my $mysql = Net::MySQL->new(
        hostname => '127.0.0.1',
        port => '3306',
        database => 'my_bbs',
        user     => 'root',
        password => '123456'
    );
    $mysql->query("SET NAMES 'utf8mb4'");
    
    $mysql->query("SELECT id,title,content,userId,userNickename,replyNum,updateTime FROM content where id='".$id."'");
    my $record_set = $mysql->create_record_iterator;
    
    my %row = ();
    while (my $ref = $record_set->each) {
        $ref->[1] = decode("utf-8", $ref->[1]);
        $ref->[2] = decode("utf-8", $ref->[2]);
        $ref->[4] = decode("utf-8", $ref->[4]);
        $ref->[6] = decode("utf-8", $ref->[6]);
        $row{"id"} = $ref->[0];
        $row{"title"} = $ref->[1];
        $row{"content"} = $ref->[2];
        $row{"userId"} = $ref->[3];
        $row{"userNickename"} = $ref->[4];
        $row{"replyNum"} = $ref->[5];
        $row{"updateTime"} = $ref->[6];
    }
    
    my $res_data = {'code' => 0, 'msg' => '', 'data' => \%row};
    return to_json($res_data);
};

get '/post/add' => sub {
    content_type 'application/json; charset=utf-8';
    
    my $title = param "title";
    my $content = param "content";
    my $sessionId = param "sessionId";
    
    $title = encode("utf-8", $title);
    $content = encode("utf-8", $content);
    my %userInfo = getloginuserinfo($sessionId);
    my $userId = $userInfo{"id"};
    my $nickname = $userInfo{"nickname"};
    $nickname = encode("utf-8", $nickname);
    
    my $mysql = Net::MySQL->new(
        hostname => '127.0.0.1',
        port => '3306',
        database => 'my_bbs',
        user     => 'root',
        password => '123456'
    );
    $mysql->query("SET NAMES 'utf8mb4'");
    
    $mysql->query("insert into content(title, content, userId, userNickename) value('".$title."', '".$content."', ".$userId.", '".$nickname."')");
    my $insertid = $mysql->get_insert_id();
    
    my $res_data = {'code' => 0, 'msg' => '', 'data' => $insertid};
    return to_json($res_data);
};

get '/post/edit' => sub {
    content_type 'application/json; charset=utf-8';
    
    my $id = param "id";
    my $title = param "title";
    my $content = param "content";
    my $sessionId = param "sessionId";
    
    $title = encode("utf-8", $title);
    $content = encode("utf-8", $content);
    my %userInfo = getloginuserinfo($sessionId);
    my $userId = $userInfo{"id"};
    my $nickname = $userInfo{"nickname"};
    $nickname = encode("utf-8", $nickname);
    
    my $mysql = Net::MySQL->new(
        hostname => '127.0.0.1',
        port => '3306',
        database => 'my_bbs',
        user     => 'root',
        password => '123456'
    );
    $mysql->query("SET NAMES 'utf8mb4'");
    
    $mysql->query("update content set title='".$title."',content='".$content."',userId=".$userId.",userNickename='".$nickname."' where id=".$id." and userId=".$userId);
    
    my $res_data = {'code' => 0, 'msg' => '', 'data' => ''};
    return to_json($res_data);
};

get '/post/delete' => sub {
    content_type 'application/json; charset=utf-8';
    
    my $id = param "id";
    my $sessionId = param "sessionId";
    
    my %userInfo = getloginuserinfo($sessionId);
    my $userId = $userInfo{"id"};
    my $nickname = $userInfo{"nickname"};
    $nickname = encode("utf-8", $nickname);
    
    my $mysql = Net::MySQL->new(
        hostname => '127.0.0.1',
        port => '3306',
        database => 'my_bbs',
        user     => 'root',
        password => '123456'
    );
    $mysql->query("SET NAMES 'utf8mb4'");
    
    $mysql->query("update content set isDel=1 where id=".$id." and userId=".$userId);
    
    my $res_data = {'code' => 0, 'msg' => '', 'data' => ''};
    return to_json($res_data);
};

get '/reply/list' => sub {
    content_type 'application/json; charset=utf-8';
    
    my $page = param "page";
    my $contentId = param "contentId";
    
    if(!$page){
        $page = 1;
    }
    my $start = ($page - 1) * $pagesize;
    
    my $mysql = Net::MySQL->new(
        hostname => '127.0.0.1',
        port => '3306',
        database => 'my_bbs',
        user     => 'root',
        password => '123456'
    );
    $mysql->query("SET NAMES 'utf8mb4'");
    
    my $addsql = " isDel=0 and contentId=".$contentId." ";
    
    $mysql->query("SELECT count(1) as count FROM reply where ".$addsql);
    my $record_set = $mysql->create_record_iterator;
    my $count = 0;
    while (my $ref = $record_set->each) {
        $count = $ref->[0];
    }
    my $totalpage = int(ceil($count / $pagesize));
    
    $mysql->query("SELECT id,content,replyUserId,replyUserNickename,addTime FROM reply where ".$addsql." order by id asc limit ".$start.",".$pagesize);
    $record_set = $mysql->create_record_iterator;
    
    my @post_lists = ();
    while (my $ref = $record_set->each) {
        $ref->[1] = decode("utf-8", $ref->[1]);
        $ref->[3] = decode("utf-8", $ref->[3]);
        $ref->[4] = decode("utf-8", $ref->[4]);
        push(@post_lists, {"id" => $ref->[0], "content" => $ref->[1], "replyUserId" => $ref->[2], "replyUserNickename" => $ref->[3], "addTime" => $ref->[4]})
    }
    
    my $res_data = {'code' => 0, 'msg' => '', 'data' => {'totalpage' => $totalpage, 'data' => \@post_lists}};
    return to_json($res_data);
};

get '/reply/detail' => sub {
    content_type 'application/json; charset=utf-8';
    
    my $id = param "id";
    
    my $mysql = Net::MySQL->new(
        hostname => '127.0.0.1',
        port => '3306',
        database => 'my_bbs',
        user     => 'root',
        password => '123456'
    );
    $mysql->query("SET NAMES 'utf8mb4'");
    
    $mysql->query("SELECT id,content,replyUserId,replyUserNickename,addTime FROM reply where id='".$id."'");
    my $record_set = $mysql->create_record_iterator;
    
    my %row = ();
    while (my $ref = $record_set->each) {
        $ref->[1] = decode("utf-8", $ref->[1]);
        $ref->[3] = decode("utf-8", $ref->[3]);
        $ref->[4] = decode("utf-8", $ref->[4]);
        $row{"id"} = $ref->[0];
        $row{"content"} = $ref->[1];
        $row{"replyUserId"} = $ref->[2];
        $row{"replyUserNickename"} = $ref->[3];
        $row{"addTime"} = $ref->[4];
    }
    
    my $res_data = {'code' => 0, 'msg' => '', 'data' => \%row};
    return to_json($res_data);
};

get '/reply/add' => sub {
    content_type 'application/json; charset=utf-8';
    
    my $contentId = param "contentId";
    my $content = param "content";
    my $sessionId = param "sessionId";
    
    $content = encode("utf-8", $content);
    
    my %userInfo = getloginuserinfo($sessionId);
    my $userId = $userInfo{"id"};
    my $nickname = $userInfo{"nickname"};
    $nickname = encode("utf-8", $nickname);
    
    my $mysql = Net::MySQL->new(
        hostname => '127.0.0.1',
        port => '3306',
        database => 'my_bbs',
        user     => 'root',
        password => '123456'
    );
    $mysql->query("SET NAMES 'utf8mb4'");
    
    $mysql->query("update content set replyNum=replyNum+1 where id=".$contentId);
    $mysql->query("insert into reply(contentId, content, replyUserId, replyUserNickename) value(".$contentId.", '".$content."', ".$userId.", '".$nickname."')");
    my $insertid = $mysql->get_insert_id();
    
    my $res_data = {'code' => 0, 'msg' => '', 'data' => $insertid};
    return to_json($res_data);
};

get '/reply/edit' => sub {
    content_type 'application/json; charset=utf-8';
    
    my $id = param "id";
    my $content = param "content";
    my $sessionId = param "sessionId";
    
    $content = encode("utf-8", $content);
    
    my %userInfo = getloginuserinfo($sessionId);
    my $userId = $userInfo{"id"};
    my $nickname = $userInfo{"nickname"};
    $nickname = encode("utf-8", $nickname);
    
    my $mysql = Net::MySQL->new(
        hostname => '127.0.0.1',
        port => '3306',
        database => 'my_bbs',
        user     => 'root',
        password => '123456'
    );
    $mysql->query("SET NAMES 'utf8mb4'");
    
    $mysql->query("update reply set content='".$content."',replyUserId=".$userId.",replyUserNickename='".$nickname."' where id=".$id." and replyUserId=".$userId);
    
    my $res_data = {'code' => 0, 'msg' => '', 'data' => ''};
    return to_json($res_data);
};

get '/reply/delete' => sub {
    content_type 'application/json; charset=utf-8';
    
    my $id = param "id";
    my $sessionId = param "sessionId";
    
    my %userInfo = getloginuserinfo($sessionId);
    my $userId = $userInfo{"id"};
    my $nickname = $userInfo{"nickname"};
    $nickname = encode("utf-8", $nickname);
    
    my $mysql = Net::MySQL->new(
        hostname => '127.0.0.1',
        port => '3306',
        database => 'my_bbs',
        user     => 'root',
        password => '123456'
    );
    $mysql->query("SET NAMES 'utf8mb4'");
    
    $mysql->query("SELECT id,content,replyUserId,replyUserNickename,addTime,contentId FROM reply where id='".$id."'");
    my $record_set = $mysql->create_record_iterator;
    
    my %row = ();
    while (my $ref = $record_set->each) {
        $ref->[1] = decode("utf-8", $ref->[1]);
        $ref->[3] = decode("utf-8", $ref->[3]);
        $ref->[4] = decode("utf-8", $ref->[4]);
        $row{"id"} = $ref->[0];
        $row{"content"} = $ref->[1];
        $row{"replyUserId"} = $ref->[2];
        $row{"replyUserNickename"} = $ref->[3];
        $row{"addTime"} = $ref->[4];
        $row{"contentId"} = $ref->[5];
    }
    
    $mysql->query("update content set replyNum=replyNum-1 where id=".$row{"contentId"});
    $mysql->query("update reply set isDel=1 where id=".$id." and replyUserId=".$userId);
    
    my $res_data = {'code' => 0, 'msg' => '', 'data' => ''};
    return to_json($res_data);
};


set port => 1098;
dance;

 

输出:

D:\workspace\studys\study_bbs>D:\software\Strawberry\perl\bin\perl.exe D:\workspace\studys\study_bbs\start_web_dancer.pl
>> Dancer 1.3521 server 2996 listening on http://0.0.0.0:1098
== Entering the development dance floor ...

 

标签:ref,bbs,perl,content,dancer,mysql,my,id,row
From: https://www.cnblogs.com/xuxiaobo/p/18392631

相关文章

  • 一个练习项目,好玩的bbs-python-cherrypy
    代码:importcherrypyimportMySQLdbimportjsonimporthashlibimportrandomimportmathimportosfromdatetimeimportdatetimeclassDateEncoder(json.JSONEncoder):defdefault(self,obj):ifisinstance(obj,datetime):returnobj......
  • 一个练习项目,好玩的bbs-python-tornado
    代码:importos.pathimporttornado.httpserverimporttornado.webimporttornado.optionsimporttornado.ioloopfromtornado.optionsimportdefine,optionsimportMySQLdbimportjsonimporthashlibimportrandomimportmathimportosfromdatetimeimportda......
  • 一个练习项目,好玩的bbs-python-flask
    代码:fromflaskimportFlask,make_response,requestimportos.pathimportMySQLdbimportjsonimporthashlibimportrandomimportmathimportosfromdatetimeimportdatetimeapp=Flask(__name__)classDateEncoder(json.JSONEncoder):defdefault(se......
  • 一个练习项目,好玩的bbs-python-fastapi
    代码:fromfastapiimportFastAPI,Response,Cookie,Dependsfromfastapi.responsesimportJSONResponsefromfastapi.responsesimportHTMLResponseimportos.pathimportMySQLdbimportjsonimporthashlibimportrandomimportmathimportosfromdatetimeim......
  • 一个练习项目,好玩的bbs-python-bottle
    代码:frombottleimportroute,run,templatefrombottleimportBottle,request,responseimportos.pathimportMySQLdbimportjsonimporthashlibimportrandomimportmathimportosfromdatetimeimportdatetimeclassDateEncoder(json.JSONEncoder):......
  • 一个练习项目,好玩的bbs-python-pyramid
    代码:fromwsgiref.simple_serverimportmake_serverfrompyramid.configimportConfiguratorfrompyramid.viewimportview_configfrompyramid.responseimportResponseimportos.pathimportMySQLdbimportjsonimporthashlibimportrandomimportmathimport......
  • 一个练习项目,好玩的bbs-python-webpy
    代码:importwebimportos.pathimportMySQLdbimportjsonimporthashlibimportrandomimportmathimportosfromdatetimeimportdatetimeclassDateEncoder(json.JSONEncoder):defdefault(self,obj):ifisinstance(obj,datetime):......
  • django.core.exceptions.ImproperlyConfigured: 'django.contrib.gis.db.backends.mys
     没解决此问题(venv)[root@VM-8-12-centosMYPROJECT-django20240830]#python3manage.py runserver0.0.0.0:8080Exceptioninthreaddjango-main-thread:Traceback(mostrecentcalllast): File"/root/MYPROJECT/backend/venv/lib/python3.8/site-packages/django/d......
  • Sql语句出现ORA-00933: SQL command not properly ended的解决方法
    目录1.问题所示2.原理分析3.解决方法1.问题所示执行sql语句的时候出现如下问题:ORA-00933:SQLcommandnotproperlyended截图如下所示:2.原理分析ORA-00933:SQLcommandnotproperlyended是Oracle数据库中的错误,指示SQL语句存在语法问题MyS......
  • 打造Perl中的词法分析器:深入自定义文本处理
    打造Perl中的词法分析器:深入自定义文本处理Perl作为一种强大的文本处理语言,提供了丰富的工具来实现词法分析器(Lexer)。词法分析是编译原理中将源代码分解成一系列词素(Tokens)的过程,是构建编译器或解释器的第一步。本文将详细探讨如何在Perl中实现一个自定义的词法分析器,包括......