thinkphp论坛项目开发

效果图

thinkphp论坛项目开发_第1张图片

首先是数据库

/*
Navicat MySQL Data Transfer

Source Server         : xm
Source Server Version : 50553
Source Host           : localhost:3306
Source Database       : test

Target Server Type    : MYSQL
Target Server Version : 50553
File Encoding         : 65001

Date: 2019-01-17 15:00:23
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for test_topic
-- ----------------------------
DROP TABLE IF EXISTS `test_topic`;
CREATE TABLE `test_topic` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  `content` text NOT NULL,
  `user_id` int(11) NOT NULL,
  `created_at` int(11) NOT NULL,
  `is_delete` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=57 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of test_topic
-- ----------------------------
INSERT INTO `test_topic` VALUES ('48', 'php是最好的语言4', '沙发是的范德萨撒范德萨发敖德萨多第三方撒地方大SAV大V', '33', '1547610289', '0');
INSERT INTO `test_topic` VALUES ('49', 'ceshi ', '大大沙发斯蒂芬', '33', '1547610343', '0');
INSERT INTO `test_topic` VALUES ('50', '的是非得失', '第三个梵蒂冈', '33', '1547610400', '0');
INSERT INTO `test_topic` VALUES ('51', '多福多寿', '阿道夫', '33', '1547610507', '0');
INSERT INTO `test_topic` VALUES ('52', '王企鹅去吧', '无法让我', '33', '1547633627', '0');
INSERT INTO `test_topic` VALUES ('53', '我的钱多额', 'werewolf未确认翁', '33', '1547633684', '0');
INSERT INTO `test_topic` VALUES ('54', '2323232', '232323', '33', '1547633838', '0');
INSERT INTO `test_topic` VALUES ('55', '2323232', '232323', '33', '1547634620', '0');
INSERT INTO `test_topic` VALUES ('56', '56ttt', '232323跳跳糖', '33', '1547634633', '0');

-- ----------------------------
-- Table structure for test_user
-- ----------------------------
DROP TABLE IF EXISTS `test_user`;
CREATE TABLE `test_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL COMMENT '用户名',
  `email` varchar(100) CHARACTER SET latin1 NOT NULL COMMENT '邮箱',
  `avatar` varchar(255) CHARACTER SET latin1 NOT NULL,
  `password` varchar(100) CHARACTER SET latin1 NOT NULL COMMENT '密码',
  `created_at` int(11) NOT NULL,
  `is_delete` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=35 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of test_user
-- ----------------------------
INSERT INTO `test_user` VALUES ('23', 'zxj', '[email protected]', 'images/avatar.jpg', '9db06bcff9248837f86d1a6bcf41c9e7', '1547607239', '0');
INSERT INTO `test_user` VALUES ('29', '', '[email protected]', 'images/avatar.jpg', '9db06bcff9248837f86d1a6bcf41c9e7', '1547608741', '0');
INSERT INTO `test_user` VALUES ('30', '', '[email protected]', 'images/avatar.jpg', '9db06bcff9248837f86d1a6bcf41c9e7', '1547608915', '0');
INSERT INTO `test_user` VALUES ('31', '', '[email protected]', 'images/avatar.jpg', '9db06bcff9248837f86d1a6bcf41c9e7', '1547609026', '0');
INSERT INTO `test_user` VALUES ('32', '', '[email protected]', 'images/avatar.jpg', '9db06bcff9248837f86d1a6bcf41c9e7', '1547609076', '0');
INSERT INTO `test_user` VALUES ('33', 'root', '[email protected]', 'images/avatar.jpg', '9db06bcff9248837f86d1a6bcf41c9e7', '1547609167', '0');
INSERT INTO `test_user` VALUES ('34', 'root', '[email protected]', 'images/avatar.jpg', '9db06bcff9248837f86d1a6bcf41c9e7', '1547609206', '0');

模型层

application/common/model/Base.php

php
namespace app\common\model;
use think\Model;
class Base extends Model{

    protected $autoWriteTimestamp = true;
    protected $createTime='created_at';
    /**
     * 新增逻辑
     * @auth  cyy
     * @param array  $data [description]
     * @return  int
     */
    public function add($data = []) {
        if(empty($data) || !is_array($data)) {
            return false;
        }

        $this->allowField(true)->save($data);
        return $this->id;
    }
}

application/common/model/Topic.php

php
namespace app\common\model;
class Topic extends Base
{
    //查询属于哪个用户
    public function user()
    {
        // 连接topic表中的user_id
        return $this->hasOne('User','id','user_id');
    }

