1.edit.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
  9. <style>
  10. img{
  11. width: 30px;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <form action="/doEdit" method="post" role="form" enctype="multipart/form-data">
  17. <legend>添加成员</legend>
  18. <input type="hidden" value="{{@data._id}}" name="id">
  19. <div class="form-group">
  20. <label for="">服装名称</label>
  21. <input type="text" name="shopname" value="{{data.shopname}}" class="form-control" id="" placeholder="请输入服装名称">
  22. </div>
  23. <div class="form-group">
  24. <label for="">服装价格</label>
  25. <input type="text" name="price" value="{{data.price}}" class="form-control" id="" placeholder="请输入服装价格">
  26. </div>
  27. <div class="form-group">
  28. <label for="">服装销量</label>
  29. <input type="text" name="shopNumber" value="{{data.shopNumber}}" class="form-control" id="" placeholder="请输入服装销量">
  30. </div>
  31. <div class="form-group">
  32. <label for="">上传图片</label>
  33. <img src="{{data.avatar}}" alt="">
  34. <input type="file" name="file" >
  35. </div>
  36. <button type="submit" class="btn btn-primary">提交</button>
  37. </form>
  38. </body>
  39. </html>

2.edit.js

  1. const Router = require("koa-router");
  2. const router = new Router();
  3. const ShopMangeModel = require("../models/shopMange");
  4. // 查询数据库
  5. router.get("/edit",async ctx=>{
  6. var {id} = ctx.query;
  7. var data = await ShopMangeModel.findOne({_id:id});
  8. console.log(data);
  9. await ctx.render("edit",{data:data})
  10. })
  11. module.exports = router;

3.doEdit.js

  1. const router = require("koa-router")();
  2. const path = require("path");
  3. const ShopMangeModel = require("../models/shopMange");
  4. // 查询数据库
  5. const fileUpload = require("../utils/upload");
  6. router.post("/doEdit", async ctx => {
  7. console.log(ctx.request.files);
  8. var file = ctx.request.files.file;
  9. var uploadName = path.basename(file.path);
  10. var isUpload = Boolean(file.name.trim());
  11. var {id,shopname,price,shopNumber} = ctx.request.body;
  12. var data = ({
  13. shopname,
  14. price,
  15. shopNumber
  16. })
  17. /* 将本地图片放到服务器的static文件夹中 */
  18. if (isUpload) {
  19. fileUpload(file.path, uploadName);
  20. var avatar = ctx.origin + "/" + uploadName;
  21. data.avatar = avatar;
  22. }
  23. await ShopMangeModel.updateOne({ _id: id},data)
  24. ctx.redirect("/")
  25. })
  26. module.exports = router;