post.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. class PostService extends Service {
  4. async add(uid, data)
  5. {
  6. let mysql = this.app.mysql;
  7. data.user_id = uid;
  8. let ret = await mysql.insert("posts", data);
  9. return ret;
  10. }
  11. async list(query) {
  12. let mysql = this.app.mysql;
  13. let limit = ` limit ${query.page*query.size},${query.size}`;
  14. let where = "";
  15. let orderBy = "";
  16. if( query.cmplyIds ) { //保险公司
  17. }
  18. if( query.insDur ) { //保险期间
  19. }
  20. if( query.insCategory) //保险分类
  21. {
  22. }
  23. if( query.orderBy == 0 ) { //智能排序
  24. } else if( query.orderBy == 1 ) { //保额高到低
  25. } else if( query.orderBy == 2 ) { //价格高到低
  26. } else if( query.orderBy == 3 ) { //价格低到高
  27. }
  28. let ret = await mysql.query(`select id, title, subtitle, tags, coverage, price from posts ${where} ${orderBy} ${limit}`);
  29. return {page:query.page,size:query.size, list:ret};
  30. }
  31. async delete(uid, id) {
  32. let mysql = this.app.mysql;
  33. let ret = await mysql.query("delete from posts where id=? and user_id=?",[id, uid]);
  34. return ret.affectedRows == 1;
  35. }
  36. async update( data ) {
  37. let mysql = this.app.mysql;
  38. let ret = await mysql.update("posts", data, {where: {id: data.id}});
  39. return ret.affectedRows == 1;
  40. }
  41. async postDetail(uid, id ) {
  42. let mysql = this.app.mysql;
  43. console.log( id );
  44. let ret = await mysql.query("select id,coverage, price, tags, title, html, agree_num, like_num, comment_num from posts where id =?",[id]);
  45. ret = ret[0];
  46. //获取返对,收藏,喜欢状态
  47. let unagree = await mysql.query("select id from post_unagree where post_id=? and user_id=?",[id, uid]);
  48. unagree = unagree[0];
  49. let collected = await mysql.query("select id from post_collect where post_id=? and user_id=?",[id, uid]);
  50. collected = collected[0];
  51. let liked = await mysql.query("select id from post_like where post_id=? and user_id=?",[id, uid]);
  52. liked = liked[0];
  53. ret.unagreed = !!unagree;
  54. ret.collected = !!collected;
  55. ret.liked = !!liked;
  56. return ret;
  57. }
  58. async agree(id) {
  59. let mysql = this.app.mysql;
  60. let ret = await mysql.query("update posts set agree_num=agree_num+1 where id=?", [id]);
  61. return ret.affectedRows == 1;
  62. }
  63. async unagree(uid, id, unagree) {
  64. let mysql = this.app.mysql;
  65. let ret = await mysql.query("select id from post_unagree where post_id=? and user_id=?", [id, uid]);
  66. if( unagree == 1 ) {
  67. if( ret[0] ) {
  68. return true;
  69. }
  70. ret = await mysql.insert("post_unagree",{post_id: id, user_id: uid});
  71. return ret.affectedRows == 1;
  72. }
  73. ret = await mysql.query("delete from post_unagree where post_id=? and user_id=? ",[id, uid]);
  74. return ret.affectedRows == 1;
  75. }
  76. async like(uid, id, like) {
  77. let mysql = this.app.mysql;
  78. let ret;
  79. if( like == 1 ) {
  80. ret = await mysql.query("select * from post_like where post_id=? and user_id=?",[id, uid]);
  81. ret = ret[0];
  82. if( !ret )
  83. {
  84. await mysql.query("update posts set like_num=like_num+1 where id=?", [id]);
  85. ret = await mysql.insert("post_like", {post_id:id, user_id: uid});
  86. }
  87. } else {
  88. ret = await mysql.query("delete from post_like where post_id =? and user_id = ?",[id, uid]);
  89. if( ret.affectedRows == 1 ) {
  90. await mysql.query("update posts set like_num=like_num-1 where id=?", [id]);
  91. }
  92. }
  93. console.log( ret );
  94. return true;
  95. }
  96. async collect(uid, id, collect) {
  97. let mysql = this.app.mysql;
  98. let ret = await mysql.query("select id from post_collect where post_id=? and user_id=?", [id, uid]);
  99. if( collect == 1 ) {
  100. if( ret[0] ) {
  101. return true;
  102. }
  103. ret = await mysql.insert("post_collect",{post_id: id, user_id: uid});
  104. return ret.affectedRows == 1;
  105. }
  106. ret = await mysql.query("delete from post_collect where post_id=? and user_id=? ",[id, uid]);
  107. return ret.affectedRows == 1;
  108. }
  109. }
  110. module.exports = PostService;