    //获取所有留言
    public static function getTopics()
    {
        $topics = model("Topic")
            ->order("id", "desc")
            ->select();
        return $topics;
    }
    
    //获取某条留言
    public static function getTopic($id)
    {
        $topic = self::find(['id' => $id]);
        //print_r(model("Topic")->getLastSql());die();
        return $topic;
    }
}

application/common/model/User.php

php
namespace app\common\model;
use think\Model;

class User extends Base{
    //查询账号
    public function isUser($condition) {
        return $this->where($condition)
             ->limit(1)
             ->select();
    }
}

控制器层

application/index/controller/Base.php

php
namespace app\index\controller;
use think\Controller;
class Base extends Controller {

    /**
     * 空操作
     * @auth   singwa
     * @param  [type] $name [description]
     * @return [type]       [description]
     */
    public function _empty($name) {
        return $name;
    }
}

application/index/controller/Index.php

php
namespace app\index\controller;
use think\Db;
use app\common\model\User;
use app\common\model\Topic as TopicModel;
class Index extends Base
{
    public function index()
    {
        //获取session
        $name=session('name');
        //获取留言列表
        $topic=model('Topic');
        $topics=$topic->getTopics();
        //输出到模板
        
        echo  $this->fetch('',[
            'name'=>$name,
            'topics'=>$topics,
        ]);
    }

    //注册
    public function register()
    {
        echo  $this->fetch();
    }

    //添加账号
    public function add(){
        //如果有数据提交
        if(!empty(input('param.'))){
            //接收数据
            $userdata=input('param.');
            $name=$userdata['username'];
               $email=$userdata['email'];
            $password=$userdata['password'];
            $password_confirmation=$userdata['password_confirmation'];
            //验证确认密码是否正确
            if($password_confirmation!=$password){
                $this->error('两次密码输入不一致','index/register');
            }
            $data=[
                'name'=>$name,
                'email'=>$email,
                'password'=>$password,
            ];
            //存储数据
            $user=model('User');
            $id=$user->add($data);
            $this->success('注册成功','index/login');
        }
    }

    //登录
    public function login()
    {
        //检测是否登录
        if(session('name')){
            return $this->error('您已经登录',url('index/index'));
        }
        echo  $this->fetch();
        //获取session
        $name=session('name');
        //输出到头部模板
        echo  $this->fetch('common/header',[
            'name'=>$name,
        ]);
    }

    //登录检测
    public function isUser(){
        //如果有数据提交
        if(!empty(input('param.'))){
            //接收数据
            $userdata=input('param.');
            $login=$userdata['login'];
            $password=$userdata['password'];
            //查询条件
            $condition1=[
                'name'=>$login,
                'password'=>$password,
            ];
            $condition2=[
                    'email'=>$login,
                    'password'=>$password,
                ];
            //查询
            $user=model('User');
            $login1=$user->isUser($condition1);
            $login2=$user->isUser($condition2);
            if(!empty($login1)){
                //姓名登录
                session('id',$login1[0]->id);
                session('name',$login1[0]->name);
                $this->success('登录成功','index/index');
            }else if(!empty($login2)){
                //邮箱登录
                session('id',$login2[0]->id);
                session('name',$login2[0]->name);
                $this->success('登录成功','index/index');
            }else{
                $this->error('登录失败','index/login');
            }
        }
    }

    //退出
    public function logout(){
        session('name',null);
        $this->redirect('index/index');
    }
}

application/index/controller/Topic.php

php
namespace app\index\controller;
use think\Db;
use app\common\model\Topic as TopicModel;
class Topic extends Base
{
    public function _initialize(){
        //检测是否登录
        if(!session('name')){
            $this->error('您没有登录',url('index/login'));
        }
    }
    //发帖页面
    public function new_topic(){
        echo  $this->fetch();
        //获取session
        $name=session('name');
        //输出到头部模板
        echo  $this->fetch('common/header',[
            'name'=>$name,
        ]);
    }

    //添加新帖
    public function add_topic(){
        //如果有数据提交
        if(!empty(input('param.'))){
            //接收数据
            $topicdata=input('param.');
            $title=$topicdata['title'];
               $content=$topicdata['content'];
               $user_id=session('id');

            $data=[
                'title'=>$title,
                'content'=>$content,
                'user_id'=>$user_id,
            ];
            //存储数据
            $topic=model('Topic');
            $id=$topic->add($data);
            $this->success('添加成功','index/index');
        }
    }

