111.【金橘社区1.0】(四)

简介: 111.【金橘社区1.0】
(2).后端页面
package com.jsxs.kumquat.controller;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jsxs.kumquat.pojo.CkqnBlog;
import com.jsxs.kumquat.pojo.CkqnUser;
import com.jsxs.kumquat.service.CkqnBlogService;
import com.jsxs.kumquat.utils.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.UUID;
/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 吉士先生
 * @since 2023-04-09
 */
@Controller
@RequestMapping("/article")
public class CkqnBlogController {
    @Resource
    CkqnBlogService ckqnBlogService;
    @Resource
    HttpSession session;
    // 文章上传
    @RequestMapping("/publish")
    public String publishArticle(CkqnBlog article) {
        CkqnUser user = (CkqnUser) session.getAttribute("loginUser");
        // 1.作者信息
        article.setAuthorId(user.getUid());
        article.setAuthorName(user.getUsername());
        article.setAuthorAvatar(user.getAvatar());
        // 2.博客ID
        String replace = UUID.randomUUID().toString().replace("-", "");
        article.setBid(replace);
        if (article.getTitle() == null) {
            System.out.println("标题为空?????????");
        }
        boolean res = ckqnBlogService.save(article);
        if (res) {
            return "tipSuccess";
        }
        return "tipFalse";
    }
    // 文件上传
    @RequestMapping("/image/upload")
    @ResponseBody
    public JSONObject imageUpload(@RequestParam("editormd-image-file") MultipartFile image) {
        JSONObject jsonObject = new JSONObject();
        if (image != null) {
            String path = FileUtils.uploadFile(image);
//            回调给 editor
            System.out.println(path);
            jsonObject.put("url", path);
            jsonObject.put("success", 1);
            jsonObject.put("message", "upload success!");
            return jsonObject;
        }
        jsonObject.put("success", 0);
        jsonObject.put("message", "upload error!");
        return jsonObject;
    }
    //  1.一篇详细文章
    @RequestMapping("/get/{id}")
    public ModelAndView getArticleById(@PathVariable(name = "id") int id) {
        ModelAndView modelAndView = new ModelAndView();
        CkqnBlog article = ckqnBlogService.getById(id);
        modelAndView.setViewName("WriteCenter/article");
        if (article == null) {
            modelAndView.addObject("article", new CkqnBlog());
        }
        modelAndView.addObject("article", article);
        return modelAndView;
    }
    // 开始修改
    @RequestMapping("/update/editor/{authorId}/{bid}")
    public String updateArticleByUser(@PathVariable("authorId") String authorId, @PathVariable("bid") String bid, Model model) {
//        CkqnUser user = (CkqnUser)session.getAttribute("loginUser");
        QueryWrapper<CkqnBlog> wrapper = new QueryWrapper<>();
        //  查询uid
        CkqnBlog blog = ckqnBlogService.getOne(wrapper.eq("bid", bid));
        System.out.println(authorId + " " + bid);
        // 2.查询作者
        if (!blog.getAuthorId().equals(authorId)) {
            System.out.println("对不起,你在非法编辑");
            //  直接注销账号
            return "redirect:/index.html";
        }
        // 3. 存放文章
        model.addAttribute("blog", blog);
        //  4.没有重定向
        return "WriteCenter/update";
    }
}

4.Aax 密码验证

需要引入js- 一个都不能变,注意路径

<link rel="stylesheet" th:href="@{/editor.md/css/examples/style.css}" />
    <link rel="stylesheet" th:href="@{/editor.md/css/editormd.css}" />
    <link rel="shortcut icon" href="https://pandao.github.io/editor.md/favicon.ico" type="image/x-icon" />
    <script th:src="@{/Jquery/jquery-3.6.1.js}"></script>
