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

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

文章新增和修改

添加文章详细内容查看和保存路由。
目录:server/admin/routers/articles.js

router.get('/:id',async (req,res,next)=>{
  let id = req.params['id'];
  let myConnect = null;
  let defaultForm = {
    title:"",
    user_id:"",
    category_id:0,
    tag_id:0,
    content:"",
    read_num:0,
    content_html:"",
    introduction:"",
    articles_img:"",
    keyword:""
  }
  if(id){
    myConnect = await mysql.getConnect();
    let sql = `select title,user_id,category_id,tag_id,keyword,content,read_num,introduction,articles_img from articles where id = ${myConnect.escape(id)}`;
    let result = await mysql.query(myConnect,sql);
    if(result.length){
      result[0]['content_html'] = marked(result[0]['content']) // 转成markdown输出html
      defaultForm = Object.assign({},defaultForm,result[0])
    }
  }
  let tagssql = `select id,name from tags`;
  let categorySql = `select id,name from category`;
  let tags = await mysql.query(myConnect,tagssql);
  let categorys = await mysql.query(myConnect,categorySql);
  await mysql.release(myConnect);
  return res.render('articles_edit', {
    module_dir: config.admin.route,
    id,
    defaultForm,
    tags,
    categorys
  })
})
router.post('/edit',async (req,res,next)=>{
  let {pushForm,id} = req.body;
  let userid = config.user_id || req.session.user.id
  if(userid){
    let myConnect = await mysql.getConnect();
    let sql='';
    let params = null;
    let result = null;
    if(id==0){
      sql = `insert into articles(user_id,title,tag_id,category_id,content,introduction,articles_img,keyword) values (?)`;
      params = [userid,pushForm.title,pushForm.tag,pushForm.category,pushForm.content,pushForm.introduction,pushForm.articles_img,pushForm.keyword];
      result = await mysql.query(myConnect,sql,params);
    }else{
      sql = `UPDATE articles SET title = ?, tag_id = ? ,category_id = ? ,content = ?,introduction = ?, articles_img = ? , keyword = ? WHERE id = ${id}`;
      result = await mysql.query(myConnect,sql,pushForm.title,pushForm.tag,pushForm.category,pushForm.content,pushForm.introduction,pushForm.articles_img,pushForm.keyword);
    }
   //更新分类的个数,用于前台展示
    if(pushForm.category != pushForm.end_category_id){
      //不相等才进来
        //新记录+1
        await mysql.query(myConnect,`UPDATE category SET articles_num = articles_num + 1 WHERE id=${pushForm.category}`)
        //原记录-1
        await mysql.query(myConnect,`UPDATE category SET articles_num = articles_num - 1 WHERE id=${pushForm.end_category_id}`)
    }
   //更新标签的个数,用于前台展示
    if(pushForm.tag != pushForm.end_tag_id){
        //新记录+1
        await mysql.query(myConnect,`UPDATE tags SET articles_num = articles_num + 1 WHERE id=${pushForm.tag}`)
        //原记录-1
        await mysql.query(myConnect,`UPDATE tags SET articles_num = articles_num - 1 WHERE id=${pushForm.end_tag_id}`)
    }
    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:"请登录"
    }))
  }
})

由于篇幅有限,html代码下个章节说明


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