    //编辑新帖页面
    public function detail_topic(){
        //接收id
        $id=input('param.id');
        //查询这条帖子的发布者
           $condition=[
               'id'=>$id,
           ];
           $user=model('Topic')->where($condition)->find();
           $user_id=$user->user_id;
           //判断是否有权限编辑帖子
           if(session('id') != $user_id){
               $this->error('您没有权限修改!');
           }
        $topic=model('Topic')->getTopic($id);
        echo  $this->fetch('',[
            'topic'=>$topic,
        ]);
        //获取session
        $name=session('name');
        //输出到头部模板
        echo  $this->fetch('common/header',[
            'name'=>$name,
        ]);
    }

    //更新帖子数据
    public function update_topic(){
        //如果有数据提交
        if(!empty(input('param.'))){
            //接收数据
            $topicdata=input('param.');
            $title=$topicdata['title'];
               $content=$topicdata['content'];
               $id=$topicdata['id'];

            $data=[
                'title'=>$title,
                'content'=>$content,
            ];
            $condition=[
                   'id'=>$id,
               ];
            //存储数据
            $res=model('Topic')->where($condition)->update($data);
            if($res){
                $this->success('编辑成功','index/index');
            }else{
                $this->error('编辑失败');
            }
            
        }
    }
}

视图层

application/index/view/common/header.html

DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>test论坛title>
  <link rel="stylesheet" href="css/all-df86af5803.css">
  <script src="js/all-17476e6cc3.js">script>
head>
<body class="forum" data-page="forum">
  <div class="header">
    <nav class="navbar navbar-inverse navbar-fixed-top navbar-default">
      <div class="container">
        <div class="navbar-header" id="navbar-header">
          <a href="" class="navbar-brand"><img src="images/logo.png">a>
        div>
        <div id="main-nav-menu">
          <ul class="nav navbar-nav">
            <li class="active">
              <a href="">
                <i class="fa fa-home">i>
                <span class="hidden-xs hidden-sm">首页span>
              a>
            li>
          ul>
        div>
        <ul class="nav navbar-nav navbar-right">
          php if(!isset($name)): ?>
            <li><a href="" id="signup-btn">注册a>li>
            <li><a href="" id="login-btn">登录a>li>
          php else: ?>
            <li><a href="">发帖a>li>
            <li>
              <ul class="nav user-bar navbar-nav navbar-right">
                <li class="dropdown">
                  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">php echo $name; ?>
                       <span class="caret">span>a>
                  <button class="navbar-toggle" type="button" data-toggle="dropdown" role="button" aria-expanded="false">
                    <span class="sr-only">Togglespan>
                    <i class="fa fa-reorder">i>
                  button>
                  <ul class="dropdown-menu" role="menu">

                    <li class='divider'>li>
                    <li>
                        <a href="" onclick="return confirm('你确定要退出吗?')"><i class="fa fa-sign-out">i>退出a>
                    li>
                  ul>
                li> 
              ul>
            li>
          php endif; ?>
        ul>
      div>
    nav>
  div>

application/index/view/common/footer.html

    <footer class="footer">
      <div class="container">
        <div class="copyright">
          <blockquote class="pull-left">
            <p>test论坛p>
            <p>Copyright © 2015-2016 <a href="#">testa> 1.0.0-rc2 <span class="pipe">|span><span class="pipe">|span>p>
          blockquote>
        div>
        <div class="pull-right hidden-sm hidden-xs">
          <p>
            <a href="#"target="_blank">
              <img src="images/test-logo.png" border="0" height="40" data-toggle="tooltip" data-placement="top" title="Powered by test"/>
            a>
          p>
        div>
      div>
    footer>
  body>
html>

application/index/view/common/topic_list.html

thinkphp论坛项目开发_第2张图片

 

 

<div class="col-md-9 threads-index main-col">
    <div class="panel panel-default">
        <div class="panel-heading">
            <div class="pull-left hidden-sm hidden-xs">
                <i class="fa fa-list">i> 首页
            div>
            <div class="clearfix">div>
        div>
        <div class="panel-body remove-padding-horizontal">
            php foreach($topics as $vo): ?>
            <ul class="list-group row thread-list">
                <li class="list-group-item media " style="margin-top: 0px;">
                    <div class="avatar pull-left">
                        <a href="?id=">
                            <img class="media-object img-thumbnail avatar-48" alt="imooc"
                                 src=""/>
                        a>
                    div>
                    <div class="infos">
                        <div class="media-heading">
                            <i class="">php echo $vo['title']; ?>i>
                            <a href="?id=">
                        div>
                        <div class="media-body meta">
                            <a href="?id=">
                                <span>php echo $vo['content']; ?>span>
                            a>

              <span class="timeago" data-toggle="tooltip" data-placement="top"
                    title="">
              span>
                        div>
                    div>
                li>
            ul>
            php endforeach; ?>
        div>
    div>
