Skip to content
关注公众号,获取新课通知

修改礼物


控制器:app/controller/admin/gift.js

js
	async edit(){
        const { ctx, app } = this;
        const id = ctx.params.id
        let data = await app.model.Gift.findOne({
            where:{
                id
            }
        })
        if(!data){
            return await ctx.pageFail('该记录不存在')
        }

        data = JSON.parse(JSON.stringify(data))

        await ctx.renderTemplate({
            id:ctx.params.id,
            title: "修改礼物",
            tempType: "form",
            form: {
                // 提交地址
                action: "/admin/gift/" + ctx.params.id,
                fields:[{
					label: "礼物名称",
					type: "text",
					name: "name",
					placeholder: "礼物名称",
				}, {
					label: "礼物图标",
					type: "file",
					name: "image",
				},
				{
					label: "金币",
					type: "number",
					name: "coin",
					default: 0
				}],
                data
            },
            // 新增成功跳转路径
            successUrl:"/admin/gift"
        })
    }

    async update(){
        const { ctx, app } = this;
        ctx.validate({
            id:{
                type:"int",
                required:true
            },
            name:{
                type:"string",
                required:true
            },
            image:{
                type:"string",
            },
            coin:{
                type:"int",
            }
        })
        const id = ctx.params.id
        const { name,image,coin } = ctx.request.body
        // 当前管理员是否存在
        let gift = await app.model.Gift.findOne({
            where:{ id }
        })
        if(!gift){
            return ctx.apiFail('该记录不存在')
        }

        gift.name = name
        gift.image = image
        gift.coin = coin

        ctx.apiSuccess(await gift.save())
    }

路由:app/router.js

js
router.get("/admin/gift/edit/:id", controller.admin.gift.edit);
router.post("/admin/gift/:id", controller.admin.gift.update);