product.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. const d_product = require('../database/d_product');
  2. const {searchHandle} = require('../tools/searchSql');
  3. const {handle} = require('../tools/handle_cjs');
  4. const codeMap = require("../map/rcodeMap");
  5. const log = require("../logger").logger("c_product","info")
  6. /**
  7. * 加载产品
  8. * @param key 产品类别
  9. * @param p 页码
  10. * @param l 每页数量
  11. * @returns {Promise<*[]>} [err,res]
  12. */
  13. async function loadProduct(key,p,l)
  14. {
  15. p = p || 1;
  16. l = l || 10;
  17. let [err,res] = await handle(d_product.loadProducts(key, p, l));
  18. if(err){
  19. return [err,null];
  20. }
  21. return [null,res];
  22. }
  23. /**
  24. * 获取产品信息
  25. * @param id 产品id
  26. * @returns {Promise<*[]>}
  27. */
  28. async function getProductInfo(id)
  29. {
  30. let [err,res] = await handle(d_product.getProductInfo(id));
  31. if(err){
  32. return [err,null];
  33. }
  34. let data = {};
  35. if(res.length){
  36. data = res[0];
  37. }else{
  38. return [
  39. {
  40. eCode:codeMap.NotFound,
  41. eMsg:`无法找到对应产品`
  42. },null]
  43. }
  44. // console.log(data);
  45. return [null,data];
  46. }
  47. async function searchProduct(type, key, p, l)
  48. {
  49. p = p || 1;
  50. l = l || 10;
  51. let _params = {
  52. }
  53. if(type !== 'all'){
  54. _params.type = type;
  55. }
  56. if(key){
  57. _params.key = key
  58. }
  59. return await searchHandle(
  60. '搜索产品失败',
  61. d_product.searchProducts,
  62. _params,
  63. null,
  64. p,
  65. l);
  66. }
  67. async function searchProductByMini(type, key, p, l){
  68. p = p || 1;
  69. l = l || 10;
  70. let _params = {
  71. }
  72. if(type !== 'all'){
  73. _params.type = type;
  74. }
  75. if(key){
  76. _params.key = key
  77. }
  78. return await searchHandle(
  79. '搜索产品失败',
  80. d_product.searchProductsByMini,
  81. _params,
  82. null,
  83. p,
  84. l);
  85. }
  86. module.exports = {
  87. loadProduct,
  88. getProductInfo,
  89. searchProduct,
  90. searchProductByMini
  91. };