Skip to content

Commit

Permalink
🎨修改为前后端分离
Browse files Browse the repository at this point in the history
  • Loading branch information
mewhz committed Jan 2, 2022
1 parent fc78cc2 commit e1078cb
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class CodeController {

@RequestMapping("/code")
public ModelAndView code(@RequestParam String id){
ModelAndView mav = new ModelAndView("/code.html?id=" + id);
ModelAndView mav = new ModelAndView("/code.html");
return mav;
}

Expand Down
22 changes: 0 additions & 22 deletions src/main/java/com/mewhz/paste/controller/Heap.java

This file was deleted.

42 changes: 37 additions & 5 deletions src/main/java/com/mewhz/paste/controller/ListController.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package com.mewhz.paste.controller;

import cn.hutool.db.Entity;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.mewhz.paste.utils.CodeSQL;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
* @author mewhz
*/
@Controller

@RestController
public class ListController {

@ResponseBody
@RequestMapping(value = "/list")
public String list(@RequestParam(value = "id", required = false) String id ){

Expand Down Expand Up @@ -82,4 +82,36 @@ public String list(@RequestParam(value = "id", required = false) String id ){
return html.toString();
}


@RequestMapping("/lists")
public ModelAndView lists(@RequestParam(value = "id", required = false) String id ){

ModelAndView mav = new ModelAndView("/list.html");

return mav;

}

@RequestMapping("/selectList")
@CrossOrigin("http://localhost:63343")
public JSONObject selectList(@RequestParam(value = "id", required = false) String id){
JSONObject json = JSONUtil.createObj();
CodeSQL codeSQL = new CodeSQL();

List<Entity> list = codeSQL.findIdentifyingCode(id);
if (list.size() == 0){
json.append("info", "zero");
return json;
}
for (Entity e : list){
json.append("date", e.get("date"));
json.append("time_id", e.get("time_id"));
json.append("remark", e.get("remark"));
json.append("type", e.get("type"));
json.append("text", e.get("text"));
}
System.out.println(json);
return json;
}

}
8 changes: 4 additions & 4 deletions src/main/resources/static/code.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
</head>
<body>
<pre>
<code id="code">
<code id="code" class="code">
</code>
</pre>
<button id="button" class="btn btn-success" onclick="copy()" data-clipboard-target="#code" data-clipboard-action="copy">一键复制</button>
<button class="btn btn-success button" data-clipboard-target="#code" data-clipboard-action="copy">一键复制</button>
<script src="js/prism.js"></script>
<script src="js/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/clipboard.min.js"></script>
<script src="js/code.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/toastr.js/latest/js/toastr.min.js"></script><script src="js/prism-java.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
<script src="js/code.js" type="module"></script>
</body>
</html>
32 changes: 9 additions & 23 deletions src/main/resources/static/js/code.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,14 @@
function copy(){
let clipboard = new ClipboardJS('#button');
clipboard.on('success', function(e) {
e.clearSelection();
clipboard.destroy();
// 释放内存,如果不释放,下次总会多复制
toastr.success("复制成功,快去粘贴吧!");
});
import {copy, getQueryVariable} from './utils.js'

clipboard.on('error', function(e) {
toastr.error("一键复制失败,请手动复制!")
});
}
$(document).ready(()=>{

function getQueryVariable(variable){
$(".button").click((e)=>{

let query = window.location.search.substring(1);
let vars = query.split("&");
for (let i = 0; i < vars.length; i ++){
let pair = vars[i].split("=");
if (pair[0] === variable) return pair[1];
}
return false;
}
let id = $(e.target).attr("id");
// 单击事件复制
copy(id);
});

$(document).ready(()=>{
let id = getQueryVariable("id");
const codeId = $("#code");
const title = $("title");
Expand All @@ -37,7 +22,8 @@ $(document).ready(()=>{
if (result.remark === "") {
title.text("贴代码");
}
switch (result.type){
console.log(result.type[0]);
switch (result.type[0]){
case "java":
body.append('<script src="js/prism-java.min.js"></script>');
break;
Expand Down
40 changes: 40 additions & 0 deletions src/main/resources/static/js/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {copy, getQueryVariable} from './utils.js'

$(document).ready(()=>{
let all = $("#all");
let body = $("body");
let set = new Set();
let id = getQueryVariable("id");

$.ajax({
url: "/selectList?id=" + id,
success: (result) => {
console.log(result.type);
let len = result.type.length;
let type = result.type;
let date = result.date;
let time_id = result.time_id;
let text = result.text;
let remark = result.remark;
for (let i = 0; i < len; i ++){
all.append("<details><summary>"+ date[i] +" " + time_id[i] + " " + remark[i] + "</summary>" +
"<pre><code class='language-"+ type[i] +"'>"+ text[i] +"</code></pre></details>");
if (!set.has(type[i])){
switch (type[i]){
case "java":
body.append('<script src="js/prism-java.min.js"></script>');
break;
case "cpp":
body.append('<script src="js/prism-c.min.js"></script>');
body.append('<script src="js/prism-cpp.min.js"></script>');
break;
case "python":
body.append('<script src="js/prism-python.min.js"></script>');
}
set.add(type[i]);
}
}
Prism.highlightAll();
}
})
});
22 changes: 22 additions & 0 deletions src/main/resources/static/js/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export function copy(id){
let clipboard = new ClipboardJS('.button');
clipboard.on('success', (e)=>{
e.clearSelection();
// 释放内存,否则会多复制
clipboard.destroy();
toastr.success("复制成功,快去站贴吧!");
});

clipboard.on('error', (e)=>{
toastr.error("一键复制失败,请手动复制!");
})
}
export function getQueryVariable(variable){
let query = window.location.search.substring(1);
let vars = query.split("&");
for (let i = 0; i < vars.length; i ++){
let pair = vars[i].split("=");
if (pair[0] === variable) return pair[1];
}
return false;
}
37 changes: 5 additions & 32 deletions src/main/resources/static/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,11 @@
<link href="css/prism.css" rel="stylesheet">
</head>
<body>
<details>
<summary>NiHao</summary>
<pre>
<code class="language-cpp">
<div id="all">

using namespace std;
const int N = 205;
int score[5][5] = {{0,0,1,1,0},{1,0,0,1,0},{0,1,0,0,1},{0,0,1,0,1},{1,1,0,0,0}};
int a,b,n;
int na[N],nb[N];
int main(){
scanf("%d%d%d", &n,&a,&b);
int A = 0, B = 0;
for (int i = 0; i < a; i++) scanf("%d", &na[i]);
for (int i = 0; i < b; i++) scanf("%d", &nb[i]);
for (int i = 0; i < n; i++){
A += score[na[i % a]][nb[i % b]];
B += score[nb[i % b]][na[i % a]];
}
cout << A << " " << B;
return 0;
}
</code>
</pre>
</details>
<details>
<summary>One</summary>
<p>World</p>
</details>
<script src="js/prism.js"></script>
<script src="js/prism-c.min.js"></script>
<script src="js/prism-cpp.min.js"></script>
<script src="js/prism-java.min.js"></script>
</div>
<script src="js/prism.js"></script>
<script src="js/jquery-3.6.0.min.js"></script>
<script src="js/list.js" type="module"></script>
</body>
</html>

0 comments on commit e1078cb

Please sign in to comment.