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

news/2024/11/15 16:37:41/文章来源:https://www.cnblogs.com/xuxiaobo/p/18392631

代码:

#! 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 ...

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/791018.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

一个练习项目,好玩的bbs-go-beego

代码:package main/** go env -w GO111MODULE=on go env -w GOPROXY=https://goproxy.cn go mod init gos go mod tidy go mod vendor */import ("crypto/md5""database/sql""fmt""log""math""strconv""git…

一个练习项目,好玩的bbs-python-webpy

代码:import web import os.path import MySQLdb import json import hashlib import random import math import os from datetime import datetimeclass DateEncoder(json.JSONEncoder):def default(self, obj):if isinstance(obj, datetime):return obj.strftime("%Y-…

一个练习项目,好玩的bbs-python-flask

代码:from flask import Flask, make_response, request import os.path import MySQLdb import json import hashlib import random import math import os from datetime import datetimeapp = Flask(__name__)class DateEncoder(json.JSONEncoder):def default(self, obj):…

新电脑第一次拉取仓库代码

为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置git config --global user.name xxxxxxxx git config --global user.email xxxxxxxx初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置1 生成 RSA 密钥ssh-keygen -t rsa2 获取 RSA 公…

java~重写hashcode和equals

单字段和多字段重写hashcode 在 Java 中,重写 hashCode 方法的场景通常与对象的哈希值计算有关,特别是在使用哈希表(如 HashMap, HashSet 等)时。下面是你提供的两种 hashCode 实现的具体使用场景分析: 1. 第一种实现 @Override public boolean equals(Object o) {if (thi…

鱼眼相机成像模型和畸变模型

一文详解分析鱼眼相机投影成像模型和畸变模型 - feng..liu - 博客园 (cnblogs.com) 深入洞察OpenCV鱼眼模型之成像投影和畸变表估计系数相互转化 - 知乎 (zhihu.com)

第十二周-云计算运维作业

总结zabbix安装过程(选择课程演示版本)。1. 下载仓库 # wget https://repo.zabbix.com/zabbix/6.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_6.0-4+ubuntu22.04_all.deb # dpkg -i zabbix-release_6.0-4+ubuntu22.04_all.deb # apt update 2. 安装Zabbix server,W…

OpenCV开发笔记(八十):基于特征点匹配实现全景图片拼接

前言一个摄像头视野不大的时候,我们希望进行两个视野合并,这样让正视的视野增大,从而可以看到更广阔的标准视野。拼接的方法分为两条路,第一条路是Sticher类,第二条思路是特征点匹配。  本篇使用特征点匹配,进行两张图来视野合并拼接。 Demo100%的点匹配  换了一幅图…

中国最著名十大风水大师排名

风水大师,又称阴阳先生,因为它们常常在看风水时利用阴阳学说进行解释,于是便有了这样的称谓。风水大师从古至今非常之多,其中也不乏江湖骗子。本期风水大师就来说说中国知名的有真材实料的风水大师吧。 中国最著名十大风水大师排名 Top1裴翁 裴翁(又名裴伟胜),是世界地理名…

从实在智能最新发布的实在Agent 7.0,看RPA Agent的迭代升级

智能体进化发展了一年,现在的RPA Agent迭代到什么程度了? 从实在智能最新发布的实在Agent 7.0,看RPA Agent的迭代升级 抓取豆瓣信息、自己制作PPT,这款AI Agent真的实现了流程全自动化 AI Agent构建到执行全自动化,持续进化RPA Agent再次降低智能体应用门槛 实在智能重磅发…

丝滑解决Chatgpt频繁奔溃、断网掉线问题

这段时间使用Chatgpt的时候频繁出现`something wrong`等断网掉线问题,中间还频繁出现物品转向的人机验证(我那么具有迷惑性吗...),被烦的不行。后面了解到有一个**KeepChatgpt**的插件可以解决这些问题,于是把这个方法介绍给大家,希望大家也都可以丝滑gpt。事件缘由 这段…