d_news.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. const mysql = require('./mysql');
  2. const {searchSql,limitSql} = require("../tools/searchSql");
  3. const {getUnixTimeStamp} = require("../tools/time_cjs");
  4. const log = require("../logger").logger("d_news","info");
  5. function addReadNum(id){
  6. let sql = `UPDATE hfy_news SET hits = hits + 1 WHERE id = ? limit 1`;
  7. let values = [id];
  8. return mysql.pq(sql,values);
  9. }
  10. function loadTypes() {
  11. let sql = `SELECT * FROM hfy_news_type`;
  12. return mysql.pq(sql, []);
  13. }
  14. function getTypeByKey(key){
  15. let sql = `SELECT * FROM hfy_news_type WHERE type_key = ? limit 1`;
  16. return mysql.pq(sql,[key]);
  17. }
  18. // 轻量搜索接口
  19. function searchAllNewsMini(type='array',searchParam,sort,page,limit){
  20. let sql;
  21. let values = [];
  22. if(type === 'count'){
  23. sql = `select count(*) as total `;
  24. }else{
  25. sql = `select
  26. news.id as id ,
  27. news.title as name,
  28. n_type.type_key
  29. `;
  30. }
  31. sql += `
  32. from
  33. hfy_news as news ,
  34. hfy_news_type as n_type
  35. `
  36. sql += ` where news.type_id = n_type.type_id`
  37. if(searchParam.key){
  38. sql += ` and news.title like '%${searchParam.key}%'`
  39. }
  40. if(searchParam.type){
  41. sql += ` and n_type.type_key = ?`
  42. values.push(searchParam.type)
  43. }
  44. return searchSql(mysql.pq,type,sql,values,limit,page);
  45. }
  46. function getNewsById(id){
  47. let sql = `SELECT * FROM hfy_news WHERE id = ? limit 1`;
  48. return mysql.pq(sql,[id]);
  49. }
  50. function addArticle(article, typeId){
  51. let sql = `INSERT INTO hfy_news (title, remark, author, content, type_id, image, date_time) VALUES (?, ?, ?, ?, ?, ?, ?)`;
  52. let values = [];
  53. values.push(article.title);
  54. values.push(article.remark);
  55. values.push(article.author);
  56. values.push(article.content);
  57. values.push(typeId);
  58. values.push(article.cover);
  59. values.push(getUnixTimeStamp());
  60. log.info(`[新增文章] sql=${sql}, values=${values}`);
  61. return mysql.pq(sql,values);
  62. }
  63. module.exports = {
  64. addReadNum,
  65. loadTypes,
  66. searchAllNewsMini,
  67. getNewsById,
  68. getTypeByKey,
  69. addArticle
  70. }