<script th:src="@{/editor.md/examples/js/jquery.min.js}"></script>
<script th:src="@{/editor.md/lib/marked.min.js}"></script>
<script th:src="@{/editor.md/lib/prettify.min.js}"></script>
<script th:src="@{/editor.md/lib/raphael.min.js}"></script>
<script th:src="@{/editor.md/lib/underscore.min.js}"></script>
<script th:src="@{/editor.md/lib/sequence-diagram.min.js}"></script>
<script th:src="@{/editor.md/lib/flowchart.min.js}"></script>
<script th:src="@{/editor.md/lib/jquery.flowchart.min.js}"></script>
<script th:src="@{/editor.md/editormd.js}"></script>

文章主题部分

<div id="test-editormd">
                <textarea style="display:none;" th:text="${article.content}"></textarea>
    </div>

JS

<script type="text/javascript">
    var testEditor;
    $(function () {
        testEditor = editormd.markdownToHTML("test-editormd", {
            width: "90%",
            height: 700,
            path: "/editor.md/lib/",
            preview: true,
            watch: true,
            editor: false,
        })
    })
    ! function (e, t, a) {
        function r() {
            for (var e = 0; e < s.length; e++) s[e].alpha <= 0 ? (t.body.removeChild(s[e].el), s.splice(e, 1)) : (s[
                e].y--, s[e].scale += .004, s[e].alpha -= .013, s[e].el.style.cssText = "left:" + s[e].x +
                "px;top:" + s[e].y + "px;opacity:" + s[e].alpha + ";transform:scale(" + s[e].scale + "," + s[e]
                    .scale + ") rotate(45deg);background:" + s[e].color + ";z-index:99999");
            requestAnimationFrame(r)
        }
        function n() {
            var t = "function" == typeof e.onclick && e.onclick;
            e.onclick = function (e) {
                t && t(), o(e)
            }
        }
        function o(e) {
            var a = t.createElement("div");
            a.className = "heart", s.push({
                el: a,
                x: e.clientX - 5,
                y: e.clientY - 5,
                scale: 1,
                alpha: 1,
                color: c()
            }), t.body.appendChild(a)
        }
        function i(e) {
            var a = t.createElement("style");
            a.type = "text/css";
            try {
                a.appendChild(t.createTextNode(e))
            } catch (t) {
                a.styleSheet.cssText = e
            }
            t.getElementsByTagName("head")[0].appendChild(a)
        }
        function c() {
            return "rgb(" + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + "," + ~~(255 * Math
                .random()) + ")"
        }
        var s = [];
        e.requestAnimationFrame = e.requestAnimationFrame || e.webkitRequestAnimationFrame || e
            .mozRequestAnimationFrame || e.oRequestAnimationFrame || e.msRequestAnimationFrame || function (e) {
            setTimeout(e, 1e3 / 60)
        }, i(
            ".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"
        ), n(), r()
    }(window, document);
    </script>

全体文章

package com.jsxs.kumquat.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * @Author Jsxs
 * @Date 2023/4/10 17:07
 * @PackageName:com.jsxs.kumquat.controller
 * @ClassName: AjaxController
 * @Description: TODO
 * @Version 1.0
 */
@RestController
public class AjaxController {
    @RequestMapping("/a3")
    public String a3(String name,String pwd){
        String msg="";
        System.out.println(name);
        System.out.println(pwd);
        if (name!=null){
            if ("吉士先生".equals(name)){
                msg="OK";
            }else {
                msg="Error";
            }
        }
        if (pwd!=null){
            if ("121788".equals(pwd)){
                msg="OK";
            }else {
                msg="Error";
            }
        }
        return msg;
    }
}

以上可以实现异步登入判断

(三)、知识点新发现

1. template包中创建包静态资源路径一样.

2. SpringBoot自定义400、500错误。只需要在template包下创建error包即可。

3. 加入我们要使用mybatis-plus那么条件构造其实际上就是对SQL的拼接操作。构造器的泛型是: 实体类

