使用nuxt,express,mysql,nginx创建个人博客系列-12

259
2020-01-24 18:37
10 个月前

保存文章标签

首先我们需要一个进到文章标签页面,然后通过提交按钮,修改或新增文章标签,判断是否是新增只需要传入id=0即可,下面我们首先添加路由


路由目录:server/admin/routers/tags.js

//文章类型查看路由,根据id去渲染tags_edit.html
router.get('/:id',async (req,res,next)=>{
  let id = req.params['id'];
  let name =""
  if(id){
    let myConnect = await mysql.getConnect();
    let sql = `select * from tags where id = ${id}`;
    let result = await mysql.query(myConnect,sql);
    await mysql.release(myConnect);
    if(result.length){
      name = result[0]['name']
    }
  }
  return res.render('tags_edit', {
    module_dir: config.admin.route,
    id,
    name
  })
})
//保存文章标签
router.post('/edit',async (req,res,next)=>{
  let {name,id} = req.body;
  let userid =req.session.user.id
  if(userid){
    let myConnect = await mysql.getConnect();
    let sql='';
    let params = null;
    if(id==0){
      sql = `insert into tags(user_id,name) values (?)`;
      params = [userid,name];
    }else{
      sql = `UPDATE tags SET name = '${name}' WHERE id = ${id}`;
    }
    let result = await mysql.query(myConnect,sql,params);
    console.log(result)
    await mysql.release(myConnect);
    if(result.affectedRows===1){
      return res.send(util.resSuccess({
        data:result
      }))
    }
    return res.send(util.resError({
      code:'user_id',
      msg:"插入错误"
    }))
  }else{
    return res.send(util.resError({
      code:'user_id',
      msg:"请登录"
    }))
  }

})

下面我们添加tags_edit.html 目录:server/admin/views/tags_edit.html

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <% include header.ejs%>
    <title>tags</title>
</head>

<body>
  <% include nav.ejs%>
    <div class="container mt-3">
      <div class="container-fluid">
        <div class="row">
          <div class="col-12">
            <a class="btn btn-primary btn-outline-dark btn-block" href="<%= module_dir%>/tags/0" role="button">创建标签</a>
          </div>
        </div>
        <div class="row">
            <%if(list.length){%>
              <div class="col-12 bd-content mt-3">
                  <div class="bd-example">
                    <table class="table table-dark">
                      <thead>
                        <tr>
                          <th scope="col">id</th>
                          <th scope="col">name</th>
                          <th scope="col">修改时间</th>
                        </tr>
                      </thead>
                      <tbody>
                        <%for(i in list){%>
                          <tr>
                              <th scope="row"><a href="<%= module_dir%>/tags/<%= list[i]['id']%>"><%= list[i]['id']%></a></th>
                              <td><%= list[i]['name']%></td>
                              <td><%= list[i]['updated_at']%></td>
                          </tr>
                        <%}%>
                      </tbody>
                    </table>
                  </div>
                </div>
            <%}%>
        </div>
      </div>
    </div>
</body>
<script>
</script>

</html>

你会看到这样的页面
categorys_edit.html


上一篇-使用nuxt,express,mysql,nginx创建个人博客系列-11
下一篇-使用nuxt,express,mysql,nginx创建个人博客系列-13