npmrun 2 years ago
parent
commit
1b9be4dd2d
  1. BIN
      data/data.db
  2. BIN
      public/upload/00018-4071989711_2023_05_17_1684324644633.png
  3. 5
      route.txt
  4. 86
      source/models/color.ts
  5. 11
      source/plugins/index.ts
  6. 36
      source/route/views/index.ts
  7. 88
      template/htmx/path/color.pug
  8. 2
      template/layout/layout.pug

BIN
data/data.db

Binary file not shown.

BIN
public/upload/00018-4071989711_2023_05_17_1684324644633.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB

5
route.txt

@ -1,9 +1,10 @@
D:\1XYX\pro\hapi-demo\source\route\htmx对应路径:
/home/topuser/Code/@project/hapi-demo/source/route/htmx对应路径:
不需权限(提供无需验证): GET /htmx/path/{path*}
D:\1XYX\pro\hapi-demo\source\route\views对应路径:
/home/topuser/Code/@project/hapi-demo/source/route/views对应路径:
不需权限(提供无需验证): GET /404
不需权限(提供无需验证): GET /
不需权限(提供无需验证): GET /about
不需权限(提供无需验证): POST /color
不需权限(提供无需验证): GET /color
需要权限 : GET /docs/{path*}
不需权限(提供无需验证): GET /{path*}

86
source/models/color.ts