//1. 通过账户UserUid进行查找用户
    @RequestMapping("/code")
    public String SelectByUserUid(){
        // 1.定义条件构造器-> 这里的泛型是实体类
        QueryWrapper<CkqnUser> wrapper = new QueryWrapper<>();
        wrapper.eq("uid","root");
        CkqnUser one = ckqnUserService.getOne(wrapper.eq("uid", "root"));
        return ""+one;
    }

4.我们在验证登入操作的时候,只需要根据username进行判断即可,因为这样可以减少对数据库的一次访问

//1. 通过账户UserUid进行查找用户 -> 可以减少对数据库的访问频次(可以减少对密码的查询)
    @PostMapping("/code")
    public String SelectByUserUid() throws Exception {
        // 1.定义条件构造器-> 这里的泛型是实体类
        QueryWrapper<CkqnUser> wrapper = new QueryWrapper<>();
        // 2.添加条件: 是否相等
        wrapper.eq("uid","root");
        // 3.执行查找数据的操作
        CkqnUser ckqnUser = ckqnUserService.getOne(wrapper.eq("uid", "root"));
        // 4.optional检测是否为空
        Optional<CkqnUser> optional = Optional.ofNullable(ckqnUser);
        // 5.假如为空直接抛出异常
        optional.orElseThrow(()->new Exception("非常抱歉出现了空指针异常!!"));
        // 6.查看密码
        String password = ckqnUser.getPassword();
        CkqnUser ckqnUser1 = ckqnUserService.getOne(wrapper.eq("password", password));
        if (ckqnUser1.equals(ckqnUser)){
            return "index";
        }else {
            return "login";
        }

(四)、金橘社区前端设计

金橘社区

1. 渐变动态背景CSS .

<style>
        body {
    margin: 0;
    padding: 0;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background: linear-gradient(135deg, #34ac40, #2c8ae8, #dc47cd, #e7e373);
    background-size: 500%;
    animation: bgAnimation 6s linear infinite;
}
h1 {
    color: white;
    letter-spacing: 5px;
}
@keyframes bgAnimation {
    0% {
        background-position: 0% 50%;
    }
    50% {
        background-position: 100% 50%;
    }
    100% {
        background-position: 0% 50%;
    }
}
    </style>

2. sleep() 休眠的工具类

function sleep(numberMillis) {
        var now = new Date();
        var exitTime = now.getTime() + numberMillis;
        while (true) {
            now = new Date();
            if (now.getTime() > exitTime)
                return true;
        }
    }

3. 利用JS页面的跳转,我们一定要用异步跳转

// 这里一定要异步对其进行跳转页面
                    setTimeout(()=>{
                        document.location.href="two.html"
                    },600)


相关文章
|
8月前
|
前端开发 JavaScript
111.【金橘社区1.0】(二)
111.【金橘社区1.0】
27 0
|
8月前
|
前端开发
111.【金橘社区1.0】(六)
111.【金橘社区1.0】
34 0
|
8月前
|
前端开发
111.【金橘社区1.0】(五)
111.【金橘社区1.0】
44 0
|
8月前
|
前端开发 JavaScript Java
111.【金橘社区1.0】(一)
111.【金橘社区1.0】
87 0
111.【金橘社区1.0】(一)
|
5月前
|
SQL 前端开发 NoSQL
IT社区|网络社区平台|基于SpringBoot的IT社区平台
IT社区|网络社区平台|基于SpringBoot的IT社区平台
|
8月前
|
前端开发
111.【金橘社区1.0】(三)
111.【金橘社区1.0】
21 0
|
10月前
|
机器学习/深度学习 算法 安全
隐语社区【提问解答】 第1期
隐语社区【提问解答】 第1期
275 1
|
12月前
|
数据安全/隐私保护 Docker 容器
JetLinks 社区版安装
JetLinks 社区版安装
444 0
|
开发者
我和社区的2022
简述我和社区一路走来的点点滴滴
我和社区的2022
|
云计算 开发者
阿里云社区
学习阿里云