div>
ALTER TABLE test_user ALTER avatar SET DEFAULT "images/avatar.jpg" ; 

application/index/view/index/index.html

php require(APP_PATH.'/index/view/common/header.html');?>
<div id="main" class="main-container container">
    
    php require(APP_PATH.'/index/view/common/topic_list.html');?>
div>
<div class="clearfix">div>
php require(APP_PATH.'/index/view/common/footer.html');?>

application/index/view/index/login.html

thinkphp论坛项目开发_第3张图片

 

 

php require(APP_PATH.'/index/view/common/header.html');?>
<div id="main" class="main-container container">
<div class="container-fluid">
    <div class="row">
        <div class="col-md-5 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">登录div>
                <div class="panel-body">
                    <form role="form" method="POST" action="">
                        <input type="hidden" name="_token" value="BAnCy9yzQxp30BiIkeU58OMjR5uWeDM9dCSMNwKH">
                        <div class="form-group">
                            <input type="login" class="form-control" name="login" value="" placeholder="用户名或邮箱地址">
                        div>
                        <div class="form-group">
                            <input type="password" class="form-control" name="password" placeholder="密码">
                        div>
                        <div class="form-group">
                            <input type="submit" name="commit" value="登录" class="btn btn-primary btn-lg btn-block">
                        div>
                    form>
                div>
                <div class="panel-footer">
                    <a href="">注册a>
                    
                div>
            div>
        div>
        <div class="col-md-3">
            <div class="panel panel-default">
                <div class="panel-heading">登陆提示div>
                <ul class="list-group">
                    <li class="list-group-item">
                        如果您忘记了密码,请联系管理员。
                    li>
                ul>
            div>
        div>
    div>
div>

                        div>
php require(APP_PATH.'/index/view/common/footer.html');?>

application/index/view/index/register.html

thinkphp论坛项目开发_第4张图片

 

 

php require(APP_PATH.'/index/view/common/header.html');?>
<div id="main" class="main-container container">
                <div class="container-fluid">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <div class="panel panel-default">
                <div class="panel-heading">注册div>
                <div class="panel-body">
                    <form role="form" method="POST" action="">
                        <div class="form-group">
                            <input type="text" class="form-control" name="username" value="" placeholder="用户名">
                        div>
                        <div class="form-group">
                            <input type="text" class="form-control" name="email" value="" placeholder="邮箱地址">
                        div>
                        <div class="form-group">
                            <input type="password" class="form-control" name="password" placeholder="密码">
                        div>
                        <div class="form-group">
                            <input type="password" class="form-control" name="password_confirmation" placeholder="确认密码">
                        div>
                        <div class="form-group">div>
            <div class="form-group">
                            <button type="submit" class="btn btn-primary">
                                注册
                            button>
                            <a href="/" class="btn btn-default">取消a>
                        div>
                    form>
                div>
                <div class="panel-footer">
                    已注册或使用社交账号登录,请点击 <a href="">这里a> 进行登录。
                div>
            div>
        div>
    div>
div>
div>
php require(APP_PATH.'/index/view/common/footer.html');?>

application/index/view/topic/detail_topic.html

thinkphp论坛项目开发_第5张图片

 

 

php require(APP_PATH.'/index/view/common/header.html');?>
<div id="main" class="main-container container">
                                <div class="row max-width">
    <div class="col-xs-12">
        <ul class="breadcrumb">
            <li>
                <a href="index.html">首页a>
            li>
           <li>
           <strong>发表新帖strong>
            li>
           ul>
    div>
