On this page
订单列表
数据表迁移文件
shell
npx sequelize migration:generate --name=order
js
"use strict";
module.exports = {
up: (queryInterface, Sequelize) => {
const { INTEGER, STRING, DATE, ENUM, TEXT } = Sequelize;
return queryInterface.createTable("order", {
id: {
type: INTEGER(20),
primaryKey: true,
autoIncrement: true,
},
no: {
type: STRING(100),
allowNull: false,
defaultValue: "",
comment: "订单号",
unique: true,
},
user_id: {
type: INTEGER,
allowNull: false,
defaultValue: 0,
comment: "用户id",
references: {
model: "user",
key: "id",
},
onDelete: "cascade",
onUpdate: "restrict", // 更新时操作
},
price: {
type: INTEGER,
allowNull: false,
defaultValue: 0,
comment: "价格",
},
status: {
type: ENUM,
values: ["pending", "success", "fail"],
allowNull: false,
defaultValue: "pending",
comment: "支付状态",
},
created_time: DATE,
updated_time: DATE,
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable("order");
},
};
模型:app/model/order.js
js
module.exports = (app) => {
const { STRING, INTEGER, DATE, ENUM, TEXT } = app.Sequelize;
const Order = app.model.define("order", {
id: {
type: INTEGER(20),
primaryKey: true,
autoIncrement: true,
},
no: {
type: STRING(100),
allowNull: false,
defaultValue: "",
comment: "订单号",
unique: true,
},
user_id: {
type: INTEGER,
allowNull: false,
defaultValue: 0,
comment: "用户id",
references: {
model: "user",
key: "id",
},
onDelete: "cascade",
onUpdate: "restrict", // 更新时操作
},
price: {
type: INTEGER,
allowNull: false,
defaultValue: 0,
comment: "价格",
},
status: {
type: ENUM,
values: ["pending", "success", "fail"],
allowNull: false,
defaultValue: "pending",
comment: "支付状态",
},
created_time: DATE,
updated_time: DATE,
});
// 关联关系
Order.associate = function (models) {
// 关联主播
Order.belongsTo(app.model.User);
};
return Order;
};
控制器:app/controller/admin/order.js
js
async index() {
const { ctx, app } = this;
let data = await ctx.page('Order',{},{
include:[{
model:app.model.User,
attributes:['id','username','avatar']
}]
})
data = JSON.parse(JSON.stringify(data))
data = data.map(item=>{
item.username = item.user.username
item.avatar = item.user.avatar
return item
})
await ctx.renderTemplate({
title: "用户管理",
tempType: "table",
table: {
// 表头
columns: [{
title: 'ID',
key: 'id',
width: 60,
fixed: 'center'
},{
title: '订单号',
key: 'no',
width: 200,
fixed: 'center'
},{
title: '用户',
fixed: 'left',
render(item) {
let avatar = item.avatar || '/public/assets/img/profiles/avatar-01.jpg'
return `
<h2 class="table-avatar">
<a class="avatar avatar-sm mr-2"><img class="avatar-img rounded-circle bg-light" src="${avatar}"></a>
<a>${item.username}</a>
</h2>`
},
}, {
title: '价格',
key: 'price',
width: 180,
fixed: 'center'
},{
title: '订单状态',
width: 180,
fixed: 'center',
render(item) {
const o = {
pending:{
text:"未支付",
color:"warning"
},
success:{
text:"支付成功",
color:"success"
},
fail:{
text:"支付失败",
color:"danger"
}
}
let v = o[item.status]
return `
<span class="badge badge-${v.color}">${v.text}</span>
`
},
},{
title: '创建时间',
key: 'created_time',
width: 180,
fixed: 'center'
}, {
title: "操作",
width: 200,
fixed: 'center',
action:{
delete:function(id){
return `/admin/order/delete/${id}`
},
}
}],
data
}
})
}
路由:app/router.js
js
router.get("/admin/order", controller.admin.order.index);