diff --git a/data/data.db b/data/data.db index 5bf9e51..3653b20 100644 Binary files a/data/data.db and b/data/data.db differ diff --git a/public/upload/00018-4071989711_2023_05_17_1684324644633.png b/public/upload/00018-4071989711_2023_05_17_1684324644633.png new file mode 100644 index 0000000..ccd87b8 Binary files /dev/null and b/public/upload/00018-4071989711_2023_05_17_1684324644633.png differ diff --git a/route.txt b/route.txt index 017f45b..2db2a91 100644 --- a/route.txt +++ b/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*} diff --git a/source/models/color.ts b/source/models/color.ts index f55a051..b7a23f7 100644 --- a/source/models/color.ts +++ b/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 {} -export interface ColorOutput extends Required {} +export interface ColorInput extends Optional { } +export interface ColorOutput extends Required { } export type TColorModel = ReturnType -type DT = typeof DataTypes -export default function ColorModel(sequelize: Sequelize, DataTypes: DT) { - class Color extends Model 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 { } + const Color = sequelize.define("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 } diff --git a/source/plugins/index.ts b/source/plugins/index.ts index 8e025af..8e3bbd1 100644 --- a/source/plugins/index.ts +++ b/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, diff --git a/source/route/views/index.ts b/source/route/views/index.ts index c1ec9e7..1484d90 100644 --- a/source/route/views/index.ts +++ b/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*}") diff --git a/template/htmx/path/color.pug b/template/htmx/path/color.pug index f350f84..6bd727e 100644 --- a/template/htmx/path/color.pug +++ b/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") 提交 \ No newline at end of file + 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") 提交 \ No newline at end of file diff --git a/template/layout/layout.pug b/template/layout/layout.pug index 633546e..56dc3e7 100644 --- a/template/layout/layout.pug +++ b/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")