div>
    <div class="thread_create">
        <div class="col-md-9 main-col">
            <div class="panel panel-default corner-radius">
                <div class="panel-heading">发表新帖div>
                <div class="panel-body">
                    <div class="reply-box form box-block">
                      <form method="POST" action="" accept-charset="UTF-8" id="thread_create_form" class="create_form">
                        <div class="form-group">
                          <input class="form-control" id="thread_title" placeholder="标题" name="title" type="text" value="title?>">
                            <input class="form-control" id="id" placeholder="标题" name="id" type="hidden" value="id?>">
                        div>

                        
                        <div class="editor-toolbar">
                          <ul class="nav nav-pills" style="clear:none;">
                            <li class="edit active"><a href="#">帖子内容:a>li>
                          ul>
                        div>
                        
                        <div class="form-group">
                            <textarea class="post-editor form-control" rows="15"
                                      style="overflow:hidden" id="body_field" placeholder="请撰写内容" name="content" cols="50" >= $topic->content ?>textarea>
                        div>

                        <div class="form-group status-post-submit">
                            <input class="btn btn-primary col-xs-2" id="" type="submit" value="发布">
                        div>

                        form>
                    div>
                div>
            div>

        div>

        <div class="col-md-3 side-bar">


            <div class="panel panel-default corner-radius help-box">
                <div class="panel-heading text-center">
                    <h3 class="panel-title">发帖提示h3>
                div>
                <div class="panel-body">
                    <ul class="list">
                        <li>主题标题
                            <p>请在标题中描述内容要点。p>
                        li>
                        <li>选择节点
                            <p>请为你的主题选择一个节点。恰当的归类会让你发布的信息更有用。p>
                        li>
                        <li>正文
                            <p>imooc 支持 <span style="font-family: Consolas, 'Panic Sans', mono"><a href="https://help.github.com/articles/github-flavored-markdown" target="_blank">GitHub Flavored Markdowna>span> 文本标记语法。你可以在页面下方实时预览正文的实际渲染效果。p>
                        li>
                    ul>
                div>
            div>

            <div class="panel panel-default corner-radius help-box">
                <div class="panel-heading text-center">
                    <h3 class="panel-title">社区指导原则h3>
                div>
                <div class="panel-body">
                    <ul class="list">
                        <li>尊重原创
                            <p>请不要在 imooc 发布任何盗版链接,包括软件、音乐、电影等。p>
                        li>
                        <li>友好互助
                            <p>保持对陌生人的友善。用知识去帮助别人。p>
                        li>
                    ul>
                div>
            div>

        div>
    div>


div>
php require(APP_PATH.'/index/view/common/footer.html');?>

application/index/view/topic/new_topic.html

php require(APP_PATH.'/index/view/common/header.html');?>
<div id="main" class="main-container container">
                                <div class="row max-width">
    <div class="col-xs-12">
        <ul class="breadcrumb">
            <li>
                <a href="index.html">首页a>
            li>
           <li>
           <strong>发表新帖strong>
            li>
           ul>
    div>
div>
    <div class="thread_create">
        <div class="col-md-9 main-col">
            <div class="panel panel-default corner-radius">
                <div class="panel-heading">发表新帖div>
                <div class="panel-body">
                    <div class="reply-box form box-block">
                      <form method="POST" action="" accept-charset="UTF-8" id="thread_create_form" class="create_form">
                        <div class="form-group">
                          <input class="form-control" id="thread_title" placeholder="标题" name="title" type="text">
                        div>
                        
                        <div class="editor-toolbar">
                          <ul class="nav nav-pills" style="clear:none;">
                            <li class="edit active"><a href="#">帖子内容a>li>
                          ul>
                        div>
                        
                        <div class="form-group">
                            <textarea class="post-editor form-control" rows="15"
                                      style="overflow:hidden" id="body_field" placeholder="请撰写内容" name="content" cols="50">textarea>
                        div>
                        <div class="form-group status-post-submit">
                            <input class="btn btn-primary col-xs-2" id="" type="submit" value="发布">
                        div>

                        form>
                    div>
                div>
            div>

        div>

        <div class="col-md-3 side-bar">


            <div class="panel panel-default corner-radius help-box">
                <div class="panel-heading text-center">
                    <h3 class="panel-title">发帖提示h3>
                div>
                <div class="panel-body">
                    <ul class="list">
                        <li>主题标题
                            <p>请在标题中描述内容要点。p>
                        li>
                        <li>选择节点
                            <p>请为你的主题选择一个节点。恰当的归类会让你发布的信息更有用。p>
                        li>
                        <li>正文
                            <p>imooc 支持 <span style="font-family: Consolas, 'Panic Sans', mono"><a href="https://help.github.com/articles/github-flavored-markdown" target="_blank">GitHub Flavored Markdowna>span> 文本标记语法。你可以在页面下方实时预览正文的实际渲染效果。p>
                        li>
                    ul>
                div>
            div>

            <div class="panel panel-default corner-radius help-box">
                <div class="panel-heading text-center">
                    <h3 class="panel-title">社区指导原则h3>
                div>
                <div class="panel-body">
                    <ul class="list">
                        <li>尊重原创
                            <p>请不要在 imooc 发布任何盗版链接,包括软件、音乐、电影等。p>
                        li>
                        <li>友好互助
                            <p>保持对陌生人的友善。用知识去帮助别人。p>
                        li>
                    ul>
                div>
            div>

        div>
    div>


div>
php require(APP_PATH.'/index/view/common/footer.html');?>

你可能感兴趣的:(thinkphp论坛项目开发)