@ -2,83 +2,51 @@ import { Sequelize, DataTypes, Optional, Model } from "sequelize"
interface ColorAttributes {
id: number
/**
*
*/
name: string
/**
*
*/
value: string
/**
*
*/
desctibe: string
/**
*
*/
example_link: string
createdAt?: Date
updatedAt?: Date
deletedAt?: Date
}
export interface ColorInput extends Optional<ColorAttributes, "id"> {}
export interface ColorOutput extends Required<ColorAttributes> {}
export interface ColorInput extends Optional<ColorAttributes, "id"> { }
export interface ColorOutput extends Required<ColorAttributes> { }
export type TColorModel = ReturnType<typeof ColorModel>
type DT = typeof DataTypes
export default function ColorModel(sequelize: Sequelize, DataTypes: DT) {
class Color extends Model<ColorAttributes, ColorInput> implements ColorAttributes {
public id!: number
public name!: string
public value!: string
public desctibe: string
public example_link: string
// timestamps!
public readonly createdAt!: Date
public readonly updatedAt!: Date
public readonly deletedAt!: Date
}
Color.init(
{
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
value: {
type: DataTypes.STRING,
allowNull: false,
},
desctibe: {
type: DataTypes.STRING,
},
example_link: {
type: DataTypes.STRING,
},
export default function ColorModel(sequelize: Sequelize) {
interface UserInstance
extends Model<ColorAttributes, ColorInput>,
ColorAttributes { }
const Color = sequelize.define<UserInstance>("color", {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
name: {
type: DataTypes.STRING,
},
value: {
type: DataTypes.STRING,
allowNull: false,
},
{
modelName: "color",
sequelize,
underscored: true,
deletedAt: true,
timestamps: true,
paranoid: true, // 对模型施加了一个软删除
desctibe: {
type: DataTypes.STRING,
},
)
}, {
underscored: true,
deletedAt: true,
paranoid: true,
timestamps: true,
})
// 覆盖Color的toJSON方法
Color.prototype.toJSON = function () {
const values = Object.assign({}, this.get()) as ColorAttributes
delete values.deletedAt
return values
}
Color.associate = function (models) {}
Color.associate = function (models) { }
return Color
}

11
source/plugins/index.ts

@ -108,6 +108,16 @@ export default [
// @ts-ignore
if (request.yar && request.yar.flash && request.response.variety === "view") {
var flash = request.yar.flash()
let temp = {}
if(flash["view"]){
temp = flash["view"].reduce((a, b)=>{
return {
...a,
...b
}
}, {})
}
console.log(temp)
request.yar.set("_flash", {})
// @ts-ignore
request.response.source.context = Hoek.applyToDefaults(
@ -115,6 +125,7 @@ export default [
flash: !!Object.keys(flash).length ? flash : false,
isLogin: isLogin,
user: isLogin ? request.auth.credentials : false,
temp
},
// @ts-ignore
request.response.source.context,

36
source/route/views/index.ts

@ -41,15 +41,35 @@ export default class Index {
}
@route("/color")
async color(request: Req, h: Res) {
const filePath = path.resolve(baseDir, "template/views", "color.pug")
if (fs.existsSync(filePath)) {
const isRenderHtmx = Reflect.has(request.query, "htmx")
if (isRenderHtmx) {
return h.view(`htmx/path/color.pug`)
}
return h.view(`views/color.pug`)
@method("POST")
async color_post(request: Req, h: Res) {
const { name, value, desctibe } = request.payload as any
if (!value) {
request.yar.flash("error", "请输入颜色值")
return h.redirect("/color")
}
try {
const ColorModel = request.getModel("color")
await ColorModel.create({ name, value, desctibe })
request.yar.flash("success", "成功")
} catch (error) {
loggerSite.error(error)
request.yar.flash("error", "插入颜色失败")
}
return h.redirect("/color")
}
@route("/color")
async color_get(request: Req, h: Res) {
const ColorModel = request.getModel("color")
const colorList = await ColorModel.findAll()
const isRenderHtmx = Reflect.has(request.query, "htmx")
if (isRenderHtmx) {
return h.view(`htmx/path/color.pug`, { list: colorList })
}
return h.view(`views/color.pug`, { list: colorList })
}
@route("/docs/{path*}")

88
template/htmx/path/color.pug

@ -8,46 +8,68 @@ title 颜色
div(class="container page")
style
:public style/views/color.css
h1 颜色表
h1 颜色表 #{temp.a}
div(class="color_list")
-
var list=[
{ color:"#00ffff",title: "青色",describe:""},
{ color:"#222222",title: "辅助色",describe:""},
{ color:"#8f8f8f",title: "辅助色",describe:""},
{ color:"#00d1b2",title: "辅助色",describe:""},
{ color:"#999999",title: "辅助色",describe:""},
{ color:"#999999",title: "辅助色",describe:""},
{ color:"#999999",title: "辅助色",describe:""},
{ color:"#999999",title: "辅助色",describe:""},
{ color:"#999999",title: "辅助色",describe:""},
{ color:"#999999",title: "辅助色",describe:""},
{ color:"#999999",title: "辅助色",describe:""},
{ color:"#999999",title: "辅助色",describe:""},
{ color:"#999999",title: "辅助色",describe:""},
{ color:"#999999",title: "辅助色",describe:""},
{ color:"#999999",title: "辅助色",describe:""},
{ color:"#999999",title: "辅助色",describe:""},
{ color:"#999999",title: "辅助色",describe:""},
{ color:"#999999",title: "辅助色",describe:""},
{ color:"#999999",title: "辅助色",describe:""},
{ color:"#999999",title: "辅助色",describe:""},
]
//- -
//- var list=[
//- { color:"#00ffff",title: "青色",describe:""},
//- { color:"#222222",title: "辅助色",describe:""},
//- { color:"#8f8f8f",title: "辅助色",describe:""},
//- { color:"#00d1b2",title: "辅助色",describe:""},
//- { color:"#999999",title: "辅助色",describe:""},
//- { color:"#999999",title: "辅助色",describe:""},
//- { color:"#999999",title: "辅助色",describe:""},
//- { color:"#999999",title: "辅助色",describe:""},
//- { color:"#999999",title: "辅助色",describe:""},
//- { color:"#999999",title: "辅助色",describe:""},
//- { color:"#999999",title: "辅助色",describe:""},
//- { color:"#999999",title: "辅助色",describe:""},
//- { color:"#999999",title: "辅助色",describe:""},
//- { color:"#999999",title: "辅助色",describe:""},
//- { color:"#999999",title: "辅助色",describe:""},
//- { color:"#999999",title: "辅助色",describe:""},
//- { color:"#999999",title: "辅助色",describe:""},
//- { color:"#999999",title: "辅助色",describe:""},
//- { color:"#999999",title: "辅助色",describe:""},
//- { color:"#999999",title: "辅助色",describe:""},
//- ]
if list.length === 0
div 空
each item in list
div(class="color_item")
div(class="color_item_bg" style=`background: ${item.color}`)
div(class="color_item_bg" style=`background: ${item.value}`)
div(class="color_toggle_list")
div(class="color_toggle_item") hex
div(class="color_toggle_item") rgba
div(class="color_item_name") #{item.color}
div(class="color_item_content")
div(class="color_item_title") #{item.title}
div(class="color_item_desc") #{item.describe || "暂无描述"}
div(class="color_item_title") #{item.name}
div(class="color_item_desc" title=item.desctibe) #{item.desctibe || "暂无描述"}
if isLogin
div 删除
//- div(class="color_item")
//- img(src="/public/image/add.png", alt="添加" title="添加" class="color_add")
if isLogin
div
form(action="POST" method="post")
div(class=".wrapper_input"): input(type="text" tabindex="1" value="sadsa" name="a")
div(class=".wrapper_input"): input(type="text" tabindex="3" value="sadsa" name="b")
button(type="submit" tabindex="2") 提交
if isLogin
form(action="/color" method="post" style="margin: 0 auto; width: 500px;margin-top: 50px")
h1 提交新颜色
.field
.label 名称
.control
input.input(type="text" name="name" placeholder="请输入名称")
.field
.label 颜色值
.control
input.input(type="text" name="value" placeholder="请输入颜色值")
.field
.label 描述
.control
textarea.textarea(placeholder="请输入描述" name="desctibe")
+security
.field.is-grouped
.control
button.button.is-link(type="submit") 提交
//- div
//- form(action="POST" method="post")
//- div(class=".wrapper_input"): input(type="text" tabindex="1" value="sadsa" name="a")
//- div(class=".wrapper_input"): input(type="text" tabindex="3" value="sadsa" name="b")
//- button(type="submit" tabindex="2") 提交

2
template/layout/layout.pug

@ -1,6 +1,6 @@
doctype html
include @/helper/helper.pug
include @/helper/flush.pug
//- include @/helper/flush.pug
block var
html(lang="zh-cn" class=hideHeader?"":"has-navbar-fixed-top")

Loading…
Cancel
Save